-
Notifications
You must be signed in to change notification settings - Fork 151
/
users.v
239 lines (231 loc) · 5.23 KB
/
users.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
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
import ui
import gg
import gx
import os
const win_width = 780
const win_height = 385
const nr_cols = 4
const cell_height = 25
const cell_width = 100
const table_width = cell_width * nr_cols
struct User {
first_name string
last_name string
age int
country string
}
@[heap]
struct State {
mut:
first_name string
last_name string
age string
password string
pbar &ui.ProgressBar
users []User
window &ui.Window = unsafe { nil }
label &ui.Label
country &ui.Radio
txt_pos int
started bool
is_error bool
}
fn main() {
mut logo := os.resource_abs_path(os.join_path('../assets/img', 'logo.png'))
$if android {
logo = 'img/logo.png'
}
mut app := &State{
users: [
User{
first_name: 'Sam'
last_name: 'Johnson'
age: 29
country: 'United States'
},
User{
first_name: 'Kate'
last_name: 'Williams'
age: 26
country: 'Canada'
},
]
country: ui.radio(
width: 200
values: ['United States', 'Canada', 'United Kingdom', 'Australia']
title: 'Country'
)
pbar: ui.progressbar(
width: 170
max: 10
val: 2
)
label: ui.label(text: '2/10')
}
window := ui.window(
width: win_width
height: win_height
title: 'V UI Demo'
// bg_color: gx.light_blue
children: [
ui.row(
margin: ui.Margin{10, 10, 10, 10}
widths: [200.0, ui.stretch]
spacing: 30
children: [
ui.column(
spacing: 13
children: [
ui.textbox(
max_len: 20
width: 200
placeholder: 'First name'
text: &app.first_name
// is_focused: &app.started
is_error: &app.is_error
is_focused: true
),
ui.textbox(
max_len: 50
width: 200
placeholder: 'Last name'
text: &app.last_name
is_error: &app.is_error
),
ui.textbox(
max_len: 3
width: 200
placeholder: 'Age'
is_numeric: true
text: &app.age
is_error: &app.is_error
),
ui.textbox(
width: 200
placeholder: 'Password'
is_password: true
max_len: 20
text: &app.password
),
ui.checkbox(
checked: true
text: 'Online registration'
),
ui.checkbox(text: 'Subscribe to the newsletter'),
app.country,
ui.row(
spacing: 65
widths: ui.compact
children: [
ui.button(
text: 'Add user'
on_click: app.btn_add_click
),
ui.button(
text: '?'
on_click: btn_help_click
),
]
),
ui.row(
spacing: 5
children: [
app.pbar,
app.label,
]
),
]
),
ui.column(
alignments: ui.HorizontalAlignments{
center: [
0,
]
right: [
1,
]
}
widths: [
ui.stretch,
ui.compact,
]
heights: [
ui.stretch,
100.0,
]
children: [
ui.canvas(
width: 400
height: 275
draw_fn: app.canvas_draw
),
ui.picture(
width: 100
height: 100
path: logo
),
]
),
]
),
// ui.menu(
// items: [ui.MenuItem{'Delete all users', menu_click},
// ui.MenuItem{'Export users', menu_click}, ui.MenuItem{'Exit', menu_click}]
// ),
]
)
app.window = window
ui.run(window)
}
// fn menu_click() {
// }
fn btn_help_click(b voidptr) {
ui.message_box('Built with V UI')
}
fn (mut app State) btn_add_click(b &ui.Button) {
// println('nr users=$app.users.len')
// ui.notify('user', 'done')
// app.window.set_cursor(.hand)
if app.users.len >= 10 {
return
}
if app.first_name == '' || app.last_name == '' || app.age == '' {
app.is_error = true
return
}
new_user := User{
first_name: app.first_name // first_name.text
last_name: app.last_name // .text
age: app.age.int()
country: app.country.selected_value()
}
app.users << new_user
app.pbar.val++
app.first_name = ''
// app.first_name.focus()
app.last_name = ''
app.age = ''
app.password = ''
app.label.set_text('${app.users.len}/10')
// ui.message_box('$new_user.first_name $new_user.last_name has been added')
}
fn (app &State) canvas_draw(gg_ &gg.Context, c &ui.Canvas) { // x_offset int, y_offset int) {
x_offset, y_offset := c.x, c.y
w, h := c.width, c.height
x := x_offset
gg_.draw_rect_filled(x - 20, 0, w + 120, h + 120, gx.white)
for i, user in app.users {
y := y_offset + 20 + i * cell_height
// Outer border
gg_.draw_rect_empty(x, y, table_width, cell_height, gx.gray)
// Vertical separators
gg_.draw_line(x + cell_width, y, x + cell_width, y + cell_height, gx.gray)
gg_.draw_line(x + cell_width * 2, y, x + cell_width * 2, y + cell_height, gx.gray)
gg_.draw_line(x + cell_width * 3, y, x + cell_width * 3, y + cell_height, gx.gray)
// Text values
gg_.draw_text_def(x + 5, y + 5, user.first_name)
gg_.draw_text_def(x + 5 + cell_width, y + 5, user.last_name)
gg_.draw_text_def(x + 5 + cell_width * 2, y + 5, user.age.str())
gg_.draw_text_def(x + 5 + cell_width * 3, y + 5, user.country)
}
}