Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v3] Added AppDataDirectory path #3823

Merged
24 changes: 24 additions & 0 deletions mkdocs-website/docs/en/API/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ API: `Show()`
app.Show()
```

### Path

API: `Path(selector Path) string`

`Path(selector Path)` returns the full path for the given path type. It provides a cross-platform way to access common application directories.

The `Path` type is an enum with the following values:
- `AppData`: Returns the path to the application data directory
- `UserCache`: Returns the path to the user's cache directory
- `UserConfig`: Returns the path to the user's configuration directory

```go
// Get the application data directory path
appDataPath := app.Path(application.AppData)

// Get the user cache directory path
userCachePath := app.Path(application.UserCache)

// Get the user config directory path
userConfigPath := app.Path(application.UserConfig)
```



--8<--
./docs/en/API/application_window.md
./docs/en/API/application_menu.md
Expand Down
1 change: 1 addition & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748)
- Upgrade to `go-webview2` v1.0.16 by [leaanthony](https://github.com/leaanthony)
- Fixed `Screen` type to include `ID` not `Id` by [etesam913](https://github.com/etesam913) in [#3778](https://github.com/wailsapp/wails/pull/3778)
- Add `Path` method to `application` package by [ansxuman](https://github.com/ansxuman) in [#3823](https://github.com/wailsapp/wails/pull/3823)

## v3.0.0-alpha.7 - 2024-09-18

Expand Down
26 changes: 26 additions & 0 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ var alphaAssets embed.FS

var globalApplication *App

type Path int

const (
AppData Path = iota
UserCache
UserConfig
)

// AlphaAssets is the default assets for the alpha application
var AlphaAssets = AssetOptions{
Handler: BundledAssetFileServer(alphaAssets),
Expand Down Expand Up @@ -192,6 +200,9 @@ type (
GetFlags(options Options) map[string]any
isOnMainThread() bool
isDarkMode() bool
getAppDataPath() string
getUserCachePath() string
getUserConfigPath() string
}

runnable interface {
Expand Down Expand Up @@ -1013,3 +1024,18 @@ func (a *App) shouldQuit() bool {
}
return true
}

// Path returns the path for the given selector

func (a *App) Path(selector Path) string {
switch selector {
case AppData:
return a.impl.getAppDataPath()
case UserCache:
return a.impl.getUserCachePath()
case UserConfig:
return a.impl.getUserConfigPath()
default:
return ""
}
}
ansxuman marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions v3/pkg/application/application_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ static const char* serializationNSDictionary(void *dict) {
import "C"
import (
"encoding/json"
"os"
"path/filepath"
"unsafe"

"github.com/wailsapp/wails/v3/internal/operatingsystem"
Expand Down Expand Up @@ -363,3 +365,18 @@ func (a *App) platformEnvironment() map[string]any {
func fatalHandler(errFunc func(error)) {
return
}

func (a *macosApp) getAppDataPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Application Support")
}

func (a *macosApp) getUserCachePath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Caches")
}

func (a *macosApp) getUserConfigPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, "Library", "Preferences")
}
16 changes: 16 additions & 0 deletions v3/pkg/application/application_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import "C"
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -260,3 +261,18 @@ func fatalHandler(errFunc func(error)) {
// Stub for windows function
return
}

func (a *linuxApp) getAppDataPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".local", "share")
}
ansxuman marked this conversation as resolved.
Show resolved Hide resolved

func (a *linuxApp) getUserCachePath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".cache")
}
ansxuman marked this conversation as resolved.
Show resolved Hide resolved

func (a *linuxApp) getUserConfigPath() string {
home, _ := os.UserHomeDir()
return filepath.Join(home, ".config")
}
ansxuman marked this conversation as resolved.
Show resolved Hide resolved
ansxuman marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 17 additions & 0 deletions v3/pkg/application/application_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package application

import (
"fmt"
"os"
"path/filepath"
"sync"
"syscall"
"unsafe"
Expand Down Expand Up @@ -349,3 +351,18 @@ func fatalHandler(errFunc func(error)) {
w32.Fatal = errFunc
return
}

func (a *windowsApp) getAppDataPath() string {
path := os.Getenv("APPDATA")
return path
}

func (a *windowsApp) getUserCachePath() string {
path := filepath.Join(os.Getenv("LOCALAPPDATA"), "Temp")
return path
}

func (a *windowsApp) getUserConfigPath() string {
path := os.Getenv("LOCALAPPDATA")
return path
}
ansxuman marked this conversation as resolved.
Show resolved Hide resolved