Replace the bare-paren tuple grammar with explicit, position-unambiguous
forms, mirroring how structs work:
type `(A, B)` -> `Tuple(A, B)` (named keeps `:`)
value `(a, b)` -> `.(a, b)` (named uses `=`)
typed (new) -> `Tuple(A, B).(a, b)` (like `Point.{...}`)
failable `-> (T, !)` -> `-> T !`
`-> (T1, T2, !)`-> `-> Tuple(T1, T2) !` (channel outside Tuple)
Bare `(...)` is now grouping only, everywhere; a comma in bare parens is a
hard error with a migration hint. Grouping, function types `(A, B) -> R`,
param lists, lambdas, and match bindings are unaffected.
`Tuple(...)` is strictly a TYPE in every position (including `size_of` /
`type_info` args); a tuple VALUE comes only from `.(...)` (anonymous) or
`Tuple(...).(...)` (explicitly typed). A bare `Tuple(1, 2)` is a tuple
type with non-type elements -> rejected.
The ~110 tuple-bearing corpus files were migrated with a one-shot
AST-aware migrator (the `sx migrate` tool from the prior commit, removed
here). New examples: 0130 (new syntax), 0131 (typed construction), 1060
(named-tuple failable return). 1116 golden updated for the new hint text.
92 lines
2.7 KiB
Plaintext
92 lines
2.7 KiB
Plaintext
#import "modules/std.sx";
|
|
#import "modules/ui/types.sx";
|
|
#import "modules/ui/render.sx";
|
|
#import "modules/ui/events.sx";
|
|
#import "modules/ui/view.sx";
|
|
#import "modules/ui/label.sx";
|
|
#import "modules/ui/font.sx";
|
|
|
|
ButtonStyle :: struct {
|
|
background: Color;
|
|
foreground: Color;
|
|
hover_bg: Color;
|
|
pressed_bg: Color;
|
|
corner_radius: f32;
|
|
padding: EdgeInsets;
|
|
|
|
default :: () -> ButtonStyle {
|
|
ButtonStyle.{
|
|
background = COLOR_BLUE,
|
|
foreground = COLOR_WHITE,
|
|
hover_bg = Color.rgb(0, 100, 220),
|
|
pressed_bg = Color.rgb(0, 80, 180),
|
|
corner_radius = 6.0,
|
|
padding = EdgeInsets.symmetric(16.0, 8.0)
|
|
}
|
|
}
|
|
}
|
|
|
|
Button :: struct {
|
|
label: string;
|
|
font_size: f32;
|
|
style: ButtonStyle;
|
|
on_tap: ?Closure();
|
|
hovered: bool;
|
|
pressed: bool;
|
|
|
|
}
|
|
|
|
impl View for Button {
|
|
size_that_fits :: (self: *Button, proposal: ProposedSize) -> Size {
|
|
text_size := measure_text(self.label, self.font_size);
|
|
Size.{
|
|
width = text_size.width + self.style.padding.horizontal(),
|
|
height = text_size.height + self.style.padding.vertical()
|
|
}
|
|
}
|
|
|
|
layout :: (self: *Button, bounds: Frame) {}
|
|
|
|
render :: (self: *Button, ctx: *RenderContext, frame: Frame) {
|
|
bg := if self.pressed then self.style.pressed_bg
|
|
else if self.hovered then self.style.hover_bg
|
|
else self.style.background;
|
|
|
|
ctx.add_rounded_rect(frame, bg, self.style.corner_radius);
|
|
|
|
// Text centered in frame
|
|
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.* == {
|
|
case .mouse_moved: (d) {
|
|
self.hovered = frame.contains(d.position);
|
|
return false;
|
|
}
|
|
case .mouse_down: (d) {
|
|
if frame.contains(d.position) {
|
|
self.pressed = true;
|
|
return true;
|
|
}
|
|
}
|
|
case .mouse_up: (d) {
|
|
if self.pressed {
|
|
self.pressed = false;
|
|
if frame.contains(d.position) {
|
|
if handler := self.on_tap {
|
|
handler();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
false
|
|
}
|
|
}
|