changed border to uint

This commit is contained in:
Alessandro Mauri 2025-07-07 14:28:08 +02:00
parent b35bb427a7
commit fa334ed154
6 changed files with 54 additions and 47 deletions

View File

@ -147,7 +147,8 @@ fn void? Ctx.checkbox_id(&ctx, Id id, String description, Point off, bool* activ
Rect check = elem.bounds.add({x, x, -x*2, -x*2}); Rect check = elem.bounds.add({x, x, -x*2, -x*2});
Style s = *style; Style s = *style;
s.bg = s.primary; s.bg = s.primary;
s.margin = s.border = s.padding = {}; s.margin = s.padding = {};
s.border = 0;
ctx.push_rect(check, parent.div.z_index, &s)!; ctx.push_rect(check, parent.div.z_index, &s)!;
} }
} }
@ -180,6 +181,7 @@ fn void? Ctx.toggle_id(&ctx, Id id, String description, Point off, bool* active)
Rect t = elem.bounds.add({*active ? (style.size+3) : +3, +3, -style.size-6, -6}); Rect t = elem.bounds.add({*active ? (style.size+3) : +3, +3, -style.size-6, -6});
Style s = *style; Style s = *style;
s.bg = s.primary; s.bg = s.primary;
s.margin = s.border = s.padding = {}; s.margin = s.padding = {};
s.border = 0;
ctx.push_rect(t, parent.div.z_index, &s)!; ctx.push_rect(t, parent.div.z_index, &s)!;
} }

View File

@ -93,19 +93,19 @@ fn void? Ctx.push_scissor(&ctx, Rect rect, int z_index)
fn void? Ctx.push_rect(&ctx, Rect rect, int z_index, Style* style) fn void? Ctx.push_rect(&ctx, Rect rect, int z_index, Style* style)
{ {
Rect border = style.border;
Rect padding = style.padding; Rect padding = style.padding;
ushort border = style.border;
ushort radius = style.radius; ushort radius = style.radius;
Color bg = style.bg; Color bg = style.bg;
Color border_color = style.secondary; Color border_color = style.secondary;
if (!border.is_null()) { if (border != 0) {
Cmd cmd = { Cmd cmd = {
.type = CMD_RECT, .type = CMD_RECT,
.rect.rect = rect, .rect.rect = rect,
.rect.color = border_color, .rect.color = border_color,
.rect.radius = radius, .rect.radius = radius+border,
.rect.thickness = border.x, .rect.thickness = border,
}; };
ctx.push_cmd(&cmd, z_index)!; ctx.push_cmd(&cmd, z_index)!;
} }
@ -113,10 +113,10 @@ fn void? Ctx.push_rect(&ctx, Rect rect, int z_index, Style* style)
Cmd cmd = { Cmd cmd = {
.type = CMD_RECT, .type = CMD_RECT,
.rect.rect = { .rect.rect = {
.x = rect.x + border.x + padding.x, .x = rect.x + border + padding.x,
.y = rect.y + border.y + padding.y, .y = rect.y + border + padding.y,
.w = rect.w - (border.x+border.w) - (padding.x+padding.w), .w = rect.w - (border*2) - (padding.x+padding.w),
.h = rect.h - (border.y+border.h) - (padding.y+padding.h), .h = rect.h - (border*2) - (padding.y+padding.h),
}, },
.rect.color = bg, .rect.color = bg,
.rect.radius = radius, .rect.radius = radius,

View File

@ -144,22 +144,22 @@ fn Rect Ctx.position_element(&ctx, Elem *parent, Rect rect, Style* style)
child_occupied = child_occupied.off(origin.add(rect.position())); child_occupied = child_occupied.off(origin.add(rect.position()));
Rect margin = style.margin; Rect margin = style.margin;
Rect border = style.border;
Rect padding = style.padding; Rect padding = style.padding;
ushort border = style.border;
// padding, grows both the placement and occupied area // padding, grows both the placement and occupied area
child_placement = child_placement.grow(padding.position().add(padding.size())); child_placement = child_placement.grow(padding.position().add(padding.size()));
child_occupied = child_occupied.grow(padding.position().add(padding.size())); child_occupied = child_occupied.grow(padding.position().add(padding.size()));
// border, grows both the placement and occupied area // border, grows both the placement and occupied area
child_placement = child_placement.grow(border.position().add(border.size())); child_placement = child_placement.grow({border*2, border*2});
child_occupied = child_occupied.grow(border.position().add(border.size())); child_occupied = child_occupied.grow({border*2, border*2});
// margin, offsets the placement and grows the occupied area // margin, offsets the placement and grows the occupied area
child_placement = child_placement.off(margin.position()); child_placement = child_placement.off(margin.position());
child_occupied = child_occupied.grow(margin.position().add(margin.size())); child_occupied = child_occupied.grow(margin.position().add(margin.size()));
// oh yeah also adjust the rect if i was to grow // oh yeah also adjust the rect if i was to grow
if (adapt_x) rect.w -= padding.x+padding.w + border.x+border.w + margin.x+margin.w; if (adapt_x) rect.w -= padding.x+padding.w + border*2 + margin.x+margin.w;
if (adapt_y) rect.h -= padding.y+padding.h + border.y+border.h + margin.y+margin.h; if (adapt_y) rect.h -= padding.y+padding.h + border*2 + margin.y+margin.h;
// set the size // set the size
child_placement = child_placement.grow(rect.size()); child_placement = child_placement.grow(rect.size());

View File

@ -7,8 +7,10 @@ import std::io;
// global style, similar to the css box model // global style, similar to the css box model
struct Style { // css box model struct Style { // css box model
Rect padding; Rect padding;
Rect border;
Rect margin; Rect margin;
ushort border;
ushort radius;
ushort size;
Color bg; // background color Color bg; // background color
Color fg; // foreground color Color fg; // foreground color
@ -16,14 +18,12 @@ struct Style { // css box model
Color secondary; // secondary color Color secondary; // secondary color
Color accent; // accent color Color accent; // accent color
ushort radius;
short size;
} }
const Style DEFAULT_STYLE = { const Style DEFAULT_STYLE = {
.margin = {2, 2, 2, 2}, .margin = {2, 2, 2, 2},
.border = {2, 2, 2, 2}, .padding = {},
.padding = {1, 1, 1, 1}, .border = 2,
.radius = 12, .radius = 12,
.size = 16, .size = 16,
@ -89,8 +89,8 @@ fn int Ctx.import_style_from_file(&ctx, String path)
* Style can be serialized and deserialized with a subset of CSS * Style can be serialized and deserialized with a subset of CSS
* <style name> { * <style name> {
* padding: left right top bottom; * padding: left right top bottom;
* border: left right top bottom;
* margin: left right top bottoms; * margin: left right top bottoms;
* border: uint;
* radius: uint; * radius: uint;
* size: uint; * size: uint;
* Color: #RRGGBBAA; * Color: #RRGGBBAA;
@ -331,11 +331,6 @@ fn bool Parser.parse_property(&p)
if (p.parse_size(&padding) == false) return false; if (p.parse_size(&padding) == false) return false;
p.style.padding = padding; p.style.padding = padding;
case "border":
Rect border;
if (p.parse_size(&border) == false) return false;
p.style.border = border;
case "margin": case "margin":
Rect margin; Rect margin;
if (p.parse_size(&margin) == false) return false; if (p.parse_size(&margin) == false) return false;
@ -366,23 +361,32 @@ fn bool Parser.parse_property(&p)
if (p.parse_color(&accent) == false) return false; if (p.parse_color(&accent) == false) return false;
p.style.accent = accent; p.style.accent = accent;
case "radius": case "border":
short r; short border;
if (p.parse_number(&r) == false) return false; if (p.parse_number(&border) == false) return false;
if (r < 0) { if (border < 0) {
io::eprintfn("CSS parsing error at %d:%d: 'radius' must be a positive number, got %d", t.line, t.col, r); io::eprintfn("CSS parsing error at %d:%d: 'border' must be a positive number, got %d", t.line, t.col, border);
return false; return false;
} }
p.style.radius = (ushort)r; p.style.border = (ushort)border;
case "radius":
short radius;
if (p.parse_number(&radius) == false) return false;
if (radius < 0) {
io::eprintfn("CSS parsing error at %d:%d: 'radius' must be a positive number, got %d", t.line, t.col, radius);
return false;
}
p.style.radius = (ushort)radius;
case "size": case "size":
short s; short size;
if (p.parse_number(&s) == false) return false; if (p.parse_number(&size) == false) return false;
if (s < 0) { if (size < 0) {
io::eprintfn("CSS parsing error at %d:%d: 'size' must be a positive number, got %d", t.line, t.col, s); io::eprintfn("CSS parsing error at %d:%d: 'size' must be a positive number, got %d", t.line, t.col, size);
return false; return false;
} }
p.style.size = (ushort)s; p.style.size = (ushort)size;
default: default:

View File

@ -8,8 +8,8 @@ default {
button { button {
margin: 2 2 2 2; margin: 2 2 2 2;
border: 2 2 2 2; padding: 2 2 2 2;
padding: 1 1 1 1; border: 2;
radius: 10; radius: 10;
bg: #3c3836ff; bg: #3c3836ff;
@ -21,8 +21,8 @@ button {
button-active { button-active {
margin: 2 2 2 2; margin: 2 2 2 2;
border: 2 2 2 2;
padding: 1 1 1 1; padding: 1 1 1 1;
border: 2;
radius: 10; radius: 10;
bg: #504945ff; bg: #504945ff;
@ -34,8 +34,8 @@ button-active {
checkbox { checkbox {
margin: 2 2 2 2; margin: 2 2 2 2;
border: 2 2 2 2;
padding: 1 1 1 1; padding: 1 1 1 1;
border: 2;
radius: 10; radius: 10;
size: 16; size: 16;
bg: #3c3836ff; bg: #3c3836ff;
@ -47,9 +47,8 @@ checkbox {
toggle { toggle {
margin: 2 2 2 2; margin: 2 2 2 2;
border: 2 2 2 2; border: 2;
padding: 1 1 1 1; radius: 0;
radius: 10;
size: 16; size: 16;
bg: #3c3836ff; bg: #3c3836ff;
fg: #fbf1c7ff; fg: #fbf1c7ff;
@ -61,7 +60,7 @@ toggle {
slider { slider {
margin: 2 2 2 2; margin: 2 2 2 2;
padding: 2 2 2 2; padding: 2 2 2 2;
border: 1 1 1 1; border: 2;
radius: 4; radius: 4;
size: 8; size: 8;
bg: #3c3836ff; bg: #3c3836ff;

View File

@ -13,6 +13,7 @@ alias Times = ringbuffer::RingBuffer{time::NanoDuration[128]};
fn void Times.print_stats(&times) fn void Times.print_stats(&times)
{ {
if (times.written == 0);
time::NanoDuration min, max, avg, x; time::NanoDuration min, max, avg, x;
min = times.get(0); min = times.get(0);
for (usz i = 0; i < times.written; i++) { for (usz i = 0; i < times.written; i++) {
@ -21,7 +22,7 @@ fn void Times.print_stats(&times)
if (x > max) { max = x; } if (x > max) { max = x; }
avg += x; avg += x;
} }
avg = (NanoDuration)((ulong)avg/128.0); avg = (NanoDuration)((ulong)avg/times.written);
io::printfn("min=%s, max=%s, avg=%s", min, max, avg); io::printfn("min=%s, max=%s, avg=%s", min, max, avg);
} }
@ -32,6 +33,7 @@ struct TimeStats {
fn TimeStats Times.get_stats(&times) fn TimeStats Times.get_stats(&times)
{ {
if (times.written == 0) return {};
time::NanoDuration min, max, avg, x; time::NanoDuration min, max, avg, x;
min = times.get(0); min = times.get(0);
for (usz i = 0; i < times.written; i++) { for (usz i = 0; i < times.written; i++) {
@ -40,7 +42,7 @@ fn TimeStats Times.get_stats(&times)
if (x > max) { max = x; } if (x > max) { max = x; }
avg += x; avg += x;
} }
avg = (NanoDuration)((ulong)avg/128.0); avg = (NanoDuration)((ulong)avg/times.written);
return {.min = min, .max = max, .avg = avg}; return {.min = min, .max = max, .avg = avg};
} }