436 lines
15 KiB
Plaintext
436 lines
15 KiB
Plaintext
#module "button"
|
|
#import "style"
|
|
#import "surface"
|
|
#import "control_props"
|
|
#import "button_props"
|
|
#import "input_props"
|
|
#import "paint"
|
|
#import "material"
|
|
#import "ui_text.h"
|
|
#import "ui_core.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: i32) -> i32 #export {
|
|
if direction == 1 {
|
|
return 62
|
|
}
|
|
if direction == 2 {
|
|
return 94
|
|
}
|
|
if direction == 3 {
|
|
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)
|
|
}
|
|
|
|
# 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
|
|
}
|
|
|
|
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
|
|
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
|
|
}
|
|
|
|
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 }
|
|
|
|
half_w: float = spec.bounds.width * 0.5
|
|
half_h: float = spec.bounds.height * 0.5
|
|
inset: float = 6.0 * scale
|
|
border_width: float = spec.face.value.border_width
|
|
focus_width: float = spec.face.value.border_width
|
|
|
|
paint.bounds = spec.bounds
|
|
paint.checker_base = spec.bounds
|
|
paint.checker_a.x = spec.bounds.x
|
|
paint.checker_a.y = spec.bounds.y
|
|
paint.checker_a.width = half_w
|
|
paint.checker_a.height = half_h
|
|
paint.checker_b.x = spec.bounds.x + half_w
|
|
paint.checker_b.y = spec.bounds.y + half_h
|
|
paint.checker_b.width = half_w
|
|
paint.checker_b.height = half_h
|
|
paint.swatch = spec.bounds
|
|
paint.focus_bounds = spec.bounds
|
|
paint.swatch_color = spec.color
|
|
paint.label_x = spec.bounds.x + inset
|
|
paint.radius = spec.face.value.radius
|
|
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 = "" }
|
|
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
|
|
}
|