From 75762cc0a2abf4ce2aedf75df0cd829f8afecf76 Mon Sep 17 00:00:00 2001 From: benebsiny Date: Mon, 4 Sep 2023 17:02:48 +0800 Subject: [PATCH] Updated --- global_shortcut.go | 123 ---------------- global_shortcuts.go | 135 ++++++++++++++++++ ...ortcut_test.go => global_shortcuts_test.go | 16 +-- 3 files changed, 143 insertions(+), 131 deletions(-) delete mode 100644 global_shortcut.go create mode 100644 global_shortcuts.go rename global_shortcut_test.go => global_shortcuts_test.go (67%) diff --git a/global_shortcut.go b/global_shortcut.go deleted file mode 100644 index bbd0e61..0000000 --- a/global_shortcut.go +++ /dev/null @@ -1,123 +0,0 @@ -package astilectron - -import "context" - -const ( - EventNameGlobalShortcutCmdRegister = "global.shortcuts.event.registered" - EventNameGlobalShortcutCmdIsRegistered = "global.shortcuts.event.is.registered" - EventNameGlobalShortcutCmdUnregister = "global.shortcuts.event.unregistered" - EventNameGlobalShortcutCmdUnregisterAll = "global.shortcuts.event.unregistered.all" - EventNameGlobalShortcutEventProcessFinished = "global.shortcuts.event.process.finished" // Register or Unregister process is finished - EventNameGlobalShortcutEventTriggered = "global.shortcuts.event.triggered" -) - -type callback func() - -// GlobalShortcuts represents a global shortcut -type GlobalShortcuts struct { - *object - callbacks map[string]*callback // Store all registered Global Shortcuts -} - -func newGlobalShortcuts(ctx context.Context, d *dispatcher, i *identifier, w *writer) (gs *GlobalShortcuts) { - var obj = newObject(ctx, d, i, w, i.new()) - gs = &GlobalShortcuts{object: obj, callbacks: make(map[string]*callback)} - - obj.On(EventNameGlobalShortcutEventTriggered, func(e Event) (deleteListener bool) { // Register the listener for the triggered event - globalShortcutHandler(gs, e.GlobalShortcuts.Accelerator) - return - }) - return -} - -// Register Register global shortcuts -func (gs *GlobalShortcuts) Register(accelerator string, callback callback) (isRegistered bool, err error) { - - if err = gs.ctx.Err(); err != nil { - return - } - - // Send an event to astilectron to register the global shortcut - var event = Event{Name: EventNameGlobalShortcutCmdRegister, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} - result, err := synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutEventProcessFinished) - - if err != nil { - return - } - - // If registered successfully, add the callback to the map - if result.GlobalShortcuts.IsRegistered { - gs.callbacks[accelerator] = &callback - } - - isRegistered = result.GlobalShortcuts.IsRegistered - return -} - -// IsRegistered Check if a global shortcut is registered -func (gs *GlobalShortcuts) IsRegistered(accelerator string) (isRegistered bool, err error) { - - if err = gs.ctx.Err(); err != nil { - return - } - - // Send an event to astilectron to check if global shortcut is registered - var event = Event{Name: EventNameGlobalShortcutCmdIsRegistered, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} - result, err := synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutEventProcessFinished) - - if err != nil { - return - } - - isRegistered = result.GlobalShortcuts.IsRegistered - return -} - -// Unregister Unregister a global shortcut -func (gs *GlobalShortcuts) Unregister(accelerator string) (err error) { - - if err = gs.ctx.Err(); err != nil { - return - } - - // Send an event to astilectron to unregister the global shortcut - var event = Event{Name: EventNameGlobalShortcutCmdUnregister, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} - _, err = synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutEventProcessFinished) - - if err != nil { - return - } - - // No need to find the callback from the map and delete it - // because that event will no longer be triggerred - // If the same global shortcut is registered again, the original callback will be replaced with the new one - - return -} - -// UnregisterAll Unregister all global shortcuts -func (gs *GlobalShortcuts) UnregisterAll() (err error) { - - if err = gs.ctx.Err(); err != nil { - return - } - - // Send an event to astilectron to unregister all global shortcuts - var event = Event{Name: EventNameGlobalShortcutCmdUnregisterAll, TargetID: gs.id} - _, err = synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutEventProcessFinished) - - if err != nil { - return - } - - gs.callbacks = make(map[string]*callback) // Clear the map - - return -} - -// globalShortcutHandler Handle the GlobalShortcuts event triggered from astilectron -func globalShortcutHandler(gs *GlobalShortcuts, accelerator string) { - if callback, ok := gs.callbacks[accelerator]; ok { - (*callback)() - } -} diff --git a/global_shortcuts.go b/global_shortcuts.go new file mode 100644 index 0000000..4bc50e1 --- /dev/null +++ b/global_shortcuts.go @@ -0,0 +1,135 @@ +package astilectron + +import ( + "context" + "sync" +) + +const ( + EventNameGlobalShortcutsCmdRegister = "global.shortcuts.cmd.register" + EventNameGlobalShortcutsCmdIsRegistered = "global.shortcuts.cmd.is.register" + EventNameGlobalShortcutsCmdUnregister = "global.shortcuts.cmd.unregister" + EventNameGlobalShortcutsCmdUnregisterAll = "global.shortcuts.cmd.unregister.all" + EventNameGlobalShortcutsEventRegistered = "global.shortcuts.event.registered" + EventNameGlobalShortcutsEventIsRegistered = "global.shortcuts.event.is.registered" + EventNameGlobalShortcutsEventUnregistered = "global.shortcuts.event.unregistered" + EventNameGlobalShortcutsEventUnregisteredAll = "global.shortcuts.event.unregistered.all" + EventNameGlobalShortcutEventTriggered = "global.shortcuts.event.triggered" +) + +type globalShortcutsCallback func() + +// GlobalShortcuts represents a global shortcut +type GlobalShortcuts struct { + *object + m *sync.Mutex + callbacks map[string]*globalShortcutsCallback // Store all registered Global Shortcuts +} + +func newGlobalShortcuts(ctx context.Context, d *dispatcher, i *identifier, w *writer) (gs *GlobalShortcuts) { + + gs = &GlobalShortcuts{object: newObject(ctx, d, i, w, i.new()), m: new(sync.Mutex), callbacks: make(map[string]*globalShortcutsCallback)} + gs.On(EventNameGlobalShortcutEventTriggered, func(e Event) (deleteListener bool) { // Register the listener for the triggered event + gs.execCallback(e.GlobalShortcuts.Accelerator) + return + }) + return +} + +// Register Register global shortcuts +func (gs *GlobalShortcuts) Register(accelerator string, callback globalShortcutsCallback) (isRegistered bool, err error) { + + if err = gs.ctx.Err(); err != nil { + return + } + + // Send an event to astilectron to register the global shortcut + var event = Event{Name: EventNameGlobalShortcutsCmdRegister, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} + result, err := synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutsEventRegistered) + + if err != nil { + return + } + + // If registered successfully, add the callback to the map + if result.GlobalShortcuts.IsRegistered { + gs.m.Lock() + gs.callbacks[accelerator] = &callback + gs.m.Unlock() + } + + isRegistered = result.GlobalShortcuts.IsRegistered + return +} + +// IsRegistered Check if a global shortcut is registered +func (gs *GlobalShortcuts) IsRegistered(accelerator string) (isRegistered bool, err error) { + + if err = gs.ctx.Err(); err != nil { + return + } + + // Send an event to astilectron to check if global shortcut is registered + var event = Event{Name: EventNameGlobalShortcutsCmdIsRegistered, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} + result, err := synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutsEventIsRegistered) + + if err != nil { + return + } + + isRegistered = result.GlobalShortcuts.IsRegistered + return +} + +// Unregister Unregister a global shortcut +func (gs *GlobalShortcuts) Unregister(accelerator string) (err error) { + + if err = gs.ctx.Err(); err != nil { + return + } + + // Send an event to astilectron to unregister the global shortcut + var event = Event{Name: EventNameGlobalShortcutsCmdUnregister, TargetID: gs.id, GlobalShortcuts: &EventGlobalShortcuts{Accelerator: accelerator}} + _, err = synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutsEventUnregistered) + + if err != nil { + return + } + + // No need to find the callback from the map and delete it + // because that event will no longer be triggerred + // If the same global shortcut is registered again, the original callback will be replaced with the new one + + return +} + +// UnregisterAll Unregister all global shortcuts +func (gs *GlobalShortcuts) UnregisterAll() (err error) { + + if err = gs.ctx.Err(); err != nil { + return + } + + // Send an event to astilectron to unregister all global shortcuts + var event = Event{Name: EventNameGlobalShortcutsCmdUnregisterAll, TargetID: gs.id} + _, err = synchronousEvent(gs.ctx, gs, gs.w, event, EventNameGlobalShortcutsEventUnregisteredAll) + + if err != nil { + return + } + + gs.m.Lock() + gs.callbacks = make(map[string]*globalShortcutsCallback) // Clear the map + gs.m.Unlock() + + return +} + +// execCallback Execute the GlobalShortcuts event triggered from astilectron +func (gs *GlobalShortcuts) execCallback(accelerator string) { + gs.m.Lock() + if callback, ok := gs.callbacks[accelerator]; ok { + (*callback)() + } + gs.m.Unlock() +} diff --git a/global_shortcut_test.go b/global_shortcuts_test.go similarity index 67% rename from global_shortcut_test.go rename to global_shortcuts_test.go index 08c1314..12a4e50 100644 --- a/global_shortcut_test.go +++ b/global_shortcuts_test.go @@ -18,25 +18,25 @@ func TestGlobalShortcut_Actions(t *testing.T) { testObjectAction(t, func() error { _, e := gs.Register("Ctrl+X", func() {}) return e - }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+X"}}%s`, EventNameGlobalShortcutCmdRegister, gs.id, "\n"), - EventNameGlobalShortcutEventProcessFinished, &Event{GlobalShortcuts: &EventGlobalShortcuts{Accelerator: "Ctrl+X", IsRegistered: true}}) + }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+X"}}%s`, EventNameGlobalShortcutsCmdRegister, gs.id, "\n"), + EventNameGlobalShortcutsEventRegistered, &Event{GlobalShortcuts: &EventGlobalShortcuts{Accelerator: "Ctrl+X", IsRegistered: true}}) // IsRegistered testObjectAction(t, func() error { _, e := gs.IsRegistered("Ctrl+Y") return e - }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+Y"}}%s`, EventNameGlobalShortcutCmdIsRegistered, gs.id, "\n"), - EventNameGlobalShortcutEventProcessFinished, &Event{GlobalShortcuts: &EventGlobalShortcuts{Accelerator: "Ctrl+Y", IsRegistered: false}}) + }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+Y"}}%s`, EventNameGlobalShortcutsCmdIsRegistered, gs.id, "\n"), + EventNameGlobalShortcutsEventIsRegistered, &Event{GlobalShortcuts: &EventGlobalShortcuts{Accelerator: "Ctrl+Y", IsRegistered: false}}) // Unregister testObjectAction(t, func() error { return gs.Unregister("Ctrl+Z") - }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+Z"}}%s`, EventNameGlobalShortcutCmdUnregister, gs.id, "\n"), - EventNameGlobalShortcutEventProcessFinished, nil) + }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s","globalShortcuts":{"accelerator":"Ctrl+Z"}}%s`, EventNameGlobalShortcutsCmdUnregister, gs.id, "\n"), + EventNameGlobalShortcutsEventUnregistered, nil) // UnregisterAll testObjectAction(t, func() error { return gs.UnregisterAll() - }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s"}%s`, EventNameGlobalShortcutCmdUnregisterAll, gs.id, "\n"), - EventNameGlobalShortcutEventProcessFinished, nil) + }, gs.object, wrt, fmt.Sprintf(`{"name":"%s","targetID":"%s"}%s`, EventNameGlobalShortcutsCmdUnregisterAll, gs.id, "\n"), + EventNameGlobalShortcutsEventUnregisteredAll, nil) }