Files
kryon/runtime/button.kry
T

914 lines
37 KiB
Plaintext

#module "button"
#import "style"
#import "theme"
#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
}
# Widget defaults produce the shared Style value. Hosts adapt theme records;
# they must not independently assemble button colors, sizing, or presence bits.
DefaultButtonStyle :: (tone: i32, emphasis: i32, state: i32, size: i32,
pill: bool, circle: bool, palette: Palette, metrics: Metrics) -> StyleData #export {
style: StyleData
style.background = ButtonBackground(tone, emphasis, state, palette.surface,
palette.accent, palette.accent_hover, palette.accent_pressed,
palette.surface_raised, palette.danger, palette.success, palette.warning)
style.foreground = ButtonForeground(tone, emphasis, state, palette.surface,
palette.on_accent, palette.text, palette.danger, palette.on_danger,
palette.success, palette.on_success, palette.warning, palette.on_warning,
palette.link, palette.text_disabled, palette.accent)
style.border = ButtonBorder(tone, emphasis, state, palette.surface,
palette.accent, palette.surface_raised, palette.danger, palette.success, palette.warning)
style.focus = ButtonFocus(tone, emphasis, palette.surface, palette.focus,
palette.success)
radius: float = metrics.radius_medium
# Standard resting faces are softer than the compact interaction edge.
# Small and large size variants retain their own compact silhouette.
if state == ButtonStateNormal || state == ButtonStateHover {
radius = SizeValue(size, metrics.radius_medium,
(metrics.radius_medium + metrics.radius_large) * 0.5, metrics.radius_medium)
}
# Resting outlines have a softer silhouette. Focus and press use the
# compact edge; Style transitions interpolate this change with the face.
if emphasis == ButtonEmphasisOutline &&
(state == ButtonStateNormal || state == ButtonStateHover) {
radius = SizeValue(size, metrics.radius_medium, metrics.radius_large, metrics.radius_large)
}
style.radius = ShapeRadius(radius, metrics.radius_pill, pill, circle)
style.border_width = metrics.border_width
style.opacity = 1.0
style.padding_x = SizeValue(size, metrics.control_padding_small,
metrics.control_padding_medium, metrics.control_padding_large)
style.padding_y = 8.0
style.gap = metrics.control_gap
style.font_size = SizeValue(size, metrics.font_size_small,
metrics.font_size_medium, metrics.font_size_large)
style.icon_size = SizeValue(size, metrics.icon_size_small,
metrics.icon_size_medium, metrics.icon_size_large)
# All basic fields are present; the optional gradient endpoint is absent.
style.material = 0
style.fields = DefaultFields()
if size == ControlSizeLarge && !LightSurface(palette.surface) {
style.typeface = "semibold"
style.fields = style.fields | (u32)StyleTypeface
}
return style
}
CanActivate :: (disabled: bool, loading: bool) -> bool #export {
return !disabled && !loading
}
# One resolution path for every host: effective state selects both the
# material defaults and the matching override from the same style record.
ResolveAppearance :: (tone: i32, emphasis: i32, state: i32, size: i32,
pill: bool, circle: bool, disabled: bool, loading: bool, selected: bool,
palette: Palette, metrics: Metrics, styles: StyleStates) -> StyleData #export {
flags: StateFlags = ResolveFlags(state, disabled, loading, selected)
effective: i32 = ResolveState(state, flags.disabled, flags.loading, false, false, false, flags.selected)
base: StyleData = DefaultButtonStyle(tone, emphasis, effective, size,
pill, circle, palette, metrics)
return ResolveValues(base, styles, effective)
}
# Layout and paint can consume one resolved frame. The host supplies motion
# amounts, but state override selection and transition eligibility live here.
ResolveFrame :: (tone: i32, emphasis: i32, state: i32, size: i32,
pill: bool, circle: bool, disabled: bool, loading: bool, selected: bool,
palette: Palette, metrics: Metrics, styles: StyleStates,
automatic: bool, h: float, p: float, f: float) -> StyleFrame #export {
flags: StateFlags = ResolveFlags(state, disabled, loading, selected)
frame: StyleFrame
frame.value = ResolveAppearance(tone, emphasis, state, size, pill, circle,
disabled, loading, selected, palette, metrics, styles)
frame.fill = FillState(frame.value.fields, frame.value.background, frame.value.background_end)
if automatic && !flags.disabled && !flags.loading && !flags.selected {
if h <= 0.0 && p <= 0.0 && f <= 0.0 { return frame }
normal: StyleData = frame.value
if state != ButtonStateNormal {
normal = ResolveAppearance(tone, emphasis, ButtonStateNormal, size,
pill, circle, false, false, false, palette, metrics, styles)
}
hover: StyleData = normal
press: StyleData = normal
focus: StyleData = normal
if h > 0.0 {
hover = ResolveAppearance(tone, emphasis, ButtonStateHover, size,
pill, circle, false, false, false, palette, metrics, styles)
}
if p > 0.0 {
press = ResolveAppearance(tone, emphasis, ButtonStatePressed, size,
pill, circle, false, false, false, palette, metrics, styles)
}
if f > 0.0 {
focus = ResolveAppearance(tone, emphasis, ButtonStateFocus, size,
pill, circle, false, false, false, palette, metrics, styles)
}
frame = TransitionFrame(frame.value, normal, hover, press, focus, h, p, f)
}
return frame
}
# 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
palette: Palette
metrics: Metrics
}
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.metrics.border_width
focus_width: float = spec.metrics.focus_width
if border_width <= 0.0 { border_width = 1.0 }
if focus_width <= 0.0 { focus_width = 1.0 }
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.metrics.radius_medium
paint.border_width = border_width
paint.focus_width = focus_width
paint.checker_base_color = PackedColor((u32)0xB4B4B4, (u32)255)
paint.checker_alt_color = PackedColor((u32)0xDCDCDC, (u32)255)
paint.border_color = spec.hovered ? spec.palette.accent_hover : spec.palette.border
paint.focus_color = spec.palette.focus
paint.text_color = spec.disabled ? spec.palette.text_disabled : spec.palette.text
paint.show_focus = !spec.disabled && spec.focused
if spec.disabled {
paint.border_color = spec.palette.border
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(props.font, style_font, fallback_font)
result.foreground = Opacity(appearance.value.foreground, appearance.value.opacity)
result.repaint = motion.active || (props.loading && !props.disabled)
return result
}
# Resolve an animated frame from host theme data. Input is sampled separately
# so retained painting can reuse it without consuming activation twice.
AdvanceFrame :: (key: u64, props: ButtonProps, input: ButtonInput,
palette: Palette, metrics: Metrics, styles: StyleStates, enabled: bool,
delta_ms: float, surface: Rectangle, ambient: u32, scale: float,
fallback_font: i32) -> ButtonFrame #export {
props.disabled = input.flags.disabled
props.loading = input.flags.loading
props.selected = input.flags.selected
motion: InteractionMotion = AdvanceButtonMotion(key, (i32)props.state, input, enabled,
delta_ms, metrics.transition_normal_ms, metrics.transition_fast_ms)
appearance: StyleFrame = ResolveFrame((i32)props.tone, (i32)props.emphasis,
input.interaction.state, (i32)props.size, props.pill, props.circle,
props.disabled, props.loading, props.selected, palette, metrics, styles,
(i32)props.state == ButtonStateAuto, motion.hover.value,
motion.press.value, motion.focus.value)
return BuildFrame(props, input, appearance, motion, surface, ambient, scale,
(i32)((float)(i32)appearance.value.font_size * scale + 0.5), fallback_font)
}
# 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
}
MixByte :: (from: u32, to: u32, amount: u32) -> u32 #export {
return (from * ((u32)100 - amount) + to * amount + (u32)50) / (u32)100
}
MixColor :: (from: u32, to: u32, amount: u32) -> u32 #export {
fr: u32 = (from >> (u32)24) & (u32)255
fg: u32 = (from >> (u32)16) & (u32)255
fb: u32 = (from >> (u32)8) & (u32)255
fa: u32 = from & (u32)255
tr: u32 = (to >> (u32)24) & (u32)255
tg: u32 = (to >> (u32)16) & (u32)255
tb: u32 = (to >> (u32)8) & (u32)255
ta: u32 = to & (u32)255
return (MixByte(fr, tr, amount) << (u32)24) |
(MixByte(fg, tg, amount) << (u32)16) |
(MixByte(fb, tb, amount) << (u32)8) |
MixByte(fa, ta, amount)
}
ToneColor :: (
tone: i32,
accent: u32,
neutral: u32,
danger: u32,
success: u32,
warning: u32
) -> u32 #export {
if tone == ButtonToneAccent { return accent }
if tone == ButtonToneDanger { return danger }
if tone == ButtonToneSuccess { return success }
if tone == ButtonToneWarning { return warning }
return neutral
}
LightSurface :: (color: u32) -> bool #export {
r: u32 = (color >> (u32)24) & (u32)255
g: u32 = (color >> (u32)16) & (u32)255
b: u32 = (color >> (u32)8) & (u32)255
return r * (u32)299 + g * (u32)587 + b * (u32)114 > (u32)150000
}
ButtonBackground :: (
tone: i32,
emphasis: i32,
state: i32,
surface: u32,
accent: u32,
accent_hover: u32,
accent_pressed: u32,
neutral: u32,
danger: u32,
success: u32,
warning: u32
) -> u32 #export {
base: u32 = ToneColor(tone, accent, neutral, danger, success, warning)
background: u32 = base
light: bool = LightSurface(surface)
# Dark semantic controls carry their color in the edge and light field;
# the resting face stays deep rather than becoming a flat signal swatch.
if !light && emphasis == ButtonEmphasisFilled && tone != ButtonToneAccent && tone != ButtonToneNeutral {
background = MixColor(surface, base, 32)
if tone == ButtonToneDanger { background = MixColor(surface, base, 57) }
if tone == ButtonToneWarning { background = MixColor(surface, base, 18) }
}
if light && emphasis == ButtonEmphasisFilled && tone != ButtonToneAccent {
background = MixColor(surface, base, 16)
if tone == ButtonToneDanger || tone == ButtonToneWarning {
amount: u32 = (u32)18
# Available warning material carries a little more body color;
# unavailable faces retain their quieter tint.
if tone == ButtonToneWarning && state != ButtonStateDisabled { amount = (u32)20 }
background = MixColor(surface, ChromaColor(base, (u32)255), amount)
}
if tone == ButtonToneSuccess {
background = MixColor(surface, ChromaColor(base, (u32)200), 20)
}
}
if emphasis == ButtonEmphasisSoft {
background = MixColor(surface, base, 14)
if light { background = MixColor(surface, base, 6) }
if light && tone == ButtonToneNeutral { background = base }
if !light && tone == ButtonToneNeutral { background = MixColor(surface, base, 85) }
}
if emphasis == ButtonEmphasisOutline {
background = surface
if light {
# A trace of the outline tone gives pale material a visible body;
# it must not make an explicitly transparent surface opaque.
tinted: u32 = MixColor(surface, base, 4)
background = ((tinted >> (u32)8) << (u32)8) | (surface & (u32)255)
}
}
if emphasis == ButtonEmphasisGhost { background = surface }
if emphasis == ButtonEmphasisLink { background = surface }
if state == ButtonStateHover {
# Neutral raised material catches the same cool light as the accent;
# mixing it with its own base barely changes the resting surface.
if !light && tone == ButtonToneNeutral && emphasis == ButtonEmphasisSoft {
# Keep the cool reflection restrained so neutral material does
# not turn into another saturated accent face on hover.
return MixColor(background, accent_hover, 12)
}
if !light && emphasis == ButtonEmphasisFilled && tone != ButtonToneAccent && tone != ButtonToneNeutral {
# Retain a deep material body; the surface policy supplies the
# luminous lower edge instead of flooding the whole face.
if tone == ButtonToneDanger { return MixColor(surface, base, 65) }
if tone == ButtonToneWarning { return MixColor(surface, base, 36) }
return MixColor(surface, base, 58)
}
if tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
# Keep the pale hover body chromatic; inward surface illumination
# supplies the highlight rather than washing out the base color.
if light { return MixColor(accent_hover, surface, 46) }
# A small body lift leaves room for the lower surface reflection;
# using the full hover tint makes the entire face a flat bright tile.
return MixColor(accent, accent_hover, 15)
}
if light {
# Light material catches illumination on hover. Mixing a pastel
# face toward its dark label tone instead makes it look dirty.
if emphasis == ButtonEmphasisGhost {
# A borderless face needs a little body tint to remain visible
# as the hover reflection lifts its lower shading.
tinted: u32 = MixColor(surface, base, 5)
return ((tinted >> (u32)8) << (u32)8) | (surface & (u32)255)
}
if emphasis == ButtonEmphasisOutline || emphasis == ButtonEmphasisLink {
return MixColor(surface, base, 3)
}
return MixColor(background, surface, 16)
}
return MixColor(background, base, 8)
}
if state == ButtonStatePressed {
if light {
# Press removes elevation in the surface policy; preserve each
# emphasis here so an outline does not become a filled button.
if tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
return MixColor(surface, MixColor(base, accent_pressed, 30), 18)
}
if emphasis == ButtonEmphasisOutline || emphasis == ButtonEmphasisLink {
return MixColor(surface, base, 3)
}
if tone == ButtonToneDanger && emphasis == ButtonEmphasisFilled {
return MixColor(background, surface, 20)
}
return background
}
if emphasis == ButtonEmphasisFilled && tone != ButtonToneNeutral && tone != ButtonToneAccent {
if tone == ButtonToneWarning { return MixColor(surface, base, 10) }
return MixColor(surface, base, 18)
}
if tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
return MixColor(surface, accent_pressed, 22)
}
if emphasis == ButtonEmphasisOutline {
# Press adds a restrained tint inside the outline; its inner
# reflection supplies depth without turning it into a filled face.
return MixColor(background, base, 12)
}
return MixColor(background, base, 14)
}
if light && state == ButtonStateFocus && tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
# The focus edge carries the accent; its pale face keeps only a trace.
return MixColor(surface, base, 10)
}
if !light && state == ButtonStateFocus && tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
return accent_pressed
}
if !light && state == ButtonStateFocus && tone == ButtonToneNeutral && emphasis == ButtonEmphasisSoft {
# A neutral face picks up a little diffuse light under focus, while
# the surface supplies the sharper blue edge. Keep its own alpha.
white: u32 = ((u32)16777215 << (u32)8) | (background & (u32)255)
return MixColor(background, white, 5)
}
if !light && state == ButtonStateFocus && tone == ButtonToneDanger && emphasis == ButtonEmphasisFilled {
return MixColor(surface, base, 38)
}
if !light && state == ButtonStateFocus && tone == ButtonToneWarning && emphasis == ButtonEmphasisFilled {
return MixColor(surface, base, 22)
}
if state == ButtonStateFocus && emphasis == ButtonEmphasisLink {
# A focused link gathers a faint trace of its tone in the face;
# the borderless ghost remains neutral and the focus rim stays separate.
tinted: u32 = MixColor(surface, base, 8)
return ((tinted >> (u32)8) << (u32)8) | (surface & (u32)255)
}
if state == ButtonStateSelected { return MixColor(background, base, 12) }
if state == ButtonStateLoading {
if light { return MixColor(surface, background, 4) }
if emphasis == ButtonEmphasisFilled && tone != ButtonToneAccent && tone != ButtonToneNeutral {
# Loading material uses one subdued tint from its source hue;
# dimming an already subdued face twice erases semantic color.
return MixColor(surface, base, 14)
}
return MixColor(surface, background, 18)
}
if state == ButtonStateDisabled {
if light && tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
return MixColor(surface, background, 8)
}
if !light && tone == ButtonToneAccent && emphasis == ButtonEmphasisFilled {
return MixColor(surface, background, 20)
}
return MixColor(surface, background, 45)
}
return background
}
ButtonForeground :: (
tone: i32,
emphasis: i32,
state: i32,
surface: u32,
on_accent: u32,
text: u32,
danger: u32,
on_danger: u32,
success: u32,
on_success: u32,
warning: u32,
on_warning: u32,
link: u32,
disabled_text: u32,
accent: u32
) -> u32 #export {
if state == ButtonStateDisabled {
if LightSurface(surface) {
if tone != ButtonToneNeutral {
amount: u32 = (u32)70
if tone == ButtonToneDanger { amount = (u32)55 }
return MixColor(disabled_text, ToneColor(tone, link, text, danger, success, warning), amount)
}
return MixColor(disabled_text, text, 35)
}
# Unavailable controls retain readable labels. The subdued material
# and disabled input policy communicate state without erasing text.
if emphasis == ButtonEmphasisLink { return MixColor(link, text, 35) }
return MixColor(disabled_text, text, 90)
}
if state == ButtonStateLoading { return ToneColor(tone, accent, text, danger, success, warning) }
if LightSurface(surface) {
# Pale semantic faces need deeper ink than their border and activity
# colors. Darken within the tone without changing its supplied alpha.
success = MixColor(success, success & (u32)255, 50)
warning = MixColor(warning, warning & (u32)255, 60)
}
if emphasis == ButtonEmphasisLink { return link }
if emphasis != ButtonEmphasisFilled {
if !LightSurface(surface) && tone == ButtonToneAccent { return text }
return ToneColor(tone, link, text, danger, success, warning)
}
if LightSurface(surface) && tone != ButtonToneAccent {
return ToneColor(tone, link, text, danger, success, warning)
}
if LightSurface(surface) && (state == ButtonStatePressed || state == ButtonStateHover || state == ButtonStateFocus) { return link }
if tone == ButtonToneDanger { return on_danger }
if tone == ButtonToneSuccess { return on_success }
if tone == ButtonToneWarning {
if state == ButtonStatePressed {
# Pressed amber material warms its ink without changing opacity.
warm: u32 = ((warning >> (u32)8) << (u32)8) | (on_warning & (u32)255)
return MixColor(on_warning, warm, 15)
}
return on_warning
}
if tone == ButtonToneNeutral { return text }
return on_accent
}
# Focus stays distinct from the material border. Light low-emphasis controls
# use a quiet edge; dark success controls retain their luminous green edge.
# ResolveAppearance merges explicit styles, including transparent focus.
ButtonFocus :: (tone: i32, emphasis: i32, surface: u32,
focus: u32, success: u32) -> u32 #export {
alpha: u32 = focus & (u32)255
if !LightSurface(surface) {
if tone == ButtonToneSuccess {
luminous: u32 = ChromaColor(success, (u32)255)
return ((luminous >> (u32)8) << (u32)8) | alpha
}
return focus
}
if emphasis == ButtonEmphasisSoft || emphasis == ButtonEmphasisGhost || emphasis == ButtonEmphasisLink {
alpha = alpha * (u32)18 / (u32)100
}
if tone == ButtonToneSuccess {
return ((success >> (u32)8) << (u32)8) | alpha
}
return ((focus >> (u32)8) << (u32)8) | alpha
}
ButtonBorder :: (
tone: i32,
emphasis: i32,
state: i32,
surface: u32,
accent: u32,
neutral: u32,
danger: u32,
success: u32,
warning: u32
) -> u32 #export {
if emphasis == ButtonEmphasisGhost { return (u32)0 }
if emphasis == ButtonEmphasisLink { return (u32)0 }
base: u32 = ToneColor(tone, accent, neutral, danger, success, warning)
if LightSurface(surface) && emphasis == ButtonEmphasisFilled && tone != ButtonToneAccent {
base = MixColor(surface, base, 28)
}
if state == ButtonStateDisabled {
# Colored disabled rims recede into the surrounding surface. Neutral
# material already has low contrast and retains its quiet boundary.
amount: u32 = (u32)25
if tone == ButtonToneNeutral { amount = (u32)45 }
return MixColor(surface, base, amount)
}
if LightSurface(surface) && state == ButtonStatePressed && tone == ButtonToneAccent {
# A pressed pale face loses the resting luminous rim. Its recessed
# edge retains the accent hue without looking like a focus outline.
return MixColor(surface, base, 25)
}
if state == ButtonStateLoading && LightSurface(surface) {
# Activity belongs to the spinner. The light surface keeps a quiet
# hairline, with outline controls retaining slightly more definition.
if emphasis == ButtonEmphasisOutline { return MixColor(surface, base, 25) }
return MixColor(surface, base, 12)
}
if state == ButtonStateLoading && !LightSurface(surface) {
# Neutral and outlined activity surfaces have no hard rim. Filled
# chromatic material retains a subdued edge to contain its light.
border: u32 = MixColor(surface, base, 16)
if tone == ButtonToneNeutral || emphasis == ButtonEmphasisOutline {
return Opacity(border, 0.0)
}
return border
}
if emphasis == ButtonEmphasisSoft {
if LightSurface(surface) {
# Neutral pale material reflects the accent at its edge instead
# of outlining itself in white. Semantic tones keep their hue.
if tone == ButtonToneNeutral { base = accent }
return MixColor(surface, base, 14)
}
# Raised neutral material catches the accent field on hover. Its
# resting edge stays quiet; the shared Style transition fades the tint.
if tone == ButtonToneNeutral && state == ButtonStateHover {
return MixColor(base, accent, 45)
}
return MixColor(surface, base, 28)
}
return base
}