-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracker/gioui): preferences.yml for window size or maximized (#185)
- Loading branch information
Showing
5 changed files
with
94 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package gioui | ||
|
||
import ( | ||
_ "embed" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
||
"gioui.org/unit" | ||
) | ||
|
||
type ( | ||
Preferences struct { | ||
Window WindowPreferences | ||
YmlError error | ||
} | ||
|
||
WindowPreferences struct { | ||
Width int | ||
Height int | ||
Maximized bool `yaml:",omitempty"` | ||
} | ||
) | ||
|
||
//go:embed preferences.yml | ||
var defaultPreferencesYaml []byte | ||
|
||
func loadDefaultPreferences() Preferences { | ||
var preferences Preferences | ||
err := yaml.Unmarshal(defaultPreferencesYaml, &preferences) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to unmarshal preferences: %w", err)) | ||
} | ||
return preferences | ||
} | ||
|
||
// ReadCustomConfigYml modifies the target argument, i.e. needs a pointer | ||
func ReadCustomConfigYml(filename string, target interface{}) (exists bool, err error) { | ||
configDir, err := os.UserConfigDir() | ||
if err != nil { | ||
return false, err | ||
} | ||
path := filepath.Join(configDir, "sointu", filename) | ||
bytes, err2 := os.ReadFile(path) | ||
if err2 != nil { | ||
return false, err2 | ||
} | ||
err = yaml.Unmarshal(bytes, target) | ||
return true, err | ||
} | ||
|
||
func MakePreferences() Preferences { | ||
preferences := loadDefaultPreferences() | ||
exists, err := ReadCustomConfigYml("preferences.yml", &preferences) | ||
if exists { | ||
preferences.YmlError = err | ||
} | ||
return preferences | ||
} | ||
|
||
func (p Preferences) WindowSize() (unit.Dp, unit.Dp) { | ||
return unit.Dp(p.Window.Width), unit.Dp(p.Window.Height) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
window: | ||
width: 800 | ||
height: 600 | ||
maximized: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters