-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsettings-view.go
349 lines (330 loc) · 10.2 KB
/
settings-view.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
package main
import (
"log"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/component"
materials "gioui.org/x/component"
"git.sr.ht/~whereswaldon/sprig/core"
"git.sr.ht/~whereswaldon/sprig/icons"
sprigWidget "git.sr.ht/~whereswaldon/sprig/widget"
sprigTheme "git.sr.ht/~whereswaldon/sprig/widget/theme"
)
type SettingsView struct {
manager ViewManager
core.App
widget.List
ConnectionForm sprigWidget.TextForm
IdentityButton widget.Clickable
CommunityList layout.List
CommunityBoxes []widget.Bool
ProfilingSwitch widget.Bool
ThemeingSwitch widget.Bool
NotificationsSwitch widget.Bool
TestNotificationsButton widget.Clickable
TestResults string
BottomBarSwitch widget.Bool
DockNavSwitch widget.Bool
DarkModeSwitch widget.Bool
UseOrchardStoreSwitch widget.Bool
}
type Section struct {
*material.Theme
Heading string
Items []layout.Widget
}
var sectionItemInset = layout.UniformInset(unit.Dp(8))
var itemInset = layout.Inset{
Left: unit.Dp(8),
Right: unit.Dp(8),
Top: unit.Dp(2),
Bottom: unit.Dp(2),
}
func (s Section) Layout(gtx C) D {
items := make([]layout.FlexChild, len(s.Items)+1)
items[0] = layout.Rigid(component.SubheadingDivider(s.Theme, s.Heading).Layout)
for i := range s.Items {
items[i+1] = layout.Rigid(s.Items[i])
}
return layout.Flex{Axis: layout.Vertical}.Layout(gtx, items...)
}
type SimpleSectionItem struct {
*material.Theme
Control layout.Widget
Context string
}
func (s SimpleSectionItem) Layout(gtx C) D {
return layout.Inset{
Top: unit.Dp(4),
Bottom: unit.Dp(4),
}.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return s.Control(gtx)
}),
layout.Rigid(func(gtx C) D {
if s.Context == "" {
return D{}
}
return itemInset.Layout(gtx, material.Body2(s.Theme, s.Context).Layout)
}),
)
})
}
var _ View = &SettingsView{}
func NewCommunityMenuView(app core.App) View {
c := &SettingsView{
App: app,
}
c.List.Axis = layout.Vertical
c.ConnectionForm.TextField.SetText(c.Settings().Address())
c.ConnectionForm.TextField.SingleLine = true
c.ConnectionForm.TextField.Submit = true
return c
}
func (c *SettingsView) HandleIntent(intent Intent) {}
func (c *SettingsView) AppBarData() (bool, string, []materials.AppBarAction, []materials.OverflowAction) {
return true, "Settings", []materials.AppBarAction{}, []materials.OverflowAction{}
}
func (c *SettingsView) NavItem() *materials.NavItem {
return &materials.NavItem{
Name: "Settings",
Icon: icons.SettingsIcon,
}
}
func (c *SettingsView) Update(gtx layout.Context) {
settingsChanged := false
for i := range c.CommunityBoxes {
box := &c.CommunityBoxes[i]
if box.Update(gtx) {
log.Println("updated")
}
}
if c.IdentityButton.Clicked(gtx) {
c.manager.RequestViewSwitch(IdentityFormID)
}
if c.ProfilingSwitch.Update(gtx) {
c.manager.SetProfiling(c.ProfilingSwitch.Value)
}
if c.ThemeingSwitch.Update(gtx) {
c.manager.SetThemeing(c.ThemeingSwitch.Value)
}
if c.ConnectionForm.Submitted() {
c.Settings().SetAddress(c.ConnectionForm.TextField.Text())
settingsChanged = true
c.Sprout().ConnectTo(c.Settings().Address())
}
if c.NotificationsSwitch.Update(gtx) {
c.Settings().SetNotificationsGloballyAllowed(c.NotificationsSwitch.Value)
settingsChanged = true
}
if c.TestNotificationsButton.Clicked(gtx) {
err := c.Notifications().Notify("Testing!", "This is a test notification from sprig.")
if err == nil {
c.TestResults = "Sent without errors"
} else {
c.TestResults = "Failed: " + err.Error()
}
}
if c.BottomBarSwitch.Update(gtx) {
c.Settings().SetBottomAppBar(c.BottomBarSwitch.Value)
settingsChanged = true
}
if c.DockNavSwitch.Update(gtx) {
c.Settings().SetDockNavDrawer(c.DockNavSwitch.Value)
settingsChanged = true
}
if c.DarkModeSwitch.Update(gtx) {
c.Settings().SetDarkMode(c.DarkModeSwitch.Value)
settingsChanged = true
}
if c.UseOrchardStoreSwitch.Update(gtx) {
c.Settings().SetUseOrchardStore(c.UseOrchardStoreSwitch.Value)
settingsChanged = true
}
if settingsChanged {
c.manager.ApplySettings(c.Settings())
go c.Settings().Persist()
}
}
func (c *SettingsView) BecomeVisible() {
c.ConnectionForm.TextField.SetText(c.Settings().Address())
c.NotificationsSwitch.Value = c.Settings().NotificationsGloballyAllowed()
c.BottomBarSwitch.Value = c.Settings().BottomAppBar()
c.DockNavSwitch.Value = c.Settings().DockNavDrawer()
c.DarkModeSwitch.Value = c.Settings().DarkMode()
c.UseOrchardStoreSwitch.Value = c.Settings().UseOrchardStore()
}
func (c *SettingsView) Layout(gtx layout.Context) layout.Dimensions {
sTheme := c.Theme().Current()
theme := sTheme.Theme
sections := []Section{
{
Heading: "Identity",
Items: []layout.Widget{
func(gtx C) D {
if c.Settings().ActiveArborIdentityID() != nil {
id, _ := c.Settings().Identity()
return itemInset.Layout(gtx, sprigTheme.AuthorName(sTheme, string(id.Name.Blob), id.ID(), true).Layout)
}
return itemInset.Layout(gtx, material.Button(theme, &c.IdentityButton, "Create new Identity").Layout)
},
},
},
{
Heading: "Connection",
Items: []layout.Widget{
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return itemInset.Layout(gtx, func(gtx C) D {
form := sprigTheme.TextForm(sTheme, &c.ConnectionForm, "Connect", "HOST:PORT")
return form.Layout(gtx)
})
},
Context: "You can restart your connection to a relay by hitting the Connect button above without changing the address.",
}.Layout,
},
},
{
Heading: "Notifications",
Items: []layout.Widget{
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.NotificationsSwitch, "Enable Notifications").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Enable notifications").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Button(theme, &c.TestNotificationsButton, "Test").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body2(theme, c.TestResults).Layout)
}),
)
},
Context: "Currently supported on Android and Linux/BSD. macOS support coming soon.",
}.Layout,
},
},
{
Heading: "Store",
Items: []layout.Widget{
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.UseOrchardStoreSwitch, "Use Orchard Store").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Use Orchard store").Layout)
}),
)
},
Context: "Orchard is a single-file read-oriented database for storing nodes.",
}.Layout,
},
},
{
Heading: "User Interface",
Items: []layout.Widget{
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.BottomBarSwitch, "Use Bottom App Bar").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Use bottom app bar").Layout)
}),
)
},
Context: "Only recommended on mobile devices.",
}.Layout,
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.DockNavSwitch, "Dock navigation").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Dock navigation to the left edge of the UI").Layout)
}),
)
},
Context: "Only recommended on desktop devices.",
}.Layout,
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.DarkModeSwitch, "Dark Mode").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Dark Mode").Layout)
}),
)
},
}.Layout,
},
},
{
Heading: "Developer",
Items: []layout.Widget{
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.ProfilingSwitch, "Enable Profiling").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Display graphics profiling").Layout)
}),
)
},
}.Layout,
SimpleSectionItem{
Theme: theme,
Control: func(gtx C) D {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Switch(theme, &c.ThemeingSwitch, "Enable Theme Editor").Layout)
}),
layout.Rigid(func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, "Display theme editor").Layout)
}),
)
},
}.Layout,
func(gtx C) D {
return itemInset.Layout(gtx, material.Body1(theme, VersionString).Layout)
},
},
},
}
return material.List(theme, &c.List).Layout(gtx, len(sections), func(gtx C, index int) D {
return layout.UniformInset(unit.Dp(8)).Layout(gtx, func(gtx C) D {
return component.Surface(theme).Layout(gtx, func(gtx C) D {
gtx.Constraints.Min.X = gtx.Constraints.Max.X
return itemInset.Layout(gtx, func(gtx C) D {
sections[index].Theme = theme
return sections[index].Layout(gtx)
})
})
})
})
}
func (c *SettingsView) SetManager(mgr ViewManager) {
c.manager = mgr
}