2019-10-31 11:08:01 +01:00
|
|
|
module main
|
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
import (
|
|
|
|
ui
|
|
|
|
gx
|
2019-11-01 17:02:38 +01:00
|
|
|
os
|
|
|
|
darwin
|
2019-09-23 19:34:08 +02:00
|
|
|
)
|
2019-08-19 23:19:55 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
NR_COLS = 3
|
|
|
|
CELL_HEIGHT = 25
|
|
|
|
CELL_WIDTH = 100
|
|
|
|
TABLE_WIDTH = CELL_WIDTH * NR_COLS
|
|
|
|
)
|
|
|
|
|
|
|
|
struct User {
|
|
|
|
first_name string
|
|
|
|
last_name string
|
|
|
|
age int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Context {
|
2019-09-21 20:38:12 +02:00
|
|
|
mut:
|
2019-08-19 23:19:55 +02:00
|
|
|
first_name ui.TextBox
|
|
|
|
last_name ui.TextBox
|
|
|
|
age ui.TextBox
|
|
|
|
users []User
|
2019-08-20 13:34:29 +02:00
|
|
|
window &ui.Window
|
2019-08-19 23:19:55 +02:00
|
|
|
txt_pos int
|
|
|
|
}
|
|
|
|
|
2019-09-21 20:38:12 +02:00
|
|
|
/*
|
2019-09-23 19:34:08 +02:00
|
|
|
// Current V
|
2019-09-21 20:38:12 +02:00
|
|
|
struct Window {
|
|
|
|
width: 500
|
|
|
|
height: 300
|
|
|
|
title: 'Users'
|
2019-09-23 19:34:08 +02:00
|
|
|
body: Layout {
|
|
|
|
body: [
|
2019-09-21 20:38:12 +02:00
|
|
|
TextBox {
|
|
|
|
placeholder: 'First name'
|
|
|
|
},
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Last name'
|
|
|
|
},
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Age'
|
|
|
|
},
|
|
|
|
Button {
|
|
|
|
title: 'Add user'
|
|
|
|
onclick: btn_click
|
|
|
|
},
|
|
|
|
]
|
2019-12-12 21:42:51 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 19:34:08 +02:00
|
|
|
|
|
|
|
// Improved V
|
|
|
|
struct Window {
|
|
|
|
width: 500
|
|
|
|
height: 300
|
|
|
|
title: 'Users'
|
2019-12-12 21:42:51 +01:00
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
Layout {
|
|
|
|
[
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'First name'
|
|
|
|
}
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Last name'
|
|
|
|
}
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Age'
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
title: 'Add user'
|
|
|
|
onclick: btn_click
|
|
|
|
}
|
|
|
|
]
|
2019-12-12 21:42:51 +01:00
|
|
|
}
|
|
|
|
}
|
2019-09-21 20:38:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Window {
|
|
|
|
width: 500
|
|
|
|
height: 300
|
|
|
|
title: 'Users'
|
|
|
|
VLayout {
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'First name'
|
|
|
|
}
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Last name'
|
|
|
|
}
|
|
|
|
TextBox {
|
|
|
|
placeholder: 'Age'
|
|
|
|
}
|
|
|
|
Button {
|
|
|
|
title: 'Add user'
|
|
|
|
onclick: btn_click
|
|
|
|
}
|
2019-12-12 21:42:51 +01:00
|
|
|
}
|
|
|
|
|
2019-09-21 20:38:12 +02:00
|
|
|
draw: draw_fn
|
2019-12-12 21:42:51 +01:00
|
|
|
}
|
2019-09-21 20:38:12 +02:00
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
2019-08-19 23:19:55 +02:00
|
|
|
fn main() {
|
|
|
|
mut ctx := &Context {
|
|
|
|
txt_pos: 10
|
|
|
|
}
|
2019-12-12 21:42:51 +01:00
|
|
|
ctx.users << User {
|
|
|
|
first_name: 'Sam'
|
|
|
|
last_name: 'Johnson'
|
|
|
|
age: 29
|
|
|
|
}
|
2019-08-19 23:19:55 +02:00
|
|
|
ctx.window = ui.new_window(ui.WinCfg {
|
|
|
|
width: 500
|
|
|
|
height: 300
|
|
|
|
title: 'Users'
|
|
|
|
draw_fn: draw
|
|
|
|
ptr: ctx
|
|
|
|
})
|
|
|
|
ctx.first_name = ctx.add_textbox('First name')
|
|
|
|
ctx.last_name = ctx.add_textbox('Last name')
|
|
|
|
ctx.age = ctx.add_textbox('Age')
|
|
|
|
mut btn := ui.new_button('Add user', ctx.window, btn_click)
|
2019-12-12 21:42:51 +01:00
|
|
|
btn.onclick(btn_click)
|
2019-08-19 23:19:55 +02:00
|
|
|
btn.widget.set_pos(TABLE_WIDTH + 50, ctx.txt_pos)
|
2019-12-12 21:42:51 +01:00
|
|
|
|
2019-08-19 23:19:55 +02:00
|
|
|
for {
|
|
|
|
ui.wait_events()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO replace with `fn (ctx mut Context) btn_click() {`
|
2019-09-21 20:38:12 +02:00
|
|
|
fn btn_click(_ctx &Context) {
|
2019-09-23 19:34:08 +02:00
|
|
|
println('users.v: button click')
|
2019-09-21 20:38:12 +02:00
|
|
|
mut ctx := _ctx// TODO hack
|
2019-08-19 23:19:55 +02:00
|
|
|
ctx.users << User {
|
|
|
|
first_name: ctx.first_name.text()
|
|
|
|
last_name: ctx.last_name.text()
|
2019-09-21 20:38:12 +02:00
|
|
|
age: ctx.age.text().int()
|
2019-08-19 23:19:55 +02:00
|
|
|
}
|
|
|
|
ctx.window.refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO replace with `fn (ctx mut Context) draw() {`
|
2019-09-21 20:38:12 +02:00
|
|
|
fn draw(ctx &Context) {
|
2019-08-19 23:19:55 +02:00
|
|
|
for i, user in ctx.users {
|
|
|
|
x := 10
|
|
|
|
y := 10 + i * CELL_HEIGHT
|
|
|
|
// Outer border
|
2019-09-21 20:38:12 +02:00
|
|
|
ui.draw_empty_rect(x, y, TABLE_WIDTH, CELL_HEIGHT, gx.Gray)
|
2019-08-19 23:19:55 +02:00
|
|
|
// Vertical separators
|
2019-09-21 20:38:12 +02:00
|
|
|
ui.draw_line(x + CELL_WIDTH, y, x + CELL_WIDTH, y + CELL_HEIGHT, gx.Gray)
|
|
|
|
ui.draw_line(x + CELL_WIDTH * 2, y, x + CELL_WIDTH * 2, y + CELL_HEIGHT, gx.Gray)
|
2019-08-19 23:19:55 +02:00
|
|
|
// Text values
|
2019-09-21 20:38:12 +02:00
|
|
|
ui.draw_text_def(x + 5, y + 5, user.first_name)
|
|
|
|
ui.draw_text_def(x + 5 + CELL_WIDTH, y + 5, user.last_name)
|
|
|
|
ui.draw_text_def(x + 5 + CELL_WIDTH * 2, y + 5, user.age.str())
|
2019-08-19 23:19:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (ctx mut Context) add_textbox(placeholder string) ui.TextBox {
|
2019-12-12 21:42:51 +01:00
|
|
|
|
2019-08-19 23:19:55 +02:00
|
|
|
mut txt_box := ui.new_textbox(ctx.window, false)
|
|
|
|
txt_box.set_placeholder(placeholder)
|
|
|
|
txt_box.widget.set_pos(TABLE_WIDTH + 50, ctx.txt_pos)
|
|
|
|
ctx.txt_pos += 30
|
|
|
|
return txt_box
|
|
|
|
}
|
|
|
|
|