turbo-editors/turbo-corepublic Fork 0
main
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.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
view.go · 73 lines · 2.4 KBGo Blame HistoryRaw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ui

import (
	"github.com/gdamore/tcell/v2"

	"codeberg.org/turbo-editors/turbo-core/theme"
)

// Widget is anything the framework draws and hands events to.
//
// Bounds are in absolute screen coordinates. That makes hit-testing a mouse
// click a plain rectangle test, at the cost of every container having to place
// its children in screen space — a trade this package makes everywhere.
//
// HandleKey and HandleMouse return whether they consumed the event. An
// unconsumed event travels on to whatever is behind, which is how a click that
// misses every control lands on the window underneath.
type Widget interface {
	Bounds() Rect
	SetBounds(r Rect)
	Draw(p *Painter, th *theme.Theme)
	HandleKey(ev *tcell.EventKey) bool
	HandleMouse(ev *tcell.EventMouse) bool
}

// Focusable is a widget that can hold the keyboard focus inside a dialog.
type Focusable interface {
	Widget
	SetFocused(focused bool)
	Focused() bool
	CanFocus() bool
}

// Box is the state every widget has: where it is. Embed it to get Bounds,
// SetBounds and event handlers that ignore everything, then override only what
// the widget actually reacts to.
type Box struct {
	bounds Rect
}

// Bounds returns the widget's rectangle, in absolute screen coordinates.
func (b *Box) Bounds() Rect { return b.bounds }

// SetBounds places the widget. Containers call it when they lay out.
func (b *Box) SetBounds(r Rect) { b.bounds = r }

// HandleKey ignores the key. Widgets that react to keys override this.
func (b *Box) HandleKey(*tcell.EventKey) bool { return false }

// HandleMouse ignores the click. Widgets that react to the mouse override it.
func (b *Box) HandleMouse(*tcell.EventMouse) bool { return false }

// hit reports whether a mouse event landed on the widget.
func (b *Box) hit(ev *tcell.EventMouse) bool {
	x, y := ev.Position()
	return b.bounds.Contains(x, y)
}

// FocusBox is Box plus the focus flag, for widgets that live in a dialog.
type FocusBox struct {
	Box
	focused bool
}

// Focused reports whether this widget currently has the keyboard focus.
func (f *FocusBox) Focused() bool { return f.focused }

// SetFocused gives or takes the keyboard focus.
func (f *FocusBox) SetFocused(focused bool) { f.focused = focused }

// CanFocus reports whether the focus ring should stop here. Widgets that are
// only decoration, such as a label, override this to return false.
func (f *FocusBox) CanFocus() bool { return true }