-
Notifications
You must be signed in to change notification settings - Fork 27
/
window.go
243 lines (211 loc) · 5.76 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
package nanogui
import (
"fmt"
"github.com/shibukawa/glfw"
"github.com/shibukawa/nanovgo"
)
type Window struct {
WidgetImplement
title string
buttonPanel Widget
modal bool
drag bool
draggable bool
depth int
}
type IWindow interface {
Widget
RefreshRelativePlacement()
SetDepth(d int)
}
func NewWindow(parent Widget, title string) *Window {
if title == "" {
title = "Untitled"
}
window := &Window{
title: title,
draggable: true,
}
InitWidget(window, parent)
return window
}
// Title() returns the window title
func (w *Window) Title() string {
return w.title
}
// SetTitle() sets the window title
func (w *Window) SetTitle(title string) {
w.title = title
}
// Modal() returns is this a model dialog?
func (w *Window) Modal() bool {
return w.modal
}
// SetModal() set whether or not this is a modal dialog
func (w *Window) SetModal(m bool) {
w.modal = m
}
func (w *Window) Draggable() bool {
return w.draggable
}
func (w *Window) SetDraggable(flag bool) {
w.draggable = flag
}
func (w *Window) ButtonPanel() Widget {
if w.buttonPanel == nil {
w.buttonPanel = NewWidget(w)
w.buttonPanel.SetLayout(NewBoxLayout(Horizontal, Middle, 0, 4))
}
return w.buttonPanel
}
// Dispose() disposes the window
func (w *Window) Dispose() {
var widget Widget = w
var parent Widget = w.Parent()
for parent != nil {
widget = parent
parent = widget.Parent()
}
screen := widget.(*Screen)
screen.DisposeWindow(w)
}
// Center() makes the window center in the current Screen
func (w *Window) Center() {
var widget Widget = w
var parent Widget = w.Parent()
for parent != nil {
widget = parent
parent = widget.Parent()
}
screen := widget.(*Screen)
screen.CenterWindow(w)
}
// RefreshRelativePlacement is internal helper function to maintain nested window position values; overridden in \ref Popup
func (w *Window) RefreshRelativePlacement() {
// overridden in Popup
}
func (w *Window) MouseButtonEvent(self Widget, x, y int, button glfw.MouseButton, down bool, modifier glfw.ModifierKey) bool {
if w.WidgetImplement.MouseButtonEvent(self, x, y, button, down, modifier) {
return true
}
if button == glfw.MouseButton1 && w.draggable {
w.drag = down && (y-w.y) < w.theme.WindowHeaderHeight
return true
}
return false
}
func (w *Window) MouseDragEvent(self Widget, x, y, relX, relY, button int, modifier glfw.ModifierKey) bool {
if w.drag && (button&1<<uint(glfw.MouseButton1)) != 0 {
pW, pH := self.Parent().Size()
w.x = clampI(w.x+relX, 0, pW-w.w)
w.y = clampI(w.y+relY, 0, pH-w.h)
return true
}
return false
}
func (w *Window) ScrollEvent(self Widget, x, y, relX, relY int) bool {
w.WidgetImplement.ScrollEvent(self, x, y, relX, relY)
return true
}
func (w *Window) PreferredSize(self Widget, ctx *nanovgo.Context) (int, int) {
if w.buttonPanel != nil {
w.buttonPanel.SetVisible(false)
}
width, height := w.WidgetImplement.PreferredSize(self, ctx)
if w.buttonPanel != nil {
w.buttonPanel.SetVisible(true)
}
ctx.SetFontSize(18.0)
ctx.SetFontFace(w.theme.FontBold)
_, bounds := ctx.TextBounds(0, 0, w.title)
return maxI(width, int(bounds[2]-bounds[0])+20), maxI(height, int(bounds[3]-bounds[1]))
}
func (w *Window) OnPerformLayout(self Widget, ctx *nanovgo.Context) {
if w.buttonPanel == nil {
w.WidgetImplement.OnPerformLayout(self, ctx)
} else {
w.buttonPanel.SetVisible(false)
w.WidgetImplement.OnPerformLayout(self, ctx)
for _, c := range w.buttonPanel.Children() {
c.SetFixedSize(22, 22)
c.SetFontSize(15)
}
w.buttonPanel.SetVisible(true)
w.buttonPanel.SetSize(w.Width(), 22)
panelW, _ := w.buttonPanel.PreferredSize(w.buttonPanel, ctx)
w.buttonPanel.SetPosition(w.Width()-(panelW+5), 3)
w.buttonPanel.OnPerformLayout(w.buttonPanel, ctx)
}
}
func (w *Window) Draw(self Widget, ctx *nanovgo.Context) {
ds := float32(w.theme.WindowDropShadowSize)
cr := float32(w.theme.WindowCornerRadius)
hh := float32(w.theme.WindowHeaderHeight)
// Draw window
wx := float32(w.x)
wy := float32(w.y)
ww := float32(w.w)
wh := float32(w.h)
ctx.Save()
ctx.BeginPath()
ctx.RoundedRect(wx, wy, ww, wh, cr)
if w.mouseFocus {
ctx.SetFillColor(w.theme.WindowFillFocused)
} else {
ctx.SetFillColor(w.theme.WindowFillUnfocused)
}
ctx.Fill()
// Draw a drop shadow
shadowPaint := nanovgo.BoxGradient(wx, wy, ww, wh, cr*2, ds*2, w.theme.DropShadow, w.theme.Transparent)
ctx.BeginPath()
ctx.Rect(wx-ds, wy-ds, ww+ds*2, wh+ds*2)
ctx.RoundedRect(wx, wy, ww, wh, cr)
ctx.PathWinding(nanovgo.Hole)
ctx.SetFillPaint(shadowPaint)
ctx.Fill()
if w.title != "" {
headerPaint := nanovgo.LinearGradient(wx, wy, ww, wh+hh, w.theme.WindowHeaderGradientTop, w.theme.WindowHeaderGradientBot)
ctx.BeginPath()
ctx.RoundedRect(wx, wy, ww, hh, cr)
ctx.SetFillPaint(headerPaint)
ctx.Fill()
ctx.BeginPath()
ctx.RoundedRect(wx, wy, ww, wh, cr)
ctx.SetStrokeColor(w.theme.WindowHeaderSepTop)
ctx.Scissor(wx, wy, ww, 0.5)
ctx.Stroke()
ctx.ResetScissor()
ctx.BeginPath()
ctx.MoveTo(wx+0.5, wy+hh-1.5)
ctx.LineTo(wx+ww-0.5, wy+hh-1.5)
ctx.SetStrokeColor(w.theme.WindowHeaderSepTop)
ctx.Stroke()
ctx.SetFontSize(18.0)
ctx.SetFontFace(w.theme.FontBold)
ctx.SetTextAlign(nanovgo.AlignCenter | nanovgo.AlignMiddle)
ctx.SetFontBlur(2.0)
ctx.SetFillColor(w.theme.DropShadow)
ctx.Text(wx+ww*0.5, wy+hh*0.5, w.title)
ctx.SetFontBlur(0.0)
if w.focused {
ctx.SetFillColor(w.theme.WindowTitleFocused)
} else {
ctx.SetFillColor(w.theme.WindowTitleUnfocused)
}
ctx.Text(wx+ww*0.5, wy+hh*0.5-1, w.title)
}
ctx.Restore()
w.WidgetImplement.Draw(self, ctx)
}
func (w *Window) FindWindow() IWindow {
return w
}
func (w *Window) String() string {
return w.StringHelper(fmt.Sprintf("Window(%d)", w.Depth()), w.title)
}
func (w *Window) SetDepth(d int) {
w.depth = d
}
func (w *Window) Depth() int {
return w.depth
}