gocui/eventMouseMove.go

135 lines
3.4 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
// 2025 note by jcarr:
// this is one of the coolest things ever worked with.
// Personally, I've been working on making a gocui GO plugin
// so I can use it as a generalized console GUI toolkit.
//
// Well done everyone that has contributed to this gocui project !!!
// I am in your debt. Happy hacking & peace.
package main
import (
"fmt"
"github.com/awesome-gocui/gocui"
log "go.wit.com/log"
"go.wit.com/widget"
)
var currentDrag *guiWidget
// this function uses the mouse position to highlight & unhighlight things
// this is run every time the user moves the mouse over the terminal window
func mouseMove(g *gocui.Gui) {
w, h := g.MousePosition()
if me.supermouse {
for _, tk := range findByXY(w, h) {
s := fmt.Sprintf("SM (%3d,%3d)", w, h)
tk.dumpWidget(s)
}
}
if me.globalMouseDown {
// log.Info("msgMouseDown == true")
// plugin will segfault if you don't keep this inside a check for msgMouseDown
// don't move this code out of here
var found bool = false
if currentDrag != nil {
currentDrag.moveNew(g)
return
}
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Window {
currentDrag = tk
return
}
}
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Flag {
currentDrag = tk
// tk.moveNew(g)
return
}
}
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Stdout {
currentDrag = tk
// tk.moveNew(g)
return
}
if tk.node.WidgetType == widget.Label {
currentDrag = tk
// tk.moveNew(g)
return
}
found = true
}
if !found {
log.Info(fmt.Sprintf("findByXY() empty. nothing to move at (%d,%d)", w, h))
}
}
if widgetView, _ := g.View("msg"); widgetView == nil {
if createStdout(g) {
return
}
return
}
for _, view := range g.Views() {
view.Highlight = false
}
if v, err := g.ViewByPosition(w, h); err == nil {
v.Highlight = true
}
}
// this is how the window gets dragged around
func (tk *guiWidget) moveNew(g *gocui.Gui) {
w, h := g.MousePosition()
if tk.node.WidgetType == widget.Window {
w1, h1 := tk.Size()
g.SetView(tk.cuiName, w, h, w+w1, h+h1, 0)
tk.verifyRect()
s := fmt.Sprintf("move(%dx%d) %s ###", w, h, tk.cuiName)
tk.dumpWidget(s)
return
}
if tk.node.WidgetType == widget.Flag {
// outputW, outputH := tk.Size()
// g.SetView(tk.cuiName, w-xOffset, h-yOffset, w-xOffset+outputW+20, h-yOffset+outputH+me.FramePadH+20, 0)
g.SetView(tk.cuiName, w-3, h-3, w+20, h+20, 0)
tk.verifyRect()
s := fmt.Sprintf("move(%dx%d) %s ###", w, h, tk.cuiName)
tk.dumpWidget(s)
// me.startOutputW = w - xOffset
// me.startOutputH = h - yOffset
// g.SetViewOnBottom(tk.cuiName)
return
} else {
log.Info("NOT MOVE FLAG. PASSING MOVE TO MSG", tk.node.WidgetType)
}
tk.dumpWidget("moveNew() on " + tk.cuiName)
outputW := 180
outputH := 40
g.SetView("msg", w-xOffset, h-yOffset, w-xOffset+outputW, h-yOffset+outputH+me.FramePadH, 0)
me.startOutputW = w - xOffset
me.startOutputH = h - yOffset
g.SetViewOnBottom("msg")
}
func createStdout(g *gocui.Gui) bool {
makeOutputWidget(g, "this is a create before a mouse click")
if me.logStdout != nil {
msg := fmt.Sprintf("test out gocuiEvent() %d\n", me.ecount)
// me.logStdout.v.Write([]byte(msg))
me.logStdout.Write([]byte(msg))
log.Log(NOW, "logStdout test out")
}
return true
}