-
Notifications
You must be signed in to change notification settings - Fork 1
/
window_manager.go
232 lines (201 loc) · 5.37 KB
/
window_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
package ui
import (
"errors"
"fmt"
"git.kirsle.net/go/render"
)
/*
window_manager.go holds data types and Supervisor methods related to the
management of ui.Window widgets.
*/
// Window button options. OR these together in a call to Window.SetButtons().
const (
CloseButton = 0x01
// NOTICE: MaximizeButton behavior is currently buggy, window doesn't
// redraw itself at the new size properly.
MaximizeButton = 0x02
// Minimize button has no default behavior attached; you can bind it with
// window.Handle(MinimizeWindow) to set your own event handler.
MinimizeButton = 0x04
)
// FocusedWindow is a doubly-linked list of recently focused Windows, with
// the current and most-recently focused on top. TODO make not exported.
type FocusedWindow struct {
window *Window
prev *FocusedWindow
next *FocusedWindow
}
// String of the FocusedWindow returns the underlying Window's String().
func (fw FocusedWindow) String() string {
return fw.window.String()
}
// Print the structure of the linked list from top to bottom.
func (fw *FocusedWindow) Print() {
var (
node = fw
i = 0
)
for node != nil {
fmt.Printf("[%d] window=%s prev=%s next=%s\n",
i, node.window, node.prev, node.next,
)
node = node.next
i++
}
}
// addWindow installs a Window into the supervisor to be managed. It is called
// by ui.Window.Supervise() and the newly added window becomes the focused
// one by default at the top of the linked list.
func (s *Supervisor) addWindow(win *Window) {
// Record in the window that it is managed by Supervisor, useful to control
// event propagation to non-focused windows.
win.managed = true
if s.winFocus == nil {
// First window added.
s.winFocus = &FocusedWindow{
window: win,
}
s.winBottom = s.winFocus
win.SetFocus(true)
} else {
// New window, make it the top one.
oldTop := s.winFocus
s.winFocus = &FocusedWindow{
window: win,
next: oldTop,
}
oldTop.prev = s.winFocus
oldTop.window.SetFocus(false)
win.SetFocus(true)
}
}
// FocusWindow brings the given window to the top of the supervisor's focus.
//
// The window must have previously been added to the supervisor's Window Manager
// by calling the Supervise() method of the window.
func (s *Supervisor) FocusWindow(win *Window) error {
if s.winFocus == nil {
return errors.New("no windows managed by supervisor")
}
// If the top window is already the target, return.
if s.winFocus.window == win {
return nil
}
// Find the window in the linked list.
var (
item = s.winFocus // item as we iterate the list
oldTop = s.winFocus // original first item in the list
target *FocusedWindow // identified target window to raise
newBottom *FocusedWindow // if the target was the bottom, this is new bottom
i = 0
)
for item != nil {
if item.window == win {
// Found it!
target = item
// Is it the last window in the list? Record the new bottom node.
if item.next == nil && item.prev != nil {
newBottom = item.prev
}
// Remove it from its position in the linked list. Join its
// previous and next nodes to bridge the gap.
if item.next != nil {
item.next.prev = item.prev
}
if item.prev != nil {
item.prev.next = item.next
}
break
}
item = item.next
i++
}
// Found it?
if target != nil {
// Put the target at the top of the list, pointing to the old top.
target.next = oldTop
target.prev = nil
oldTop.prev = target
s.winFocus = target
// Fix the top and bottom pointers.
if newBottom != nil {
s.winBottom = newBottom
}
// Toggle the focus states.
oldTop.window.SetFocus(false)
target.window.SetFocus(true)
}
return nil
}
// IsPointInWindow returns whether the given Point overlaps with a window managed
// by the Supervisor.
func (s *Supervisor) IsPointInWindow(point render.Point) bool {
node := s.winFocus
for node != nil {
if point.Inside(AbsoluteRect(node.window)) && !node.window.hidden {
return true
}
node = node.next
}
return false
}
// CloseAllWindows closes all open windows being managed by supervisor.
// Returns the number of windows closed.
func (s *Supervisor) CloseAllWindows() int {
var (
node = s.winFocus
i = 0
)
for node != nil {
i++
node.window.Close()
node = node.next
}
return i
}
// CloseActiveWindow closes the topmost active window.
func (s *Supervisor) CloseActiveWindow() bool {
var node = s.winFocus
if node != nil {
node.window.Close()
}
// Find the next visible window to focus.
for node != nil {
if !node.window.Hidden() {
s.FocusWindow(node.window)
break
}
node = node.next
}
return true
}
// PrintWindows is a debug function that walks the window tree and prints them to your console.
func (s *Supervisor) PrintWindows() {
var (
node = s.winBottom
i int
)
fmt.Println("From the bottom:")
for node != nil {
i++
fmt.Printf("%d. %s focused=%+v hidden=%+v\n", i, node.window, node.window.Focused(), node.window.Hidden())
node = node.prev
}
node = s.winFocus
i = 0
fmt.Println("Focus order:")
for node != nil {
i++
fmt.Printf("%d. %s focused=%+v hidden=%+v\n", i, node.window, node.window.Focused(), node.window.Hidden())
node = node.next
}
}
// presentWindows draws the windows from bottom to top.
func (s *Supervisor) presentWindows(e render.Engine) {
item := s.winBottom
for item != nil {
item.window.Compute(e)
item.window.Present(e, item.window.Point())
item = item.prev
}
}