gocui/eventMouseClick.go

156 lines
4.1 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"
"github.com/awesome-gocui/gocui"
"go.wit.com/log"
"go.wit.com/widget"
)
/*
// this didn't work. panic()
func (tk *guiWidget) DeleteNode() {
p := tk.parent
for i, child := range p.children {
log.Log(GOCUI, "parent has child:", i, child.node.WidgetId, child.node.GetProgName())
if tk == child {
log.Log(GOCUI, "Found child ==", i, child.node.WidgetId, child.node.GetProgName())
log.Log(GOCUI, "Found n ==", i, tk.node.WidgetId, tk.node.GetProgName())
p.children = append(p.children[:i], p.children[i+1:]...)
}
}
for i, child := range p.children {
log.Log(TREE, "parent now has child:", i, child.WidgetId, child.GetProgName())
}
}
*/
func (tk *guiWidget) doWindowClick() {
w, h := me.baseGui.MousePosition()
// compare the mouse location to the 'X' indicator to close the window
if tk.gocuiSize.inRect(w, h) {
offset := w - tk.gocuiSize.w1
if (offset < 2) && (offset > -2) {
// close enough // close the window here
tk.dumpWidget(fmt.Sprintf("Close Window(%d,%d) (off=%d)", w, h, offset))
tk.hideWindow()
n := tk.node
tk.deleteNode()
me.myTree.SendWindowCloseEvent(n)
/*
n.DeleteNode()
tk.DeleteNode()
*/
return
}
if tk.window.collapsed {
tk.dumpWidget(fmt.Sprintf("collapse = false"))
tk.window.collapsed = false
} else {
tk.dumpWidget(fmt.Sprintf("collapse = true"))
tk.window.collapsed = true
}
} else {
tk.window.collapsed = false
// tk.dumpWidget(fmt.Sprintf("No (%d,%d)", w, h))
}
// if there is a current window, hide it
if me.currentWindow != nil {
me.currentWindow.setColor(&colorWindow)
}
// now set this window as the current window
me.currentWindow = tk
tk.redrawWindow(w, h)
setThingsOnTop() // sets help, Stdout, etc on the top after windows have been redrawn
}
// 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) {
switch tk.node.WidgetType {
case widget.Window:
tk.doWindowClick()
return
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:
me.myTree.SendFromUser(tk.node)
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()")
}
}
// sends the mouse click to a widget underneath
func clickOLD(g *gocui.Gui, v *gocui.View) error {
mouseW, mouseH := me.baseGui.MousePosition()
w := mouseW
h := mouseH
// Flag widgets (dropdown menus, etc) are the highest priority. ALWAYS SEND MOUSE CLICKS THERE FIRST
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Flag {
tk.doWidgetClick(w, h)
return nil
}
}
// Button widgets
for _, tk := range findByXY(w, h) {
if tk.node.WidgetType == widget.Button {
tk.doWidgetClick(w, h)
return nil
}
}
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
}
tk.doWidgetClick(w, h)
return nil
}
log.Log(GOCUI, "click() nothing was at:", v.Name(), mouseW, mouseH)
return nil
/*
// not sure what SetCurrentView() does right now. it was here before
// SetCurrentView dies if it's sent an non-existent view
if _, err := g.SetCurrentView(v.Name()); err != nil {
log.Log(GOCUI, "click() END v.Name =", v.Name(), "err =", err)
// return err // return causes gocui.MainLoop() to exit. Do we ever want that to happen here?
return nil
}
*/
}