Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vlang/ui
Browse files Browse the repository at this point in the history
  • Loading branch information
rcqls committed Apr 10, 2023
2 parents b692dd3 + 32fdc35 commit c36668e
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/7guis/flightbooker.v
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ fn valid_date(date string) bool {
t := time.parse(ts) or { no_time }
// println("$t.day/$t.month/$t.year")
nd := time.days_in_month(t.month, t.year) or { -1 }
return (t != no_time) && (t.day <= nd)
return t != no_time && t.day <= nd
}
6 changes: 3 additions & 3 deletions examples/demo_calculate.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ fn main() {
}
layout: ui.box_layout(
children: {
'rect: (0, 25) -> (1,1)': ui.rectangle(
'rect: (0, 25) -> (1,1)': ui.rectangle(
color: gx.orange
)
'tb: (0,0) -> (1, 25)': ui.textbox(
'tb: (0,0) -> (100%, 25)': ui.textbox(
id: 'tb'
text_value: '((23.3 + 10) / 4) - 3'
on_enter: fn (mut tb ui.TextBox) {
mut res := ui.Widget(tb).get[ui.Label]('res')
res.set_text(tb.ui.window.calculate(tb.get_text()).str())
}
)
'res: (0, 30) -> (1,1)': ui.label(
'res: (0, 30) -> (1,1)': ui.label(
id: 'res'
justify: ui.center
text_size: 24
Expand Down
2 changes: 1 addition & 1 deletion examples/layout/box_layout.v
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
'id2: (30,30) -> (-30.5,-30.5)': ui.rectangle(
color: gx.rgb(100, 255, 100)
)
'id3: (0.5,0.5) -> (1,1)': ui.rectangle(
'id3: (50%,50%) -> (100%,100%)': ui.rectangle(
color: gx.rgb(100, 100, 255)
)
'id4: (-30.5, -30.5) ++ (30,30)': ui.rectangle(
Expand Down
10 changes: 5 additions & 5 deletions examples/layout/box_layout_inside_row.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ fn main() {
ui.box_layout(
id: 'bl'
children: {
'id1: (0,0) ++ (0.3,0.3)': ui.rectangle(
'id1: (0,0) ++ (30%,30%)': ui.rectangle(
color: gx.rgb(255, 100, 100)
)
'id2: (0.3,0.3) ++ (0.4,0.4)': ui.rectangle(
'id2: (0.3,0.3) ++ (40%,40%)': ui.rectangle(
color: gx.rgb(100, 255, 100)
)
'id3: (0.7,0.7) ++ (0.3,0.3)': make_tb(mut app, mut text, false)
'btn: (0.7,0.1) ++ (50,20)': ui.button(
'id3: (70%,70%) ++ (30%,30%)': make_tb(mut app, mut text, false)
'btn: (70%,10%) ++ (50,20)': ui.button(
text: 'switch'
on_click: app.btn_click
)
Expand All @@ -73,5 +73,5 @@ fn main() {

fn (mut app App) btn_click(_ &ui.Button) {
mut bl := app.window.get_or_panic[ui.BoxLayout]('bl')
bl.update_boundings('id3: (0.8,0.8) ++ (0.2,0.2)')
bl.update_boundings('id3: (80%,80%) ++ (20%,20%)')
}
4 changes: 2 additions & 2 deletions examples/layout/box_layout_with_textbox.v
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ fn main() {
'id2: (30,30) -> (-30.5,-30.5)': ui.rectangle(
color: gx.rgb(100, 255, 100)
)
'id3: (0.5,0.5) -> (1,1)': make_tb(mut app, with_row)
'id3: (50%,50%) -> (100%,100%)': make_tb(mut app, with_row)
'id4: (-30.5, -30.5) ++ (30,30)': ui.rectangle(
color: gx.white
)
'id5: (0.7,0.2) ++ (50,20)': app.make_btn()
'id5: (70%,20%) ++ (50,20)': app.make_btn()
}
)
))
Expand Down
4 changes: 2 additions & 2 deletions src/extra_text_syntax.v
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ fn (mut sh SyntaxHighLighter) parse_chunk_between_one_rune(typ string, sep rune)
}

fn (mut sh SyntaxHighLighter) parse_chunk_numeric() {
if !sh.is_alpha_underscore_before(sh.start) && ((sh.ustr[sh.i] in ui.numeric_set)
|| sh.ustr[sh.i] == `-`) {
if !sh.is_alpha_underscore_before(sh.start)
&& (sh.ustr[sh.i] in ui.numeric_set || sh.ustr[sh.i] == `-`) {
sh.i++
for {
if sh.i == sh.ustr.len {
Expand Down
24 changes: 13 additions & 11 deletions src/layout_box.v
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mut:
component voidptr
// debug stuff to be removed
debug_ids []string
mc MiniCalc = mini_calc()
}

[params]
Expand Down Expand Up @@ -206,7 +207,7 @@ pub fn (mut b BoxLayout) set_child_bounding(key string, mut child Widget) {
id, mut bounding := parse_boxlayout_child_key(key, b.id)
// Non-sense here no underscore when setting a child bounding box
// bounding = b.update_child_current_box_expression(id, bounding)
mode, bounding_vec, has_z_index, z_index, box_expr := parse_boxlayout_child_bounding(bounding)
mode, bounding_vec, has_z_index, z_index, box_expr := b.parse_boxlayout_child_bounding(bounding)
match mode {
'rect' { b.add_child_rect(id, child, bounding_vec) }
'lt2rb' { b.add_child_lt2rb(id, child, bounding_vec) }
Expand Down Expand Up @@ -269,7 +270,7 @@ fn (mut b BoxLayout) add_child_box_expr(id string, child Widget, box_expr string
pub fn (mut b BoxLayout) update_child_bounding(key string) {
id, mut bounding := parse_boxlayout_child_key(key, b.id)
bounding = b.update_child_current_box_expression(id, bounding)
mode, bounding_vec, has_z_index, z_index, box_expr := parse_boxlayout_child_bounding(bounding)
mode, bounding_vec, has_z_index, z_index, box_expr := b.parse_boxlayout_child_bounding(bounding)
match mode {
'rect' { b.update_child_rect(id, bounding_vec) }
'lt2rb' { b.update_child_lt2rb(id, bounding_vec) }
Expand Down Expand Up @@ -502,7 +503,7 @@ fn (mut b BoxLayout) preprocess_child_box_expression(i int, id string) {
bounding = b.precompute_box_expression(bounding)
// println("precompute: $bounding")
// set the child_box[i] and child_mode[i] (already done normally)
mode, bounding_vec, _, _, _ := parse_boxlayout_child_bounding(bounding)
mode, bounding_vec, _, _, _ := b.parse_boxlayout_child_bounding(bounding)
// println("preprocess $id $bounding_vec")
match mode {
'rect' { b.update_child_rect(id, bounding_vec) }
Expand All @@ -524,14 +525,15 @@ fn (b &BoxLayout) precompute_box_expression(bounding string) string {
for i in 0 .. 2 {
if tmp[i].contains_any('+-*/()') {
bbs = tmp[i].split(',').map(it.trim_space())
tmp[i] = '${b.calculate(bbs[i * 2])}, ${b.calculate(bbs[i * 2 + 1])}'
tmp[i] = '${b.calculate(bbs[0])}, ${b.calculate(bbs[1])}'
}
}
return '(${tmp[0]}) ${op} (${tmp[1]})'
}

fn (b &BoxLayout) calculate(formula string) f32 {
return b.ui.window.calculate(formula)
mut mc := b.mc
return mc.calculate(formula)
}

// TODO: documentation
Expand Down Expand Up @@ -870,7 +872,7 @@ fn parse_boxlayout_child_key(key string, bl_id string) (string, string) {
}

// parse child bounding and return
fn parse_boxlayout_child_bounding(bounding string) (string, []f32, bool, int, string) {
fn (b &BoxLayout) parse_boxlayout_child_bounding(bounding string) (string, []f32, bool, int, string) {
mut vec4, mut mode, mut has_z_index, mut z_index, mut box_expr := []f32{}, '', false, 0, ''
mut tmp2 := []string{}
if bounding == 'hidden' {
Expand All @@ -884,13 +886,13 @@ fn parse_boxlayout_child_bounding(bounding string) (string, []f32, bool, int, st
mode = 'box_expr'
} else if bounding.contains('++') {
tmp2 = bounding.split('++').map(it.trim_space())
vec4, has_z_index, z_index = parse_bounding_with_possible_zindex(tmp2[0], tmp2[1])
vec4, has_z_index, z_index = b.parse_bounding_with_possible_zindex(tmp2[0], tmp2[1])
if vec4.len == 4 {
mode = 'rect'
}
} else if bounding.contains('->') {
tmp2 = bounding.split('->').map(it.trim_space())
vec4, has_z_index, z_index = parse_bounding_with_possible_zindex(tmp2[0], tmp2[1])
vec4, has_z_index, z_index = b.parse_bounding_with_possible_zindex(tmp2[0], tmp2[1])
if vec4.len == 4 {
mode = 'lt2rb'
}
Expand All @@ -899,19 +901,19 @@ fn parse_boxlayout_child_bounding(bounding string) (string, []f32, bool, int, st
}

// TODO: ?int does not work yet
fn parse_bounding_with_possible_zindex(left string, right string) ([]f32, bool, int) {
fn (b &BoxLayout) parse_bounding_with_possible_zindex(left string, right string) ([]f32, bool, int) {
mut has_z_index, mut z_index := false, 0
mut tmp := left.find_between('(', ')')
mut vec4 := []f32{}
if !left.is_blank() {
vec4 = tmp.split(',').map(it.f32())
vec4 = tmp.replace('%', '/100.0').split(',').map(b.calculate(it))
if vec4.len == 3 {
has_z_index = true
z_index = int(vec4[2])
vec4 = vec4[0..2]
}
tmp2 := right.find_between('(', ')')
vec4 << tmp2.split(',').map(it.f32())
vec4 << tmp2.replace('%', '/100.0').split(',').map(b.calculate(it))
if vec4.len != 4 {
return []f32{}, false, 0
}
Expand Down
22 changes: 11 additions & 11 deletions src/layout_stack.v
Original file line number Diff line number Diff line change
Expand Up @@ -1235,42 +1235,42 @@ fn (s &Stack) margin(side Side) int {
.bottom { s.margins.bottom }
.left { s.margins.left }
}
mut isize := int(size)
mut i_size := int(size)
if 0.0 < size && size < 1.0 {
psize := if side in [.left, .right] { s.real_width } else { s.real_height }
$if margin ? {
println('margin(${side}) = ${size} * ${psize}')
}
isize = int(size * f32(psize))
i_size = int(size * f32(psize))
}
$if margin ? {
println('margin(${side}) = ${isize}')
println('margin(${side}) = ${i_size}')
}
if s.title != '' {
text_height := s.ui.dd.text_height(s.title)
match side {
.top { isize += int(f32(text_height) * 1.25) }
.bottom { isize += int(f32(text_height) * 0.75) }
else { isize += text_height / 2 }
.top { i_size += int(f32(text_height) * 1.25) }
.bottom { i_size += int(f32(text_height) * 0.75) }
else { i_size += text_height / 2 }
}
}
return isize
return i_size
}

fn (s &Stack) spacing(i int) int {
size := s.spacings[i]
mut isize := int(size)
mut i_size := int(size)
if 0.0 < size && size < 1.0 {
psize := if s.direction == .row { s.real_width } else { s.real_height }
$if spacing ? {
println('spacing(${i}) = ${size} * ${psize}')
}
isize = int(size * f32(psize))
i_size = int(size * f32(psize))
}
$if spacing ? {
println('spacing(${i}) = ${isize}')
println('spacing(${i}) = ${i_size}')
}
return isize
return i_size
}

fn (s &Stack) total_spacing() int {
Expand Down
4 changes: 2 additions & 2 deletions src/textbox.v
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ fn tb_char(mut tb TextBox, e &KeyEvent, window &Window) {
return
}
// if (tb.is_numeric && (s.len > 1 || !s[0].is_digit() ) {
if tb.is_numeric && (s.len > 1 || (!s[0].is_digit() && ((s[0] != `-`)
|| ((text.runes().len > 0) && (tb.cursor_pos > 0))))) {
if tb.is_numeric && (s.len > 1 || (!s[0].is_digit() && (s[0] != `-`
|| (text.runes().len > 0 && tb.cursor_pos > 0)))) {
return
}
// println('inserting codepoint=$e.codepoint mods=$e.mods ..')
Expand Down
3 changes: 1 addition & 2 deletions src/window.v
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub mut:
// settings SettingsUI
// shortcuts
shortcuts Shortcuts
mini_calc MiniCalc
mini_calc MiniCalc = mini_calc()
mx f64 // do not remove this, temporary
my f64
}
Expand Down Expand Up @@ -247,7 +247,6 @@ pub fn window(cfg WindowParams) &Window {
}
window.style_params.bg_color = cfg.bg_color
window.top_layer = canvas_layer()
window.mini_calc = mini_calc()
mut dd := &DrawDeviceContext{
Context: gg.new_context(
width: width
Expand Down

0 comments on commit c36668e

Please sign in to comment.