gocui/eventMouseClick.go

175 lines
4.5 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"
"go.wit.com/log"
"go.wit.com/widget"
)
func (tk *guiWidget) doButtonClick() {
if tk.node.IsEnabled() {
tk.dumpWidget("click()") // enable this to debug widget clicks
me.myTree.SendFromUser(tk.node)
} else {
log.Info("button is currently disabled by the application")
// tk.dumpWidget("disabled()") // enable this to debug widget clicks
}
}
// this whole things was impossible to make but it got me where I am now
// the debugging is way way better now with it being visible in the Stdout window
// so now it's possible to redo all this and make it better
func (tk *guiWidget) doWidgetClick(w int, h int) {
tk.dumpWidget(fmt.Sprintf("doWidgetClick(%d,%d)", w, h))
switch tk.node.WidgetType {
case widget.Checkbox:
if tk.node.State.Checked {
log.Log(WARN, "checkbox is being set to false")
tk.node.State.Checked = false
tk.setCheckbox()
} else {
log.Log(WARN, "checkbox is being set to true")
tk.node.State.Checked = true
tk.setCheckbox()
}
me.myTree.SendUserEvent(tk.node)
case widget.Button:
tk.doButtonClick()
case widget.Combobox:
tk.showDropdown()
case widget.Dropdown:
tk.showDropdown()
case widget.Textbox:
tk.showTextbox()
case widget.Flag:
tk.dropdownClicked(w, h)
case widget.Stdout:
tk.dumpWidget("stdout click()")
default:
tk.dumpWidget("undef click()")
}
}
// handles a mouse click
func doMouseClick(w int, h int) {
// Flag widgets (dropdown menus, etc) are the highest priority. ALWAYS SEND MOUSE CLICKS THERE FIRST
// handle an open dropdown menu or text entry window first
if me.dropdown.active || me.textbox.active {
// can't drag or do anything when dropdown or textbox are visible
for _, tk := range findByXY(w, h) {
if tk.node.WidgetId == me.dropdown.wId {
log.Info("got dropdwon click", w, h, tk.cuiName)
tk.dropdownClicked(w, h)
return
}
if tk.node.WidgetId == me.textbox.wId {
log.Info("got textbox click", w, h, tk.cuiName)
textboxClosed()
return
}
}
log.Info("a dropdown or textbox is active. you can't click anywhere else (otherwise hit ESC)", w, h)
return
}
win := findWindowUnderMouse()
if win != nil {
// look in this window for widgets
// widgets have priority. send the click here first
for _, tk := range win.findByXYreal(w, h) {
switch tk.node.WidgetType {
case widget.Checkbox:
if tk.node.State.Checked {
log.Log(WARN, "checkbox is being set to false")
tk.node.State.Checked = false
tk.setCheckbox()
} else {
log.Log(WARN, "checkbox is being set to true")
tk.node.State.Checked = true
tk.setCheckbox()
}
me.myTree.SendUserEvent(tk.node)
return
case widget.Button:
tk.doButtonClick()
return
case widget.Combobox:
tk.showDropdown()
return
case widget.Dropdown:
tk.showDropdown()
return
case widget.Textbox:
tk.showTextbox()
return
default:
// TODO: enable the GUI debugger in gocui
// tk.dumpWidget("undef click()") // enable this to debug widget clicks
}
}
if win.checkWindowClose(w, h) {
return
}
// log.Info("you clicked on a window, but not any widgets. check for title / close window here", win.cuiName)
win.redrawWindow(win.gocuiSize.w0, win.gocuiSize.h0)
me.stdout.outputOnTop = false
setThingsOnTop()
return
}
var found bool
for _, tk := range findByXY(w, h) {
// will show you everything found on a mouse click. great for debugging!
// tk.dumpWidget("click()")
if tk.node.WidgetType == widget.Stdout {
// don't send clicks to the stdout debugging window
// continue
}
found = true
// tk.doWidgetClick(w, h)
return
}
if found {
return
}
log.Log(GOCUI, "click() nothing was at:", w, h)
return
}
func doMouseDoubleClick(w int, h int) {
me.mouse.double = false
// log.Printf("actually a double click (%d,%d)", w, h)
if me.dropdown.active || me.textbox.active {
// can't drag or do anything when dropdown or textbox are visible
log.Info("can't double click. dropdown or textbox is active")
return
}
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Window {
tk.redrawWindow(tk.gocuiSize.w0, tk.gocuiSize.h0)
me.stdout.outputOnTop = false
setThingsOnTop()
return
}
if tk.node.WidgetType == widget.Stdout {
if me.stdout.outputOnTop {
me.stdout.outputOnTop = false
setThingsOnTop()
} else {
me.stdout.outputOnTop = true
setThingsOnTop()
}
return
}
}
}