gocui/eventMouse.go

150 lines
3.8 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"fmt"
"time"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
"go.wit.com/widget"
)
func mouseUp(g *gocui.Gui, v *gocui.View) error {
// useful to debug everything that is being clicked on
/*
for _, tk := range findByXY(w, h) {
tk.dumpWidget("mouseUp()")
}
*/
me.mouse.mouseUp = true
// me.mouse.globalMouseDown = false
me.mouse.currentDrag = nil
if me.mouse.double && (time.Since(me.mouse.down) < me.mouse.doubletime) {
me.mouse.double = false
doMouseDoubleClick(me.mouse.downW, me.mouse.downH)
return nil
}
me.mouse.double = false
if time.Since(me.mouse.down) < me.mouse.clicktime {
doMouseClick(me.mouse.downW, me.mouse.downH)
}
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 {
if me.mouse.mouseUp {
if time.Since(me.mouse.down) < me.mouse.doubletime {
me.mouse.double = true
}
// me.mouse.globalMouseDown = true
me.mouse.mouseUp = false
me.mouse.down = time.Now()
w, h := g.MousePosition()
me.mouse.downW = w
me.mouse.downH = h
win := findWindowUnderMouse()
if win != nil {
w, h := g.MousePosition()
s := fmt.Sprintf("mouse(%d,%d) ", w, h)
offW := win.gocuiSize.w1 - w
offH := win.gocuiSize.h1 - h
s += fmt.Sprintf("corner(%d,%d)", offW, offH)
if (offW < 3) && (offH < 3) {
// log.Info("mouse down resize on ", s)
me.mouse.resize = true
// store the stdout corner for computing the drag size
me.stdout.lastW = me.stdout.tk.gocuiSize.w0
me.stdout.lastH = me.stdout.tk.gocuiSize.h0
} else {
// log.Info("mouse down resize off", s)
me.mouse.resize = false
}
}
return nil
}
if time.Since(me.mouse.down) < me.mouse.clicktime {
log.Info("not yet")
return nil
}
w, h := g.MousePosition()
// if the dropdown is active, never do anything else
if me.dropdown.active {
log.Info("mouseDown() stopping here. dropdwon menu is in effect")
for _, tk := range findByXY(w, h) {
if (tk.node.WidgetType == widget.Flag) && (tk == me.dropdown.tk) {
// log.Info("SENDING CLICK TO Flag (dropdown)")
tk.doWidgetClick(w, h)
me.dropdown.active = false
return nil
}
}
log.Info("never found dropdown at", w, h)
return nil
}
// if the textbox is active, never do anything else
if me.textbox.active {
log.Info("mouseDown() stopping here. textbox widget is open")
for _, tk := range findByXY(w, h) {
if (tk.node.WidgetType == widget.Flag) && (tk == me.textbox.tk) {
me.textbox.active = false
textboxClosed()
return nil
}
}
log.Info("never found textbox at", w, h)
return nil
}
// figre out what window this is
tk := findWindowUnderMouse()
if tk == nil {
log.Info("mouseDown() nothing to click on at", w, h)
return nil
}
tk.makeWindowActive()
// log.Info("SENDING mouseDown() to findWindowUnderMouse()")
if tk.node.WidgetType == widget.Window {
// check to see if this is a direct click on a widget
for _, tk := range tk.findByXYreal(w, h) {
// tk.dumpWidget("mouseDown()")
if tk.node.WidgetType == widget.Button {
// log.Info("SENDING CLICK TO Button")
tk.doWidgetClick(w, h)
return nil
}
if tk.node.WidgetType == widget.Checkbox {
// log.Info("SENDING CLICK TO Checkbox")
tk.doWidgetClick(w, h)
return nil
}
if tk.node.WidgetType == widget.Dropdown {
// log.Info("SENDING CLICK TO Dropdown")
tk.doWidgetClick(w, h)
return nil
}
if tk.node.WidgetType == widget.Textbox {
// log.Info("SENDING CLICK TO Textbox")
tk.doWidgetClick(w, h)
return nil
}
}
}
me.mouse.currentDrag = tk
return nil
}