| 🛟 Updated. 28d5985 k33g 18h ago | 1 | package ui |
| 2 | |
| 3 | import ( |
| 4 | "github.com/gdamore/tcell/v2" |
| 5 | |
| 📦 Turbo Core f3ade8d k33g 11h ago | 6 | "rickub.com/turbo-editors/turbo-core/theme" |
| 🛟 Updated. 28d5985 k33g 18h ago | 7 | ) |
| 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. |
| 18 | type 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. |
| 27 | type 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. |
| 37 | type Box struct { |
| 38 | bounds Rect |
| 39 | } |
| 40 | |
| 41 | // Bounds returns the widget's rectangle, in absolute screen coordinates. |
| 42 | func (b *Box) Bounds() Rect { return b.bounds } |
| 43 | |
| 44 | // SetBounds places the widget. Containers call it when they lay out. |
| 45 | func (b *Box) SetBounds(r Rect) { b.bounds = r } |
| 46 | |
| 47 | // HandleKey ignores the key. Widgets that react to keys override this. |
| 48 | func (b *Box) HandleKey(*tcell.EventKey) bool { return false } |
| 49 | |
| 50 | // HandleMouse ignores the click. Widgets that react to the mouse override it. |
| 51 | func (b *Box) HandleMouse(*tcell.EventMouse) bool { return false } |
| 52 | |
| 53 | // hit reports whether a mouse event landed on the widget. |
| 54 | func (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. |
| 60 | type FocusBox struct { |
| 61 | Box |
| 62 | focused bool |
| 63 | } |
| 64 | |
| 65 | // Focused reports whether this widget currently has the keyboard focus. |
| 66 | func (f *FocusBox) Focused() bool { return f.focused } |
| 67 | |
| 68 | // SetFocused gives or takes the keyboard focus. |
| 69 | func (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. |
| 73 | func (f *FocusBox) CanFocus() bool { return true } |