Files
kryon/runtime/toggle.kry
T
2026-09-13 11:39:32 -03:00

202 lines
7.5 KiB
Plaintext

#module "toggle"
#import "button"
#import "control_props"
#import "drawing_props"
#import "material"
#import "style"
#import "surface"
ToggleSpec :: struct {
bounds: Rectangle
checked: bool
enabled: bool
hovered: bool
pressed: bool
focused: bool
has_labels: bool
off_width: i32
on_width: i32
font: i32
scale: float
track: StyleFrame
active: StyleFrame
thumb: StyleFrame
}
TogglePaint :: struct {
bounds: Rectangle
track_bounds: Rectangle
focus_bounds: Rectangle
active_bounds: Rectangle
off_label_bounds: Rectangle
on_label_bounds: Rectangle
track: StyleFrame
active: StyleFrame
thumb: StyleFrame
show_focus: bool
has_labels: bool
off_label_color: u32
on_label_color: u32
thumb_x: float
thumb_y: float
thumb_radius: float
thumb_shadow_color: u32
thumb_fill_color: u32
thumb_edge_color: u32
thumb_highlight_color: u32
thumb_glow_color: u32
}
ToggleMetric :: (fields: u32, field: u32, value: float, fallback: float,
scale: float, allow_zero: bool) -> float {
if scale <= 0.0 { scale = 1.0 }
if (fields & field) == (u32)0 || value < 0.0 ||
(!allow_zero && value <= 0.0) {
value = fallback
}
return value * scale
}
ToggleTrackWidthForStyle :: (track: StyleFrame, scale: float) -> float #export {
return ToggleMetric(track.value.fields, (u32)StylePaddingX,
track.value.padding_x, 54.0, scale, false)
}
ToggleTrackHeightForStyle :: (track: StyleFrame, scale: float) -> float #export {
return ToggleMetric(track.value.fields, (u32)StylePaddingY,
track.value.padding_y, 32.0, scale, false)
}
ToggleThumbSizeForStyle :: (thumb: StyleFrame, checked: bool,
scale: float) -> float #export {
if scale <= 0.0 { scale = 1.0 }
return ToggleMetric(thumb.value.fields, (u32)StyleIconSize,
thumb.value.icon_size, checked ? 24.0 : 20.0, scale, false)
}
ToggleMinimumWidthForStyle :: (has_labels: bool, off_width: i32,
on_width: i32, scale: float, track: StyleFrame,
active: StyleFrame) -> i32 #export {
if scale <= 0.0 { scale = 1.0 }
if !has_labels { return (i32)ToggleTrackWidthForStyle(track, scale) }
active_gap: float = ToggleMetric(active.value.fields, (u32)StyleGap,
active.value.gap, 3.0, scale, true)
widest: i32 = off_width
if on_width > widest { widest = on_width }
return (widest + (i32)(16.0 * scale)) * 2 +
(i32)(active_gap * 2.0)
}
ToggleMinimumHeightForStyle :: (scale: float, track: StyleFrame,
thumb: StyleFrame) -> i32 #export {
if scale <= 0.0 { scale = 1.0 }
track_h: float = ToggleTrackHeightForStyle(track, scale)
thumb_gap: float = ToggleMetric(thumb.value.fields, (u32)StyleGap,
thumb.value.gap, 4.0, scale, true)
thumb_min: float = ToggleThumbSizeForStyle(thumb, false, scale) +
thumb_gap * 2.0
if track_h < thumb_min { track_h = thumb_min }
return (i32)track_h
}
TogglePaintFor :: (spec: ToggleSpec) -> TogglePaint #export {
paint: TogglePaint
scale: float = spec.scale
if scale <= 0.0 { scale = 1.0 }
bounds: Rectangle = spec.bounds
min_w: float = (float)ToggleMinimumWidthForStyle(spec.has_labels,
spec.off_width, spec.on_width, scale, spec.track, spec.active)
min_h: float = (float)ToggleMinimumHeightForStyle(scale, spec.track,
spec.thumb)
if bounds.width < min_w { bounds.width = min_w }
if bounds.height < min_h { bounds.height = min_h }
track_w: float = ToggleTrackWidthForStyle(spec.track, scale)
track_h: float = ToggleTrackHeightForStyle(spec.track, scale)
if spec.has_labels {
track_w = bounds.width
track_h = bounds.height
}
if track_w > bounds.width { track_w = bounds.width }
if track_h > bounds.height { track_h = bounds.height }
track: Rectangle
track.x = bounds.x + (bounds.width - track_w) * 0.5
track.y = bounds.y + (bounds.height - track_h) * 0.5
track.width = track_w
track.height = track_h
paint.track = spec.track
paint.active = spec.active
paint.thumb = spec.thumb
focus_gap: float = ToggleMetric(spec.track.value.fields, (u32)StyleGap,
spec.track.value.gap, 4.0, scale, true)
paint.focus_bounds.x = track.x - focus_gap
paint.focus_bounds.y = track.y - focus_gap
paint.focus_bounds.width = track.width + focus_gap * 2.0
paint.focus_bounds.height = track.height + focus_gap * 2.0
paint.show_focus = spec.focused
paint.bounds = bounds
paint.track_bounds = track
paint.has_labels = spec.has_labels
paint.off_label_color = Opacity(spec.track.value.foreground,
spec.track.value.opacity)
paint.on_label_color = Opacity(spec.track.value.foreground,
spec.track.value.opacity)
if spec.has_labels {
active_gap: float = ToggleMetric(spec.active.value.fields,
(u32)StyleGap, spec.active.value.gap, 3.0, scale, true)
active_w: float = (track.width - active_gap * 2.0) * 0.5
active_x: float = track.x + active_gap
if spec.checked { active_x = track.x + track.width - active_w - active_gap }
paint.active_bounds.x = active_x
paint.active_bounds.y = track.y + active_gap
paint.active_bounds.width = active_w
paint.active_bounds.height = track.height - active_gap * 2.0
text_y: float = bounds.y + (bounds.height - (float)spec.font) * 0.5
paint.off_label_bounds.x = bounds.x + bounds.width * 0.25 - (float)spec.off_width * 0.5
paint.off_label_bounds.y = text_y
paint.off_label_bounds.width = (float)spec.off_width
paint.off_label_bounds.height = (float)spec.font
paint.on_label_bounds.x = bounds.x + bounds.width * 0.75 - (float)spec.on_width * 0.5
paint.on_label_bounds.y = text_y
paint.on_label_bounds.width = (float)spec.on_width
paint.on_label_bounds.height = (float)spec.font
if spec.checked {
paint.on_label_color = Opacity(paint.active.value.foreground,
paint.active.value.opacity)
}
else {
paint.off_label_color = Opacity(paint.active.value.foreground,
paint.active.value.opacity)
}
} else {
thumb_size: float = ToggleThumbSizeForStyle(spec.thumb, spec.checked,
scale)
if spec.pressed { thumb_size += 2.0 * scale }
thumb_gap: float = ToggleMetric(spec.thumb.value.fields,
(u32)StyleGap, spec.thumb.value.gap, 4.0, scale, true)
max_thumb: float = track.height - thumb_gap * 2.0
if thumb_size > max_thumb { thumb_size = max_thumb }
thumb_x: float = track.x + thumb_gap
if spec.checked { thumb_x = track.x + track.width - thumb_size - thumb_gap }
thumb_y: float = track.y + (track.height - thumb_size) * 0.5
paint.thumb_radius = thumb_size * 0.5
paint.thumb_x = thumb_x + paint.thumb_radius
paint.thumb_y = thumb_y + paint.thumb_radius
thumb_opacity: float = paint.thumb.value.opacity
paint.thumb_fill_color = Opacity(paint.thumb.value.background,
thumb_opacity)
paint.thumb_edge_color = Opacity(paint.thumb.value.border,
thumb_opacity)
paint.thumb_glow_color = Opacity(paint.thumb.value.focus,
0.36 * thumb_opacity)
paint.thumb_shadow_color = Opacity(paint.thumb.value.border,
(spec.enabled ? 0.36 : 0.18) * thumb_opacity)
paint.thumb_highlight_color = Opacity(paint.thumb.value.foreground,
(spec.enabled ? 0.23 : 0.09) * thumb_opacity)
}
return paint
}