This commit is contained in:
agra
2026-03-02 19:47:25 +02:00
parent 812bc6d6ec
commit e63c946116
33 changed files with 32185 additions and 202 deletions

View File

@@ -4,6 +4,7 @@
#import "ui/events.sx";
#import "ui/view.sx";
#import "ui/label.sx";
#import "ui/font.sx";
ButtonStyle :: struct {
background: Color;
@@ -37,12 +38,10 @@ Button :: struct {
impl View for Button {
size_that_fits :: (self: *Button, proposal: ProposedSize) -> Size {
scale := self.font_size / GLYPH_HEIGHT_APPROX;
text_w := xx self.label.len * GLYPH_WIDTH_APPROX * scale;
text_h := self.font_size;
text_size := measure_text(self.label, self.font_size);
Size.{
width = text_w + self.style.padding.horizontal(),
height = text_h + self.style.padding.vertical()
width = text_size.width + self.style.padding.horizontal(),
height = text_size.height + self.style.padding.vertical()
};
}
@@ -56,31 +55,29 @@ impl View for Button {
ctx.add_rounded_rect(frame, bg, self.style.corner_radius);
// Text centered in frame
scale := self.font_size / GLYPH_HEIGHT_APPROX;
text_w := xx self.label.len * GLYPH_WIDTH_APPROX * scale;
text_h := self.font_size;
text_x := frame.origin.x + (frame.size.width - text_w) * 0.5;
text_y := frame.origin.y + (frame.size.height - text_h) * 0.5;
text_frame := Frame.make(text_x, text_y, text_w, text_h);
text_size := measure_text(self.label, self.font_size);
text_x := frame.origin.x + (frame.size.width - text_size.width) * 0.5;
text_y := frame.origin.y + (frame.size.height - text_size.height) * 0.5;
text_frame := Frame.make(text_x, text_y, text_size.width, text_size.height);
ctx.add_text(text_frame, self.label, self.font_size, self.style.foreground);
}
handle_event :: (self: *Button, event: *Event, frame: Frame) -> bool {
if event.type == {
case .mouse_moved: {
self.hovered = frame.contains(event.position);
if event.* == {
case .mouse_moved: (d) {
self.hovered = frame.contains(d.position);
return false;
}
case .mouse_down: {
if frame.contains(event.position) {
case .mouse_down: (d) {
if frame.contains(d.position) {
self.pressed = true;
return true;
}
}
case .mouse_up: {
case .mouse_up: (d) {
if self.pressed {
self.pressed = false;
if frame.contains(event.position) {
if frame.contains(d.position) {
if handler := self.on_tap {
handler();
}