-
Notifications
You must be signed in to change notification settings - Fork 9
/
manager.go
374 lines (325 loc) · 9.37 KB
/
manager.go
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package winman
import (
"sync"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// WindowEdge enumerates the different window edges and corners
type WindowEdge int16
// Different window edges
const (
EdgeNone WindowEdge = iota
EdgeTop
EdgeRight
EdgeBottom
EdgeLeft
EdgeBottomRight
EdgeBottomLeft
)
// WindowZTop is used with SetZ to move a window to the top
const WindowZTop = -1
// WindowZBottom is used with SetZ to move a window to the bottom
const WindowZBottom = 0
// MinWindowWidth sets the minimum width a window can have as part of a window manager
var MinWindowWidth = 3
// MinWindowHeight sets the minimum height a window can have as part of a window manager
var MinWindowHeight = 3
// inRect returns true if the given coordinates are within the window
func inRect(wnd Window, x, y int) bool {
return NewRect(wnd.GetRect()).Contains(x, y)
}
// Manager represents a Window Manager primitive
type Manager struct {
*tview.Box
// The windows to be positioned.
windows Stack
dragOffsetX, dragOffsetY int
draggedWindow Window
draggedEdge WindowEdge
sync.Mutex
}
// NewWindowManager returns a ready to use window manager
func NewWindowManager() *Manager {
wm := &Manager{
Box: tview.NewBox(),
}
return wm
}
// NewWindow creates a new (hidden) window and adds it to this window manager
func (wm *Manager) NewWindow() *WindowBase {
wnd := NewWindow()
wm.AddWindow(wnd)
return wnd
}
// AddWindow adds the given window to the window manager
func (wm *Manager) AddWindow(window Window) *Manager {
wm.Lock()
defer wm.Unlock()
wm.windows.Push(window)
return wm
}
// RemoveWindow removes the given window from this window manager
func (wm *Manager) RemoveWindow(window Window) *Manager {
wm.Lock()
defer wm.Unlock()
wm.windows.Remove(window)
return wm
}
// Center centers the given window relative to the window manager
func (wm *Manager) Center(window Window) *Manager {
mx, my, mw, mh := wm.GetInnerRect()
_, _, width, height := window.GetRect()
x := mx + (mw-width)/2
y := my + (mh-height)/2
window.SetRect(x, y, width, height)
return wm
}
// WindowCount returns the number of windows managed by this window manager
func (wm *Manager) WindowCount() int {
wm.Lock()
defer wm.Unlock()
return len(wm.windows)
}
// Window returns the window at the given z index
func (wm *Manager) Window(z int) Window {
wm.Lock()
defer wm.Unlock()
wnd, _ := wm.windows.Item(z).(Window)
return wnd
}
func (wm *Manager) getZ(window Window) int {
return wm.windows.IndexOf(window)
}
// GetZ returns the z index of the given window
// returns -1 if the given window is not part of this manager
func (wm *Manager) GetZ(window Window) int {
wm.Lock()
defer wm.Unlock()
return wm.getZ(window)
}
func (wm *Manager) setZ(window Window, newZ int) {
wm.windows.Move(window, newZ)
}
// SetZ moves the given window to the given z index
// The special constants WindowZTop and WindowZBottom can be used
func (wm *Manager) SetZ(window Window, newZ int) *Manager {
wm.Lock()
defer wm.Unlock()
wm.setZ(window, newZ)
return wm
}
// Focus is called when this primitive receives focus
// implements tview.Primitive.Focus
func (wm *Manager) Focus(delegate func(p tview.Primitive)) {
wm.Lock()
window, _ := wm.windows.Find(func(wi interface{}) bool {
return wi.(Window).IsVisible()
}).(Window)
if window != nil {
wm.Unlock()
window.Focus(delegate)
return
}
wm.Unlock()
}
// HasFocus returns whether or not this primitive has focus.
// implements tview.Focusable
func (wm *Manager) HasFocus() bool {
wm.Lock()
defer wm.Unlock()
// iterate over all windows. If any has focus, then the
// this window manager has focus.
return nil != wm.windows.Find(func(wi interface{}) bool {
return wi.(Window).HasFocus()
})
}
// Draw draws this primitive onto the screen.
// implements tview.Primitive.Draw
func (wm *Manager) Draw(screen tcell.Screen) {
wm.Box.Draw(screen)
wm.Lock()
defer wm.Unlock()
// Ensure that the window with focus has the highest Z-index:
topWindowIndex := len(wm.windows) - 1
for i := topWindowIndex; i >= 0; i-- {
window := wm.windows[i].(Window)
if window.IsVisible() && window.HasFocus() {
if i < topWindowIndex {
wm.setZ(window, WindowZTop) // move focused window on top
}
break
}
}
// make sure windows are not out of bounds, too small,
// or too big to fit within the window manager:
for _, wndItem := range wm.windows {
window := wndItem.(Window)
if !window.IsVisible() {
continue
}
mx, my, mw, mh := wm.GetInnerRect()
x, y, w, h := window.GetRect()
// Avoid window overflowing on the left:
if x < mx {
x = mx
}
// Avoid window overflowing on the top:
if y < my {
y = my
}
// Fix window that is too narrow:
if w < MinWindowWidth {
w = MinWindowWidth
}
// Fix window that is too short:
if h < MinWindowHeight {
h = MinWindowHeight
}
// reduce windows that are too wide,
// or fix size if the window is maximized
if w > mw || window.IsMaximized() {
w = mw
x = mx
}
// reduce windows that are too tall,
// or fix size if the window is maximized
if h > mh || window.IsMaximized() {
h = mh
y = my
}
// Avoid window overflowing the right edge
if x+w > mx+mw {
x = mx + mw - w
}
// Avoid window overflowing the bottom edge
if y+h > my+mh {
y = my + mh - h
}
// reposition window to the new coordinates:
window.SetRect(x, y, w, h)
// now we can draw it
window.Draw(screen)
}
}
// MouseHandler returns the mouse handler for this primitive.
// implements tview.Primitive.MouseHandler
func (wm *Manager) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return wm.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
// ignore mouse events out of the bounds of the window manager
if !wm.InRect(event.Position()) {
return false, nil
}
wm.Lock()
// check if there is an active drag operation:
if wm.draggedWindow != nil {
switch action {
case tview.MouseLeftUp:
wm.draggedWindow = nil // if the button is released, stop the drag operation
case tview.MouseMove:
x, y := event.Position()
wx, wy, ww, wh := wm.draggedWindow.GetRect()
// depending if the drag operation is on the top or edges, either move the window or resize
if wm.draggedEdge == EdgeTop && wm.draggedWindow.IsDraggable() {
wm.draggedWindow.SetRect(x-wm.dragOffsetX, y-wm.dragOffsetY, ww, wh) // move window
} else {
// resize window pulling from the corresponding edge
if wm.draggedWindow.IsResizable() {
switch wm.draggedEdge {
case EdgeRight:
wm.draggedWindow.SetRect(wx, wy, x-wx+1, wh)
case EdgeBottom:
wm.draggedWindow.SetRect(wx, wy, ww, y-wy+1)
case EdgeLeft:
wm.draggedWindow.SetRect(x, wy, ww+wx-x, wh)
case EdgeBottomRight:
wm.draggedWindow.SetRect(wx, wy, x-wx+1, y-wy+1)
case EdgeBottomLeft:
wm.draggedWindow.SetRect(x, wy, ww+wx-x, y-wy+1)
}
}
}
wm.Unlock()
return true, nil
}
}
lastModal := false
// Pass mouse events along to the window with highest Z
// that is hit by the mouse
// Stop if the last window was a modal.
for i := len(wm.windows) - 1; i >= 0 && !lastModal; i-- {
window := wm.windows[i].(Window)
if !window.IsVisible() { // skip hidden windows
continue
}
// if this is a modal window, then don't give a chance for
// other windows to get mouse events
lastModal = window.IsModal() // if true, will exit loop on the next iteration
x, y := event.Position()
if !inRect(window, x, y) {
// skip this window since it is not hit
continue
}
if action == tview.MouseLeftDown && window.HasBorder() {
// initiate a drag operation
if !window.HasFocus() {
setFocus(window)
}
wx, wy, ww, wh := window.GetRect()
wm.draggedEdge = EdgeNone
switch {
case y == wy+wh-1:
switch {
case x == wx:
wm.draggedEdge = EdgeBottomLeft
case x == wx+ww-1:
wm.draggedEdge = EdgeBottomRight
default:
wm.draggedEdge = EdgeBottom
}
case x == wx:
wm.draggedEdge = EdgeLeft
case x == wx+ww-1:
wm.draggedEdge = EdgeRight
case y == wy:
wm.draggedEdge = EdgeTop
}
if wm.draggedEdge != EdgeNone {
// drag detected. Remember where the drag operation started
wm.draggedWindow = window
wm.dragOffsetX = x - wx
wm.dragOffsetY = y - wy
wm.Unlock()
return true, nil
}
}
wm.Unlock()
// no drag operation detected.
// pass the mouse events to the window itself.
return window.MouseHandler()(action, event, setFocus)
}
wm.Unlock()
return
})
}
// InputHandler returns a handler which receives key events when it has focus.
func (wm *Manager) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return wm.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
wm.Lock()
// Pass key events along to the window with highest Z that is visible and has focus
var window Window
for i := len(wm.windows) - 1; i >= 0; i-- {
window = wm.windows[i].(Window)
if window.HasFocus() {
break
}
window = nil
}
wm.Unlock()
if window != nil {
inputHandler := window.InputHandler()
if inputHandler != nil {
inputHandler(event, setFocus)
}
}
})
}