gocui/eventMouse.go

152 lines
3.7 KiB
Go
Raw Normal View History

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"errors"
"fmt"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
2025-02-01 18:27:42 -06:00
"go.wit.com/widget"
)
func mouseUp(g *gocui.Gui, v *gocui.View) error {
w, h := g.MousePosition()
2025-01-31 12:36:46 -06:00
// useful to debug everything that is being clicked on
/*
for _, tk := range findByXY(w, h) {
2025-02-01 13:58:53 -06:00
tk.dumpWidget("mouseUp()")
}
*/
log.Info("mouseUp() setting me.globalMouseDown = false")
me.globalMouseDown = false
2025-02-03 00:07:48 -06:00
currentDrag = nil
dropdownUnclicked(w, h)
return nil
}
// this is where you have to figure out what
// widget was underneath so you can active
// the right response for the toolkit user's app
func mouseDown(g *gocui.Gui, v *gocui.View) error {
mx, my := g.MousePosition()
2025-02-01 21:59:48 -06:00
log.Info("mouseDown() setting globalMouseDown = true")
me.globalMouseDown = true
var found bool = false
for _, tk := range findByXY(mx, my) {
2025-02-01 13:58:53 -06:00
tk.dumpWidget("mouseDown()")
2025-02-01 18:37:04 -06:00
if tk.node.WidgetType == widget.Button {
log.Info("SENDING CLICK TO Button")
tk.doWidgetClick(mx, my)
return nil
}
2025-02-01 19:42:04 -06:00
if tk.node.WidgetType == widget.Checkbox {
log.Info("SENDING CLICK TO Checkbox")
tk.doWidgetClick(mx, my)
return nil
}
2025-02-03 18:50:17 -06:00
found = true
}
for _, tk := range findByXY(mx, my) {
if tk.node.WidgetType == widget.Window {
2025-02-05 15:04:40 -06:00
tk.dragW = mx - tk.gocuiSize.w0
tk.dragH = my - tk.gocuiSize.h0
2025-02-06 04:15:36 -06:00
if (mx-tk.gocuiSize.w0 < 3) && (my-tk.gocuiSize.h0 < 3) {
log.Info("RESIZE WINDOW", tk.dragW, tk.dragH)
}
2025-02-05 15:04:40 -06:00
log.Info("SENDING CLICK TO WINDOW", tk.dragW, tk.dragH)
tk.doWidgetClick(mx-tk.dragW, my-tk.dragH)
2025-02-03 18:50:17 -06:00
return nil
}
2025-02-01 19:56:02 -06:00
if tk.node.WidgetType == widget.Label {
log.Info("IGNORE LABLE")
found = false
2025-02-06 04:15:36 -06:00
log.Info("setting mousedown to true for label")
2025-02-01 21:59:48 -06:00
// msgMouseDown = true
2025-02-01 19:56:02 -06:00
return nil
}
2025-02-01 18:37:04 -06:00
/*
if tk.node.WidgetType == widget.Label {
log.Info("SENDING CLICK TO Label")
tk.doWidgetClick(mx, my)
return nil
}
*/
found = true
}
if !found {
2025-02-01 13:58:53 -06:00
log.Log(GOCUI, fmt.Sprintf("mouseDown() found nothing at (%d,%d)", mx, my))
}
vx0, vy0, vx1, vy1, err := g.ViewPosition("msg")
if err == nil {
if mx >= vx0 && mx <= vx1 && my >= vy0 && my <= vy1 {
return msgDown(g, v)
}
}
maxX, _ := g.Size()
2025-01-31 22:56:05 -06:00
// why was this here?
// findUnderMouse()
2025-01-31 22:56:05 -06:00
// TODO: USE THIS TO MAKE TEMPORARY HELP / INSTRUCTION DIALOGS
// this message will pop up when you click on the magic thing
// figure out how this works and make it generically useful.
msg := fmt.Sprintf("This is -222 widget demo. %d,%d", mx, my)
// dropdownClicked(mx, my)
x := mx - len(msg)/2
if x < 0 {
x = 0
} else if x+len(msg)+1 > maxX-1 {
x = maxX - 1 - len(msg) - 1
}
log.Log(GOCUI, "mouseDown() about to write out message to 'globalDown' view. msg =", msg)
if v, err := g.SetView("globalDown", x, my-1, x+len(msg)+1, my+1, 0); err != nil {
if !errors.Is(err, gocui.ErrUnknownView) {
return err
}
v.WriteString(msg)
}
return nil
}
2025-02-01 19:42:04 -06:00
// this needs to go
// event triggers when you push down on a mouse button
func msgDown(g *gocui.Gui, v *gocui.View) error {
2025-02-06 02:54:50 -06:00
w, h := g.MousePosition()
2025-02-01 19:42:04 -06:00
for _, tk := range findByXY(w, h) {
tk.dumpWidget("msgDown()")
}
vx, vy, _, _, err := g.ViewPosition("msg")
if err == nil {
2025-02-06 03:09:13 -06:00
me.stdout.mouseOffsetW = w - vx
me.stdout.mouseOffsetH = h - vy
2025-02-01 19:42:04 -06:00
}
2025-02-06 04:15:36 -06:00
// did the user click in the corner of the stdout window? If so, resize the window.
cornerW := w - vx
cornerH := h - vy
if (me.stdout.w-cornerW < 4) && (me.stdout.h-cornerH < 4) {
log.Info("Resize msg", cornerW, cornerH)
me.stdout.resize = true
} else {
log.Info("not Resize msg", cornerW, cornerH)
me.stdout.resize = false
}
log.Info("setting mousedown to true for msg")
2025-02-01 21:59:48 -06:00
// msgMouseDown = true
2025-02-01 19:42:04 -06:00
return nil
}