Skip to content

Commit

Permalink
Initial key binding support
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Sep 17, 2023
1 parent efc86c1 commit 793191a
Show file tree
Hide file tree
Showing 9 changed files with 461 additions and 5 deletions.
52 changes: 52 additions & 0 deletions v3/examples/keybindings/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
_ "embed"
"github.com/wailsapp/wails/v3/pkg/application"
"log"
"log/slog"
)

func main() {
app := application.New(application.Options{
Name: "Key Bindings Demo",
Description: "A demo of the Key Bindings Options",
Mac: application.MacOptions{
ApplicationShouldTerminateAfterLastWindowClosed: true,
},
KeyBindings: map[string]func(window *application.WebviewWindow){
"CmdOrCtrl+C": func(window *application.WebviewWindow) {
window.Center()
},
},
LogLevel: slog.LevelDebug,
})

app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Name: "Window 1",
Title: "Window 1",
URL: "https://wails.io",
KeyBindings: map[string]func(window *application.WebviewWindow){
"F12": func(window *application.WebviewWindow) {
window.ToggleDevTools()
},
},
})

app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
Name: "Window 2",
Title: "Window 2",
URL: "https://google.com",
KeyBindings: map[string]func(window *application.WebviewWindow){
"F12": func(window *application.WebviewWindow) {
println("Window 2: Toggle Dev Tools")
},
},
})

err := app.Run()
if err != nil {
log.Fatal(err)
}

}
27 changes: 27 additions & 0 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func New(appOptions Options) *App {
os.Exit(1)
}

// Process keybindings
if result.options.KeyBindings != nil {
result.keyBindings = processKeyBindingOptions(result.options.KeyBindings)
}

return result
}

Expand Down Expand Up @@ -251,13 +256,17 @@ type App struct {
// Capabilities
capabilities capabilities.Capabilities
isDebugMode bool

// Keybindings
keyBindings map[string]func(window *WebviewWindow)
}

func (a *App) init() {
a.applicationEventListeners = make(map[uint][]*EventListener)
a.windows = make(map[uint]*WebviewWindow)
a.systemTrays = make(map[uint]*SystemTray)
a.contextMenus = make(map[string]*Menu)
a.keyBindings = make(map[string]func(window *WebviewWindow))
a.Logger = a.options.Logger
a.pid = os.Getpid()
}
Expand Down Expand Up @@ -715,6 +724,24 @@ func (a *App) runOrDeferToAppRun(r runnable) {
}
}

func (a *App) processKeyBinding(acceleratorString string, window *WebviewWindow) bool {

if a.keyBindings == nil {
return false
}

// Check key bindings
callback, ok := a.keyBindings[acceleratorString]
if !ok {
return false
}

// Execute callback
go callback(window)

return true
}

func invokeSync(fn func()) {
var wg sync.WaitGroup
wg.Add(1)
Expand Down
42 changes: 42 additions & 0 deletions v3/pkg/application/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package application

import (
"fmt"
"runtime"
"slices"
"strconv"
"strings"
)
Expand All @@ -22,6 +24,34 @@ const (
ControlKey modifier = 4 << iota
)

func (m modifier) String() string {
return modifierStringMap[runtime.GOOS][m]
}

var modifierStringMap = map[string]map[modifier]string{
"windows": {
CmdOrCtrlKey: "Ctrl",
ControlKey: "Ctrl",
OptionOrAltKey: "Alt",
ShiftKey: "Shift",
SuperKey: "Win",
},
"darwin": {
CmdOrCtrlKey: "Cmd",
ControlKey: "Ctrl",
OptionOrAltKey: "Option",
ShiftKey: "Shift",
SuperKey: "Cmd",
},
"linux": {
CmdOrCtrlKey: "Ctrl",
ControlKey: "Ctrl",
OptionOrAltKey: "Alt",
ShiftKey: "Shift",
SuperKey: "Super",
},
}

var modifierMap = map[string]modifier{
"cmdorctrl": CmdOrCtrlKey,
"cmd": CmdOrCtrlKey,
Expand All @@ -40,6 +70,18 @@ type accelerator struct {
Modifiers []modifier
}

func (a *accelerator) String() string {
result := strings.Builder{}
// Sort modifiers
slices.Sort(a.Modifiers)
for _, modifier := range a.Modifiers {
result.WriteString(modifier.String())
result.WriteString("+")
}
result.WriteString(a.Key)
return strings.ToLower(result.String())
}

var namedKeys = map[string]struct{}{
"backspace": {},
"tab": {},
Expand Down
Loading

0 comments on commit 793191a

Please sign in to comment.