-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.go
171 lines (144 loc) · 3.29 KB
/
context.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024 Hajime Hoshi
package guigui
import (
"fmt"
"log/slog"
"os"
"slices"
"strings"
"github.com/hajimehoshi/guigui/internal/locale"
"golang.org/x/text/language"
)
type ColorMode int
var defaultColorMode ColorMode = ColorModeLight
func init() {
// TODO: Consider the system color mode.
switch mode := os.Getenv("GUIGUI_COLOR_MODE"); mode {
case "light", "":
defaultColorMode = ColorModeLight
case "dark":
defaultColorMode = ColorModeDark
default:
slog.Warn(fmt.Sprintf("invalid GUIGUI_COLOR_MODE: %s", mode))
}
}
var envLocales []language.Tag
func init() {
if locales := os.Getenv("GUIGUI_LOCALES"); locales != "" {
for _, tag := range strings.Split(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
}
const (
ColorModeLight ColorMode = iota
ColorModeDark
)
type Context struct {
app *app
deviceScale float64
appScaleMinus1 float64
colorMode ColorMode
hasColorMode bool
locales []language.Tag
}
func (c *Context) Scale() float64 {
return c.deviceScale * c.AppScale()
}
func (c *Context) DeviceScale() float64 {
return c.deviceScale
}
func (c *Context) setDeviceScale(deviceScale float64) {
if c.deviceScale == deviceScale {
return
}
c.deviceScale = deviceScale
c.app.requestRedraw(c.app.bounds())
}
func (c *Context) AppScale() float64 {
return c.appScaleMinus1 + 1
}
func (c *Context) SetAppScale(scale float64) {
if c.appScaleMinus1 == scale-1 {
return
}
c.appScaleMinus1 = scale - 1
c.app.requestRedraw(c.app.bounds())
}
func (c *Context) ColorMode() ColorMode {
if c.hasColorMode {
return c.colorMode
}
return defaultColorMode
}
func (c *Context) SetColorMode(mode ColorMode) {
if c.hasColorMode && mode == c.colorMode {
return
}
c.colorMode = mode
c.hasColorMode = true
c.app.requestRedraw(c.app.bounds())
}
func (c *Context) ResetColorMode() {
c.hasColorMode = false
}
func (c *Context) AppendLocales(locales []language.Tag) []language.Tag {
origLen := len(locales)
// App locales
for _, l := range c.locales {
if slices.Contains(locales[origLen:], l) {
continue
}
locales = append(locales, l)
}
// Env locales
for _, l := range envLocales {
if slices.Contains(locales[origLen:], l) {
continue
}
locales = append(locales, l)
}
// System locales
for _, l := range systemLocales {
if slices.Contains(locales[origLen:], l) {
continue
}
locales = append(locales, l)
}
return locales
}
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
}
func (c *Context) SetAppLocales(locales []language.Tag) {
if slices.Equal(c.locales, locales) {
return
}
c.locales = append([]language.Tag(nil), locales...)
c.app.requestRedraw(c.app.bounds())
}
func (c *Context) AppSize() (int, int) {
return c.app.bounds().Dx(), c.app.bounds().Dy()
}