Skip to content

Commit e7ff5d4

Browse files
committed
Store new desktop app data outside the fyne subdirectory
Apps that already have data under the "fyne" directory keep using it, so nothing is moved behind the user's back. Apps without any data there get ~/.config/app_id and ~/.cache/app_id (or the platform equivalent). The Fyne-wide settings.json and theme.json stay where they are. Fixes #6464
1 parent 856f9eb commit e7ff5d4

3 files changed

Lines changed: 57 additions & 2 deletions

File tree

app/cache_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ import (
1111

1212
func rootCacheDir(a fyne.App) string {
1313
desktopCache, _ := os.UserCacheDir()
14-
return filepath.Join(desktopCache, "fyne", a.UniqueID())
14+
return a.(*fyneApp).appDir(filepath.Join(desktopCache, "fyne"))
1515
}

app/preferences_other.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package app
44

55
import (
6+
"os"
67
"path/filepath"
78

89
"fyne.io/fyne/v2/internal/app"
@@ -15,7 +16,23 @@ func (p *preferences) storagePath() string {
1516

1617
// storageRoot returns the location of the app storage
1718
func (a *fyneApp) storageRoot() string {
18-
return filepath.Join(app.RootConfigDir(), a.UniqueID())
19+
return a.appDir(app.RootConfigDir())
20+
}
21+
22+
// appDir returns the directory for this app's data given the "fyne" directory
23+
// that used to hold all apps. Apps that already have data in there keep using
24+
// it, new apps live directly under the parent (e.g. ~/.config/app_id).
25+
// Apps without an ID have nothing to migrate, so they stay in the old place,
26+
// as does everything when the root is not a "fyne" directory (test builds).
27+
func (a *fyneApp) appDir(fyneDir string) string {
28+
old := filepath.Join(fyneDir, a.UniqueID())
29+
if a.missingID || filepath.Base(fyneDir) != "fyne" {
30+
return old
31+
}
32+
if _, err := os.Stat(old); err == nil {
33+
return old
34+
}
35+
return filepath.Join(filepath.Dir(fyneDir), a.UniqueID())
1936
}
2037

2138
func (p *preferences) watch() {

app/preferences_other_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//go:build !ios && !android && !mobile && !wasm
2+
3+
package app
4+
5+
import (
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestFyneApp_appDir(t *testing.T) {
15+
parent := t.TempDir()
16+
fyneDir := filepath.Join(parent, "fyne")
17+
testDir := filepath.Join(parent, "fyne-test")
18+
require.NoError(t, os.MkdirAll(filepath.Join(fyneDir, "io.fyne.old"), 0o700))
19+
20+
noID, noIDOld := &fyneApp{}, &fyneApp{}
21+
require.NoError(t, os.MkdirAll(filepath.Join(fyneDir, noIDOld.UniqueID()), 0o700))
22+
23+
for name, tt := range map[string]struct {
24+
app *fyneApp
25+
root string
26+
want string
27+
}{
28+
"existing app keeps fyne subdirectory": {&fyneApp{uniqueID: "io.fyne.old"}, fyneDir, filepath.Join(fyneDir, "io.fyne.old")},
29+
"new app moves out of fyne subdirectory": {&fyneApp{uniqueID: "io.fyne.new"}, fyneDir, filepath.Join(parent, "io.fyne.new")},
30+
"root not named fyne is left alone": {&fyneApp{uniqueID: "io.fyne.new"}, testDir, filepath.Join(testDir, "io.fyne.new")},
31+
"app without ID stays in fyne subdirectory": {noID, fyneDir, filepath.Join(fyneDir, noID.UniqueID())},
32+
"app without ID keeps existing data": {noIDOld, fyneDir, filepath.Join(fyneDir, noIDOld.UniqueID())},
33+
} {
34+
t.Run(name, func(t *testing.T) {
35+
assert.Equal(t, tt.want, tt.app.appDir(tt.root))
36+
})
37+
}
38+
}

0 commit comments

Comments
 (0)