gocui/eventBindings.go

204 lines
6.7 KiB
Go
Raw Normal View History

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
2025-02-01 11:42:31 -06:00
// Use of this source code is governed by the GPL 3.0
package main
import (
2025-01-31 16:05:34 -06:00
"syscall"
"github.com/awesome-gocui/gocui"
2025-01-31 16:05:34 -06:00
"go.wit.com/log"
)
2025-01-31 16:05:34 -06:00
// register how the 'gocui' will work as a GO toolkit plugin
// all applications will use these keys. they are universal.
// tells 'gocui' where to send events
func registerHandlers(g *gocui.Gui) {
2025-01-31 16:05:34 -06:00
// mouse handlers
2025-02-06 16:19:36 -06:00
g.SetKeybinding("", gocui.MouseLeft, gocui.ModNone, mouseDown) // normal left mouse down
g.SetKeybinding("", gocui.MouseLeft, gocui.ModMouseCtrl, ctrlDown) // mouse with the ctrl key held down
g.SetKeybinding("", gocui.MouseRelease, gocui.ModNone, mouseUp) // mouse button release
g.SetKeybinding("", gocui.MouseWheelUp, gocui.ModNone, wheelsUp) // mouse button release
g.SetKeybinding("", gocui.MouseWheelDown, gocui.ModNone, wheelsDown) // mouse button release
2025-01-31 16:05:34 -06:00
// Ctrl key handlers
g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, doExit) // CTRL-C : exits the application
g.SetKeybinding("", gocui.KeyCtrlV, gocui.ModNone, doPanic) // CTRL-V : force a panic()
g.SetKeybinding("", gocui.KeyCtrlD, gocui.ModNone, openDebuggger) // CTRL-D : open the (D)ebugger
2025-02-02 23:36:33 -06:00
keyForced, modForced := gocui.MustParse("ctrl+z") // setup ctrl+z
2025-01-31 16:05:34 -06:00
g.SetKeybinding("", keyForced, modForced, handle_ctrl_z) // CTRL-Z :cleverly let's you background gocui (breaks cursor mouse on return)
2025-02-08 10:20:00 -06:00
g.SetKeybinding("", gocui.KeyEsc, gocui.ModNone, doEsc) // escape key
2025-01-31 16:05:34 -06:00
// regular keys
2025-02-07 03:26:05 -06:00
g.SetKeybinding("", 'H', gocui.ModNone, theHelp) // 'H' toggles on and off the help menu
g.SetKeybinding("", 'O', gocui.ModNone, theStdout) // 'O' toggle the STDOUT window
g.SetKeybinding("", 'D', gocui.ModNone, theDarkness) // 'D' toggles light/dark mode
2025-02-06 07:01:27 -06:00
g.SetKeybinding("", 'q', gocui.ModNone, doExit) // 'q' exit
g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, tabCycleWindows) // '2' use this to test new ideas
2025-02-07 16:44:01 -06:00
// stdout keys
g.SetKeybinding("", gocui.KeyPgup, gocui.ModNone, stdoutPgup) // Pgup scroll up the Stdout buffer
g.SetKeybinding("", gocui.KeyPgdn, gocui.ModNone, stdoutPgdn) // Pgdn scroll down the Stdout buffer
g.SetKeybinding("", gocui.KeyHome, gocui.ModNone, stdoutHome) // Pgdn scroll down the Stdout buffer
g.SetKeybinding("", gocui.KeyArrowUp, gocui.ModNone, stdoutArrowUp) // Pgdn scroll down the Stdout buffer
g.SetKeybinding("", gocui.KeyArrowDown, gocui.ModNone, stdoutArrowDown) // Pgdn scroll down the Stdout buffer
// debugging
2025-02-06 02:15:21 -06:00
g.SetKeybinding("", '2', gocui.ModNone, theNotsure) // '2' use this to test new ideas
g.SetKeybinding("", 'S', gocui.ModNone, theSuperMouse) // 'S' Super Mouse mode!
2025-02-05 07:24:14 -06:00
g.SetKeybinding("", 'M', gocui.ModNone, printWidgetPlacements) // 'M' list all widgets with positions
g.SetKeybinding("", 'L', gocui.ModNone, printWidgetTree) // 'L' list all widgets in tree view
2025-02-06 02:15:21 -06:00
g.SetKeybinding("", 'f', gocui.ModNone, theFind) // 'f' shows what is under your mouse
2025-02-05 07:24:14 -06:00
g.SetKeybinding("", 'd', gocui.ModNone, theLetterD) // 'd' toggles on and off debugging buttons
g.SetKeybinding("", 'q', gocui.ModNone, quit) // 'q' only exits gocui. plugin stays alive (?)
2025-01-31 16:05:34 -06:00
}
2025-02-06 02:15:21 -06:00
// flips on 'super mouse' mode // this was awesome for debugging gocui. never remove this code.
2025-01-31 16:05:34 -06:00
// while this is turned on, it will print out every widget found under the mouse
2025-02-06 02:15:21 -06:00
func theSuperMouse(g *gocui.Gui, v *gocui.View) error {
2025-01-31 16:05:34 -06:00
if me.supermouse {
log.Log(GOCUI, "supermouse off")
me.supermouse = false
} else {
me.supermouse = true
log.Log(GOCUI, "supermouse on")
}
return nil
}
2025-02-06 02:15:21 -06:00
// use this to test code ideas // put whatever you want here and hit '2' to activate it
2025-01-31 22:56:05 -06:00
func theNotsure(g *gocui.Gui, v *gocui.View) error {
2025-02-08 12:50:05 -06:00
log.Info("got keypress 2. now what? dark =", me.dark)
2025-02-06 16:19:36 -06:00
return nil
}
2025-02-07 03:26:05 -06:00
func theDarkness(g *gocui.Gui, v *gocui.View) error {
if me.dark {
me.dark = false
log.Info("you have seen the light")
} else {
me.dark = true
log.Info("you have entered into darkness")
}
return nil
}
2025-02-06 16:19:36 -06:00
func wheelsUp(g *gocui.Gui, v *gocui.View) error {
log.Info("private wheels up")
return nil
}
func wheelsDown(g *gocui.Gui, v *gocui.View) error {
log.Info("you've landed")
2025-02-06 07:01:27 -06:00
return nil
}
func tabCycleWindows(g *gocui.Gui, v *gocui.View) error {
2025-02-06 14:13:31 -06:00
// log.Info("try to switch windows here")
2025-02-06 07:01:27 -06:00
if len(me.allwin) != len(findWindows()) {
me.allwin = findWindows()
}
2025-02-06 13:47:19 -06:00
tk := findNextWindow()
if tk == nil {
2025-02-06 07:01:27 -06:00
log.Info("findNextWindow() err. returned nil")
return nil
}
2025-02-06 13:47:19 -06:00
tk.makeWindowActive()
tk.redrawWindow(tk.gocuiSize.w0, tk.gocuiSize.h0)
setThingsOnTop() // sets help, Stdout, etc on the top after windows have been redrawn
2025-01-31 22:56:05 -06:00
return nil
}
2025-02-08 10:20:00 -06:00
func doEsc(g *gocui.Gui, v *gocui.View) error {
log.Info("got escape key")
if me.dropdown.active {
me.dropdown.tk.Hide()
me.dropdown.active = false
log.Info("escaped from dropdown")
}
2025-02-08 10:30:19 -06:00
if me.textbox.active {
me.textbox.tk.Hide()
me.textbox.active = false
log.Info("escaped from textbox")
}
2025-02-08 10:20:00 -06:00
return nil
}
2025-01-31 16:05:34 -06:00
func theShow(g *gocui.Gui, v *gocui.View) error {
var w *guiWidget
w = me.treeRoot.TK.(*guiWidget)
w.showWidgets()
return nil
}
func doExit(g *gocui.Gui, v *gocui.View) error {
2025-01-31 16:05:34 -06:00
standardExit()
return nil
}
func doPanic(g *gocui.Gui, v *gocui.View) error {
2025-01-31 16:05:34 -06:00
log.Log(GOCUI, "do panic() here")
standardClose()
panic("forced panic in gocui")
}
func openDebuggger(g *gocui.Gui, v *gocui.View) error {
2025-01-31 16:05:34 -06:00
me.myTree.SendEnableDebugger()
return nil
}
func theFind(g *gocui.Gui, v *gocui.View) error {
w, h := g.MousePosition()
for _, tk := range findByXY(w, h) {
2025-02-02 23:36:33 -06:00
// tk.v.BgColor = gocui.ColorGreen
2025-02-01 13:58:53 -06:00
tk.dumpWidget("theFind()")
2025-02-04 15:48:14 -06:00
// tk.verifyRect()
}
return nil
}
// is run whenever anyone hits 'd' (in an open space)
func theLetterD(g *gocui.Gui, v *gocui.View) error {
// widgets that don't have physical existance in
// a display toolkit are hidden. In the case
// of gocui, they are set as not 'visible' and put offscreen
// or have the size set to zero
// (hopefully anyway) lots of things with the toolkit
// still don't work
fakeStartWidth = me.FakeW
fakeStartHeight = me.TabH + me.FramePadH
2025-02-06 02:54:50 -06:00
if me.showDebug {
showFake()
2025-02-06 02:54:50 -06:00
me.showDebug = false
} else {
hideFake()
2025-02-06 02:54:50 -06:00
me.showDebug = true
}
return nil
}
func theHelp(g *gocui.Gui, v *gocui.View) error {
if me.showHelp {
2025-02-01 19:00:15 -06:00
log.Info("Show the help!")
showHelp()
} else {
2025-02-01 19:00:15 -06:00
log.Info("Hide the help!")
hideHelp()
}
return nil
}
2025-01-31 16:05:34 -06:00
// todo: find and give credit to the person that I found this patch in their forked repo
// handle ctrl+z
func handle_ctrl_z(g *gocui.Gui, v *gocui.View) error {
gocui.Suspend()
log.Info("got ctrl+z")
syscall.Kill(syscall.Getpid(), syscall.SIGSTOP)
log.Info("got ctrl+z syscall() done")
gocui.Resume()
return nil
}