summaryrefslogtreecommitdiff
path: root/eventBindings.go
blob: 5e06f8b1ec8ceeb6cfb0dc4558a037ceb3713e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"syscall"

	"github.com/awesome-gocui/gocui"
	"go.wit.com/log"
)

// 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) {
	defer func() {
		if r := recover(); r != nil {
			log.Info("EVENT BINDINGS recovered in r", r)
			return
		}
	}()

	// mouse handlers
	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

	// 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
	keyForced, modForced := gocui.MustParse("ctrl+z")                 // setup ctrl+z
	g.SetKeybinding("", keyForced, modForced, handle_ctrl_z)          // CTRL-Z :cleverly let's you background gocui (breaks cursor mouse on return)
	g.SetKeybinding("", gocui.KeyEsc, gocui.ModNone, doEsc)           // escape key

	// regular keys
	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
	g.SetKeybinding("", 'q', gocui.ModNone, doExit)                   // 'q' exit
	g.SetKeybinding("", gocui.KeyTab, gocui.ModNone, tabCycleWindows) // '2' use this to test new ideas

	// 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
	g.SetKeybinding("", '2', gocui.ModNone, theNotsure)            // '2' use this to test new ideas
	g.SetKeybinding("", 'S', gocui.ModNone, theSuperMouse)         // 'S' Super Mouse mode!
	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
	g.SetKeybinding("", 'f', gocui.ModNone, theFind)               // 'f' shows what is under your mouse
	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 (?)
}

// flips on 'super mouse' mode  // this was awesome for debugging gocui. never remove this code.
// while this is turned on, it will print out every widget found under the mouse
func theSuperMouse(g *gocui.Gui, v *gocui.View) error {
	if me.supermouse {
		log.Log(GOCUI, "supermouse off")
		me.supermouse = false
	} else {
		me.supermouse = true
		log.Log(GOCUI, "supermouse on")
	}
	return nil
}

// use this to test code ideas // put whatever you want here and hit '2' to activate it
func theNotsure(g *gocui.Gui, v *gocui.View) error {
	log.Info("got to theNotsure(). now what? dark =", me.dark)
	me.refresh = true
	log.Info("running VerifyParentId()")
	me.treeRoot.VerifyParentId()
	/*
		if me.debug {
			log.Info("debugging off")
			me.debug = false
		} else {
			log.Info("debugging on")
			me.debug = true
		}
		win := findWindowUnderMouse()
		if win != nil {
			win.dumpWidget("found() win. redrawing window:")
			win.makeWindowActive()
		}
	*/
	return nil
}

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 (you may need to trigger SIGWINCH)")
		log.Info("or maybe open a new window. notsure. This obviously isn't finished.")
		log.Info("submit patches to this and you will definitely get free cloud credits at WIT")
	}
	return nil
}

func wheelsUp(g *gocui.Gui, v *gocui.View) error {
	stdoutWheelsUp()
	return nil
}

func wheelsDown(g *gocui.Gui, v *gocui.View) error {
	stdoutWheelsDown()
	return nil
}

func tabCycleWindows(g *gocui.Gui, v *gocui.View) error {
	// log.Info("try to switch windows here")
	if len(me.allwin) != len(findWindows()) {
		me.allwin = findWindows()
	}
	tk := findNextWindow()
	if tk == nil {
		log.Info("findNextWindow() err. returned nil")
		return nil
	}
	tk.makeWindowActive()
	return nil
}

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")
		me.baseGui.SetCurrentView(me.notify.clock.tk.cuiName)
	}
	if me.textbox.active {
		me.textbox.tk.Hide()
		me.textbox.active = false
		log.Info("escaped from textbox")
		me.baseGui.SetCurrentView(me.notify.clock.tk.cuiName)
	}
	return nil
}

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 {
	standardExit()
	return nil
}

func doPanic(g *gocui.Gui, v *gocui.View) error {
	log.Log(GOCUI, "do panic() here")
	standardClose()
	panic("forced panic in gocui")
}

func openDebuggger(g *gocui.Gui, v *gocui.View) error {
	me.myTree.SendEnableDebugger()
	return nil
}

func theFind(g *gocui.Gui, v *gocui.View) error {
	w, h := g.MousePosition()
	for _, tk := range findByXY(w, h) {
		// tk.v.BgColor = gocui.ColorGreen
		tk.dumpWidget("theFind()")
		// 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
	if me.showDebug {
		showFake()
		me.showDebug = false
	} else {
		hideFake()
		me.showDebug = true
	}
	return nil
}

func theHelp(g *gocui.Gui, v *gocui.View) error {
	if me.showHelp {
		log.Info("Show the help!")
		showHelp()
	} else {
		log.Info("Hide the help!")
		hideHelp()
	}
	return nil
}

// 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
}