v0.1.0 47aaa0f

interaction.go RawHistoryFolder

1package ui
2
3import (
4	"math"
5	"strconv"
6	"strings"
7	"time"
8
9	"github.com/bnema/nefergui/internal/css"
10	"github.com/bnema/nefergui/internal/edit"
11	"github.com/bnema/nefergui/internal/keyboard"
12	"github.com/bnema/nefergui/internal/layout"
13	"github.com/bnema/nefergui/internal/text"
14)
15
16// platformInput is the UI-loop port. Coordinates and viewport dimensions are
17// logical pixels; the platform converts physical pointer coordinates once.
18type platformInput struct {
19	Kind                 string // motion, press, release, axis, leave, key, focus-in, focus-out, resize
20	X, Y, DX, DY         float64
21	Width, Height, Scale float64
22	Key                  keyboard.Key
23	Button               uint32 // evdev button code (BTN_LEFT is primary)
24	Shift, Ctrl          bool
25	IME                  edit.IMEBatch
26	Time                 time.Time // optional event timestamp; zero uses receipt time
27}
28
29const pointerPrimary uint32 = 0x110 // Linux BTN_LEFT
30
31type interactionState struct {
32	hover, capture, focus, space *identity
33	button                       uint32
34	down, visible, inside        bool
35	x, y                         float64
36	lastTextPress                time.Time
37	lastTextX, lastTextY         float64
38	lastTextTarget               *identity
39	scroll                       map[string]layout.Size
40}
41
42func (s interactionState) flags(e *element) css.StateFlags {
43	var flags css.StateFlags
44	if e.identity.same(s.hover) {
45		flags |= css.Hover
46	}
47	if (e.identity.same(s.capture) && s.down && s.button == pointerPrimary) || (s.space != nil && e.identity.same(s.space)) {
48		flags |= css.Active
49	}
50	if e.identity.same(s.focus) {
51		flags |= css.Focus
52		if s.visible {
53			flags |= css.FocusVisible
54		}
55	}
56	if e.disabled {
57		flags |= css.Disabled
58	}
59	if e.checked {
60		flags |= css.Checked
61	}
62	return flags
63}
64
65// idKey is the stable string form of an identity path, computed once when
66// the identity is built so identities stay immutable and safe to share.
67func idKey(i *identity) string {
68	if i == nil {
69		return ""
70	}
71	if i.path == "" {
72		return identityPath(i)
73	}
74	return i.path
75}
76func identityPath(i *identity) string {
77	switch {
78	case i.parent == nil:
79		return "root"
80	case i.explicit:
81		return idKey(i.parent) + "/k" + strconv.Itoa(len(i.key)) + ":" + i.key
82	default:
83		return idKey(i.parent) + "/p" + strconv.Itoa(i.position) + ":" + strconv.Itoa(len(i.typ)) + ":" + i.typ
84	}
85}
86func layoutKind(e *element) layout.Kind {
87	switch e.typ {
88	case "row":
89		return layout.Row
90	case "column":
91		return layout.Column
92	case "stack":
93		return layout.Stack
94	case "scroll":
95		return layout.Scroll
96	// Labelled controls measure and paint their label like text.
97	case "text", "heading", "input", "textarea", "button", "checkbox", "radio", "slider":
98		return layout.Text
99	case "image":
100		return layout.Image
101	default:
102		return layout.Box
103	}
104}
105func (r *runtime) layoutTree(e *element) *layout.Node {
106	n := &layout.Node{ID: idKey(e.identity), Kind: layoutKind(e), Content: e.text, ImageSize: e.imageSize, Image: e.image, NoWrap: e.typ == "input"}
107	if e.computed != nil {
108		n.Style = e.computed.Style
109	}
110	if offset, ok := r.state.scroll[n.ID]; ok {
111		n.ScrollX, n.ScrollY = offset.W, offset.H
112	}
113	if e.typ == "input" || e.typ == "textarea" {
114		// Editor clips are intrinsic even if the authored CSS leaves overflow visible.
115		n.EditorScroll = true
116	}
117	for _, child := range e.children {
118		n.Children = append(n.Children, r.layoutTree(child))
119	}
120	return n
121}
122func (r *runtime) lookup(id string) *element {
123	var walk func(*element) *element
124	walk = func(e *element) *element {
125		if e == nil {
126			return nil
127		}
128		if idKey(e.identity) == id {
129			return e
130		}
131		for _, child := range e.children {
132			if found := walk(child); found != nil {
133				return found
134			}
135		}
136		return nil
137	}
138	return walk(r.committed)
139}
140func (r *runtime) hit(x, y float64) *element { return r.lookup(layout.Hit(r.output.Tree, x, y)) }
141
142// cursor is the CSS cursor of the element under the pointer, from the last
143// committed styles. While a primary button is captured, the capturing element
144// keeps its cursor even if the pointer leaves it (as when dragging a slider).
145// auto and unknown values map to default.
146func (r *runtime) cursor() css.Keyword {
147	r.mu.Lock()
148	defer r.mu.Unlock()
149	id := r.state.hover
150	if r.state.down && r.state.capture != nil {
151		id = r.state.capture
152	}
153	if !r.state.inside || id == nil {
154		return css.KeywordDefault
155	}
156	if e := r.lookup(idKey(id)); e != nil && e.computed != nil {
157		return e.computed.Style.Cursor
158	}
159	return css.KeywordDefault
160}
161func focusable(e *element) bool {
162	if e == nil || e.disabled {
163		return false
164	}
165	switch e.typ {
166	case "button", "checkbox", "input", "radio", "slider", "textarea":
167		return true
168	}
169	return false
170}
171func (r *runtime) focusOrder() []*element {
172	var result []*element
173	var walk func(*layout.Result)
174	walk = func(n *layout.Result) {
175		if n == nil {
176			return
177		}
178		e := r.lookup(n.ID)
179		if focusable(e) {
180			result = append(result, e)
181		}
182		for _, child := range n.Children {
183			walk(child)
184		}
185	}
186	walk(r.output.Tree)
187	return result
188}
189func (r *runtime) setFocus(e *element, visible bool) bool {
190	var id *identity
191	if focusable(e) {
192		id = e.identity
193	}
194	if r.state.focus.same(id) && (id == nil || r.state.visible == visible) {
195		return false
196	}
197	if !r.state.focus.same(id) {
198		r.state.space = nil // changing focus cancels a held Space
199	}
200	r.state.focus, r.state.visible = id, id != nil && visible
201	return true
202}
203func (r *runtime) tab(reverse bool) bool {
204	order := r.focusOrder()
205	if len(order) == 0 {
206		return r.setFocus(nil, false)
207	}
208	idx := -1
209	for i, e := range order {
210		if e.identity.same(r.state.focus) {
211			idx = i
212			break
213		}
214	}
215	if reverse {
216		if idx < 0 {
217			idx = 0
218		}
219		idx = (idx - 1 + len(order)) % len(order)
220	} else {
221		idx = (idx + 1) % len(order)
222	}
223	return r.setFocus(order[idx], true)
224}
225func scrollable(e *element, n *layout.Result) bool {
226	if e == nil || n == nil || !n.Clipped || e.computed == nil {
227		return false
228	}
229	st := e.computed.Style
230	return e.typ == "scroll" || st.OverflowX == css.KeywordScroll || st.OverflowX == css.KeywordAuto || st.OverflowY == css.KeywordScroll || st.OverflowY == css.KeywordAuto
231}
232
233// physicalToLogical is used only at the platform boundary, never for layout or Hit.
234func physicalToLogical(x, y, scale float64) (float64, float64, bool) {
235	if !validPoint(x, y) || !validPoint(scale, 0) || scale <= 0 {
236		return 0, 0, false
237	}
238	return x / scale, y / scale, true
239}
240func validPoint(x, y float64) bool {
241	return !math.IsNaN(x) && !math.IsNaN(y) && !math.IsInf(x, 0) && !math.IsInf(y, 0)
242}
243func (r *runtime) route(in platformInput) {
244	r.mu.Lock()
245	changed := false
246	s := &r.state
247	queue := func(e *element, kind string) {
248		if e != nil {
249			r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: kind})
250			changed = true
251		}
252	}
253	switch in.Kind {
254	case "resize":
255		if validPoint(in.Width, in.Height) && in.Width >= 0 && in.Height >= 0 && (r.width != in.Width || r.height != in.Height) {
256			r.width, r.height = in.Width, in.Height
257			changed = true
258		}
259		if in.Scale > 0 && !math.IsInf(in.Scale, 0) && !math.IsNaN(in.Scale) && r.scale != in.Scale {
260			r.scale = in.Scale
261			changed = true
262		}
263	case "focus-out":
264		if s.inside || s.hover != nil || s.down || s.capture != nil || s.space != nil {
265			s.inside = false
266			s.hover = nil
267			s.capture = nil
268			s.down = false
269			s.space = nil
270			changed = true
271		}
272		if r.setFocus(nil, false) {
273			changed = true
274		}
275	case "focus-in": // keyboard focus returns only after an explicit traversal/click
276	case "ime-done":
277		if e := r.lookup(idKey(s.focus)); focusable(e) && (e.typ == "input" || e.typ == "textarea") {
278			r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "ime-done", IME: in.IME})
279			changed = true
280		}
281	case "leave":
282		// A pointer leave is not a button release. Keep capture for the
283		// matching release, but do not hit-test its stale coordinates.
284		if s.inside || s.hover != nil {
285			s.inside = false
286			s.hover = nil
287			changed = true
288		}
289	case "motion", "press", "release", "axis":
290		if !validPoint(in.X, in.Y) {
291			break
292		}
293		// A release after leave must not turn stale in-surface coordinates
294		// into a new hover or an activation.
295		if in.Kind != "release" || s.inside {
296			s.x, s.y = in.X, in.Y
297			s.inside = true
298		}
299		var target *element
300		if s.inside {
301			target = r.hit(in.X, in.Y)
302		}
303		var hover *identity
304		if target != nil {
305			hover = target.identity
306		}
307		if !s.hover.same(hover) {
308			s.hover = hover
309			changed = true
310		}
311		if s.down && s.button == pointerPrimary && in.Kind == "motion" {
312			if captured := r.lookup(idKey(s.capture)); focusable(captured) && (captured.typ == "input" || captured.typ == "textarea") {
313				r.pending = append(r.pending, inputEvent{Target: captured.identity, Kind: "edit-pointer", X: in.X, Y: in.Y, Shift: true})
314				changed = true
315			}
316			if captured := r.lookup(idKey(s.capture)); focusable(captured) && captured.typ == "slider" {
317				r.pending = append(r.pending, inputEvent{Target: captured.identity, Kind: "slider-pointer", X: in.X, Track: resultByID(r.output.Tree, idKey(captured.identity)).Content})
318				changed = true
319			}
320		}
321		switch in.Kind {
322		case "press":
323			if !s.down {
324				if in.Button != pointerPrimary || target == nil || (target.typ != "input" && target.typ != "textarea") {
325					s.lastTextPress = time.Time{}
326				}
327				s.down = true
328				s.button = in.Button
329				changed = true
330				if target != nil {
331					s.capture = target.identity
332				}
333				if in.Button == pointerPrimary && r.setFocus(target, false) {
334					changed = true
335				}
336				if in.Button == pointerPrimary && focusable(target) && (target.typ == "input" || target.typ == "textarea") {
337					at := in.Time
338					if at.IsZero() {
339						at = time.Now()
340					}
341					double := target.identity.same(s.lastTextTarget) && !s.lastTextPress.IsZero() && at.Sub(s.lastTextPress) >= 0 && at.Sub(s.lastTextPress) <= 500*time.Millisecond && math.Hypot(in.X-s.lastTextX, in.Y-s.lastTextY) <= 4
342					kind := "edit-pointer"
343					if double {
344						kind = "edit-word"
345					}
346					r.pending = append(r.pending, inputEvent{Target: target.identity, Kind: kind, X: in.X, Y: in.Y, Shift: in.Shift})
347					s.lastTextPress, s.lastTextX, s.lastTextY, s.lastTextTarget = at, in.X, in.Y, target.identity
348					changed = true
349				}
350				if in.Button == pointerPrimary && focusable(target) && target.typ == "slider" {
351					r.pending = append(r.pending, inputEvent{Target: target.identity, Kind: "slider-pointer", X: in.X, Track: resultByID(r.output.Tree, idKey(target.identity)).Content})
352					changed = true
353				}
354			}
355		case "release":
356			if s.down && in.Button == s.button {
357				if s.button == pointerPrimary {
358					if captured := r.lookup(idKey(s.capture)); focusable(captured) && captured.typ == "slider" && s.inside {
359						r.pending = append(r.pending, inputEvent{Target: captured.identity, Kind: "slider-pointer", X: in.X, Track: resultByID(r.output.Tree, idKey(captured.identity)).Content})
360						changed = true
361					}
362					if captured := r.lookup(idKey(s.capture)); captured != nil && captured == target && resultByID(r.output.Tree, idKey(s.capture)) != nil && focusable(captured) {
363						if captured.typ == "button" || captured.typ == "checkbox" || captured.typ == "radio" {
364							queue(captured, "activate")
365						}
366					}
367				}
368				s.down = false
369				s.capture = nil
370				changed = true
371			}
372		case "axis":
373			if !validPoint(in.DX, in.DY) {
374				break
375			}
376			for e := target; e != nil; e = e.parent {
377				n := resultByID(r.output.Tree, idKey(e.identity))
378				if !scrollable(e, n) {
379					continue
380				}
381				old := s.scroll[n.ID]
382				next := layout.Size{W: math.Max(0, math.Min(n.ContentSize.W-n.Content.W, old.W+in.DX)), H: math.Max(0, math.Min(n.ContentSize.H-n.Content.H, old.H+in.DY))}
383				if next != old {
384					if s.scroll == nil {
385						s.scroll = make(map[string]layout.Size)
386					}
387					s.scroll[n.ID] = next
388					changed = true
389				}
390				break
391			}
392		}
393	case "key":
394		name := strings.ToLower(in.Key.Name)
395		if name == "space" && !in.Key.Pressed {
396			if s.space != nil {
397				if e := r.lookup(idKey(s.space)); e != nil && e.identity.same(s.focus) && focusable(e) && resultByID(r.output.Tree, idKey(s.space)) != nil {
398					queue(e, "activate")
399				}
400				s.space = nil
401				changed = true
402			}
403			break
404		}
405		if !in.Key.Pressed {
406			break
407		}
408		if e := r.lookup(idKey(s.focus)); focusable(e) && (e.typ == "input" || e.typ == "textarea" || e.typ == "slider") {
409			if name == "tab" || name == "iso_left_tab" {
410				if !in.Key.Repeat && r.tab(in.Shift || name == "iso_left_tab") {
411					changed = true
412				}
413				break
414			}
415			if e.typ == "slider" {
416				switch name {
417				case "left", "right", "up", "down", "home", "end", "page_up", "page_down":
418					queue(e, "slider-key")
419					r.pending[len(r.pending)-1].Text = name
420				}
421			} else {
422				switch name {
423				case "left", "right", "up", "down", "home", "end", "backspace", "delete":
424					r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "edit-key", Text: name, Ctrl: in.Ctrl, Shift: in.Shift})
425					changed = true
426				case "a", "c", "x", "v":
427					if in.Ctrl && !in.Key.Repeat {
428						r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "edit-key", Text: name, Ctrl: true, Shift: in.Shift})
429						changed = true
430					} else if !in.Ctrl && in.Key.Text != "" && !r.composing(e.identity) {
431						r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "text", Text: in.Key.Text})
432						changed = true
433					}
434				case "return", "kp_enter":
435					if e.typ == "textarea" {
436						if !r.composing(e.identity) {
437							r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "text", Text: "\n"})
438							changed = true
439						}
440					} else {
441						queue(e, "submit")
442					}
443				default:
444					if in.Key.Text != "" && !in.Ctrl && !r.composing(e.identity) {
445						r.pending = append(r.pending, inputEvent{Target: e.identity, Kind: "text", Text: in.Key.Text})
446						changed = true
447					}
448				}
449			}
450			break
451		}
452		if in.Key.Repeat {
453			break
454		}
455		switch name {
456		case "escape":
457			if s.space != nil {
458				s.space = nil
459				changed = true
460			}
461		case "tab", "iso_left_tab":
462			if r.tab(in.Shift || name == "iso_left_tab") {
463				changed = true
464			}
465		case "return", "kp_enter":
466			if e := r.lookup(idKey(s.focus)); focusable(e) && resultByID(r.output.Tree, idKey(s.focus)) != nil && (e.typ == "button" || e.typ == "checkbox" || e.typ == "radio") {
467				queue(e, "activate")
468			}
469		case "space":
470			if e := r.lookup(idKey(s.focus)); s.space == nil && focusable(e) && resultByID(r.output.Tree, idKey(s.focus)) != nil && (e.typ == "button" || e.typ == "checkbox" || e.typ == "radio") {
471				s.space = e.identity
472				changed = true
473			}
474		default:
475		}
476	}
477	if changed {
478		r.redraw = true
479	}
480	r.mu.Unlock()
481	if changed {
482		select {
483		case r.wake <- struct{}{}:
484		default:
485		}
486	}
487}
488func resultByID(n *layout.Result, id string) *layout.Result {
489	if n == nil {
490		return nil
491	}
492	if n.ID == id {
493		return n
494	}
495	for _, child := range n.Children {
496		if found := resultByID(child, id); found != nil {
497			return found
498		}
499	}
500	return nil
501}
502
503// viewport and text engine are UI-loop inputs, never physical layout units.
504func (r *runtime) setTextEngine(engine *text.Engine) {
505	r.mu.Lock()
506	r.textEngine = engine
507	r.mu.Unlock()
508	r.Redraw()
509}
510
511// composing reports an input-method composition in progress on the editor.
512// Keys reaching wl_keyboard were not grabbed by the input method and are typed,
513// except while a preedit is shown: a non-grabbing input method owns that text
514// until it commits, so plain keys would interleave with the composition.
515func (r *runtime) composing(id *identity) bool {
516	editor := r.edits[idKey(id)]
517	return editor != nil && editor.IMEActive() && editor.Preedit != ""
518}

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 1

Tags 12

↑↓ moveenter openesc close