Some checks failed
Security Scan / security (push) Failing after 24s
New stub files: - browser_manager.go, browser_window.go (95 methods, full Window interface) - clipboard.go, context_menu.go, dialog.go (33 dialog methods) - environment.go, events.go, keybinding.go - menuitem.go, screen.go, services.go - webview_window_options.go (574 lines, all platform types) - window.go (Window interface ~50 methods) - window_manager_expanded.go (Get, GetByID) - application_options.go (Options, platform options, iOS/Android) App struct expanded with all manager fields. WebviewWindow and BrowserWindow both satisfy Window interface. GetAll() returns []Window (was []any). All stubs compile clean: GOWORK=off go build ./... Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package application
|
|
|
|
import "sync"
|
|
|
|
// KeyBinding pairs an accelerator string with its registered callback.
|
|
//
|
|
// binding := &KeyBinding{Accelerator: "CmdOrCtrl+K", Callback: handler}
|
|
type KeyBinding struct {
|
|
Accelerator string
|
|
Callback func(window Window)
|
|
}
|
|
|
|
// KeyBindingManager stores and dispatches global key bindings.
|
|
//
|
|
// manager.Add("CmdOrCtrl+K", func(w Window) { w.Focus() })
|
|
// handled := manager.Process("CmdOrCtrl+K", currentWindow)
|
|
type KeyBindingManager struct {
|
|
mu sync.RWMutex
|
|
bindings map[string]func(window Window)
|
|
}
|
|
|
|
// Add registers a callback for the given accelerator string.
|
|
//
|
|
// manager.Add("CmdOrCtrl+Shift+P", func(w Window) { launchCommandPalette(w) })
|
|
func (m *KeyBindingManager) Add(accelerator string, callback func(window Window)) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if m.bindings == nil {
|
|
m.bindings = make(map[string]func(window Window))
|
|
}
|
|
m.bindings[accelerator] = callback
|
|
}
|
|
|
|
// Remove deregisters the callback for the given accelerator string.
|
|
//
|
|
// manager.Remove("CmdOrCtrl+Shift+P")
|
|
func (m *KeyBindingManager) Remove(accelerator string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
delete(m.bindings, accelerator)
|
|
}
|
|
|
|
// Process fires the callback for the given accelerator and returns true if handled.
|
|
//
|
|
// if manager.Process("CmdOrCtrl+K", window) { return }
|
|
func (m *KeyBindingManager) Process(accelerator string, window Window) bool {
|
|
m.mu.RLock()
|
|
callback, exists := m.bindings[accelerator]
|
|
m.mu.RUnlock()
|
|
if exists && callback != nil {
|
|
callback(window)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// GetAll returns a snapshot of all registered key bindings.
|
|
//
|
|
// for _, kb := range manager.GetAll() { log(kb.Accelerator) }
|
|
func (m *KeyBindingManager) GetAll() []*KeyBinding {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
bindings := make([]*KeyBinding, 0, len(m.bindings))
|
|
for accelerator, callback := range m.bindings {
|
|
bindings = append(bindings, &KeyBinding{
|
|
Accelerator: accelerator,
|
|
Callback: callback,
|
|
})
|
|
}
|
|
return bindings
|
|
}
|