examples/users_gui: V 0.0.8 fixes

pull/315/head
Alexander Medvednikov 2019-05-24 01:12:07 +02:00
parent 1e5da93ba8
commit a439faf605
1 changed files with 14 additions and 15 deletions

View File

@ -2,10 +2,10 @@ import ui
import gx import gx
const ( const (
NR_COLS = 3 NrCols = 3
CELL_HEIGHT = 25 CellHeight = 25
CELL_WIDTH = 100 CellWidth = 100
TABLE_WIDTH = CELL_WIDTH * NR_COLS TableWidth = CellWidth * NrCols
) )
struct User { struct User {
@ -38,15 +38,14 @@ fn main() {
ctx.last_name = ctx.add_textbox('Last name') ctx.last_name = ctx.add_textbox('Last name')
ctx.age = ctx.add_textbox('Age') ctx.age = ctx.add_textbox('Age')
mut btn := ui.new_button('Add user', ctx.window, btn_click) mut btn := ui.new_button('Add user', ctx.window, btn_click)
btn.widget.set_pos(TABLE_WIDTH + 50, ctx.txt_pos) btn.widget.set_pos(TableWidth + 50, ctx.txt_pos)
for { for {
ui.wait_events() ui.wait_events()
} }
} }
// TODO replace with `fn (ctx mut Context) btn_click() {` // TODO replace with `fn (ctx mut Context) btn_click() {`
fn btn_click(_ctx *Context) { fn btn_click(btn voidptr, ctx mut Context) {
mut ctx = _ctx// TODO hack
ctx.users << User { ctx.users << User {
first_name: ctx.first_name.text() first_name: ctx.first_name.text()
last_name: ctx.last_name.text() last_name: ctx.last_name.text()
@ -59,23 +58,23 @@ fn btn_click(_ctx *Context) {
fn draw(ctx *Context) { fn draw(ctx *Context) {
for i, user in ctx.users { for i, user in ctx.users {
x := 10 x := 10
y := 10 + i * CELL_HEIGHT y := 10 + i * CellHeight
// Outer border // Outer border
gx.draw_empty_rect(x, y, TABLE_WIDTH, CELL_HEIGHT, gx.GRAY) ui.draw_empty_rect(x, y, TableWidth, CellHeight, gx.GRAY)
// Vertical separators // Vertical separators
gx.draw_line(x + CELL_WIDTH, y, x + CELL_WIDTH, y + CELL_HEIGHT) ui.draw_line(x + CellWidth, y, x + CellWidth, y + CellHeight)
gx.draw_line(x + CELL_WIDTH * 2, y, x + CELL_WIDTH * 2, y + CELL_HEIGHT) ui.draw_line(x + CellWidth * 2, y, x + CellWidth * 2, y + CellHeight)
// Text values // Text values
gx.draw_text_def(x + 5, y + 5, user.first_name) ui.draw_text_def(x + 5, y + 5, user.first_name)
gx.draw_text_def(x + 5 + CELL_WIDTH, y + 5, user.last_name) ui.draw_text_def(x + 5 + CellWidth, y + 5, user.last_name)
gx.draw_text_def(x + 5 + CELL_WIDTH * 2, y + 5, user.age.str()) ui.draw_text_def(x + 5 + CellWidth * 2, y + 5, user.age.str())
} }
} }
fn (ctx mut Context) add_textbox(placeholder string) ui.TextBox { fn (ctx mut Context) add_textbox(placeholder string) ui.TextBox {
mut txt_box := ui.new_textbox(ctx.window, false) mut txt_box := ui.new_textbox(ctx.window, false)
txt_box.set_placeholder(placeholder) txt_box.set_placeholder(placeholder)
txt_box.widget.set_pos(TABLE_WIDTH + 50, ctx.txt_pos) txt_box.widget.set_pos(TableWidth + 50, ctx.txt_pos)
ctx.txt_pos += 30 ctx.txt_pos += 30
return txt_box return txt_box
} }