turbo-editors/turbo-corepublic Fork 0
v1.0.2
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

view.go · 73 lines · 2.4 KBGo Blame HistoryRaw
🛟 Updated. 28d5985 k33g 19h ago1package ui
2
3import (
4 "github.com/gdamore/tcell/v2"
5
📦 Turbo Core f3ade8d k33g 12h ago6 "rickub.com/turbo-editors/turbo-core/theme"
🛟 Updated. 28d5985 k33g 19h ago7)
8
9// Widget is anything the framework draws and hands events to.
10//
11// Bounds are in absolute screen coordinates. That makes hit-testing a mouse
12// click a plain rectangle test, at the cost of every container having to place
13// its children in screen space — a trade this package makes everywhere.
14//
15// HandleKey and HandleMouse return whether they consumed the event. An
16// unconsumed event travels on to whatever is behind, which is how a click that
17// misses every control lands on the window underneath.
18type Widget interface {
19 Bounds() Rect
20 SetBounds(r Rect)
21 Draw(p *Painter, th *theme.Theme)
22 HandleKey(ev *tcell.EventKey) bool
23 HandleMouse(ev *tcell.EventMouse) bool
24}
25
26// Focusable is a widget that can hold the keyboard focus inside a dialog.
27type Focusable interface {
28 Widget
29 SetFocused(focused bool)
30 Focused() bool
31 CanFocus() bool
32}
33
34// Box is the state every widget has: where it is. Embed it to get Bounds,
35// SetBounds and event handlers that ignore everything, then override only what
36// the widget actually reacts to.
37type Box struct {
38 bounds Rect
39}
40
41// Bounds returns the widget's rectangle, in absolute screen coordinates.
42func (b *Box) Bounds() Rect { return b.bounds }
43
44// SetBounds places the widget. Containers call it when they lay out.
45func (b *Box) SetBounds(r Rect) { b.bounds = r }
46
47// HandleKey ignores the key. Widgets that react to keys override this.
48func (b *Box) HandleKey(*tcell.EventKey) bool { return false }
49
50// HandleMouse ignores the click. Widgets that react to the mouse override it.
51func (b *Box) HandleMouse(*tcell.EventMouse) bool { return false }
52
53// hit reports whether a mouse event landed on the widget.
54func (b *Box) hit(ev *tcell.EventMouse) bool {
55 x, y := ev.Position()
56 return b.bounds.Contains(x, y)
57}
58
59// FocusBox is Box plus the focus flag, for widgets that live in a dialog.
60type FocusBox struct {
61 Box
62 focused bool
63}
64
65// Focused reports whether this widget currently has the keyboard focus.
66func (f *FocusBox) Focused() bool { return f.focused }
67
68// SetFocused gives or takes the keyboard focus.
69func (f *FocusBox) SetFocused(focused bool) { f.focused = focused }
70
71// CanFocus reports whether the focus ring should stop here. Widgets that are
72// only decoration, such as a label, override this to return false.
73func (f *FocusBox) CanFocus() bool { return true }