-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.go
142 lines (130 loc) · 3.79 KB
/
settings.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
package main
import (
"time"
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/notify"
)
// SettingsPage is for user settings
type SettingsPage struct {
a *App
back *widget.Clickable
submit *widget.Clickable
switchUseTor *widget.Bool
switchAutoConnect *widget.Bool
}
var (
inset = layout.UniformInset(unit.Dp(8))
)
const (
settingNameColumnWidth = .3
settingDetailsColumnWidth = 1 - settingNameColumnWidth
)
// Layout returns a simple centered layout prompting to update settings
func (p *SettingsPage) Layout(gtx layout.Context) layout.Dimensions {
bg := Background{
Color: th.Bg,
Inset: layout.Inset{},
}
return bg.Layout(gtx, func(gtx C) D {
return layout.Flex{Axis: layout.Vertical, Alignment: layout.End}.Layout(gtx,
// topbar
layout.Rigid(func(gtx C) D {
return layout.Flex{Axis: layout.Horizontal, Spacing: layout.SpaceBetween, Alignment: layout.Baseline}.Layout(gtx,
layout.Rigid(button(th, p.back, backIcon).Layout),
layout.Flexed(1, fill{th.Bg}.Layout),
layout.Rigid(material.H6(th, "Settings").Layout),
layout.Flexed(1, fill{th.Bg}.Layout))
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Flexed(settingNameColumnWidth, func(gtx C) D {
return inset.Layout(gtx, material.Body1(th, "Use Tor").Layout)
}),
layout.Flexed(settingDetailsColumnWidth, func(gtx C) D {
return inset.Layout(gtx, material.Switch(th, p.switchUseTor, "Use Tor").Layout)
}),
)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Alignment: layout.Middle}.Layout(gtx,
layout.Flexed(settingNameColumnWidth, func(gtx C) D {
return inset.Layout(gtx, material.Body1(th, "Connect Automatically").Layout)
}),
layout.Flexed(settingDetailsColumnWidth, func(gtx C) D {
return inset.Layout(gtx, material.Switch(th, p.switchAutoConnect, "Connect Automatically").Layout)
}),
)
}),
layout.Rigid(func(gtx C) D {
return material.Button(th, p.submit, "Apply Settings").Layout(gtx)
}),
)
})
}
type restartClient struct{}
// Event catches the widget submit events and calls Settings
func (p *SettingsPage) Event(gtx layout.Context) interface{} {
if p.back.Clicked(gtx) {
return BackEvent{}
}
if p.switchUseTor.Update(gtx) {
if p.switchUseTor.Value && !hasTor() {
p.switchUseTor.Value = false
p.a.c.DeleteBlob("UseTor")
warnNoTor()
return nil
}
if p.switchUseTor.Value {
p.a.c.AddBlob("UseTor", []byte{1})
} else {
p.a.c.DeleteBlob("UseTor")
}
}
if p.switchAutoConnect.Update(gtx) {
if p.switchAutoConnect.Value {
p.a.c.AddBlob("AutoConnect", []byte{1})
} else {
p.a.c.DeleteBlob("AutoConnect")
}
}
if p.submit.Clicked(gtx) {
go func() {
if n, err := notify.Push("Restarting", "Katzen is restarting"); err == nil {
<-time.After(notificationTimeout)
n.Cancel()
}
}()
p.a.c.Shutdown()
return restartClient{}
}
return nil
}
func (p *SettingsPage) Start(stop <-chan struct{}) {
}
func newSettingsPage(a *App) *SettingsPage {
p := &SettingsPage{a: a}
p.back = &widget.Clickable{}
p.submit = &widget.Clickable{}
if _, err := a.c.GetBlob("UseTor"); err == nil {
p.switchUseTor = &widget.Bool{Value: true}
} else {
p.switchUseTor = &widget.Bool{Value: false}
}
if _, err := a.c.GetBlob("AutoConnect"); err == nil {
p.switchAutoConnect = &widget.Bool{Value: true}
} else {
p.switchAutoConnect = &widget.Bool{Value: false}
}
return p
}
func warnNoTor() {
go func() {
if n, err := notify.Push("Failure", "Tor requested, but not available on port 9050. Disable in settings to connect."); err == nil {
<-time.After(notificationTimeout)
n.Cancel()
}
}()
}