From 83f34e602b050a90816de00e381c78f76831a027 Mon Sep 17 00:00:00 2001 From: citrons Date: Fri, 6 Jun 2025 00:46:36 -0500 Subject: mouse handling --- tui/event.go | 2 ++ tui/layout.go | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tui/event.go b/tui/event.go index 71d2f41..9d7a9a2 100644 --- a/tui/event.go +++ b/tui/event.go @@ -24,6 +24,7 @@ type MouseEvent struct { Button int Pressed bool Released bool + ReleasedAnywhere bool X, Y int Scroll int } @@ -155,6 +156,7 @@ func parseMouse(ev *Event, ch <-chan termfo.Event) error { ev.Mouse.Button = args[0] ev.Mouse.Pressed = down ev.Mouse.Released = !down + ev.Mouse.ReleasedAnywhere = !down } return nil diff --git a/tui/layout.go b/tui/layout.go index 4caa3a6..1835d72 100644 --- a/tui/layout.go +++ b/tui/layout.go @@ -23,6 +23,7 @@ type Box struct { computedSize [2]int computedRect rect isOverflow bool + mouseEvent MouseEvent } type BoxSize int @@ -88,7 +89,7 @@ func top() *Box { return layout.stack[len(layout.stack) - 1] } -func Push(id string, box Box) { +func Push(id string, box Box) MouseEvent { if len(layout.stack) > 0 { top().children = append(top().children, &box) } @@ -104,6 +105,10 @@ func Push(id string, box Box) { if box.IsOverflow != nil { *box.IsOverflow = b != nil && b.isOverflow } + if b != nil { + return b.mouseEvent + } + return MouseEvent{} } func Text(text string, style *Style) { @@ -456,6 +461,23 @@ func (b *Box) drawComputed(parentRect rect, parentStyle Style) { } } +func (b *Box) mouseEvents(ev MouseEvent) { + bev := ev + bev.X -= b.computedRect.min[0] + bev.Y -= b.computedRect.min[1] + bev.Pressed = false + bev.Released = false + if bev.X >= 0 && bev.Y >= 0 && + bev.X < b.computedSize[0] && bev.Y < b.computedSize[1] { + bev.Pressed = ev.Pressed + bev.Released = ev.Released + } + b.mouseEvent = bev + for _, c := range b.children { + c.mouseEvents(ev) + } +} + func DrawLayout() { defer func() { layout.back = layout.front @@ -478,4 +500,7 @@ func DrawLayout() { parentRect.max = [2]int {size.Width, size.Height} b.computedRect = parentRect b.drawComputed(parentRect, DefaultStyle) + + ev := getLastEvent() + b.mouseEvents(ev.Mouse) } -- cgit v1.2.3