/
main.go
236 lines (225 loc) · 6.17 KB
/
main.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
package main
import (
"embed"
"os"
"strings"
"time"
"gioui.org/app"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/unit"
"gioui.org/widget"
"gioui.org/widget/material"
"gioui.org/x/component"
"github.com/hauntedness/giom/assets/fonts"
"github.com/hauntedness/giom/internal/log"
"github.com/hauntedness/giom/play/common"
"github.com/hauntedness/giom/play/navigate/page"
"github.com/hauntedness/giom/ui/icon"
)
// App Bar looks not so powerfull at present
type App struct {
theme *material.Theme
accountClickable widget.Clickable
restaurantClickable widget.Clickable
settingsClickable widget.Clickable
heartClickable widget.Clickable
h1Anim component.VisibilityAnimation
h1Text string
h2Text string
body1Text string
body1State widget.Selectable
modalState component.ModalState
}
var _ embed.FS
//go:embed page/testdata/逍遥小贵婿.txt
var data string
var p page.Page
func init() {
p = page.From(data, 100, 10)
total := p.Total
go func() {
for t := range time.Tick(time.Second * 10) {
unixSecond := t.Unix()
offset := int(unixSecond) % total
p = page.From(data, offset, 10)
log.Infos("offset", offset)
}
}()
}
func (app *App) Layout(gtx layout.Context) layout.Dimensions {
for _, e := range gtx.Events(app) {
log.Infos("enter app event", e)
}
return layout.Flex{Axis: layout.Vertical}.Layout(
gtx,
// header
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
nd := component.NewNav(app.h1Text, app.h2Text)
return nd.Layout(gtx, app.theme, &app.h1Anim)
}),
// todo
layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(gtx, _kline.Layout)
}),
// body
layout.Flexed(8, func(gtx layout.Context) layout.Dimensions {
txt := strings.Join(p.Lines, "\n")
body1 := material.Body1(app.theme, txt)
body1.State = &app.body1State
if body1.State.SelectionLen() > 0 {
if body1.State.Focused() {
log.Infos("select", body1.State.SelectedText())
ms := component.Modal(app.theme, &app.modalState)
if ms.Clicked() {
log.Info("modal clicked")
}
ms.Appear(time.Now())
ms.Layout(gtx)
}
}
return body1.Layout(gtx)
}),
// 底栏上面的分割线
layout.Rigid(
func(gtx layout.Context) layout.Dimensions {
div := common.Div{
NoDirection: true,
Border: widget.Border{Color: common.ColorGreen, Width: unit.Dp(0)},
Inset: layout.Inset{Top: unit.Dp(10), Bottom: unit.Dp(10), Left: unit.Dp(1), Right: unit.Dp(1)},
}
dim := div.Layout(gtx, func(gtx layout.Context) layout.Dimensions {
divider := component.Divider(app.theme)
divider.Fill = common.ColorRed
divider.Thickness = unit.Dp(1)
return divider.Layout(gtx)
})
return dim
},
),
// 底部导航
layout.Rigid(
func(gtx layout.Context) layout.Dimensions {
// 添加border 方便调试
var dim layout.Dimensions = common.Div{
Inset: layout.UniformInset(unit.Dp(8)),
Border: widget.Border{
Color: common.ColorBlue,
CornerRadius: 0,
Width: 1,
},
Direction: layout.Center,
}.Layout(
gtx,
func(gtx layout.Context) layout.Dimensions {
return layout.Flex{
Axis: layout.Horizontal,
Spacing: layout.SpaceAround,
Alignment: layout.Middle,
WeightSum: 0,
}.Layout(
gtx,
layout.Flexed(
1,
func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(
gtx,
func(gtx layout.Context) layout.Dimensions {
return material.IconButton(
app.theme, &app.accountClickable, icon.AccountBalanceIcon, "account balance",
).Layout(gtx)
},
)
},
),
layout.Flexed(
1,
func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(
gtx,
func(gtx layout.Context) layout.Dimensions {
return material.IconButton(
app.theme, &app.restaurantClickable, icon.RestaurantMenuIcon, "restaurant",
).Layout(gtx)
},
)
},
),
layout.Flexed(
1,
func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
return material.IconButton(
app.theme, &app.settingsClickable, icon.SettingsIcon, "settings",
).Layout(gtx)
},
)
},
),
layout.Flexed(
1,
func(gtx layout.Context) layout.Dimensions {
return layout.Center.Layout(gtx,
func(gtx layout.Context) layout.Dimensions {
return material.IconButton(
app.theme, &app.heartClickable, icon.HeartIcon, "heart",
).Layout(gtx)
},
)
},
),
)
})
return dim
},
),
)
}
func main() {
th := fonts.NewTheme()
giomApp := &App{
theme: th,
h1Text: time.Now().Format(time.DateTime),
body1Text: "",
}
go func() {
window := app.NewWindow(app.Title("giom"))
if err := giomApp.Loop(window); err != nil {
panic(err)
}
os.Exit(0)
}()
app.Main()
}
func (app *App) Loop(w *app.Window) error {
go func() {
for range time.NewTicker(time.Second * 3).C {
app.h1Text = Fetch(time.Now())
app.body1Text = Fetch(time.Now()) + time.Now().Format(time.RFC3339)
// fees, err := starknet.EstimateFees()
// if err != nil {
// app.body1Text = err.Error()
// } else {
// app.body1Text = fmt.Sprintf("%v", fees)
// }
w.Invalidate()
}
}()
var ops op.Ops
for e := range w.Events() {
switch e := e.(type) {
case system.DestroyEvent:
return e.Err
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
app.Layout(gtx)
e.Frame(gtx.Ops)
}
}
return nil
}
func Fetch(t time.Time) string {
return "一些中文 和 some english"
}