This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.v
94 lines (82 loc) · 1.51 KB
/
main.v
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
module main
import ui
[heap]
struct App {
mut:
window ui.Window
label ui.Label
model Model
}
struct Model {
counter_value int
}
enum Message {
increment
decrement
}
fn main() {
mut app := App{}
setup_app(mut app)
ui.run(app.window)
}
fn setup_app(mut app App) {
app.window = ui.window(
width: 400
height: 400
title: 'MVU counter'
children: [
ui.column(
margin: ui.Margin{50, 50, 50, 50}
spacing: 5
children: [
app.label,
ui.row(
spacing: 5
children: [
ui.button(
text: 'Increment'
width: 100
height: 50
text_size: 20
on_click: app.send_increment_signal
),
ui.button(
text: 'Decrement'
width: 100
height: 50
text_size: 20
on_click: app.send_decrement_signal
),
]
),
]
),
]
)
app.label = ui.label(text: '0', text_size: 36)
}
fn (mut app App) send_increment_signal(_ &ui.Button) {
app.model = update(Message.increment, app.model)
app.react_on_model_change()
}
fn (mut app App) send_decrement_signal(_ &ui.Button) {
app.model = update(Message.decrement, app.model)
app.react_on_model_change()
}
fn update(message Message, current_model Model) Model {
return match message {
.increment {
Model{
counter_value: current_model.counter_value + 1
}
}
.decrement {
Model{
counter_value: current_model.counter_value - 1
}
}
}
}
fn (mut app App) react_on_model_change() {
app.label.text = app.model.counter_value.str()
}