feat(09-02): add shortcuts config package with default bindings and Wails persistence
- Create backend/shortcuts/config.go with DefaultBindings(), ApplyDefaults(), Validate() - Wire Shortcuts field into main Config struct with TOML persistence - Add GetShortcuts, SetShortcuts, SetShortcut, ResetShortcuts Wails binding methods - Regenerate Wails TypeScript bindings for new config methods - Fix wsl lint in library.go (blank line before logger call)
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"yellowjacket/backend/events"
|
||||
"yellowjacket/backend/favorites"
|
||||
"yellowjacket/backend/library"
|
||||
"yellowjacket/backend/shortcuts"
|
||||
"yellowjacket/backend/system"
|
||||
"yellowjacket/backend/theme"
|
||||
"yellowjacket/backend/tracklist"
|
||||
@@ -30,6 +31,7 @@ type Config struct {
|
||||
Window *WindowConfig `toml:"Window"`
|
||||
TrackList *tracklist.Config `toml:"TrackList"`
|
||||
Favorites *favorites.Config `toml:"Favorites"`
|
||||
Shortcuts *shortcuts.Config `toml:"Shortcuts"`
|
||||
}
|
||||
|
||||
// NewConfig creates a new config by loading it from disk.
|
||||
@@ -86,6 +88,12 @@ func (c *Config) Validate() error {
|
||||
}
|
||||
}
|
||||
|
||||
if c.Shortcuts != nil {
|
||||
if err := c.Shortcuts.Validate(); err != nil {
|
||||
configErrs = errors.Join(configErrs, err)
|
||||
}
|
||||
}
|
||||
|
||||
if configErrs != nil {
|
||||
return fmt.Errorf(
|
||||
"one or more config parts are invalid: %w",
|
||||
@@ -190,6 +198,12 @@ func (c *Config) applyDefaults() {
|
||||
}
|
||||
|
||||
c.Favorites.ApplyDefaults()
|
||||
|
||||
if c.Shortcuts == nil {
|
||||
c.Shortcuts = &shortcuts.Config{}
|
||||
}
|
||||
|
||||
c.Shortcuts.ApplyDefaults()
|
||||
}
|
||||
|
||||
// SetContext sets the Wails runtime context for event emission.
|
||||
@@ -582,3 +596,101 @@ func (c *Config) emitFavoritesChanged() {
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// GetShortcuts returns the current shortcut bindings map.
|
||||
func (c *Config) GetShortcuts() map[string]string {
|
||||
if c.Shortcuts == nil {
|
||||
c.Shortcuts = &shortcuts.Config{}
|
||||
c.Shortcuts.ApplyDefaults()
|
||||
}
|
||||
|
||||
return c.Shortcuts.Bindings
|
||||
}
|
||||
|
||||
// SetShortcuts saves the entire shortcut bindings map.
|
||||
func (c *Config) SetShortcuts(
|
||||
bindings map[string]string,
|
||||
) error {
|
||||
if c.Shortcuts == nil {
|
||||
c.Shortcuts = &shortcuts.Config{}
|
||||
}
|
||||
|
||||
c.Shortcuts.Bindings = bindings
|
||||
|
||||
if err := c.Save(); err != nil {
|
||||
return fmt.Errorf(
|
||||
"could not save shortcuts config: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
if c.ctx != nil {
|
||||
runtime.EventsEmit(
|
||||
c.ctx,
|
||||
events.ShortcutsConfigChanged,
|
||||
bindings,
|
||||
)
|
||||
}
|
||||
|
||||
c.logger.Info("shortcuts config updated")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetShortcut saves a single shortcut binding.
|
||||
func (c *Config) SetShortcut(
|
||||
action string, key string,
|
||||
) error {
|
||||
if c.Shortcuts == nil {
|
||||
c.Shortcuts = &shortcuts.Config{}
|
||||
c.Shortcuts.ApplyDefaults()
|
||||
}
|
||||
|
||||
c.Shortcuts.Bindings[action] = key
|
||||
|
||||
if err := c.Save(); err != nil {
|
||||
return fmt.Errorf(
|
||||
"could not save shortcut: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
if c.ctx != nil {
|
||||
runtime.EventsEmit(
|
||||
c.ctx,
|
||||
events.ShortcutsConfigChanged,
|
||||
c.Shortcuts.Bindings,
|
||||
)
|
||||
}
|
||||
|
||||
c.logger.Info(
|
||||
"shortcut updated",
|
||||
"action", action,
|
||||
"key", key,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ResetShortcuts resets all shortcuts to defaults.
|
||||
func (c *Config) ResetShortcuts() error {
|
||||
c.Shortcuts = &shortcuts.Config{
|
||||
Bindings: shortcuts.DefaultBindings(),
|
||||
}
|
||||
|
||||
if err := c.Save(); err != nil {
|
||||
return fmt.Errorf(
|
||||
"could not save shortcuts reset: %w", err,
|
||||
)
|
||||
}
|
||||
|
||||
if c.ctx != nil {
|
||||
runtime.EventsEmit(
|
||||
c.ctx,
|
||||
events.ShortcutsConfigChanged,
|
||||
c.Shortcuts.Bindings,
|
||||
)
|
||||
}
|
||||
|
||||
c.logger.Info("shortcuts reset to defaults")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user