-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontext.go
More file actions
497 lines (436 loc) · 14.3 KB
/
context.go
File metadata and controls
497 lines (436 loc) · 14.3 KB
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 The Guigui Authors
package guigui
import (
"fmt"
"image"
"log/slog"
"os"
"slices"
"strings"
"sync"
"github.com/hajimehoshi/ebiten/v2"
"golang.org/x/text/language"
"github.com/guigui-gui/guigui/internal/locale"
)
var envLocales []language.Tag
func init() {
if locales := os.Getenv("GUIGUI_LOCALES"); locales != "" {
for tag := range strings.SplitSeq(os.Getenv("GUIGUI_LOCALES"), ",") {
l, err := language.Parse(strings.TrimSpace(tag))
if err != nil {
slog.Warn(fmt.Sprintf("invalid GUIGUI_LOCALES: %s", tag))
continue
}
envLocales = append(envLocales, l)
}
}
}
var systemLocales []language.Tag
func init() {
ls, err := locale.Locales()
if err != nil {
slog.Error(err.Error())
return
}
systemLocales = ls
}
type Context struct {
app *app
inBuild bool
appScaleMinus1 float64
defaultColorWarnOnce sync.Once
locales []language.Tag
allLocales []language.Tag
frontLayer int64
envSource EnvSource
defaultTickMethodCalled bool
}
// Scale returns the overall scale factor used for rendering.
// Scale is the product of [Context.DeviceScale] and [Context.AppScale].
func (c *Context) Scale() float64 {
return c.DeviceScale() * c.AppScale()
}
// DeviceScale returns the device scale factor.
func (c *Context) DeviceScale() float64 {
return c.app.deviceScale
}
// AppScale returns the application scale factor set by [Context.SetAppScale].
// The default value is 1.
func (c *Context) AppScale() float64 {
return c.appScaleMinus1 + 1
}
// SetAppScale sets the application scale factor.
func (c *Context) SetAppScale(scale float64) {
if c.appScaleMinus1 == scale-1 {
return
}
c.appScaleMinus1 = scale - 1
c.app.requestRedraw(c.app.bounds(), requestRedrawReasonAppScale, nil)
}
// ColorMode returns the resolved color mode.
//
// ColorMode never returns [ebiten.ColorModeUnknown].
func (c *Context) ColorMode() ebiten.ColorMode {
if mode := ebiten.WindowColorMode(); mode != ebiten.ColorModeUnknown {
return mode
}
if mode := ebiten.SystemColorMode(); mode != ebiten.ColorModeUnknown {
return mode
}
return ebiten.ColorModeLight
}
// PreferredColorMode returns the color mode set by SetPreferredColorMode.
//
// PreferredColorMode might return [ebiten.ColorModeUnknown] if the color mode is not set.
func (c *Context) PreferredColorMode() ebiten.ColorMode {
return ebiten.WindowColorMode()
}
// SetPreferredColorMode sets the preferred color mode.
//
// If mode is [ebiten.ColorModeUnknown], SetPreferredColorMode specifies the default system color mode.
func (c *Context) SetPreferredColorMode(mode ebiten.ColorMode) {
if mode == ebiten.WindowColorMode() {
return
}
ebiten.SetWindowColorMode(mode)
c.app.requestRebuild(c.app.root.widgetState(), requestRedrawReasonColorMode)
}
var (
envColorModeStr = os.Getenv("GUIGUI_COLOR_MODE")
)
func init() {
switch envColorModeStr {
case "light":
ebiten.SetWindowColorMode(ebiten.ColorModeLight)
case "dark":
ebiten.SetWindowColorMode(ebiten.ColorModeDark)
case "":
default:
slog.Warn(fmt.Sprintf("invalid GUIGUI_COLOR_MODE: %s", envColorModeStr))
}
}
// FirstLocale returns the first effective locale.
// The effective locales are determined by the app locales, the environment variable GUIGUI_LOCALES,
// and the system locales, in that priority order.
// If no locales are available, the zero value of language.Tag is returned.
func (c *Context) FirstLocale() language.Tag {
c.ensureAllLocales()
if len(c.allLocales) > 0 {
return c.allLocales[0]
}
return language.Tag{}
}
// AppendLocales appends all effective locales to the given slice and returns the result.
// The effective locales are determined by the app locales, the environment variable GUIGUI_LOCALES,
// and the system locales, in that priority order.
func (c *Context) AppendLocales(locales []language.Tag) []language.Tag {
c.ensureAllLocales()
return append(locales, c.allLocales...)
}
func (c *Context) ensureAllLocales() {
if len(c.allLocales) > 0 {
return
}
// App locales
for _, l := range c.locales {
if slices.Contains(c.allLocales, l) {
continue
}
c.allLocales = append(c.allLocales, l)
}
// Env locales
for _, l := range envLocales {
if slices.Contains(c.allLocales, l) {
continue
}
c.allLocales = append(c.allLocales, l)
}
// System locales
for _, l := range systemLocales {
if slices.Contains(c.allLocales, l) {
continue
}
c.allLocales = append(c.allLocales, l)
}
}
// AppendAppLocales appends the app locales set by [Context.SetAppLocales] to the given slice
// and returns the result.
func (c *Context) AppendAppLocales(locales []language.Tag) []language.Tag {
origLen := len(locales)
for _, l := range c.locales {
if slices.Contains(locales[origLen:], l) {
continue
}
locales = append(locales, l)
}
return locales
}
// SetAppLocales sets the application-level locales.
// These take the highest priority when resolving locales.
func (c *Context) SetAppLocales(locales []language.Tag) {
if slices.Equal(c.locales, locales) {
return
}
c.locales = slices.Delete(c.locales, 0, len(c.locales))
c.locales = append(c.locales, locales...)
c.allLocales = slices.Delete(c.allLocales, 0, len(c.allLocales))
c.app.requestRedraw(c.app.bounds(), requestRedrawReasonLocale, nil)
}
// AppBounds returns the bounds of the application.
func (c *Context) AppBounds() image.Rectangle {
return c.app.bounds()
}
// SetVisible sets whether the widget is visible.
// An invisible widget and its descendants do not receive any events and are not rendered.
func (c *Context) SetVisible(widget Widget, visible bool) {
widgetState := widget.widgetState()
if widgetState.hidden == !visible {
return
}
widgetState.hidden = !visible
if !visible {
c.blur(widget)
}
_ = traverseWidget(widget, func(w Widget) error {
w.widgetState().visibleCacheValid = false
w.widgetState().visibleCache = false
return nil
})
}
// IsVisible reports whether the widget is visible.
func (c *Context) IsVisible(widget Widget) bool {
return widget.widgetState().isVisible()
}
// SetEnabled sets whether the widget is enabled.
// A disabled widget and its descendants do not receive any input events.
func (c *Context) SetEnabled(widget Widget, enabled bool) {
widgetState := widget.widgetState()
if widgetState.disabled == !enabled {
return
}
widgetState.disabled = !enabled
if !enabled {
c.blur(widget)
}
_ = traverseWidget(widget, func(w Widget) error {
w.widgetState().enabledCacheValid = false
w.widgetState().enabledCache = false
return nil
})
}
// IsEnabled reports whether the widget is enabled.
func (c *Context) IsEnabled(widget Widget) bool {
return widget.widgetState().isEnabled()
}
// SetButtonInputReceptive sets whether the widget receives button input events
// even when it is not focused. This is useful for modeless popups like context menus,
// where the popup needs to handle keyboard navigation (Up/Down/Enter/Escape)
// while another widget retains focus.
//
// A button-input-receptive widget and all its descendants receive button input
// events via [Widget.HandleButtonInput]. Unlike focus, ancestors of a
// button-input-receptive widget do not receive button input events themselves;
// the framework only traverses through them to reach the receptive widget.
func (c *Context) SetButtonInputReceptive(widget Widget, receptive bool) {
widget.widgetState().buttonInputReceptive = receptive
}
// IsButtonInputReceptive reports whether the widget receives button input events
// even when it is not focused.
func (c *Context) IsButtonInputReceptive(widget Widget) bool {
return widget.widgetState().buttonInputReceptive
}
// SetFocused sets or removes the focus on the widget.
//
// When a widget is focused, both the widget and all its ancestors receive
// button input events via [Widget.HandleButtonInput].
// Only one widget can be focused at a time.
func (c *Context) SetFocused(widget Widget, focused bool) {
if focused {
c.focus(widget)
} else {
c.blur(widget)
}
}
func (c *Context) resolveFocusedWidget(widget Widget) Widget {
origWidget := widget
visited := map[Widget]struct{}{}
for {
if !c.canHaveFocus(widget.widgetState()) {
return nil
}
if widget.widgetState().focusDelegate == nil {
return widget
}
if _, ok := visited[widget]; ok {
panic(fmt.Sprintf("guigui: infinite focus delegation loop: %T", origWidget))
}
visited[widget] = struct{}{}
widget = widget.widgetState().focusDelegate
}
}
func (c *Context) focus(widget Widget) {
ws := c.resolveFocusedWidget(widget)
c.app.focusWidget(ws)
}
func (c *Context) blur(widget Widget) {
if c.app.focusedWidget == nil {
return
}
widgetState := widget.widgetState()
if !widgetState.isInTree(c.app.buildCount) {
return
}
_ = traverseWidget(widget, func(w Widget) error {
if !areWidgetsSame(c.app.focusedWidget, w) {
return nil
}
for ; w != nil && w.widgetState() != nil; w = w.widgetState().parent {
if ws := c.resolveFocusedWidget(w); ws != nil && !areWidgetsSame(ws, c.app.focusedWidget) {
c.app.focusWidget(ws)
break
}
}
return skipTraverse
})
}
func (c *Context) canHaveFocus(widgetState *widgetState) bool {
return widgetState.isInTree(c.app.buildCount) && widgetState.isVisible() && widgetState.isEnabled()
}
// IsFocused reports whether the widget is focused.
func (c *Context) IsFocused(widget Widget) bool {
return c.canHaveFocus(widget.widgetState()) && areWidgetsSame(c.app.focusedWidget, widget)
}
// IsFocusedOrHasFocusedChild reports whether the widget is focused
// or has a focused descendant.
//
// IsFocusedOrHasFocusedChild must not be called in [Widget.Build] implementations
// because it depends on the finished widget tree.
func (c *Context) IsFocusedOrHasFocusedChild(widget Widget) bool {
if c.inBuild {
panic("guigui: IsFocusedOrHasFocusedChild cannot be called in Build")
}
widgetState := widget.widgetState()
return widgetState.focusedOrHasFocusedChild && widgetState.isInTree(c.app.buildCount) && widgetState.isVisible()
}
// Opacity returns the opacity of the widget.
// The value is in the range [0, 1], where 0 is fully transparent and 1 is fully opaque.
func (c *Context) Opacity(widget Widget) float64 {
return widget.widgetState().opacity()
}
// SetOpacity sets the opacity of the widget.
// The value is clamped to the range [0, 1].
func (c *Context) SetOpacity(widget Widget, opacity float64) {
opacity = min(max(opacity, 0), 1)
widget.widgetState().transparency = 1 - opacity
}
// EnvSource provides information about the origin of an [Context.Env] call.
type EnvSource struct {
// Origin is the widget that originally called [Context.Env].
Origin Widget
// Child is the direct child of the current widget in the walk path.
// Child is nil when the current widget is the Origin itself.
Child Widget
}
// Env returns an environment value for the given key by walking up the widget tree.
// It calls [Widget.Env] on the given widget first. If the second return value is false,
// it tries the parent widget, repeating recursively up to the root widget.
func (c *Context) Env(widget Widget, key EnvKey) (any, bool) {
c.envSource.Origin = widget
c.envSource.Child = nil
for w := widget; w != nil; w = w.widgetState().parent {
if v, ok := w.Env(c, key, &c.envSource); ok {
return v, true
}
c.envSource.Child = w
}
return nil, false
}
// Passthrough reports whether the widget is in passthrough mode.
// A passthrough widget does not receive any input events, but its descendants do.
func (c *Context) Passthrough(widget Widget) bool {
return widget.widgetState().isPassthrough()
}
// SetPassthrough sets whether the widget is in passthrough mode.
// A passthrough widget does not receive any input events, but its descendants do.
func (c *Context) SetPassthrough(widget Widget, passthrough bool) {
widgetState := widget.widgetState()
if widgetState.passthrough == passthrough {
return
}
widgetState.passthrough = passthrough
_ = traverseWidget(widget, func(w Widget) error {
w.widgetState().passthroughCacheValid = false
w.widgetState().passthroughCache = false
return nil
})
}
func (c *Context) bringToFrontLayer(widget Widget) {
widgetState := widget.widgetState()
// If the widget is already in the front layer, do nothing.
if widgetState.layer != 0 && widgetState.layer == c.frontLayer {
return
}
// Increment the front layer so that the next layer is always on top.
c.frontLayer++
widgetState.layer = c.frontLayer
_ = traverseWidget(widget, func(w Widget) error {
w.widgetState().actualLayerPlus1Cache = 0
return nil
})
}
func (c *Context) visibleBounds(state *widgetState) image.Rectangle {
if state.hasVisibleBoundsCache {
return state.visibleBoundsCache
}
b := state.bounds
l := state.actualLayer()
for parent := state.parent; parent != nil; parent = parent.widgetState().parent {
if parent.widgetState().actualLayer() != l {
state.hasVisibleBoundsCache = true
state.visibleBoundsCache = b
return b
}
if parent.widgetState().clipChildren {
b = b.Intersect(c.visibleBounds(parent.widgetState()))
break
}
}
state.hasVisibleBoundsCache = true
state.visibleBoundsCache = b
return b
}
// SetClipChildren sets whether the children on the same layer are clipped by the widget's bounds.
// The default value is false.
//
// If the child widget is on a different layer from the parent, it is not clipped.
// Note that a widget layer can be controlled by [LayerWidget].
func (c *Context) SetClipChildren(widget Widget, clip bool) {
widget.widgetState().clipChildren = clip
}
// SetWindowTitle sets the window title.
func (c *Context) SetWindowTitle(title string) {
ebiten.SetWindowTitle(title)
}
// SetWindowSize sets the window size.
func (c *Context) SetWindowSize(width, height int) {
ebiten.SetWindowSize(width, height)
}
// SetWindowSizeLimits sets the size limits of the window.
// A negative value indicates the size is not limited.
func (c *Context) SetWindowSizeLimits(minw, minh, maxw, maxh int) {
ebiten.SetWindowSizeLimits(minw, minh, maxw, maxh)
}
func (c *Context) isDefaultTickMethodCalled() bool {
return c.defaultTickMethodCalled
}
func (c *Context) resetDefaultTickMethodCalled() {
c.defaultTickMethodCalled = false
}
func (c *Context) setDefaultTickMethodCalledFlag() {
c.defaultTickMethodCalled = true
}
// DelegateFocus delegates the focus to another widget.
func (c *Context) DelegateFocus(widget Widget, delegate Widget) {
widget.widgetState().focusDelegate = delegate
}