forked from VladimirMarkelov/clui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
389 lines (331 loc) · 8.35 KB
/
window.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
)
// Window is an implemetation of View managed by Composer.
type Window struct {
BaseControl
buttons ViewButton
maximized bool
// maximization support
origWidth int
origHeight int
origX int
origY int
hidden bool
immovable bool
fixedSize bool
onClose func(Event) bool
onScreenResize func(Event)
onKeyDown *keyDownCb
}
type keyDownCb struct {
data interface{}
fn func(evt Event, data interface{}) bool
}
func CreateWindow(x, y, w, h int, title string) *Window {
wnd := new(Window)
wnd.BaseControl = NewBaseControl()
if w == AutoSize || w < 1 || w > 1000 {
w = 10
}
if h == AutoSize || h < 1 || h > 1000 {
w = 5
}
wnd.SetConstraints(w, h)
wnd.SetSize(w, h)
wnd.SetPos(x, y)
wnd.SetTitle(title)
wnd.buttons = ButtonClose | ButtonBottom | ButtonMaximize
wnd.children = make([]Control, 0)
wnd.SetPaddings(1, 1)
wnd.SetGaps(1, 0)
wnd.SetScale(1)
return wnd
}
func (wnd *Window) buttonCount() int {
cnt := 0
if wnd.buttons&ButtonClose == ButtonClose {
cnt += 1
}
if wnd.buttons&ButtonMaximize == ButtonMaximize {
cnt += 1
}
if wnd.buttons&ButtonBottom == ButtonBottom {
cnt += 1
}
return cnt
}
func (wnd *Window) drawFrame() {
PushAttributes()
defer PopAttributes()
var bs BorderStyle
if wnd.inactive {
bs = BorderThin
} else {
bs = BorderThick
}
DrawFrame(wnd.x, wnd.y, wnd.width, wnd.height, bs)
}
func (wnd *Window) drawTitle() {
PushAttributes()
defer PopAttributes()
btnCount := wnd.buttonCount()
maxw := wnd.width - 2 - btnCount
if btnCount > 0 {
maxw -= 2
}
fitTitle := wnd.title
rawText := UnColorizeText(fitTitle)
if xs.Len(rawText) > maxw {
fitTitle = SliceColorized(fitTitle, 0, maxw-3) + "..."
}
DrawText(wnd.x+1, wnd.y, fitTitle)
}
func (wnd *Window) drawButtons() {
btnCount := wnd.buttonCount()
if btnCount == 0 {
return
}
PushAttributes()
defer PopAttributes()
chars := []rune(SysObject(ObjViewButtons))
cMax, cBottom, cClose, cOpenB, cCloseB := chars[0], chars[1], chars[2], chars[3], chars[4]
pos := wnd.x + wnd.width - btnCount - 2
putCharUnsafe(pos, wnd.y, cOpenB)
pos += 1
if wnd.buttons&ButtonBottom == ButtonBottom {
putCharUnsafe(pos, wnd.y, cBottom)
pos += 1
}
if wnd.buttons&ButtonMaximize == ButtonMaximize {
putCharUnsafe(pos, wnd.y, cMax)
pos += 1
}
if wnd.buttons&ButtonClose == ButtonClose {
putCharUnsafe(pos, wnd.y, cClose)
pos += 1
}
putCharUnsafe(pos, wnd.y, cCloseB)
}
func (wnd *Window) Draw() {
WindowManager().BeginUpdate()
defer WindowManager().EndUpdate()
PushAttributes()
defer PopAttributes()
fg, bg := RealColor(wnd.fg, wnd.Style(), ColorViewText), RealColor(wnd.bg, wnd.Style(), ColorViewBack)
SetBackColor(bg)
FillRect(wnd.x, wnd.y, wnd.width, wnd.height, ' ')
wnd.DrawChildren()
SetBackColor(bg)
SetTextColor(fg)
wnd.drawFrame()
wnd.drawTitle()
wnd.drawButtons()
}
func (c *Window) HitTest(x, y int) HitResult {
if x > c.x && x < c.x+c.width-1 &&
y > c.y && y < c.y+c.height-1 {
return HitInside
}
hResult := HitOutside
if x == c.x && y == c.y {
hResult = HitTopLeft
} else if x == c.x+c.width-1 && y == c.y {
hResult = HitTopRight
} else if x == c.x && y == c.y+c.height-1 {
hResult = HitBottomLeft
} else if x == c.x+c.width-1 && y == c.y+c.height-1 {
hResult = HitBottomRight
} else if x == c.x && y > c.y && y < c.y+c.height-1 {
hResult = HitLeft
} else if x == c.x+c.width-1 && y > c.y && y < c.y+c.height-1 {
hResult = HitRight
} else if y == c.y && x > c.x && x < c.x+c.width-1 {
btnCount := c.buttonCount()
if x < c.x+c.width-1-btnCount {
hResult = HitTop
} else {
hitRes := []HitResult{HitTop, HitTop, HitTop}
pos := 0
if c.buttons&ButtonBottom == ButtonBottom {
hitRes[pos] = HitButtonBottom
pos += 1
}
if c.buttons&ButtonMaximize == ButtonMaximize {
hitRes[pos] = HitButtonMaximize
pos += 1
}
if c.buttons&ButtonClose == ButtonClose {
hitRes[pos] = HitButtonClose
pos += 1
}
hResult = hitRes[x-(c.x+c.width-1-btnCount)]
}
} else if y == c.y+c.height-1 && x > c.x && x < c.x+c.width-1 {
hResult = HitBottom
}
if hResult != HitOutside {
if c.immovable && hResult == HitTop {
hResult = HitInside
}
if c.fixedSize &&
(hResult == HitBottom || hResult == HitLeft ||
hResult == HitRight || hResult == HitTopLeft ||
hResult == HitTopRight || hResult == HitBottomRight ||
hResult == HitBottomLeft || hResult == HitButtonMaximize) {
hResult = HitInside
}
}
return hResult
}
func (c *Window) ProcessEvent(ev Event) bool {
switch ev.Type {
case EventMove:
c.PlaceChildren()
case EventResize:
c.ResizeChildren()
c.PlaceChildren()
case EventClose:
if c.onClose != nil {
if !c.onClose(ev) {
return false
}
}
return true
case EventKey:
if ev.Key == term.KeyTab || ev.Key == term.KeyArrowUp || ev.Key == term.KeyArrowDown {
if SendEventToChild(c, ev) {
return true
}
aC := ActiveControl(c)
nC := NextControl(c, aC, ev.Key != term.KeyArrowUp)
var clipped Control
if aC != nil && aC.Clipped() {
clipped = aC
} else {
clipped = ClippedParent(nC)
}
if clipped != nil {
dir := 1
if ev.Key != term.KeyArrowUp {
dir = -1
}
clipped.ProcessEvent(Event{Type: EventActivateChild, Target: nC, X: dir})
}
if nC != aC {
if aC != nil {
aC.SetActive(false)
aC.ProcessEvent(Event{Type: EventActivate, X: 0})
}
if nC != nil {
nC.SetActive(true)
nC.ProcessEvent(Event{Type: EventActivate, X: 1})
}
}
return true
} else {
if SendEventToChild(c, ev) {
return true
}
if c.onKeyDown != nil {
return c.onKeyDown.fn(ev, c.onKeyDown.data)
}
return false
}
default:
if ev.Type == EventMouse && ev.Key == term.MouseLeft {
DeactivateControls(c)
}
return SendEventToChild(c, ev)
}
return false
}
// OnClose sets the callback that is called when the Window is about to destroy
func (w *Window) OnClose(fn func(Event) bool) {
w.onClose = fn
}
// OnKeyDown sets the callback that is called when a user presses a key
// while the Window is active
func (w *Window) OnKeyDown(fn func(Event, interface{}) bool, data interface{}) {
if fn == nil {
w.onKeyDown = nil
} else {
w.onKeyDown = &keyDownCb{data: data, fn: fn}
}
}
// OnScreenResize sets the callback that is called when size of terminal changes
func (w *Window) OnScreenResize(fn func(Event)) {
w.onScreenResize = fn
}
// SetMaximized opens the view to full screen or restores its
// previous size
func (w *Window) SetMaximized(maximize bool) {
if maximize == w.maximized {
return
}
if maximize {
w.origX, w.origY = w.Pos()
w.origWidth, w.origHeight = w.Size()
w.maximized = true
w.SetPos(0, 0)
width, height := ScreenSize()
w.SetSize(width, height)
} else {
w.maximized = false
w.SetPos(w.origX, w.origY)
w.SetSize(w.origWidth, w.origHeight)
}
w.ResizeChildren()
w.PlaceChildren()
}
// Maximized returns if the view is in full screen mode
func (w *Window) Maximized() bool {
return w.maximized
}
// Visible returns if the window must be drawn on the screen
func (w *Window) Visible() bool {
return !w.hidden
}
// SetVisible allows to temporarily remove the window from screen
// and show it later without reconstruction
func (w *Window) SetVisible(visible bool) {
if w.hidden == visible {
w.hidden = !visible
if w.hidden {
w.SetModal(false)
if WindowManager().topWindow() == w {
WindowManager().moveActiveWindowToBottom()
}
} else {
WindowManager().activateWindow(w)
}
}
}
// Movable returns if the Window can be moved with mouse or keyboard
func (w *Window) Movable() bool {
return !w.immovable
}
// Sizable returns if size of the Window can be changed with mouse or keyboard
func (w *Window) Sizable() bool {
return !w.fixedSize
}
// SetMovable turns on and off ability to change Window position with mouse
// or keyboard
func (w *Window) SetMovable(movable bool) {
w.immovable = !movable
}
// SetSizable turns on and off ability to change Window size with mouse
// or keyboard
func (w *Window) SetSizable(sizable bool) {
w.fixedSize = !sizable
}
// TitleButtons returns a set of buttons shown in the Window title bar
func (w *Window) TitleButtons() ViewButton {
return w.buttons
}
// SetTitleButtons sets the title bar buttons available for a user
func (w *Window) SetTitleButtons(buttons ViewButton) {
w.buttons = buttons
}