Use hand pointer when hovering over buttons in ask kitten

This commit is contained in:
Kovid Goyal
2023-10-15 21:35:51 +05:30
parent d66074f19f
commit 17ce474b79
4 changed files with 81 additions and 10 deletions

View File

@@ -54,6 +54,7 @@ type Loop struct {
style_cache map[string]func(...any) string
style_ctx style.Context
atomic_update_active bool
pointer_shapes []PointerShape
// Suspend the loop restoring terminal state, and run the provided function. When it returns terminal state is
// put back to what it was before suspending unless the function returns an error or an error occurs saving/restoring state.
@@ -449,3 +450,48 @@ func (self *Loop) CopyTextToPrimarySelection(text string) {
func (self *Loop) CopyTextToClipboard(text string) {
self.copy_text_to(text, "c")
}
func (self *Loop) PushPointerShape(s PointerShape) {
self.pointer_shapes = append(self.pointer_shapes, s)
self.QueueWriteString("\x1b]22;" + s.String() + "\x1b\\")
}
func (self *Loop) PopPointerShape() {
if len(self.pointer_shapes) > 0 {
self.pointer_shapes = self.pointer_shapes[:len(self.pointer_shapes)-1]
self.QueueWriteString("\x1b]22;<\x1b\\")
}
}
func (self *Loop) ClearPointerShapes() (ans []PointerShape) {
ans = self.pointer_shapes
for i := len(self.pointer_shapes) - 1; i >= 0; i-- {
self.QueueWriteString("\x1b]22;<\x1b\\")
}
self.pointer_shapes = nil
return ans
}
func (self *Loop) SetPointerShapes(ps []PointerShape) {
self.pointer_shapes = ps
if len(ps) > 0 {
s := strings.Builder{}
s.WriteString("\x1b]22;>")
for i, x := range ps {
s.WriteString(x.String())
if i+1 < len(ps) {
s.WriteByte(',')
}
}
s.WriteString("\x1b\\")
self.QueueWriteString(s.String())
}
}
func (self *Loop) CurrentPointerShape() (ans PointerShape, has_shape bool) {
if len(self.pointer_shapes) > 0 {
has_shape = true
ans = self.pointer_shapes[len(self.pointer_shapes)-1]
}
return
}

View File

@@ -346,6 +346,7 @@ func (self *Loop) run() (err error) {
self.QueueWriteString(finalizer)
}
if needs_reset_escape_codes {
self.ClearPointerShapes()
self.QueueWriteString(self.terminal_options.ResetStateEscapeCodes())
}
// flush queued data and wait for it to be written for a timeout, then wait for writer to shutdown
@@ -365,6 +366,7 @@ func (self *Loop) run() (err error) {
}
self.SuspendAndRun = func(run func() error) (err error) {
ps := self.ClearPointerShapes()
write_id := self.QueueWriteString(self.terminal_options.ResetStateEscapeCodes())
needs_reset_escape_codes = false
if err = self.wait_for_write_to_complete(write_id, self.tty_write_channel, write_done_channel, 2*time.Second); err != nil {
@@ -386,11 +388,13 @@ func (self *Loop) run() (err error) {
return err
}
write_id = self.QueueWriteString(self.terminal_options.SetStateEscapeCodes())
self.SetPointerShapes(ps)
needs_reset_escape_codes = true
return self.wait_for_write_to_complete(write_id, self.tty_write_channel, write_done_channel, 2*time.Second)
}
self.on_SIGTSTP = func() error {
ps := self.ClearPointerShapes()
write_id := self.QueueWriteString(self.terminal_options.ResetStateEscapeCodes())
needs_reset_escape_codes = false
err := self.wait_for_write_to_complete(write_id, self.tty_write_channel, write_done_channel, 2*time.Second)
@@ -406,6 +410,7 @@ func (self *Loop) run() (err error) {
return err
}
write_id = self.QueueWriteString(self.terminal_options.SetStateEscapeCodes())
self.SetPointerShapes(ps)
needs_reset_escape_codes = true
err = self.wait_for_write_to_complete(write_id, self.tty_write_channel, write_done_channel, 2*time.Second)
if err != nil {