diff --git a/mkdocs-website/docs/en/API/application.md b/mkdocs-website/docs/en/API/application.md index caddaf104..a529b96b9 100644 --- a/mkdocs-website/docs/en/API/application.md +++ b/mkdocs-website/docs/en/API/application.md @@ -133,6 +133,57 @@ 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 query common application directories. + +The `Path` type is an enum with the following values: +- `PathHome`: Returns the user's home directory +- `PathDataHome`: Returns the path to the user's data directory +- `PathConfigHome`: Returns the path to the user's configuration directory +- `PathStateHome`: Returns the path to the user's state directory +- `PathCacheHome`: Returns the path to the user's cache directory +- `PathRuntimeDir`: Returns the path to the user's runtime directory +- `PathDesktop`: Returns the path to the user's desktop directory +- `PathDownload`: Returns the path to the user's download directory +- `PathDocuments`: Returns the path to the user's documents directory +- `PathMusic`: Returns the path to the user's music directory +- `PathPictures`: Returns the path to the user's pictures directory +- `PathVideos`: Returns the path to the user's videos directory +- `PathTemplates`: Returns the path to the user's templates directory +- `PathPublicShare`: Returns the path to the user's public share directory + +```go + // Get the data home directory path + dataHomePath := app.Path(application.PathDataHome) + fmt.Println("DataHome path:", dataHomePath) + + // Output: DataHome path: /home/username/.local/share // Linux + // Output: DataHome path: /Users/username/Library/Application Support // macOS + // Output: DataHome path: C:\Users\Username\AppData\Roaming // Windows + + // Get the CacheHome directory path + cacheHomePath := app.Path(application.CacheHome) + fmt.Println("CacheHome path:", cacheHomePath) + + // Output: CacheHome path: /home/username/.cache // Linux + // Output: CacheHome path: /Users/username/Library/Caches // macOS + // Output: CacheHome path: C:\Users\Username\AppData\Local\Temp // Windows +``` + +## Paths +API: `Paths(selector Paths) []string` +`Paths(selector Path)` returns a list of paths for the given path type. It provides a cross-platform way to query common directory paths. + +The `Paths` type is an enum with the following values: +- `PathsDataDirs`: Returns the list of data directories +- `PathsConfigDirs`: Returns the list of configuration directories +- `PathsCacheDirs`: Returns the list of cache directories +- `PathsRuntimeDirs`: Returns the list of runtime directories + + --8<-- ./docs/en/API/application_window.md ./docs/en/API/application_menu.md diff --git a/mkdocs-website/docs/en/changelog.md b/mkdocs-website/docs/en/changelog.md index ed3335c9a..682601e73 100644 --- a/mkdocs-website/docs/en/changelog.md +++ b/mkdocs-website/docs/en/changelog.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - File Association support by [leaanthony](https://github.com/leaanthony) in [#3873](https://github.com/wailsapp/wails/pull/3873) - New `wails3 generate runtime` command by [leaanthony](https://github.com/leaanthony) - New `InitialPosition` option to specify if the window should be centered or positioned at the given X/Y location by [leaanthony](https://github.com/leaanthony) in [#3885](https://github.com/wailsapp/wails/pull/3885) +- Add `Path` & `Paths` methods to `application` package by [ansxuman](https://github.com/ansxuman) and [leaanthony](https://github.com/leaanthony) in [#3823](https://github.com/wailsapp/wails/pull/3823) ### Changed - Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748) diff --git a/v3/go.mod b/v3/go.mod index e822646d5..5b93d3e06 100644 --- a/v3/go.mod +++ b/v3/go.mod @@ -3,6 +3,7 @@ module github.com/wailsapp/wails/v3 go 1.22.4 require ( + github.com/adrg/xdg v0.5.0 github.com/atterpac/refresh v0.8.3 github.com/bep/debounce v1.2.1 github.com/ebitengine/purego v0.4.0-alpha.4 @@ -31,7 +32,7 @@ require ( github.com/tc-hib/winres v0.3.1 github.com/wailsapp/go-webview2 v1.0.16 github.com/wailsapp/mimetype v1.4.1 - golang.org/x/sys v0.20.0 + golang.org/x/sys v0.22.0 golang.org/x/term v0.20.0 golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d gopkg.in/yaml.v3 v3.0.1 diff --git a/v3/go.sum b/v3/go.sum index 9063ee638..c0dce034b 100644 --- a/v3/go.sum +++ b/v3/go.sum @@ -26,6 +26,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCv github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= +github.com/adrg/xdg v0.5.0 h1:dDaZvhMXatArP1NPHhnfaQUqWBLBsmx1h1HXQdMoFCY= +github.com/adrg/xdg v0.5.0/go.mod h1:dDdY4M4DF9Rjy4kHPeNL+ilVF+p2lK8IdM9/rTSGcI4= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= @@ -213,8 +215,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tc-hib/winres v0.3.1 h1:CwRjEGrKdbi5CvZ4ID+iyVhgyfatxFoizjPhzez9Io4= github.com/tc-hib/winres v0.3.1/go.mod h1:C/JaNhH3KBvhNKVbvdlDWkbMDO9H4fKKDaN7/07SSuk= github.com/wailsapp/go-webview2 v1.0.16 h1:wffnvnkkLvhRex/aOrA3R7FP7rkvOqL/bir1br7BekU= @@ -284,8 +286,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= -golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/v3/pkg/application/application.go b/v3/pkg/application/application.go index 2fe286251..9d575a145 100644 --- a/v3/pkg/application/application.go +++ b/v3/pkg/application/application.go @@ -1013,3 +1013,13 @@ func (a *App) shouldQuit() bool { } return true } + +// Path returns the path for the given selector +func (a *App) Path(selector Path) string { + return paths[selector] +} + +// Paths returns the paths for the given selector +func (a *App) Paths(selector Paths) []string { + return pathdirs[selector] +} diff --git a/v3/pkg/application/path.go b/v3/pkg/application/path.go new file mode 100644 index 000000000..867e5b678 --- /dev/null +++ b/v3/pkg/application/path.go @@ -0,0 +1,123 @@ +package application + +import "github.com/adrg/xdg" + +type Path int + +const ( + // PathHome is the user's home directory. + PathHome Path = iota + + // PathDataHome defines the base directory relative to which user-specific + // data files should be stored. This directory is defined by the + // $XDG_DATA_HOME environment variable. If the variable is not set, + // a default equal to $HOME/.local/share should be used. + PathDataHome + + // PathConfigHome defines the base directory relative to which user-specific + // configuration files should be written. This directory is defined by + // the $XDG_CONFIG_HOME environment variable. If the variable is + // not set, a default equal to $HOME/.config should be used. + PathConfigHome + + // PathStateHome defines the base directory relative to which user-specific + // state files should be stored. This directory is defined by the + // $XDG_STATE_HOME environment variable. If the variable is not set, + // a default equal to ~/.local/state should be used. + PathStateHome + + // PathCacheHome defines the base directory relative to which user-specific + // non-essential (cached) data should be written. This directory is + // defined by the $XDG_CACHE_HOME environment variable. If the variable + // is not set, a default equal to $HOME/.cache should be used. + PathCacheHome + + // PathRuntimeDir defines the base directory relative to which user-specific + // non-essential runtime files and other file objects (such as sockets, + // named pipes, etc.) should be stored. This directory is defined by the + // $XDG_RUNTIME_DIR environment variable. If the variable is not set, + // applications should fall back to a replacement directory with similar + // capabilities. Applications should use this directory for communication + // and synchronization purposes and should not place larger files in it, + // since it might reside in runtime memory and cannot necessarily be + // swapped out to disk. + PathRuntimeDir + + // PathDesktop defines the location of the user's desktop directory. + PathDesktop + + // PathDownload defines a suitable location for user downloaded files. + PathDownload + + // PathDocuments defines a suitable location for user document files. + PathDocuments + + // PathMusic defines a suitable location for user audio files. + PathMusic + + // PathPictures defines a suitable location for user image files. + PathPictures + + // PathVideos defines a suitable location for user video files. + PathVideos + + // PathTemplates defines a suitable location for user template files. + PathTemplates + + // PathPublicShare defines a suitable location for user shared files. + PathPublicShare +) + +var paths = map[Path]string{ + PathHome: xdg.Home, + PathDataHome: xdg.DataHome, + PathConfigHome: xdg.ConfigHome, + PathStateHome: xdg.StateHome, + PathCacheHome: xdg.CacheHome, + PathRuntimeDir: xdg.RuntimeDir, + PathDesktop: xdg.UserDirs.Desktop, + PathDownload: xdg.UserDirs.Download, + PathDocuments: xdg.UserDirs.Documents, + PathMusic: xdg.UserDirs.Music, + PathPictures: xdg.UserDirs.Pictures, + PathVideos: xdg.UserDirs.Videos, + PathTemplates: xdg.UserDirs.Templates, + PathPublicShare: xdg.UserDirs.PublicShare, +} + +type Paths int + +const ( + // PathsDataDirs defines the preference-ordered set of base directories to + // search for data files in addition to the DataHome base directory. + // This set of directories is defined by the $XDG_DATA_DIRS environment + // variable. If the variable is not set, the default directories + // to be used are /usr/local/share and /usr/share, in that order. The + // DataHome directory is considered more important than any of the + // directories defined by DataDirs. Therefore, user data files should be + // written relative to the DataHome directory, if possible. + PathsDataDirs = iota + + // PathsConfigDirs defines the preference-ordered set of base directories + // search for configuration files in addition to the ConfigHome base + // directory. This set of directories is defined by the $XDG_CONFIG_DIRS + // environment variable. If the variable is not set, a default equal + // to /etc/xdg should be used. The ConfigHome directory is considered + // more important than any of the directories defined by ConfigDirs. + // Therefore, user config files should be written relative to the + // ConfigHome directory, if possible. + PathsConfigDirs + + // PathsFontDirs defines the common locations where font files are stored. + PathsFontDirs + + // PathsApplicationDirs defines the common locations of applications. + PathsApplicationDirs +) + +var pathdirs = map[Paths][]string{ + PathsDataDirs: xdg.DataDirs, + PathsConfigDirs: xdg.ConfigDirs, + PathsFontDirs: xdg.FontDirs, + PathsApplicationDirs: xdg.ApplicationDirs, +}