shithub: xui

Download patch

ref: bee85da05585c5f6d73208d10659f5860d3d8524
parent: ca677a50fd03b97e8421d5ddcc9c793338c81058
author: Philip Silva <philip.silva@protonmail.com>
date: Sun Jul 20 13:06:53 EDT 2025

up/down button events

--- a/events/mouse/mouse.go
+++ b/events/mouse/mouse.go
@@ -8,7 +8,10 @@
 	Enter Type = 1 << iota
 	Leave
 	Click
+	Down
 )
+
+const Up = Click
 
 // Mouse Point in local coordinates though
 //
--- a/field/field.go
+++ b/field/field.go
@@ -38,6 +38,8 @@
 
 	// Position of the cursor
 	Pos int
+	// Position of the selection
+	Pos2 int
 
 	// Offsets of the characters
 	Offsets []int
@@ -78,10 +80,21 @@
 			f.hover = true
 		case mouse.Leave:
 			f.hover = false
-		case mouse.Click:
+		case mouse.Down:
 			f.Pos, _ = slices.BinarySearch(f.Offsets, tev.Point.X)
 			f.Pos = slices.Max([]int{0, f.Pos-1})
 			f.updateTextImgs()
+			//log.Printf("f.Pos=%d", f.Pos)
+		case mouse.Up:
+			f.Pos2, _ = slices.BinarySearch(f.Offsets, tev.Point.X)
+			if f.Pos2 < f.Pos {
+				f.Pos2, f.Pos = f.Pos, f.Pos2
+			}
+			f.Pos2 = clamp(0, f.Pos2-1, len(f.Text)-1)
+			f.Pos2 = slices.Max([]int{0, f.Pos2-1})
+			//log.Printf("f.Pos2=%d", f.Pos2)
+			//log.Printf("selected %d/%d: %s", f.Pos, f.Pos2, f.Text[f.Pos:f.Pos2+1])
+			//f.updateTextImgs()
 		}
 
 		if f.cb != nil {
@@ -117,6 +130,12 @@
 		//log.Printf("event: call updateTextImgs")
 		f.updateTextImgs()
 	}
+}
+
+func clamp(a, x, b int) int {
+	x = slices.Min([]int{x, b})
+	x = slices.Max([]int{a, x})
+	return x
 }
 
 func (f *Field) Render() *memdraw.Image {
--- a/xui.go
+++ b/xui.go
@@ -152,6 +152,7 @@
 			})()
 		}
 	}()
+	btns := 0
 	for {
 		select {
 		case m := <-x.mousectl.C:
@@ -167,8 +168,13 @@
 				var ev mouse.Event
 
 				if m.Buttons&1 > 0 {
+					if btns&1 == 0 {
+						ev.Type |= mouse.Down
+					}
+				} else if btns&1 > 0 {
 					ev.Type |= mouse.Click
 				}
+				btns = m.Buttons
 
 
 				if x.root != nil {
@@ -183,7 +189,7 @@
 				x.mu.Lock()
 				defer x.mu.Unlock()
 
-				log.Printf("KEY %v", k)
+				//log.Printf("KEY %v", k)
 
 				ev := keyboard.Event{}
 				ev.Type = keyboard.Pressed
--