diff --git a/go.mod b/go.mod index 2798517f..02c2971a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.21.4 require ( github.com/jpillora/overseer v1.1.6 - github.com/mishamyrt/nuga-lib v0.7.3 + github.com/mishamyrt/nuga-lib v0.7.4 github.com/wailsapp/wails/v2 v2.8.0 ) diff --git a/go.sum b/go.sum index 781baf8d..41cb8570 100644 --- a/go.sum +++ b/go.sum @@ -44,8 +44,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mishamyrt/nuga-lib v0.7.3 h1:dg0bCBmddtimdmEMRjgBFlf7SEvGtDpvE70xFnqhSNk= -github.com/mishamyrt/nuga-lib v0.7.3/go.mod h1:Zvch+eplvbZyH0ubJSIV9bWoSDLJtbjOiuwu/YEPsYA= +github.com/mishamyrt/nuga-lib v0.7.4 h1:Dx3zShk/QWE0B5IKE4c2fLkSlKw156TJIx2QZmqZ7A4= +github.com/mishamyrt/nuga-lib v0.7.4/go.mod h1:Zvch+eplvbZyH0ubJSIV9bWoSDLJtbjOiuwu/YEPsYA= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 h1:KoWmjvw+nsYOo29YJK9vDA65RGE3NrOnUtO7a+RF9HU= github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8/go.mod h1:HKlIX3XHQyzLZPlr7++PzdhaXEj94dEiJgZDTsxEqUI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/internal/dto/keys.go b/internal/dto/keys.go index d9aa785e..dc40e183 100644 --- a/internal/dto/keys.go +++ b/internal/dto/keys.go @@ -2,11 +2,15 @@ package dto import ( "bytes" + "fmt" "github.com/mishamyrt/nuga-lib/features/keys" "github.com/mishamyrt/nuga-lib/features/keys/layout" ) +// ErrMacrosLengthMismatch is returned when macros and titles have different lengths +var ErrMacrosLengthMismatch = fmt.Errorf("macros and titles must have the same length") + // KeyMap represents a map of keys. type KeyMap layout.KeyMap @@ -41,6 +45,30 @@ func (m Macros) ToDomain() keys.Macros { return macros } +// MacrosFromDomain converts keys.Macros to Macros +func MacrosFromDomain(k keys.Macros, titles []string) (Macros, error) { + if len(k) != len(titles) { + return nil, ErrMacrosLengthMismatch + } + m := make([]MacroWithTitle, 0, len(k)) + for i, macro := range k { + m = append(m, MacroWithTitle{ + Macro: macro, + Title: titles[i], + }) + } + return m, nil +} + +// Titles returns titles of macros +func (m Macros) Titles() []string { + titles := make([]string, 0, len(m)) + for _, macro := range m { + titles = append(titles, macro.Title) + } + return titles +} + // Key ... as it names type Key struct { Title string `json:"title"` diff --git a/internal/dto/state.go b/internal/dto/state.go index bd080812..040f2a8e 100644 --- a/internal/dto/state.go +++ b/internal/dto/state.go @@ -6,6 +6,7 @@ import ( // StateFile represents keyboard state file type StateFile struct { - Version string `json:"version"` + Version string `json:"version"` + MacroTitles []string `json:"macro_titles"` *dump.State } diff --git a/internal/usecase/device.go b/internal/usecase/device.go index 887fa4fe..0b955d3d 100644 --- a/internal/usecase/device.go +++ b/internal/usecase/device.go @@ -13,6 +13,7 @@ import ( "github.com/mishamyrt/nuga-lib" "github.com/mishamyrt/nuga-lib/dump" + "github.com/mishamyrt/nuga-lib/features/keys" "github.com/wailsapp/wails/v2/pkg/runtime" ) @@ -109,9 +110,11 @@ func (d *DeviceUsecase) SaveState() error { if err != nil { return err } + titles := d.repo.Settings.GetMacros().Titles() return d.repo.State.WriteFile(path, dto.StateFile{ - Version: config.AppVersion, - State: &state, + Version: config.AppVersion, + State: &state, + MacroTitles: titles, }) } @@ -139,6 +142,22 @@ func (d *DeviceUsecase) RestoreState() error { if state.Name != dev.Name { return errors.ErrStateWrongDevice } + err = dump.Restore(dev.Handle, state.State) + if err != nil { + return err + } + macros, err := keys.ParseHeadlessMacros(state.Keys.Macros) + if err != nil { + return err + } + macrosSettings, err := dto.MacrosFromDomain(macros, state.MacroTitles) + if err != nil { + return err + } + err = d.repo.Settings.SetMacros(macrosSettings) + if err != nil { + return err + } return dump.Restore(dev.Handle, state.State) } diff --git a/ui/src/entities/keys/model/stores/keymap.ts b/ui/src/entities/keys/model/stores/keymap.ts index 8ad8d57b..1046c41c 100644 --- a/ui/src/entities/keys/model/stores/keymap.ts +++ b/ui/src/entities/keys/model/stores/keymap.ts @@ -50,6 +50,13 @@ sample({ target: keysInitiated }) +sample({ + clock: anyStateRestored, + source: supportsStore, + filter: ({ keys }) => keys, + target: keysInitiated +}) + // Load keys on device connect sample({ clock: [keysInitiated, anyStateRestored, modeChanged],