v0.13.0 976176a

shortcuts.go RawHistoryFolder

1package webkit
2
3import (
4	"log"
5	"strings"
6
7	"github.com/diamondburned/gotk4/pkg/gdk/v4"
8	"github.com/diamondburned/gotk4/pkg/glib/v2"
9	"github.com/diamondburned/gotk4/pkg/gtk/v4"
10)
11
12// WindowShortcuts manages keyboard shortcuts for a window
13type WindowShortcuts struct {
14	window     *Window
15	shortcuts  map[string]*gtk.Shortcut
16	controller *gtk.ShortcutController
17}
18
19// NewWindowShortcuts creates a new WindowShortcuts manager
20func NewWindowShortcuts(win *Window) *WindowShortcuts {
21	controller := gtk.NewShortcutController()
22
23	// Set scope to GLOBAL so shortcuts work at the window level
24	// and aren't captured by child widgets (like the WebView)
25	controller.SetScope(gtk.ShortcutScopeGlobal)
26
27	if win != nil && win.win != nil {
28		win.win.AddController(controller)
29	}
30
31	return &WindowShortcuts{
32		window:     win,
33		shortcuts:  make(map[string]*gtk.Shortcut),
34		controller: controller,
35	}
36}
37
38// InitializeGlobalShortcuts initializes global keyboard shortcuts for a window
39func (w *Window) InitializeGlobalShortcuts(shortcuts map[string]func()) {
40	if w == nil || w.win == nil {
41		return
42	}
43
44	// Create event controller for keyboard events
45	controller := gtk.NewEventControllerKey()
46
47	controller.ConnectKeyPressed(func(keyval, keycode uint, state gdk.ModifierType) bool {
48		// Handle keyboard shortcuts
49		// TODO: Implement proper shortcut matching
50		return false
51	})
52
53	w.win.AddController(controller)
54}
55
56// RegisterShortcut registers a keyboard shortcut
57func (ws *WindowShortcuts) RegisterShortcut(accelerator string, handler func()) {
58	if ws == nil || ws.controller == nil {
59		return
60	}
61
62	// Convert to GTK format (e.g., "ctrl+plus" -> "<Control>plus")
63	gtkFormat := convertToGtkFormat(accelerator)
64	if gtkFormat == "" {
65		log.Printf("[shortcuts] Invalid key format: %s", accelerator)
66		return
67	}
68
69	// Create a callback action that will trigger our handler
70	// ShortcutFunc signature: func(widget Widgetter, args *glib.Variant) (ok bool)
71	action := gtk.NewCallbackAction(func(widget gtk.Widgetter, args *glib.Variant) bool {
72		if handler != nil {
73			log.Printf("[shortcuts] Shortcut triggered: %s", accelerator)
74			handler()
75		}
76		// Return false to allow event propagation to WebView
77		// This lets JavaScript KeyboardService also handle events if needed
78		return false
79	})
80
81	// Parse the accelerator string into a GDK key trigger
82	// GTK4 uses gtk.ShortcutTrigger for key combinations
83	trigger := gtk.NewShortcutTriggerParseString(gtkFormat)
84	if trigger == nil {
85		log.Printf("[shortcuts] Failed to parse trigger: %s", gtkFormat)
86		return
87	}
88
89	// Create the shortcut with trigger and action
90	shortcut := gtk.NewShortcut(trigger, action)
91
92	// Add shortcut to the shared controller
93	ws.controller.AddShortcut(shortcut)
94
95	// Store the shortcut for potential cleanup
96	ws.shortcuts[accelerator] = shortcut
97
98	log.Printf("[shortcuts] Registered: %s -> %s", accelerator, gtkFormat)
99}
100
101// convertToGtkFormat converts common key formats to GTK shortcut format
102// Examples: "ctrl+plus" -> "<Control>plus", "ctrl+l" -> "<Control>l"
103func convertToGtkFormat(key string) string {
104	key = strings.ToLower(key)
105
106	// Handle cmdorctrl -> Control
107	key = strings.ReplaceAll(key, "cmdorctrl+", "ctrl+")
108	key = strings.ReplaceAll(key, "cmd+", "ctrl+")
109
110	// Split by + to get components
111	components := strings.Split(key, "+")
112	if len(components) == 0 {
113		return ""
114	}
115
116	var parts []string
117	for i, part := range components {
118		if i == len(components)-1 {
119			// Last part is the actual key
120			switch part {
121			case "f12":
122				parts = append(parts, "F12")
123			case "f5":
124				parts = append(parts, "F5")
125			case "plus":
126				parts = append(parts, "plus")
127			case "equal":
128				parts = append(parts, "equal")
129			case "minus":
130				parts = append(parts, "minus")
131			case "0":
132				parts = append(parts, "0")
133			case "left", "arrowleft":
134				parts = append(parts, "Left")
135			case "right", "arrowright":
136				parts = append(parts, "Right")
137			case "up", "arrowup":
138				parts = append(parts, "Up")
139			case "down", "arrowdown":
140				parts = append(parts, "Down")
141			default:
142				// Single letter keys like l, f, c, w, etc.
143				parts = append(parts, part)
144			}
145		} else {
146			// Modifier keys
147			switch part {
148			case "ctrl":
149				parts = append(parts, "<Control>")
150			case "shift":
151				parts = append(parts, "<Shift>")
152			case "alt":
153				parts = append(parts, "<Alt>")
154			case "super", "meta":
155				parts = append(parts, "<Super>")
156			}
157		}
158	}
159
160	return strings.Join(parts, "")
161}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close