diff --git a/kittens/choose_files/main.go b/kittens/choose_files/main.go index 8c2b2e6d8..23b8f93e2 100644 --- a/kittens/choose_files/main.go +++ b/kittens/choose_files/main.go @@ -413,10 +413,8 @@ func (h *Handler) OnKeyEvent(ev *loop.KeyEvent) (err error) { func (h *Handler) OnMouseEvent(event *loop.MouseEvent) (err error) { h.state.redraw_needed = h.state.mouse_state.UpdateState(event) - if event.Event_type == loop.MOUSE_CLICK && event.Buttons&loop.LEFT_MOUSE_BUTTON != 0 { - if err = h.state.mouse_state.ClickHoveredRegions(); err != nil { - return - } + if err = h.state.mouse_state.DispatchEventToHoveredRegions(event); err != nil { + return } if h.state.redraw_needed { err = h.draw_screen() diff --git a/tools/tui/mouse.go b/tools/tui/mouse.go index 2d08ad055..501dca8b5 100644 --- a/tools/tui/mouse.go +++ b/tools/tui/mouse.go @@ -210,10 +210,15 @@ type Point struct { X, Y int } +func (p Point) Sub(other Point) Point { + return Point{X: p.X - other.X, Y: p.Y - other.Y} +} + type CellRegion struct { TopLeft, BottomRight Point Id string - OnClick []func(id string) error + OnClick []func(id string) error // simple left click ignoring modifiers + OnClickEvent func(id string, ev *loop.MouseEvent, cell_offset Point) error // any click event PointerShape loop.PointerShape HoverStyle string // set to "default" for the global hover style } @@ -226,7 +231,7 @@ func (c CellRegion) Contains(x, y int) bool { // 0-based } type MouseState struct { - Cell, Pixel struct{ X, Y int } + Cell, Pixel Point Pressed struct{ Left, Right, Middle, Fourth, Fifth, Sixth, Seventh bool } regions []*CellRegion @@ -325,6 +330,35 @@ func (m *MouseState) ApplyHoverStyles(lp *loop.Loop, style ...string) { } } +func (m *MouseState) DispatchEventToHoveredRegions(ev *loop.MouseEvent) error { + if ev.Event_type != loop.MOUSE_CLICK { + return nil + } + is_simple_click := ev.Buttons&loop.LEFT_MOUSE_BUTTON != 0 + seen := utils.NewSet[string]() + for id := range m.hovered_ids.Iterable() { + for _, r := range m.region_id_map[id] { + if seen.Has(r.Id) { + continue + } + seen.Add(r.Id) + if is_simple_click { + for _, f := range r.OnClick { + if err := f(r.Id); err != nil { + return err + } + } + } + if r.OnClickEvent != nil { + if err := r.OnClickEvent(r.Id, ev, m.Cell.Sub(r.TopLeft)); err != nil { + return err + } + } + } + } + return nil +} + func (m *MouseState) ClickHoveredRegions() error { seen := utils.NewSet[string]() for id := range m.hovered_ids.Iterable() {