gocui/eventMouseDrag.go

175 lines
4.6 KiB
Go
Raw Normal View History

2025-02-01 16:44:43 -06:00
// 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"
2025-02-08 08:12:39 -06:00
"time"
2025-02-01 16:44:43 -06:00
"github.com/awesome-gocui/gocui"
log "go.wit.com/log"
2025-02-01 17:03:14 -06:00
"go.wit.com/widget"
2025-02-01 16:44:43 -06:00
)
// 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) {
2025-02-07 12:42:36 -06:00
me.ok = true // this tells init() it's okay to work with gocui
2025-02-08 08:07:03 -06:00
// very useful for debugging in the past. also, just fun
2025-02-01 16:44:43 -06:00
if me.supermouse {
2025-02-08 08:07:03 -06:00
w, h := g.MousePosition()
2025-02-01 16:44:43 -06:00
for _, tk := range findByXY(w, h) {
2025-02-03 01:11:01 -06:00
s := fmt.Sprintf("SM (%3d,%3d)", w, h)
tk.dumpWidget(s)
2025-02-01 16:44:43 -06:00
}
}
2025-02-01 17:17:32 -06:00
2025-02-08 08:12:39 -06:00
if time.Since(me.mouse.down) < me.mouse.clicktime {
// log.Info("not yet")
return
}
2025-02-08 08:07:03 -06:00
2025-02-08 08:36:08 -06:00
if me.dropdown.active || me.textbox.active {
// can't drag or do anything when dropdown or textbox are visible
return
}
2025-02-08 08:47:55 -06:00
// okay, the mouse is down and it has been long enough
// the user is trying to drag something. let's figure out what
2025-02-08 08:36:08 -06:00
2025-02-08 08:07:03 -06:00
w, h := g.MousePosition()
// toggle off all highlight vies except for whatever is under the mouse
for _, view := range g.Views() {
view.Highlight = false
}
2025-02-08 08:07:03 -06:00
if v, err := g.ViewByPosition(w, h); err == nil {
v.Highlight = true
}
// create the 'msg' view if it does not yet exist // TODO: put this somewhere more correct
if widgetView, _ := g.View("msg"); widgetView == nil {
if createStdout(g) {
return
}
return
}
2025-02-08 08:47:55 -06:00
/*
if me.mouse.globalMouseDown && (me.dropdown.active || me.textbox.active) {
log.Info("can't drag while dropdown or textbox are active", w, h)
return
}
*/
if me.mouse.mouseUp {
2025-02-06 05:52:00 -06:00
return
}
2025-02-08 08:47:55 -06:00
// if me.mouse.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 me.mouse.currentDrag != nil {
// me.mouse.currentDrag.dumpWidget(fmt.Sprintf("MM (%3d,%3d)", w, h))
me.mouse.currentDrag.moveNew()
return
}
// new function that is smarter
if tk := findWindowUnderMouse(); tk != nil {
2025-02-08 09:12:35 -06:00
tk.setAsDragging()
2025-02-08 08:47:55 -06:00
return
}
// first look for windows
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Window {
2025-02-08 09:12:35 -06:00
tk.setAsDragging()
2025-02-02 23:36:33 -06:00
return
}
2025-02-08 08:47:55 -06:00
}
// now look for the STDOUT window
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Flag {
2025-02-08 09:12:35 -06:00
tk.setAsDragging()
return
}
2025-02-08 08:47:55 -06:00
}
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Stdout {
2025-02-08 09:12:35 -06:00
tk.setAsDragging()
2025-02-08 08:47:55 -06:00
// tk.moveNew()
return
2025-02-02 23:36:33 -06:00
}
2025-02-08 08:47:55 -06:00
/*
if tk.node.WidgetType == widget.Label {
2025-02-08 08:36:08 -06:00
me.mouse.currentDrag = tk
2025-02-04 11:12:13 -06:00
// tk.moveNew()
2025-02-01 17:17:32 -06:00
return
2025-02-01 17:03:14 -06:00
}
2025-02-08 08:47:55 -06:00
*/
found = true
}
if !found {
log.Info(fmt.Sprintf("findByXY() empty. nothing to move at (%d,%d)", w, h))
2025-02-01 17:03:14 -06:00
}
2025-02-01 16:44:43 -06:00
}
2025-02-08 09:12:35 -06:00
func (tk *guiWidget) setAsDragging() {
me.mouse.currentDrag = tk
tk.lastW = tk.gocuiSize.w0
tk.lastH = tk.gocuiSize.h0
}
2025-02-01 17:17:32 -06:00
// this is how the window gets dragged around
2025-02-04 11:12:13 -06:00
func (tk *guiWidget) moveNew() {
w, h := me.baseGui.MousePosition()
2025-02-03 10:21:43 -06:00
if tk.node.WidgetType == widget.Window {
2025-02-06 14:46:32 -06:00
tk.window.wasDragged = true
2025-02-08 09:12:35 -06:00
// compute the new location based off how far the mouse has moved
// since the mouse button was pressed down
newW := tk.lastW + w - me.mouse.downW
newH := tk.lastH + h - me.mouse.downH
tk.redrawWindow(newW, newH)
setThingsOnTop() // sets help, Stdout, etc on the top after windows have been redrawn
2025-02-03 10:21:43 -06:00
return
}
2025-02-02 23:36:33 -06:00
if tk.node.WidgetType == widget.Flag {
2025-02-04 11:12:13 -06:00
me.baseGui.SetView(tk.cuiName, w-3, h-3, w+20, h+20, 0)
2025-02-04 15:48:14 -06:00
// tk.verifyRect()
s := fmt.Sprintf("move(%dx%d) %s ###", w, h, tk.cuiName)
tk.dumpWidget(s)
return
2025-02-07 02:44:52 -06:00
}
if tk.node.WidgetType == widget.Stdout {
2025-02-08 08:36:08 -06:00
if me.mouse.resize {
2025-02-06 04:15:36 -06:00
newW := w - me.stdout.lastW
newH := h - me.stdout.lastH
me.stdout.w = newW
me.stdout.h = newH
log.Info("Resize true", w, h, newW, newH)
// me.stdout.lastW = w - me.stdout.mouseOffsetW
// me.stdout.lastH = h - me.stdout.mouseOffsetH
tk.relocateStdout(me.stdout.lastW, me.stdout.lastH)
} else {
2025-02-08 09:12:35 -06:00
// compute the new location based off how far the mouse has moved
// since the mouse button was pressed down
newW := tk.lastW + w - me.mouse.downW
newH := tk.lastH + h - me.mouse.downH
tk.relocateStdout(newW, newH)
2025-02-06 04:15:36 -06:00
}
}
// always place the help menu on top
2025-02-06 02:15:21 -06:00
setThingsOnTop() // sets help, Stdout, etc on the top after windows have been redrawn
2025-02-01 16:44:43 -06:00
}