Files
kryon/runtime/button.kry
T
waozi 7901590204 Declare the interface host in a props module; unify the focus symbols
include/ui_core.h is deleted. runtime/core_props.kry now carries the public
ABI: TextInputPlatformCallback and FrameState — anonymous capture arrays,
inline arrays, and all — as one-line "#type" records; ui_view_width and
ui_view_height as "#global #export" moved from src/ui/frame.kry; and the
forty-eight interface, focus, and cursor calls as "#extern #export"
declarations.

SetFocus/GetFocus become KryonSetFocus/KryonGetFocus everywhere in C: the old
header renamed only the MinGW declaration via __asm__, so the exported symbol
already differed per platform, and a plain props declaration could not carry
the label. The real names remove the collision by construction; the Go API
keeps its SetFocus spelling, which never collided. Callers across the UI
hosts, tests, and the generated-runtime-parity templates follow (the parity
script's Go section keeps SetFocus for the Go interface).

Gates: generated-runtime-parity-test (17 fixtures, C and Go), fast-test,
k2c/k2cpp/k2go-syntax-test, go-runtime-test, examples-syntax-test,
kryon-boundary-check, and the refreshed public-API snapshot pass.
2026-09-21 02:27:21 -03:00

634 lines
22 KiB
Plaintext

#module "button"
#import "style"
#import "style_sheet"
#import "surface"
#import "control_props"
#import "button_props"
#import "input_props"
#import "paint"
#import "material"
#import "ui_text_props.generated.h"
#import "ui_core_props.generated.h"
MeasureTextWidth :: (text: const char*, font: i32, typeface: const char*) -> i32 #extern
ReadActivation :: (bounds: Rectangle, id: i32, enabled: bool) -> Activation #extern
ButtonInput :: struct {
flags: StateFlags
interaction: InteractionState
activated: bool
}
ButtonActionEnabled :: (disabled: bool, content_disabled: bool) -> bool #export {
return !disabled && !content_disabled
}
ButtonArrowGlyph :: (direction: ArrowDirection) -> i32 #export {
if direction == (ArrowDirection)ArrowRight {
return 62
}
if direction == (ArrowDirection)ArrowUp {
return 94
}
if direction == (ArrowDirection)ArrowDown {
return 118
}
return 60
}
# Both immediate input and retained painting use this interpretation. Painting
# supplies its stored sample and must not poll or consume activation again.
ResolveButtonInput :: (state: i32, disabled: bool, loading: bool, selected: bool,
sample: Activation) -> ButtonInput #export {
result: ButtonInput
result.flags = ResolveFlags(state, disabled, loading, selected)
enabled: bool = CanActivate(result.flags.disabled, result.flags.loading)
result.activated = enabled && sample.activated
result.interaction = ResolveInteraction(state, result.flags.disabled,
result.flags.loading, enabled && sample.pressed, enabled && sample.hovered,
enabled && sample.focused, result.flags.selected)
return result
}
ReadButtonInput :: (bounds: Rectangle, id: i32, state: i32, disabled: bool,
loading: bool, selected: bool) -> ButtonInput #export {
flags: StateFlags = ResolveFlags(state, disabled, loading, selected)
sample: Activation = ReadActivation(bounds, id, CanActivate(flags.disabled, flags.loading))
return ResolveButtonInput(state, disabled, loading, selected, sample)
}
# Style facts for the generic control path. A zero style_kind falls back to the
# Button kind so generic control callers keep one policy source.
ButtonRoleFactsFor :: (style_kind: i32, name: i32, class_name: i32, role: i32,
tone: i32, emphasis: i32, size: i32, state: i32) -> StyleFacts #export {
kind: i32 = style_kind
if kind == 0 { kind = StyleKindButton() }
return StyleControlRoleFacts(kind, name, class_name, role, tone, emphasis,
size, state)
}
# The declaration owns the retained shape; hosts only allocate it by identity.
ButtonInstance :: struct {
motion: InteractionMotion
}
AdvanceButtonMotion :: (key: u64, state: i32, input: ButtonInput, enabled: bool,
delta_ms: float, normal_ms: float, fast_ms: float) -> InteractionMotion #export {
retained: ButtonInstance #instance(key)
retained.motion = AdvanceInteractionMotion(retained.motion, input.interaction.hovered,
input.interaction.pressed, input.interaction.focused, enabled,
state != ButtonStateAuto, input.flags.disabled, input.flags.loading,
delta_ms, normal_ms, fast_ms)
return retained.motion
}
CanActivate :: (disabled: bool, loading: bool) -> bool #export {
return !disabled && !loading
}
# Hosts supply measured text and execute these bounds. All coordinates are
# local logical pixels so scale, font rasterization and texture APIs stay out
# of the widget definition.
ContentSize :: struct {
width: float
text_width: float
icon_size: float
gap: float
}
IconActionMetrics :: struct {
padding: i32
icon_size: i32
}
TextButtonMetrics :: struct {
padding_x: i32
padding_y: i32
min_width: i32
min_height: i32
}
InfoIndicatorMetrics :: struct {
min_touch: i32
diameter: i32
}
InfoIndicatorPaint :: struct {
bounds: Rectangle
hit: Rectangle
radius: i32
fill: u32
stroke: u32
text: u32
font: i32
}
ButtonInfoBounds :: struct {
bounds: Rectangle
diameter: i32
}
ButtonSplitLayout :: struct {
width: float
action_width: float
menu_offset: float
menu_width: float
divider_inset: float
}
SwatchSpec :: struct {
bounds: Rectangle
color: Color
disabled: bool
hovered: bool
focused: bool
scale: float
face: StyleFrame
}
SwatchPaint :: struct {
bounds: Rectangle
checker_base: Rectangle
checker_a: Rectangle
checker_b: Rectangle
swatch: Rectangle
focus_bounds: Rectangle
checker_base_color: u32
checker_alt_color: u32
swatch_color: Color
border_color: u32
focus_color: u32
text_color: u32
label_x: float
radius: float
swatch_roundness: float
border_width: float
focus_width: float
show_focus: bool
}
ButtonResolveSplitLayout :: (width: float, height: float) -> ButtonSplitLayout #export {
layout: ButtonSplitLayout
layout.width = width
if layout.width < height * 2.0 {
layout.width = height * 2.0
}
layout.action_width = layout.width - height
layout.menu_offset = layout.action_width
layout.menu_width = height
layout.divider_inset = 8.0
return layout
}
ButtonMetric :: (fields: u32, field: u32, value: float,
fallback: float, scale: float, allow_zero: bool) -> i32 {
if scale <= 0.0 { scale = 1.0 }
if (fields & field) == (u32)0 || value < 0.0 ||
(!allow_zero && value <= 0.0) {
value = fallback
}
return (i32)(value * scale)
}
IconActionMetricsFor :: (bounds: Rectangle, requested_icon_size: i32,
requested_padding: i32, scale: float, face: StyleFrame) -> IconActionMetrics #export {
if scale <= 0.0 { scale = 1.0 }
metrics: IconActionMetrics
if requested_padding > 0 {
metrics.padding = requested_padding
} else {
metrics.padding = ButtonMetric(face.value.fields, (u32)StylePaddingX,
face.value.padding_x, 3.0, scale, true)
}
metrics.icon_size = requested_icon_size
if metrics.icon_size <= 0 {
available: i32 = (i32)bounds.width
if bounds.height < bounds.width { available = (i32)bounds.height }
metrics.icon_size = available - metrics.padding * 2
}
if metrics.icon_size < 1 { metrics.icon_size = 1 }
return metrics
}
IconActionStyleIconSize :: (icon_size: i32, scale: float) -> float #export {
if icon_size < 0 { icon_size = 0 }
if scale <= 0.0 { scale = 1.0 }
return (float)icon_size / scale
}
IconActionStyleRadius :: (radius: float, bounds: Rectangle, scale: float) -> float #export {
if radius <= 0.0 { return 0.0 }
if scale <= 0.0 { scale = 1.0 }
side: float = bounds.width
if bounds.height < side { side = bounds.height }
if side < 0.0 { side = 0.0 }
return radius * side / (2.0 * scale)
}
TextButtonMetricsFor :: (scale: float, face: StyleFrame) -> TextButtonMetrics #export {
if scale <= 0.0 { scale = 1.0 }
metrics: TextButtonMetrics
metrics.padding_x = ButtonMetric(face.value.fields, (u32)StylePaddingX,
face.value.padding_x, 8.0, scale, true)
metrics.padding_y = ButtonMetric(face.value.fields, (u32)StylePaddingY,
face.value.padding_y, 4.0, scale, true)
metrics.min_width = ButtonMetric(face.value.fields, (u32)StyleContentOffset,
face.value.offset_x, 34.0, scale, false)
metrics.min_height = ButtonMetric(face.value.fields, (u32)StyleIconSize,
face.value.icon_size, 34.0, scale, false)
return metrics
}
InfoIndicatorMetricsFor :: (requested_diameter: i32, scale: float,
face: StyleFrame) -> InfoIndicatorMetrics #export {
if scale <= 0.0 { scale = 1.0 }
metrics: InfoIndicatorMetrics
metrics.min_touch = ButtonMetric(face.value.fields, (u32)StyleContentOffset,
face.value.offset_x, 32.0, scale, false)
if requested_diameter > 0 {
metrics.diameter = requested_diameter
} else {
metrics.diameter = ButtonMetric(face.value.fields, (u32)StyleIconSize,
face.value.icon_size, 18.0, scale, false)
}
return metrics
}
InfoGlyphSizeFor :: (diameter: i32, styled_size: i32) -> i32 #export {
minimum: i32 = diameter * 3 / 5
if styled_size < minimum { return minimum }
return styled_size
}
InfoIndicatorPaintFor :: (center_x: i32, center_y: i32,
metrics: InfoIndicatorMetrics, face: StyleFrame,
fallback_font: i32) -> InfoIndicatorPaint #export {
paint: InfoIndicatorPaint
diameter: i32 = metrics.diameter
paint.radius = diameter / 2
paint.bounds.x = (float)(center_x - paint.radius)
paint.bounds.y = (float)(center_y - paint.radius)
paint.bounds.width = (float)diameter
paint.bounds.height = (float)diameter
hit_diameter: i32 = diameter
if hit_diameter < metrics.min_touch { hit_diameter = metrics.min_touch }
paint.hit.x = (float)(center_x - hit_diameter / 2)
paint.hit.y = (float)(center_y - hit_diameter / 2)
paint.hit.width = (float)hit_diameter
paint.hit.height = (float)hit_diameter
paint.fill = face.value.background
paint.stroke = face.value.border
paint.text = face.value.foreground
styled_font: i32 = ResolveFont(0,
StyleFontValue(face.value.fields, face.value.font_size), fallback_font)
paint.font = InfoGlyphSizeFor(diameter, styled_font)
return paint
}
ButtonInfoBoundsFor :: (declared: Rectangle, scale: float,
face: StyleFrame) -> ButtonInfoBounds #export {
result: ButtonInfoBounds
requested: i32 = (i32)declared.width
if declared.height > 0.0 && declared.height < declared.width {
requested = (i32)declared.height
}
metrics: InfoIndicatorMetrics = InfoIndicatorMetricsFor(requested, scale, face)
result.bounds = declared
result.diameter = metrics.diameter
if result.bounds.width <= 0.0 { result.bounds.width = (float)result.diameter }
if result.bounds.height <= 0.0 { result.bounds.height = (float)result.diameter }
return result
}
ButtonToggleMenuOpen :: (open: bool, clicked: bool) -> bool #export {
if clicked { return !open }
return open
}
ButtonCloseMenuAfterActivation :: (open: bool, activated_id: i32) -> bool #export {
if activated_id != 0 { return false }
return open
}
SwatchPaintFor :: (spec: SwatchSpec) -> SwatchPaint #export {
paint: SwatchPaint
scale: float = spec.scale
if scale <= 0.0 { scale = 1.0 }
swatch_inset: float = 5.0 * scale
max_inset: float = spec.bounds.width / 6.0
if spec.bounds.height / 6.0 < max_inset {
max_inset = spec.bounds.height / 6.0
}
if max_inset < 0.0 { max_inset = 0.0 }
if swatch_inset > max_inset { swatch_inset = max_inset }
inner: Rectangle
inner.x = spec.bounds.x + swatch_inset
inner.y = spec.bounds.y + swatch_inset
inner.width = spec.bounds.width - 2.0 * swatch_inset
inner.height = spec.bounds.height - 2.0 * swatch_inset
half_w: float = inner.width * 0.5
half_h: float = inner.height * 0.5
inset: float = spec.face.value.padding_x
if (spec.face.value.fields & (u32)StylePaddingX) == (u32)0 ||
inset < 0.0 {
inset = 6.0
}
inset *= scale
border_width: float = spec.face.value.border_width
focus_width: float = spec.face.value.border_width
paint.bounds = spec.bounds
paint.checker_base = inner
paint.checker_a.x = inner.x
paint.checker_a.y = inner.y
paint.checker_a.width = half_w
paint.checker_a.height = half_h
paint.checker_b.x = inner.x + half_w
paint.checker_b.y = inner.y + half_h
paint.checker_b.width = half_w
paint.checker_b.height = half_h
paint.swatch = inner
paint.focus_bounds = spec.bounds
paint.swatch_color = spec.color
paint.label_x = spec.bounds.x + inset
paint.radius = spec.face.value.radius
swatch_diameter: float = inner.width
if inner.height < swatch_diameter { swatch_diameter = inner.height }
if swatch_diameter > 0.0 {
swatch_radius: float = paint.radius * scale
if swatch_radius < 0.0 { swatch_radius = 0.0 }
max_swatch_radius: float = swatch_diameter * 0.08
if swatch_radius > max_swatch_radius {
swatch_radius = max_swatch_radius
}
paint.swatch_roundness = 2.0 * swatch_radius / swatch_diameter
}
paint.border_width = border_width
paint.focus_width = focus_width
paint.checker_base_color = spec.face.value.background
paint.checker_alt_color = spec.face.value.background_end
paint.border_color = spec.face.value.border
paint.focus_color = spec.face.value.focus
paint.text_color = spec.face.value.foreground
paint.show_focus = !spec.disabled && spec.focused
if spec.disabled {
paint.swatch_color.a = (u8)128
}
return paint
}
MeasureButton :: (props: ButtonProps, paint: Style, minimum_height: float,
font: i32, available_width: float, scale: float, disclosure: bool) -> Rectangle #export {
label_width: float = (float)MeasureTextWidth(props.label, font, paint.typeface)
return MeasureBounds(props, paint, minimum_height, font, label_width,
available_width, scale, disclosure)
}
# The host supplies physical glyph measurements, available space, and scale.
# The declaration reads the real props and owns shape, label, and size policy.
MeasureBounds :: (props: ButtonProps, paint: Style, minimum_height: float,
font: i32, label_width: float, available_width: float,
scale: float, disclosure: bool) -> Rectangle #export {
if scale <= 0.0 { scale = 1.0 }
bounds: Rectangle = props.bounds
text_height: float = 0.0
if props.label != "" && !props.icon_only { text_height = (float)font / scale }
height: float = FitHeight(bounds.height / scale, minimum_height / scale,
text_height, paint.padding_y)
has_icon: bool = disclosure || props.icon.id != (u32)0 || props.icon_type != 0
content: ContentSize = MeasureContent(height, label_width / scale,
paint.icon_size, paint.gap, paint.padding_x, has_icon, props.icon_only)
width: float = ShapeWidth(bounds.width / scale, height, content.width,
available_width / scale, props.full_width,
props.square || props.icon_only, props.circle)
# Preserve explicit physical coordinates without a lossy scale roundtrip.
if bounds.height <= 0.0 { bounds.height = height * scale }
if props.square || props.circle || props.icon_only {
bounds.width = bounds.height
} else if bounds.width <= 0.0 {
bounds.width = width * scale
}
return bounds
}
# Measurement and placement share the same content rules. An absent label
# consumes neither text width nor a gap; icons fit the available face height.
# Style resolution already supplies theme defaults. Small sizes stay small;
# zero hides the icon and its gap instead of restoring a font-sized fallback.
MeasureContent :: (height: float, label_width: float,
requested_icon_size: float, gap: float, padding_x: float,
has_icon: bool, icon_only: bool) -> ContentSize #export {
result: ContentSize
# Match Style.ContentBounds: negative padding never shrinks the area
# required by content, while explicit zero remains a valid override.
if padding_x < 0.0 { padding_x = 0.0 }
icon_size: float = requested_icon_size
if icon_size > height - 12.0 { icon_size = height - 12.0 }
if icon_size < 0.0 { icon_size = 0.0 }
if icon_only { label_width = 0.0 }
if !has_icon || icon_size == 0.0 || icon_only || label_width <= 0.0 { gap = 0.0 }
result.text_width = label_width
result.icon_size = icon_size
result.gap = gap
result.width = label_width + gap + 2.0 * padding_x
if has_icon { result.width += icon_size }
return result
}
ButtonContent :: struct {
icon_x: float
icon_y: float
icon_size: float
text_x: float
text_y: float
text_width: float
text_height: float
}
ContentLayout :: (
width: float, height: float, label_width: float,
requested_icon_size: float, gap: float, has_icon: bool, icon_only: bool,
trailing: bool, offset_x: float, offset_y: float
) -> ButtonContent #export {
result: ButtonContent
measured: ContentSize = MeasureContent(height, label_width,
requested_icon_size, gap, 0.0, has_icon, icon_only)
icon_size: float = measured.icon_size
label_width = measured.text_width
gap = measured.gap
x: float = (width - measured.width) * 0.5 + offset_x
result.icon_x = x
result.icon_y = (height - icon_size) * 0.5 + offset_y
result.icon_size = icon_size
result.text_x = x
result.text_y = offset_y
result.text_width = label_width
result.text_height = height
if has_icon {
if trailing { result.icon_x += label_width + gap }
else { result.text_x += icon_size + gap }
}
return result
}
# Content selection, geometry, and ordering belong to the widget, not its host.
# Bounds and glyph measurements are physical; style lengths are logical.
ContentDrawing :: struct {
mark: Drawing
label: Drawing
}
PaintContent :: (props: ButtonProps, bounds: Rectangle, paint: StyleData,
font: i32, label_width: float, foreground: u32, ambient: u32,
scale: float, elapsed_ms: f64, disclosure: bool) -> ContentDrawing #export {
result: ContentDrawing
if scale <= 0.0 { scale = 1.0 }
has_icon: bool = disclosure || props.icon.id != (u32)0 || props.icon_type != 0
content: ButtonContent = ContentLayout(bounds.width / scale, bounds.height / scale,
label_width / scale, paint.icon_size, paint.gap, has_icon, props.icon_only,
(i32)props.icon_placement == IconPlacementTrailing,
paint.offset_x, paint.offset_y)
result.mark.color = foreground
if props.loading {
result.mark.kind = DrawingRing
ring: LoadingRingSpec = LoadingRing(bounds.width / scale, bounds.height / scale,
content.icon_size, elapsed_ms, foreground, ambient)
ring.x = bounds.x + ring.x * scale
ring.y = bounds.y + ring.y * scale
ring.inner_radius *= scale
ring.outer_radius *= scale
ring.glow_blur *= scale
result.mark.ring = ring
return result
}
if has_icon {
result.mark.bounds.x = bounds.x + content.icon_x * scale
result.mark.bounds.y = bounds.y + content.icon_y * scale
result.mark.bounds.width = content.icon_size * scale
result.mark.bounds.height = content.icon_size * scale
if disclosure {
result.mark.kind = DrawingChevron
} else if props.icon.id != (u32)0 {
result.mark.kind = DrawingTexture
result.mark.texture = props.icon
} else {
result.mark.kind = DrawingIcon
result.mark.icon = props.icon_type
}
}
if !props.icon_only && props.label != "" {
result.label.kind = DrawingText
result.label.bounds.x = bounds.x + content.text_x * scale
result.label.bounds.y = bounds.y + content.text_y * scale
result.label.bounds.width = content.text_width * scale
result.label.bounds.height = content.text_height * scale
result.label.text = props.label
result.label.font = font
result.label.color = foreground
}
return result
}
ButtonFrame :: struct {
props: ButtonProps
appearance: StyleFrame
material: MaterialPaint
content_bounds: Rectangle
font: i32
foreground: u32
repaint: bool
}
# Finish the widget frame once; native backends only adapt this result to their
# retained tree or operation stream. Rasterizers supply the physical font size.
BuildFrame :: (props: ButtonProps, input: ButtonInput, appearance: StyleFrame,
motion: InteractionMotion, surface: Rectangle, ambient: u32,
scale: float, style_font: i32, fallback_font: i32) -> ButtonFrame #export {
result: ButtonFrame
props.disabled = input.flags.disabled
props.loading = input.flags.loading
props.selected = input.flags.selected
props.pill = props.pill || props.circle
if props.loading { props.label = "" }
if props.circle {
shape_scale: float = scale
if shape_scale <= 0.0 { shape_scale = 1.0 }
appearance.value.radius = props.bounds.height * 0.5 / shape_scale
appearance.value.fields |= (u32)StyleRadius
}
result.props = props
result.appearance = appearance
result.material.bounds = props.bounds
result.material.surface = surface
result.material.value = appearance.value
result.material.light = appearance.value.border
result.material.ambient = ambient
result.material.hover = motion.hover.value
result.material.press = motion.press.value
result.material.focus = motion.focus.value
result.material.disabled = props.disabled
result.material.fill = appearance.fill
result.material.fill_valid = true
result.material.scale = scale
result.material = PrepareMaterial(result.material)
result.content_bounds = InsetBounds(props.bounds, appearance.value.padding_x,
appearance.value.padding_y, result.material.scale)
result.font = ResolveFont(0, style_font, fallback_font)
result.foreground = Opacity(appearance.value.foreground, appearance.value.opacity)
result.repaint = motion.active || (props.loading && !props.disabled)
return result
}
# Draw the resolved widget through borrowed rasterizer callbacks. Retained hosts
# invoke this after layout; callbacks never outlive this paint pass.
PaintButton :: (frame: ButtonFrame, label_width: float, elapsed_ms: f64,
disclosure: bool, surface: SurfacePainter, draw: Painter) #export {
layer: i32 = 0
while layer < MaterialLayerCount(frame.material.value.material) {
surface(PaintMaterialLayer(frame.material, layer))
layer += 1
}
content: ContentDrawing = PaintContent(frame.props,
MaterialContentBounds(frame.material), frame.appearance.value, frame.font,
label_width, frame.foreground, frame.material.ambient, frame.material.scale,
elapsed_ms, disclosure)
draw(content.mark)
draw(content.label)
}
ShapeWidth :: (
width: float,
height: float,
measured_width: float,
available_width: float,
full_width: bool,
square: bool,
circle: bool
) -> float #export {
if square || circle { return height }
if full_width && width <= 0.0 {
if available_width < height { return height }
return available_width
}
if width <= 0.0 { return measured_width }
return width
}
ShapeRadius :: (
normal_radius: float,
pill_radius: float,
pill: bool,
circle: bool
) -> float #export {
if pill || circle { return pill_radius }
return normal_radius
}