forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SplitLayout.go
134 lines (114 loc) · 3.33 KB
/
SplitLayout.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
package giu
import (
"fmt"
"github.com/HACKERALERT/imgui-go"
)
type SplitDirection uint8
const (
DirectionHorizontal SplitDirection = 1 << iota
DirectionVertical
)
var _ Disposable = &SplitLayoutState{}
type SplitLayoutState struct {
delta float32
sashPos float32
}
// Dispose implements Disposable interface
func (s *SplitLayoutState) Dispose() {
// Nothing to do here.
}
type SplitLayoutWidget struct {
id string
direction SplitDirection
layout1 Widget
layout2 Widget
originItemSpacingX float32
originItemSpacingY float32
originFramePaddingX float32
originFramePaddingY float32
sashPos float32
border bool
}
func SplitLayout(direction SplitDirection, border bool, sashPos float32, layout1, layout2 Widget) *SplitLayoutWidget {
return &SplitLayoutWidget{
direction: direction,
sashPos: sashPos,
layout1: layout1,
layout2: layout2,
border: border,
}
}
func (s *SplitLayoutWidget) restoreItemSpacing(layout Widget) Layout {
return Layout{
Custom(func() {
PushItemSpacing(s.originItemSpacingX, s.originItemSpacingY)
PushFramePadding(s.originFramePaddingX, s.originFramePaddingY)
// Restore Child bg color
bgColor := imgui.CurrentStyle().GetColor(imgui.StyleColorChildBg)
PushStyleColor(StyleColorChildBg, Vec4ToRGBA(bgColor))
}),
layout,
Custom(func() {
PopStyleColor()
PopStyleV(2)
}),
}
}
// Build Child panel. If layout is a SplitLayout, set the frame padding to zero.
func (s *SplitLayoutWidget) buildChild(width, height float32, layout Widget) Widget {
_, isSplitLayoutWidget := layout.(*SplitLayoutWidget)
return Layout{
Custom(func() {
if isSplitLayoutWidget || !s.border {
PushFramePadding(0, 0)
}
}),
Style().SetColor(StyleColorChildBg, Vec4ToRGBA(imgui.CurrentStyle().GetColor(imgui.StyleColorChildBg))).To(
Child().Border(!isSplitLayoutWidget && s.border).Size(width, height).Layout(s.restoreItemSpacing(layout)),
),
Custom(func() {
if isSplitLayoutWidget || !s.border {
PopStyle()
}
}),
}
}
func (s *SplitLayoutWidget) Build() {
s.id = GenAutoID("SplitLayout")
var splitLayoutState *SplitLayoutState
// Register state
stateID := fmt.Sprintf("SplitLayout_%s", s.id)
if state := Context.GetState(stateID); state == nil {
splitLayoutState = &SplitLayoutState{delta: 0.0, sashPos: s.sashPos}
Context.SetState(stateID, splitLayoutState)
} else {
splitLayoutState = state.(*SplitLayoutState)
}
itemSpacingX, itemSpacingY := GetItemInnerSpacing()
s.originItemSpacingX, s.originItemSpacingY = itemSpacingX, itemSpacingY
s.originFramePaddingX, s.originFramePaddingY = GetFramePadding()
s.originFramePaddingX /= Context.GetPlatform().GetContentScale()
s.originFramePaddingY /= Context.GetPlatform().GetContentScale()
var layout Layout
splitLayoutState.sashPos += splitLayoutState.delta
if s.direction == DirectionHorizontal {
layout = Layout{
Row(
s.buildChild(splitLayoutState.sashPos, 0, s.layout1),
VSplitter(&(splitLayoutState.delta)).Size(itemSpacingX, 0),
s.buildChild(0, 0, s.layout2),
),
}
} else {
layout = Layout{
Column(
s.buildChild(0, splitLayoutState.sashPos, s.layout1),
HSplitter(&(splitLayoutState.delta)).Size(0, itemSpacingY),
s.buildChild(0, 0, s.layout2),
),
}
}
PushItemSpacing(0, 0)
layout.Build()
PopStyle()
}