forked from aarzilli/nucular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nucular.go
3003 lines (2542 loc) · 82.7 KB
/
nucular.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package nucular
import (
"errors"
"image"
"image/color"
"math"
"strconv"
"sync/atomic"
"github.com/aarzilli/nucular/command"
"github.com/aarzilli/nucular/label"
"github.com/aarzilli/nucular/rect"
nstyle "github.com/aarzilli/nucular/style"
"golang.org/x/image/font"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/mouse"
)
///////////////////////////////////////////////////////////////////////////////////
// CONTEXT & PANELS
///////////////////////////////////////////////////////////////////////////////////
type UpdateFn func(*Window)
type Window struct {
LastWidgetBounds rect.Rect
Data interface{}
title string
ctx *context
idx int
flags WindowFlags
Bounds rect.Rect
Scrollbar image.Point
cmds command.Buffer
widgets widgetBuffer
layout *panel
close, first bool
moving bool
scaling bool
// trigger rectangle of nonblocking windows
header rect.Rect
// root of the node tree
rootNode *treeNode
// current tree node see TreePush/TreePop
curNode *treeNode
// parent window of a popup
parent *Window
// helper windows to implement groups
groupWnd map[string]*Window
// editor of the active property widget (see PropertyInt, PropertyFloat)
editor *TextEditor
// update function
updateFn UpdateFn
usingSub bool
began bool
rowCtor rowConstructor
menuItemWidth int
lastLayoutCnt int
adjust map[int]map[int]*adjustCol
undockedSz image.Point
}
type FittingWidthFn func(width int)
type adjustCol struct {
id int
font font.Face
width int
first bool
}
type treeNode struct {
Open bool
Children map[string]*treeNode
Parent *treeNode
}
type panel struct {
Cnt int
Flags WindowFlags
Bounds rect.Rect
Offset *image.Point
AtX int
AtY int
MaxX int
Width int
Height int
FooterH int
HeaderH int
Border int
Clip rect.Rect
Menu menuState
Row rowLayout
ReservedHeight int
}
type menuState struct {
X int
Y int
W int
H int
Offset image.Point
}
type rowLayout struct {
Type int
Index int
Index2 int
CalcMaxWidth bool
Height int
Columns int
Ratio []float64
WidthArr []int
ItemWidth int
ItemRatio float64
ItemHeight int
ItemOffset int
Filled float64
Item rect.Rect
TreeDepth int
DynamicFreeX, DynamicFreeY, DynamicFreeW, DynamicFreeH float64
}
type WindowFlags int
const (
WindowBorder WindowFlags = 1 << iota
WindowBorderHeader
WindowMovable
WindowScalable
WindowClosable
WindowDynamic
WindowNoScrollbar
WindowNoHScrollbar
WindowTitle
WindowContextualReplace
WindowNonmodal
windowPrivate
windowSub
windowGroup
windowPopup
windowNonblock
windowContextual
windowCombo
windowMenu
windowTooltip
windowEnabled
windowHDynamic
windowDocked
WindowDefaultFlags = WindowBorder | WindowMovable | WindowScalable | WindowClosable | WindowTitle
)
func createTreeNode(initialState bool, parent *treeNode) *treeNode {
return &treeNode{initialState, map[string]*treeNode{}, parent}
}
func createWindow(ctx *context, title string) *Window {
rootNode := createTreeNode(false, nil)
r := &Window{ctx: ctx, title: title, rootNode: rootNode, curNode: rootNode, groupWnd: map[string]*Window{}, first: true}
r.rowCtor.win = r
r.widgets.cur = make(map[rect.Rect]frozenWidget)
return r
}
type frozenWidget struct {
ws nstyle.WidgetStates
frameCount int
}
type widgetBuffer struct {
cur map[rect.Rect]frozenWidget
frameCount int
}
func (wbuf *widgetBuffer) PrevState(bounds rect.Rect) nstyle.WidgetStates {
return wbuf.cur[bounds].ws
}
func (wbuf *widgetBuffer) Add(ws nstyle.WidgetStates, bounds rect.Rect) {
wbuf.cur[bounds] = frozenWidget{ws, wbuf.frameCount}
}
func (wbuf *widgetBuffer) reset() {
for k, v := range wbuf.cur {
if v.frameCount != wbuf.frameCount {
delete(wbuf.cur, k)
}
}
wbuf.frameCount++
}
func (w *Window) Master() MasterWindow {
return w.ctx.mw
}
func (win *Window) style() *nstyle.Window {
switch {
case win.flags&windowCombo != 0:
return &win.ctx.Style.ComboWindow
case win.flags&windowContextual != 0:
return &win.ctx.Style.ContextualWindow
case win.flags&windowMenu != 0:
return &win.ctx.Style.MenuWindow
case win.flags&windowGroup != 0:
return &win.ctx.Style.GroupWindow
case win.flags&windowTooltip != 0:
return &win.ctx.Style.TooltipWindow
default:
return &win.ctx.Style.NormalWindow
}
}
func panelBegin(ctx *context, win *Window, title string) {
in := &ctx.Input
style := &ctx.Style
font := style.Font
wstyle := win.style()
// window dragging
if win.moving {
if in == nil || !in.Mouse.Down(mouse.ButtonLeft) {
if win.flags&windowDocked == 0 && in != nil {
win.ctx.DockedWindows.Dock(win, in.Mouse.Pos, win.ctx.Windows[0].Bounds, win.ctx.Style.Scaling)
}
win.moving = false
} else {
win.move(in.Mouse.Delta, in.Mouse.Pos)
}
}
win.usingSub = false
in.Mouse.clip = nk_null_rect
layout := win.layout
window_padding := wstyle.Padding
item_spacing := wstyle.Spacing
scaler_size := wstyle.ScalerSize
*layout = panel{}
/* panel space with border */
if win.flags&WindowBorder != 0 {
layout.Bounds = shrinkRect(win.Bounds, wstyle.Border)
} else {
layout.Bounds = win.Bounds
}
/* setup panel */
layout.Border = layout.Bounds.X - win.Bounds.X
layout.AtX = layout.Bounds.X
layout.AtY = layout.Bounds.Y
layout.Width = layout.Bounds.W
layout.Height = layout.Bounds.H
layout.MaxX = 0
layout.Row.Index = 0
layout.Row.Index2 = 0
layout.Row.CalcMaxWidth = false
layout.Row.Columns = 0
layout.Row.Height = 0
layout.Row.Ratio = nil
layout.Row.ItemWidth = 0
layout.Row.ItemRatio = 0
layout.Row.TreeDepth = 0
layout.Flags = win.flags
layout.ReservedHeight = 0
win.lastLayoutCnt = 0
for _, cols := range win.adjust {
for _, col := range cols {
col.first = false
}
}
/* calculate window header */
if win.flags&windowMenu != 0 || win.flags&windowContextual != 0 {
layout.HeaderH = window_padding.Y
layout.Row.Height = window_padding.Y
} else {
layout.HeaderH = window_padding.Y
layout.Row.Height = window_padding.Y
}
/* calculate window footer height */
if win.flags&windowNonblock == 0 && (((win.flags&WindowNoScrollbar == 0) && (win.flags&WindowNoHScrollbar == 0)) || (win.flags&WindowScalable != 0)) {
layout.FooterH = scaler_size.Y + wstyle.FooterPadding.Y
} else {
layout.FooterH = 0
}
/* calculate the window size */
if win.flags&WindowNoScrollbar == 0 {
layout.Width = layout.Bounds.W - wstyle.ScrollbarSize.X
}
layout.Height = layout.Bounds.H - (layout.HeaderH + window_padding.Y)
layout.Height -= layout.FooterH
/* window header */
var dwh drawableWindowHeader
dwh.Dynamic = layout.Flags&WindowDynamic != 0
dwh.Bounds = layout.Bounds
dwh.HeaderActive = (win.idx != 0) && (win.flags&WindowTitle != 0)
dwh.LayoutWidth = layout.Width
dwh.Style = win.style()
var closeButton rect.Rect
if dwh.HeaderActive {
/* calculate header bounds */
dwh.Header.X = layout.Bounds.X
dwh.Header.Y = layout.Bounds.Y
dwh.Header.W = layout.Bounds.W
/* calculate correct header height */
layout.HeaderH = FontHeight(font) + 2.0*wstyle.Header.Padding.Y
layout.HeaderH += 2.0 * wstyle.Header.LabelPadding.Y
layout.Row.Height += layout.HeaderH
dwh.Header.H = layout.HeaderH
/* update window height */
layout.Height = layout.Bounds.H - (dwh.Header.H + 2*item_spacing.Y)
layout.Height -= layout.FooterH
dwh.Hovered = ctx.Input.Mouse.HoveringRect(dwh.Header)
dwh.Focused = win.toplevel()
/* window header title */
t := FontWidth(font, title)
dwh.Label.X = dwh.Header.X + wstyle.Header.Padding.X
dwh.Label.X += wstyle.Header.LabelPadding.X
dwh.Label.Y = dwh.Header.Y + wstyle.Header.LabelPadding.Y
dwh.Label.H = FontHeight(font) + 2*wstyle.Header.LabelPadding.Y
dwh.Label.W = t + 2*wstyle.Header.Spacing.X
dwh.LayoutHeaderH = layout.HeaderH
dwh.RowHeight = layout.Row.Height
dwh.Title = title
win.widgets.Add(nstyle.WidgetStateInactive, layout.Bounds)
dwh.Draw(&win.ctx.Style, &win.cmds)
// window close button
closeButton.Y = dwh.Header.Y + wstyle.Header.Padding.Y
closeButton.H = layout.HeaderH - 2*wstyle.Header.Padding.Y
closeButton.W = closeButton.H
if win.flags&WindowClosable != 0 {
if wstyle.Header.Align == nstyle.HeaderRight {
closeButton.X = (dwh.Header.W + dwh.Header.X) - (closeButton.W + wstyle.Header.Padding.X)
dwh.Header.W -= closeButton.W + wstyle.Header.Spacing.X + wstyle.Header.Padding.X
} else {
closeButton.X = dwh.Header.X + wstyle.Header.Padding.X
dwh.Header.X += closeButton.W + wstyle.Header.Spacing.X + wstyle.Header.Padding.X
}
if doButton(win, label.S(wstyle.Header.CloseSymbol), closeButton, &wstyle.Header.CloseButton, in, false) {
win.close = true
}
}
} else {
dwh.LayoutHeaderH = layout.HeaderH
dwh.RowHeight = layout.Row.Height
win.widgets.Add(nstyle.WidgetStateInactive, layout.Bounds)
dwh.Draw(&win.ctx.Style, &win.cmds)
}
if (win.flags&WindowMovable != 0) && win.toplevel() {
var move rect.Rect
move.X = win.Bounds.X
move.Y = win.Bounds.Y
move.W = win.Bounds.W
move.H = FontHeight(font) + 2.0*wstyle.Header.Padding.Y + 2.0*wstyle.Header.LabelPadding.Y
if in.Mouse.IsClickDownInRect(mouse.ButtonLeft, move, true) && !in.Mouse.IsClickDownInRect(mouse.ButtonLeft, closeButton, true) {
win.moving = true
}
}
var dwb drawableWindowBody
dwb.NoScrollbar = win.flags&WindowNoScrollbar != 0
dwb.Style = win.style()
/* calculate and set the window clipping rectangle*/
if win.flags&WindowDynamic == 0 {
layout.Clip.X = layout.Bounds.X + window_padding.X
layout.Clip.W = layout.Width - 2*window_padding.X
} else {
layout.Clip.X = layout.Bounds.X
layout.Clip.W = layout.Width
}
layout.Clip.H = layout.Bounds.H - (layout.FooterH + layout.HeaderH)
// FooterH already includes the window padding
layout.Clip.H -= window_padding.Y
layout.Clip.Y = layout.Bounds.Y
/* combo box and menu do not have header space */
if win.flags&windowCombo == 0 && win.flags&windowMenu == 0 {
layout.Clip.Y += layout.HeaderH
}
clip := unify(win.cmds.Clip, layout.Clip)
layout.Clip = clip
dwb.Bounds = layout.Bounds
dwb.LayoutWidth = layout.Width
dwb.Clip = layout.Clip
win.cmds.Clip = dwb.Clip
win.widgets.Add(nstyle.WidgetStateInactive, dwb.Bounds)
dwb.Draw(&win.ctx.Style, &win.cmds)
layout.Row.Type = layoutInvalid
}
func (win *Window) specialPanelBegin() {
win.began = true
w := win.Master()
ctx := w.context()
if win.flags&windowContextual != 0 {
prevbody := win.Bounds
prevbody.H = win.layout.Height
// if the contextual menu ended up with its bottom right corner outside
// the main window's bounds and it could be moved to be inside the main
// window by popping it a different way do it.
// Since the size of the contextual menu is only knowable after displaying
// it once this must be done on the second frame.
max := ctx.Windows[0].Bounds.Max()
if win.header.Contains(prevbody.Min()) && ((prevbody.Max().X > max.X) || (prevbody.Max().Y > max.Y)) && (win.Bounds.X-prevbody.W >= 0) && (win.Bounds.Y-prevbody.H >= 0) {
win.Bounds.X = win.Bounds.X - prevbody.W
win.Bounds.Y = win.Bounds.Y - prevbody.H
} else {
win.Bounds.X = win.Bounds.X
win.Bounds.Y = win.Bounds.Y
}
}
if win.flags&windowHDynamic != 0 && !win.first {
uw := win.menuItemWidth + 2*win.style().Padding.X + 2*win.style().Border
if uw < win.Bounds.W {
win.Bounds.W = uw
}
}
if win.flags&windowCombo != 0 && win.flags&WindowDynamic != 0 {
prevbody := win.Bounds
prevbody.H = win.layout.Height
// If the combo window ends up with the right corner below the
// main winodw's lower bound make it non-dynamic and resize it to its
// maximum possible size that will show the whole combo box.
max := ctx.Windows[0].Bounds.Max()
if prevbody.Y+prevbody.H > max.Y {
prevbody.H = max.Y - prevbody.Y
win.Bounds = prevbody
win.flags &= ^windowCombo
}
}
if win.flags&windowNonblock != 0 && !win.first {
/* check if user clicked outside the popup and close if so */
in_panel := ctx.Input.Mouse.IsClickInRect(mouse.ButtonLeft, win.ctx.Windows[0].layout.Bounds)
prevbody := win.Bounds
prevbody.H = win.layout.Height
in_body := ctx.Input.Mouse.IsClickInRect(mouse.ButtonLeft, prevbody)
in_header := ctx.Input.Mouse.IsClickInRect(mouse.ButtonLeft, win.header)
if !in_body && in_panel || in_header {
win.close = true
}
}
if win.flags&windowPopup != 0 {
win.cmds.PushScissor(nk_null_rect)
panelBegin(ctx, win, win.title)
win.layout.Offset = &win.Scrollbar
}
if win.first && (win.flags&windowContextual != 0 || win.flags&windowHDynamic != 0) {
ctx.trashFrame = true
}
win.first = false
}
var nk_null_rect = rect.Rect{-8192.0, -8192.0, 16384.0, 16384.0}
func panelEnd(ctx *context, window *Window) {
var footer = rect.Rect{0, 0, 0, 0}
layout := window.layout
style := &ctx.Style
in := &Input{}
if window.toplevel() {
ctx.Input.Mouse.clip = nk_null_rect
in = &ctx.Input
}
outclip := nk_null_rect
if window.flags&windowGroup != 0 {
outclip = window.parent.cmds.Clip
}
window.cmds.PushScissor(outclip)
wstyle := window.style()
/* cache configuration data */
item_spacing := wstyle.Spacing
window_padding := wstyle.Padding
scrollbar_size := wstyle.ScrollbarSize
scaler_size := wstyle.ScalerSize
/* update the current cursor Y-position to point over the last added widget */
layout.AtY += layout.Row.Height
/* draw footer and fill empty spaces inside a dynamically growing panel */
if layout.Flags&WindowDynamic != 0 {
layout.Height = layout.AtY - layout.Bounds.Y
layout.Height = min(layout.Height, layout.Bounds.H)
// fill horizontal scrollbar space
{
var bounds rect.Rect
bounds.X = window.Bounds.X
bounds.Y = layout.AtY - item_spacing.Y
bounds.W = window.Bounds.W
bounds.H = window.Bounds.Y + layout.Height + item_spacing.Y + window.style().Padding.Y - bounds.Y
window.cmds.FillRect(bounds, 0, wstyle.Background)
}
if (layout.Offset.X == 0) || (layout.Flags&WindowNoScrollbar != 0) {
/* special case for dynamic windows without horizontal scrollbar
* or hidden scrollbars */
footer.X = window.Bounds.X
footer.Y = window.Bounds.Y + layout.Height + item_spacing.Y + window.style().Padding.Y
footer.W = window.Bounds.W + scrollbar_size.X
layout.FooterH = 0
footer.H = 0
if (layout.Offset.X == 0) && layout.Flags&WindowNoScrollbar == 0 {
/* special case for windows like combobox, menu require draw call
* to fill the empty scrollbar background */
var bounds rect.Rect
bounds.X = layout.Bounds.X + layout.Width
bounds.Y = layout.Clip.Y
bounds.W = scrollbar_size.X
bounds.H = layout.Height
window.cmds.FillRect(bounds, 0, wstyle.Background)
}
} else {
/* dynamic window with visible scrollbars and therefore bigger footer */
footer.X = window.Bounds.X
footer.W = window.Bounds.W + scrollbar_size.X
footer.H = layout.FooterH
if (layout.Flags&windowCombo != 0) || (layout.Flags&windowMenu != 0) || (layout.Flags&windowContextual != 0) {
footer.Y = window.Bounds.Y + layout.Height
} else {
footer.Y = window.Bounds.Y + layout.Height + layout.FooterH
}
window.cmds.FillRect(footer, 0, wstyle.Background)
if layout.Flags&windowCombo == 0 && layout.Flags&windowMenu == 0 {
/* fill empty scrollbar space */
var bounds rect.Rect
bounds.X = layout.Bounds.X
bounds.Y = window.Bounds.Y + layout.Height
bounds.W = layout.Bounds.W
bounds.H = layout.Row.Height
window.cmds.FillRect(bounds, 0, wstyle.Background)
}
}
}
/* scrollbars */
if layout.Flags&WindowNoScrollbar == 0 {
var bounds rect.Rect
var scroll_target float64
var scroll_offset float64
var scroll_step float64
var scroll_inc float64
{
/* vertical scrollbar */
bounds.X = layout.Bounds.X + layout.Width
bounds.Y = layout.Clip.Y
bounds.W = scrollbar_size.Y
bounds.H = layout.Clip.H
if layout.Flags&WindowBorder != 0 {
bounds.H -= 1
}
scroll_offset = float64(layout.Offset.Y)
scroll_step = float64(layout.Clip.H) * 0.10
scroll_inc = float64(layout.Clip.H) * 0.01
scroll_target = float64(layout.AtY - layout.Clip.Y)
scroll_offset = doScrollbarv(window, bounds, layout.Bounds, scroll_offset, scroll_target, scroll_step, scroll_inc, &ctx.Style.Scrollv, in, style.Font)
layout.Offset.Y = int(scroll_offset)
}
if layout.Flags&WindowNoHScrollbar == 0 {
/* horizontal scrollbar */
bounds.X = layout.Bounds.X + window_padding.X
if layout.Flags&windowSub != 0 {
bounds.H = scrollbar_size.X
bounds.Y = layout.Bounds.Y
if layout.Flags&WindowBorder != 0 {
bounds.Y++
}
bounds.Y += layout.HeaderH + layout.Menu.H + layout.Height
bounds.W = layout.Clip.W
} else if layout.Flags&WindowDynamic != 0 {
bounds.H = min(scrollbar_size.X, layout.FooterH)
bounds.W = layout.Bounds.W
bounds.Y = footer.Y
} else {
bounds.H = min(scrollbar_size.X, layout.FooterH)
bounds.Y = layout.Bounds.Y + window.Bounds.H
bounds.Y -= max(layout.FooterH, scrollbar_size.X)
bounds.W = layout.Width - 2*window_padding.X
}
scroll_offset = float64(layout.Offset.X)
scroll_target = float64(layout.MaxX - bounds.X)
scroll_step = float64(layout.MaxX) * 0.05
scroll_inc = float64(layout.MaxX) * 0.005
scroll_offset = doScrollbarh(window, bounds, scroll_offset, scroll_target, scroll_step, scroll_inc, &ctx.Style.Scrollh, in, style.Font)
layout.Offset.X = int(scroll_offset)
}
}
var dsab drawableScalerAndBorders
dsab.Style = window.style()
dsab.Bounds = window.Bounds
dsab.Border = layout.Border
dsab.HeaderH = layout.HeaderH
/* scaler */
if layout.Flags&WindowScalable != 0 {
dsab.DrawScaler = true
dsab.ScalerRect.W = max(0, scaler_size.X)
dsab.ScalerRect.H = max(0, scaler_size.Y)
dsab.ScalerRect.X = (layout.Bounds.X + layout.Bounds.W) - (window_padding.X + dsab.ScalerRect.W)
/* calculate scaler bounds */
if layout.Flags&WindowDynamic != 0 {
dsab.ScalerRect.Y = footer.Y + layout.FooterH - scaler_size.Y
} else {
dsab.ScalerRect.Y = layout.Bounds.Y + layout.Bounds.H - (scaler_size.Y + window_padding.Y)
}
/* do window scaling logic */
if window.toplevel() {
if window.scaling {
if in == nil || !in.Mouse.Down(mouse.ButtonLeft) {
window.scaling = false
} else {
window.scale(in.Mouse.Delta)
}
} else if in != nil && in.Mouse.IsClickDownInRect(mouse.ButtonLeft, dsab.ScalerRect, true) {
window.scaling = true
}
}
}
/* window border */
if layout.Flags&WindowBorder != 0 {
dsab.DrawBorders = true
if layout.Flags&WindowDynamic != 0 {
dsab.PaddingY = layout.FooterH + footer.Y
} else {
dsab.PaddingY = layout.Bounds.Y + layout.Bounds.H
}
/* select correct border color */
dsab.BorderColor = wstyle.BorderColor
/* draw border between header and window body */
if window.flags&WindowBorderHeader != 0 {
dsab.DrawHeaderBorder = true
}
}
window.widgets.Add(nstyle.WidgetStateInactive, dsab.Bounds)
dsab.Draw(&window.ctx.Style, &window.cmds)
layout.Flags |= windowEnabled
window.flags = layout.Flags
/* helper to make sure you have a 'nk_tree_push'
* for every 'nk_tree_pop' */
assert(layout.Row.TreeDepth == 0)
}
// MenubarBegin adds a menubar to the current window.
// A menubar is an area displayed at the top of the window that is unaffected by scrolling.
// Remember to call MenubarEnd when you are done adding elements to the menubar.
func (win *Window) MenubarBegin() {
layout := win.layout
layout.Menu.X = layout.AtX
layout.Menu.Y = layout.Bounds.Y + layout.HeaderH
layout.Menu.W = layout.Width
layout.Menu.Offset = *layout.Offset
layout.Offset.Y = 0
}
func (win *Window) move(delta image.Point, pos image.Point) {
if win.flags&windowDocked != 0 {
if delta.X != 0 && delta.Y != 0 {
win.ctx.DockedWindows.Undock(win)
}
return
}
if canDock, bounds := win.ctx.DockedWindows.Dock(nil, pos, win.ctx.Windows[0].Bounds, win.ctx.Style.Scaling); canDock {
win.ctx.finalCmds.FillRect(bounds, 0, color.RGBA{0x0, 0x0, 0x50, 0x50})
}
win.Bounds.X = win.Bounds.X + delta.X
win.Bounds.X = clampInt(0, win.Bounds.X, win.ctx.Windows[0].Bounds.X+win.ctx.Windows[0].Bounds.W-FontHeight(win.ctx.Style.Font))
win.Bounds.Y = win.Bounds.Y + delta.Y
win.Bounds.Y = clampInt(0, win.Bounds.Y, win.ctx.Windows[0].Bounds.Y+win.ctx.Windows[0].Bounds.H-FontHeight(win.ctx.Style.Font))
}
func (win *Window) scale(delta image.Point) {
if win.flags&windowDocked != 0 {
win.ctx.DockedWindows.Scale(win, delta, win.ctx.Style.Scaling)
return
}
window_size := win.style().MinSize
win.Bounds.W = max(window_size.X, win.Bounds.W+delta.X)
/* dragging in y-direction is only possible if static window */
if win.layout.Flags&WindowDynamic == 0 {
win.Bounds.H = max(window_size.Y, win.Bounds.H+delta.Y)
}
}
// MenubarEnd signals that all widgets have been added to the menubar.
func (win *Window) MenubarEnd() {
layout := win.layout
layout.Menu.H = layout.AtY - layout.Menu.Y
layout.Clip.Y = layout.Bounds.Y + layout.HeaderH + layout.Menu.H + layout.Row.Height
layout.Height -= layout.Menu.H
*layout.Offset = layout.Menu.Offset
layout.Clip.H -= layout.Menu.H + layout.Row.Height
layout.AtY = layout.Menu.Y + layout.Menu.H
win.cmds.PushScissor(layout.Clip)
}
func (win *Window) widget() (valid bool, bounds rect.Rect, calcFittingWidth FittingWidthFn) {
/* allocate space and check if the widget needs to be updated and drawn */
calcFittingWidth = panelAllocSpace(&bounds, win)
if !win.layout.Clip.Intersect(&bounds) {
return false, bounds, calcFittingWidth
}
return true, bounds, calcFittingWidth
}
func (win *Window) widgetFitting(item_padding image.Point) (valid bool, bounds rect.Rect) {
/* update the bounds to stand without padding */
style := win.style()
layout := win.layout
valid, bounds, _ = win.widget()
if layout.Row.Index == 1 {
bounds.W += style.Padding.X
bounds.X -= style.Padding.X
} else {
bounds.X -= item_padding.X
}
if layout.Row.Columns > 0 && layout.Row.Index == layout.Row.Columns {
bounds.W += style.Padding.X
} else {
bounds.W += item_padding.X
}
return valid, bounds
}
func panelAllocSpace(bounds *rect.Rect, win *Window) FittingWidthFn {
if win.usingSub {
panic(UsingSubErr)
}
/* check if the end of the row has been hit and begin new row if so */
layout := win.layout
if layout.Row.Columns > 0 && layout.Row.Index >= layout.Row.Columns {
panelAllocRow(win)
}
/* calculate widget position and size */
layoutWidgetSpace(bounds, win.ctx, win, true)
win.LastWidgetBounds = *bounds
layout.Row.Index++
if win.layout.Row.CalcMaxWidth {
col := win.adjust[win.layout.Cnt][win.layout.Row.Index2-1]
return func(width int) {
if width > col.width {
col.width = width
}
}
}
return nil
}
func panelAllocRow(win *Window) {
layout := win.layout
spacing := win.style().Spacing
row_height := layout.Row.Height - spacing.Y
panelLayout(win.ctx, win, row_height, layout.Row.Columns, 0)
}
func panelLayout(ctx *context, win *Window, height int, cols int, cnt int) {
/* prefetch some configuration data */
layout := win.layout
style := win.style()
item_spacing := style.Spacing
if height == 0 {
height = layout.Bounds.H - layout.FooterH - (layout.AtY - layout.Bounds.Y)
subtractHeight := true
if layout.Row.Index == 0 {
subtractHeight = false
}
if subtractHeight {
height -= layout.Row.Height
}
if layout.ReservedHeight > 0 {
height -= layout.ReservedHeight + item_spacing.Y
}
}
/* update the current row and set the current row layout */
layout.Cnt = cnt
layout.Row.Index = 0
layout.Row.Index2 = 0
layout.Row.CalcMaxWidth = false
layout.AtY += layout.Row.Height
layout.Row.Columns = cols
layout.Row.Height = height + item_spacing.Y
layout.Row.ItemOffset = 0
if layout.Flags&WindowDynamic != 0 {
win.cmds.FillRect(rect.Rect{layout.Bounds.X, layout.AtY, layout.Bounds.W, height + item_spacing.Y}, 0, style.Background)
}
}
const (
layoutDynamicFixed = iota
layoutDynamicFree
layoutDynamic
layoutStaticFree
layoutStatic
layoutInvalid
)
var InvalidLayoutErr = errors.New("invalid layout")
var UsingSubErr = errors.New("parent window used while populating a sub window")
func layoutWidgetSpace(bounds *rect.Rect, ctx *context, win *Window, modify bool) {
layout := win.layout
/* cache some configuration data */
style := win.style()
spacing := style.Spacing
padding := style.Padding
/* calculate the usable panel space */
panel_padding := 2 * padding.X
panel_spacing := int(float64(layout.Row.Columns-1) * float64(spacing.X))
panel_space := layout.Width - panel_padding - panel_spacing
/* calculate the width of one item inside the current layout space */
item_offset := 0
item_width := 0
item_spacing := 0
switch layout.Row.Type {
case layoutInvalid:
panic(InvalidLayoutErr)
case layoutDynamicFixed:
/* scaling fixed size widgets item width */
item_width = int(float64(panel_space) / float64(layout.Row.Columns))
item_offset = layout.Row.Index * item_width
item_spacing = layout.Row.Index * spacing.X
case layoutDynamicFree:
/* panel width depended free widget placing */
bounds.X = layout.AtX + int(float64(layout.Width)*layout.Row.DynamicFreeX)
bounds.X -= layout.Offset.X
bounds.Y = layout.AtY + int(float64(layout.Row.Height)*layout.Row.DynamicFreeY)
bounds.Y -= layout.Offset.Y
bounds.W = int(float64(layout.Width) * layout.Row.DynamicFreeW)
bounds.H = int(float64(layout.Row.Height) * layout.Row.DynamicFreeH)
return
case layoutDynamic:
/* scaling arrays of panel width ratios for every widget */
var ratio float64
if layout.Row.Ratio[layout.Row.Index] < 0 {
ratio = layout.Row.ItemRatio
} else {
ratio = layout.Row.Ratio[layout.Row.Index]
}
item_spacing = layout.Row.Index * spacing.X
item_width = int(ratio * float64(panel_space))
item_offset = layout.Row.ItemOffset
if modify {
layout.Row.ItemOffset += item_width
layout.Row.Filled += ratio
}
case layoutStaticFree:
/* free widget placing */
atx, aty := layout.AtX, layout.AtY
if atx < layout.Clip.X {
atx = layout.Clip.X
}
if aty < layout.Clip.Y {
aty = layout.Clip.Y
}
bounds.X = atx + layout.Row.Item.X
bounds.W = layout.Row.Item.W
if ((bounds.X + bounds.W) > layout.MaxX) && modify {
layout.MaxX = (bounds.X + bounds.W)
}
bounds.X -= layout.Offset.X
bounds.Y = aty + layout.Row.Item.Y
bounds.Y -= layout.Offset.Y
bounds.H = layout.Row.Item.H
return
case layoutStatic:
/* non-scaling array of panel pixel width for every widget */
item_spacing = layout.Row.Index * spacing.X
if len(layout.Row.WidthArr) > 0 {
item_width = layout.Row.WidthArr[layout.Row.Index]
} else {
item_width = layout.Row.ItemWidth
}
item_offset = layout.Row.ItemOffset
if modify {
layout.Row.ItemOffset += item_width
}
default:
assert(false)
}
/* set the bounds of the newly allocated widget */
bounds.W = item_width
bounds.H = layout.Row.Height - spacing.Y
bounds.Y = layout.AtY - layout.Offset.Y
bounds.X = layout.AtX + item_offset + item_spacing + padding.X
if ((bounds.X + bounds.W) > layout.MaxX) && modify {
layout.MaxX = bounds.X + bounds.W
}
bounds.X -= layout.Offset.X
}
func (ctx *context) scale(x int) int {
return int(float64(x) * ctx.Style.Scaling)
}
func rowLayoutCtr(win *Window, height int, cols int, width int) {
/* update the current row and set the current row layout */
panelLayout(win.ctx, win, height, cols, 0)
win.layout.Row.Type = layoutDynamicFixed
win.layout.Row.ItemWidth = width
win.layout.Row.ItemRatio = 0.0
win.layout.Row.Ratio = nil
win.layout.Row.ItemOffset = 0
win.layout.Row.Filled = 0
}
// Reserves space for num rows of the specified height at the bottom
// of the panel.
// If a row of height == 0 is inserted it will take reserved space
// into account.
func (win *Window) LayoutReserveRow(height int, num int) {
win.LayoutReserveRowScaled(win.ctx.scale(height), num)
}
// Like LayoutReserveRow but with a scaled height.
func (win *Window) LayoutReserveRowScaled(height int, num int) {
win.layout.ReservedHeight += height*num + win.style().Spacing.Y*num
}