summaryrefslogtreecommitdiff
path: root/find.go
blob: 4bb9ad107435e86f75ba5f818bc5c9f7dc599fbd (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"slices"

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

/*
	gocui defines the offset like this:

 	   width -> increases to the right
	----------------------------------    hieght
	|            H = 1               |   increases
	|                                |       |
	| W = 1                   W = 18 |       |
	|                                |       v
	|            H = 5               |   downwards
	-------------------------------------
*/

// change over to this name
// returns all the widgets under (X,H) that are visible
func findByXY(w int, h int) []*guiWidget {
	rootW := me.treeRoot.TK.(*guiWidget)

	// this searches the binary tree recursively (function is right below)
	return rootW.findByXYreal(w, h)
}

func (r rectType) inRect(w int, h int) bool {
	if (r.w0 <= w) && (w <= r.w1) && (r.h0 <= h) && (h <= r.h1) {
		return true
	}
	return false
}

// this checks a widget to see if it is under (W,H), then checks the widget's children
// anything that matches is passed back as an array of widgets
func (tk *guiWidget) findByXYreal(w int, h int) []*guiWidget {
	var widgets []*guiWidget

	//	if !tk.Visible() {
	// ignore widgets that are not visible
	//	} else {

	// check the location to see if this is under (W,H)
	// if it is, return this widget
	// if (tk.gocuiSize.w0 <= w) && (w <= tk.gocuiSize.w1) &&
	// 	(tk.gocuiSize.h0 <= h) && (h <= tk.gocuiSize.h1) {
	//		if tk.gocuiSize.inRect(w, h) {
	//			widgets = append(widgets, tk)
	//		} else {
	// if (tk.full.w0 <= w) && (w <= tk.full.w1) &&
	// 	(tk.full.h0 <= h) && (h <= tk.full.h1) {
	if tk.full.inRect(w, h) {
		widgets = append(widgets, tk)
	}
	// log.Log(GOCUI, "findByXY() found", widget.WidgetType(), w, h)
	//		}
	//	}
	// tk.verifyRect()

	// search through the children widgets in the binary tree
	for _, child := range tk.children {
		widgets = append(widgets, child.findByXYreal(w, h)...)
	}

	return widgets
}

// returns all the windows from the root of the binary tree
func findWindows() []*guiWidget {
	rootW := me.treeRoot.TK.(*guiWidget)
	return rootW.findWindows()
}

// walk the binary tree looking for WidgetType == Window
func (tk *guiWidget) findWindows() []*guiWidget {
	var found []*guiWidget

	if tk.WidgetType() == widget.Window {
		found = append(found, tk)
	}

	for _, child := range tk.children {
		found = append(found, child.findWindows()...)
	}
	return found
}

// used by gocui.TabKey to rotate through the windows
func findNextWindow() *guiWidget {
	var found bool
	if len(me.allwin) == 0 {
		return nil
	}
	for _, tk := range me.allwin {
		if tk.window.active {
			found = true
			continue
		}
		if found {
			return tk
		}
	}
	// at the end, loop to the beginning
	return me.allwin[0]
}

// find the window under the mouse and only the window under the mouse
func findWindowUnderMouse() *guiWidget {
	w, h := me.baseGui.MousePosition()

	if len(me.allwin) != len(findWindows()) {
		me.allwin = findWindows()
	}

	// if the stdout window is on top, check it first
	if me.stdout.outputOnTop {
		if me.stdout.tk.full.inRect(w, h) {
			// log.Info(fmt.Sprintf("findWindowUnderMouse() found %s stdout on top (%dx%d)", me.stdout.tk.cuiName, w, h))
			return me.stdout.tk
		}
	}

	// now check if the active window is below the mouse
	for _, tk := range me.allwin {
		if tk.window.active {
			if tk.full.inRect(w, h) {
				// log.Info(fmt.Sprintf("findWindowUnderMouse() found %s active window (%dx%d)", tk.cuiName, w, h))
				return tk
			}
		}
	}

	// well, just find any window then
	// sorting by order might work?
	slices.SortFunc(me.allwin, func(a, b *guiWidget) int {
		return a.window.order - b.window.order
	})

	for _, win := range me.allwin {
		if win.full.inRect(w, h) {
			// log.Info(fmt.Sprintf("findWindowUnderMouse() found %s window (%dx%d)", win.cuiName, w, h))
			return win
		}
	}

	// okay, no window. maybe the stdout is there?
	if me.stdout.tk.full.inRect(w, h) {
		// log.Info(fmt.Sprintf("findWindowUnderMouse() found %s stdout (%dx%d)", me.stdout.tk.cuiName, w, h))
		return me.stdout.tk
	}

	// geez. nothing! maybe auto return stdout?
	log.Info("findWindowUnderMouse() no window found at", w, h)
	return nil
}

func (tk *guiWidget) findParentWindow() *guiWidget {
	if tk.WidgetType() == widget.Window {
		return tk
	}
	if tk.parent == nil {
		return nil
	}
	return tk.parent.findParentWindow()
}

func (tk *guiWidget) findWidgetByName(name string) *guiWidget {
	if tk.cuiName == name {
		return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByName(name)
		if found != nil {
			return found
		}
	}
	return nil
}

func (tk *guiWidget) findWidgetByView(v *gocui.View) *guiWidget {
	if tk.v == v {
		return tk
	}
	if tk.cuiName == v.Name() {
		log.Log(NOW, "findWidget() error. names are mismatched or out of sync", tk.cuiName)
		log.Log(NOW, "findWidget() or maybe the view has been deleted")
		// return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetByView(v)
		if found != nil {
			return found
		}
	}
	return nil
}

func (tk *guiWidget) findWidgetById(id int) *guiWidget {
	if tk.WidgetId() == id {
		return tk
	}
	for _, child := range tk.children {
		found := child.findWidgetById(id)
		if found != nil {
			return found
		}
	}
	return nil
}