Change mouse pointer shape over input area

This commit is contained in:
Kovid Goyal
2025-07-05 12:23:44 +05:30
parent 834d0d3848
commit fdf0a13687
4 changed files with 22 additions and 11 deletions

View File

@@ -41,8 +41,8 @@ func (h *Handler) draw_footer() (num_lines int, err error) {
text = sfunc(text) text = sfunc(text)
} }
buf.WriteString(text) buf.WriteString(text)
if click_name != "" { if click_name != "" && click_name != h.state.current_filter {
crs = append(crs, single_line_region{x: pos, width: sz, y: len(lines), id: click_name, callback: func(filter string) error { crs = append(crs, single_line_region{x: pos, width: sz - 1, y: len(lines), id: click_name, callback: func(filter string) error {
h.set_filter(filter) h.set_filter(filter)
h.state.redraw_needed = true h.state.redraw_needed = true
return nil return nil
@@ -59,7 +59,7 @@ func (h *Handler) draw_footer() (num_lines int, err error) {
} }
offset := h.screen_size.height - len(lines) offset := h.screen_size.height - len(lines)
for _, cr := range crs { for _, cr := range crs {
h.state.mouse_state.AddCellRegion(cr.id, cr.x, cr.y+offset, cr.x+cr.width, cr.y+offset, cr.callback) h.state.mouse_state.AddCellRegion(cr.id, cr.x, cr.y+offset, cr.x+cr.width, cr.y+offset, cr.callback).HoverStyle = "default fg=red"
} }
} }
if len(lines) > 0 { if len(lines) > 0 {

View File

@@ -187,7 +187,11 @@ type Handler struct {
func (h *Handler) draw_screen() (err error) { func (h *Handler) draw_screen() (err error) {
h.state.redraw_needed = false h.state.redraw_needed = false
h.lp.StartAtomicUpdate() h.lp.StartAtomicUpdate()
defer h.lp.EndAtomicUpdate() defer func() {
h.state.mouse_state.UpdateHoveredIds()
h.state.mouse_state.ApplyHoverStyles(h.lp)
h.lp.EndAtomicUpdate()
}()
h.lp.ClearScreen() h.lp.ClearScreen()
h.state.mouse_state.ClearCellRegions() h.state.mouse_state.ClearCellRegions()
switch h.state.screen { switch h.state.screen {
@@ -207,8 +211,6 @@ func (h *Handler) draw_screen() (err error) {
case SAVE_FILE: case SAVE_FILE:
err = h.draw_save_file_name_screen() err = h.draw_save_file_name_screen()
} }
h.state.mouse_state.UpdateHoveredIds()
h.state.mouse_state.ApplyHoverStyles(h.lp)
return return
} }

View File

@@ -53,6 +53,11 @@ func (h *Handler) draw_search_bar(y int) {
h.lp.MoveCursorTo(1+left_margin, 1+y) h.lp.MoveCursorTo(1+left_margin, 1+y)
available_width := h.screen_size.width - left_margin - right_margin available_width := h.screen_size.width - left_margin - right_margin
h.draw_frame(available_width, SEARCH_BAR_HEIGHT) h.draw_frame(available_width, SEARCH_BAR_HEIGHT)
for y1 := y; y1 < y+4; y1++ {
cr := h.state.mouse_state.AddCellRegion("search-bar", left_margin, y1, left_margin+available_width, y1)
cr.PointerShape = loop.TEXT_POINTER
cr.HoverStyle = "none"
}
h.lp.MoveCursorTo(1+left_margin+1, 2+y) h.lp.MoveCursorTo(1+left_margin+1, 2+y)
h.draw_search_text(available_width - 2) h.draw_search_text(available_width - 2)
} }

View File

@@ -4,6 +4,7 @@ package tui
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
"github.com/kovidgoyal/kitty" "github.com/kovidgoyal/kitty"
@@ -214,6 +215,7 @@ type CellRegion struct {
Id string Id string
OnClick []func(id string) error OnClick []func(id string) error
PointerShape loop.PointerShape PointerShape loop.PointerShape
HoverStyle string // set to "default" for the global hover style
} }
func (c CellRegion) Contains(x, y int) bool { // 0-based func (c CellRegion) Contains(x, y int) bool { // 0-based
@@ -237,7 +239,7 @@ type MouseState struct {
} }
} }
func (m *MouseState) AddRegion(cr CellRegion) *CellRegion { func (m *MouseState) add_region(cr CellRegion) *CellRegion {
m.regions = append(m.regions, &cr) m.regions = append(m.regions, &cr)
if m.region_id_map == nil { if m.region_id_map == nil {
m.region_id_map = make(map[string][]*CellRegion) m.region_id_map = make(map[string][]*CellRegion)
@@ -251,9 +253,8 @@ func (m *MouseState) AddRegion(cr CellRegion) *CellRegion {
} }
func (m *MouseState) AddCellRegion(id string, start_x, start_y, end_x, end_y int, on_click ...func(id string) error) *CellRegion { func (m *MouseState) AddCellRegion(id string, start_x, start_y, end_x, end_y int, on_click ...func(id string) error) *CellRegion {
cr := CellRegion{ return m.add_region(CellRegion{
TopLeft: Point{start_x, start_y}, BottomRight: Point{end_x, end_y}, Id: id, OnClick: on_click, PointerShape: loop.POINTER_POINTER} TopLeft: Point{start_x, start_y}, BottomRight: Point{end_x, end_y}, Id: id, OnClick: on_click, PointerShape: loop.POINTER_POINTER, HoverStyle: "default"})
return m.AddRegion(cr)
} }
func (m *MouseState) ClearCellRegions() { func (m *MouseState) ClearCellRegions() {
@@ -307,7 +308,10 @@ func (m *MouseState) ApplyHoverStyles(lp *loop.Loop, style ...string) {
ps := loop.DEFAULT_POINTER ps := loop.DEFAULT_POINTER
for id := range m.hovered_ids.Iterable() { for id := range m.hovered_ids.Iterable() {
for _, r := range m.region_id_map[id] { for _, r := range m.region_id_map[id] {
lp.StyleRegion(hs, r.TopLeft.X, r.TopLeft.Y, r.BottomRight.X, r.BottomRight.Y) if r.HoverStyle != "" {
s := strings.Replace(r.HoverStyle, "default", hs, 1)
lp.StyleRegion(s, r.TopLeft.X, r.TopLeft.Y, r.BottomRight.X, r.BottomRight.Y)
}
is_hovered = true is_hovered = true
ps = r.PointerShape ps = r.PointerShape
} }