-
Notifications
You must be signed in to change notification settings - Fork 9
/
header.go
141 lines (121 loc) · 4.38 KB
/
header.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
package app
import (
"gioui.org/layout"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"github.com/mirzakhany/chapar/internal/domain"
"github.com/mirzakhany/chapar/internal/state"
"github.com/mirzakhany/chapar/ui/chapartheme"
"github.com/mirzakhany/chapar/ui/widgets"
)
type Header struct {
materialTheme *material.Theme
selectedEnv string
envDropDown *widgets.DropDown
envState *state.Environments
switchState *widget.Bool
themeSwitcher material.SwitchStyle
iconDarkMode material.LabelStyle
iconLightMode material.LabelStyle
OnSelectedEnvChanged func(env *domain.Environment)
OnThemeSwitched func(isLight bool)
}
const (
none = "none"
noEnvironment = "No Environment"
)
func NewHeader(envState *state.Environments, theme *chapartheme.Theme) *Header {
h := &Header{
materialTheme: theme.Material(),
selectedEnv: noEnvironment,
envState: envState,
switchState: new(widget.Bool),
}
h.iconDarkMode = widgets.MaterialIcons("dark_mode", theme)
h.iconLightMode = widgets.MaterialIcons("light_mode", theme)
h.themeSwitcher = material.Switch(theme.Material(), h.switchState, "")
h.envDropDown = widgets.NewDropDown(theme)
h.envDropDown.MinWidth = unit.Dp(150)
return h
}
func (h *Header) LoadEnvs(data []*domain.Environment) {
options := make([]*widgets.DropDownOption, 0)
options = append(options, widgets.NewDropDownOption(noEnvironment).WithIdentifier(none))
options = append(options, widgets.NewDropDownDivider())
selectEnvExist := false
for _, env := range data {
if h.selectedEnv == env.MetaData.ID {
selectEnvExist = true
}
options = append(options, widgets.NewDropDownOption(env.MetaData.Name).WithIdentifier(env.MetaData.ID))
}
h.envDropDown.SetOptions(options...)
if selectEnvExist {
h.SetSelectedEnvironment(h.envState.GetEnvironment(h.selectedEnv))
}
}
func (h *Header) SetSelectedEnvironment(env *domain.Environment) {
h.selectedEnv = env.MetaData.ID
h.envDropDown.SetSelectedByTitle(env.MetaData.Name)
}
func (h *Header) SetTheme(isDark bool) {
h.switchState.Value = !isDark
}
func (h *Header) Layout(gtx layout.Context, theme *chapartheme.Theme) layout.Dimensions {
inset := layout.Inset{Top: unit.Dp(4), Bottom: unit.Dp(4), Left: unit.Dp(4)}
if h.envDropDown.GetSelected().Identifier != h.selectedEnv {
h.selectedEnv = h.envDropDown.GetSelected().Identifier
if h.selectedEnv != none {
id := h.envDropDown.GetSelected().Identifier
h.envState.SetActiveEnvironment(h.envState.GetEnvironment(id))
if h.OnSelectedEnvChanged != nil {
h.OnSelectedEnvChanged(h.envState.GetEnvironment(id))
}
} else {
h.envState.ClearActiveEnvironment()
}
}
if h.switchState.Update(gtx) {
if h.OnThemeSwitched != nil {
go h.OnThemeSwitched(!h.switchState.Value)
}
}
content := layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return inset.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle, Spacing: layout.SpaceBetween}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: unit.Dp(10)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return material.H6(h.materialTheme, "Chapar").Layout(gtx)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Flex{Axis: layout.Horizontal, Alignment: layout.Middle}.Layout(gtx,
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
h.iconDarkMode.Color = theme.TextColor
return h.iconDarkMode.Layout(gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Right: unit.Dp(10), Left: unit.Dp(10)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return h.themeSwitcher.Layout(gtx)
})
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
h.iconLightMode.Color = theme.TextColor
return h.iconLightMode.Layout(gtx)
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
return layout.Inset{Left: unit.Dp(20), Right: unit.Dp(10)}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
return h.envDropDown.Layout(gtx, theme)
})
}),
)
}),
)
})
})
return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
content,
widgets.DrawLineFlex(theme.SeparatorColor, unit.Dp(1), unit.Dp(gtx.Constraints.Max.X)),
)
}