247 lines
11 KiB
Ziran
247 lines
11 KiB
Ziran
using SegmentedControl :: #import "segmented_control_props";
|
|
Button :: #import "button";
|
|
// Portable compositions use the same widget as retained native hosts.
|
|
#if #defined(KRYON_HOSTED_UI) {
|
|
#import "session"
|
|
host_api :: #system_library "host_api";
|
|
SegmentedControlHostedHost :: (session: Session, props: SegmentedControl.Props,
|
|
options: []SegmentedControl.SegmentOption) -> SegmentedControl.SegmentedControlResult #foreign host_api;
|
|
Draw :: (session: Session, props: SegmentedControl.Props,
|
|
options: []SegmentedControl.SegmentOption) -> SegmentedControl.SegmentedControlResult {
|
|
return SegmentedControlHostedHost(session, props, options)
|
|
}
|
|
} else {
|
|
#import "session"
|
|
#import "control_props"
|
|
#import "font_metrics"
|
|
#import "geometry"
|
|
#import "segmented_control_behavior"
|
|
#import "style"
|
|
#import "style_sheet"
|
|
#import "tree"
|
|
#import "tree_input"
|
|
#import "widget_kind"
|
|
#import "paint_queue"
|
|
#import "drawing_props"
|
|
#import "surface"
|
|
|
|
using WidgetKind;
|
|
using ButtonTone;
|
|
using ButtonEmphasis;
|
|
using ControlSize;
|
|
using ButtonState;
|
|
using StyleField;
|
|
|
|
#program_export
|
|
SegmentedControlDefaultFace :: (kind: s32) -> StyleData {
|
|
face: StyleData = .{.fields = cast(u32)StyleFontSize | cast(u32)StylePaddingX,
|
|
.font_size = 14.0, .padding_x = 10.0}
|
|
if kind == StyleKindSegmentedControl() {
|
|
face.fields = face.fields | cast(u32)StyleGap |
|
|
cast(u32)StyleIconSize | cast(u32)StyleContentOffset
|
|
face.gap = 6.0
|
|
face.icon_size = 30.0
|
|
face.offset_x = 72.0
|
|
face.offset_y = 180.0
|
|
}
|
|
return face
|
|
}
|
|
|
|
#program_export
|
|
SegmentedControlFaceFor :: (props: SegmentedControl.Props,
|
|
kind: s32) -> StyleFrame {
|
|
state: s32 = ButtonStateNormal
|
|
if props.disabled { state = ButtonStateDisabled }
|
|
facts: StyleFacts = StyleControlRoleFacts(kind, props.id,
|
|
props.class_name, StyleAny(),
|
|
cast(s32)ButtonToneNeutral, cast(s32)ButtonEmphasisSoft,
|
|
cast(s32)ControlSizeMedium, state)
|
|
frame: StyleFrame
|
|
frame.value = ResolveInstalledStyleRules(
|
|
SegmentedControlDefaultFace(kind), facts, state)
|
|
return frame
|
|
}
|
|
|
|
#program_export
|
|
SegmentedOptionKey :: (option: SegmentedControl.SegmentOption,
|
|
index: s32) -> u64 {
|
|
if option.key != cast(u64)0 { return option.key }
|
|
return cast(u64)(index + 1)
|
|
}
|
|
|
|
#program_export
|
|
SegmentedControlMetricsFor :: (props: SegmentedControl.Props) -> SegmentedMetrics {
|
|
control: StyleFrame = SegmentedControlFaceFor(props,
|
|
StyleKindSegmentedControl())
|
|
segment: StyleFrame = SegmentedControlFaceFor(props,
|
|
StyleKindSegment())
|
|
return SegmentedDefaultMetrics(props.height,
|
|
props.min_item_width, props.max_item_width,
|
|
props.scale, control, segment)
|
|
}
|
|
|
|
#program_export
|
|
SegmentedControlHeight :: (session: Session, props: SegmentedControl.Props,
|
|
options: []SegmentedControl.SegmentOption) -> s32 {
|
|
metrics: SegmentedMetrics = SegmentedControlMetricsFor(props)
|
|
if (cast(s32)options.count) <= 0 || metrics.row_height <= 0 { return 0 }
|
|
bounds: Rectangle = props.bounds
|
|
if bounds.width <= 0.0 { bounds.width = TreeCurrentParentBounds(session).width }
|
|
if bounds.width <= 0.0 || !props.wrap { return metrics.row_height }
|
|
segment: StyleFrame = SegmentedControlFaceFor(props,
|
|
StyleKindSegment())
|
|
font: s32 = ResolveFont(0,
|
|
StyleFontValue(segment.value.fields,
|
|
segment.value.font_size), 14)
|
|
row_width: s32 = 0
|
|
rows: s32 = 1
|
|
index: s32 = 0
|
|
while index < (cast(s32)options.count) {
|
|
item_width: s32 = SegmentedItemWidth(
|
|
GlyphWidth(options[index].label, font,
|
|
segment.value.typeface), metrics)
|
|
advance: SegmentedRowAdvance = SegmentedRowAdvanceFor(true,
|
|
row_width, 0, item_width, cast(s32)bounds.width, metrics)
|
|
if advance.wrap_before { rows += 1 }
|
|
row_width = advance.row_width
|
|
index += 1
|
|
}
|
|
return SegmentedHeightForRows(rows, metrics.row_height,
|
|
metrics.gap)
|
|
}
|
|
|
|
// A segment is a checked Button with the Segment style kind. Option keys
|
|
// preserve identity when caller owned options move between rows or positions.
|
|
Draw :: (session: Session, props: SegmentedControl.Props,
|
|
options: []SegmentedControl.SegmentOption) -> SegmentedControl.SegmentedControlResult {
|
|
result: SegmentedControl.SegmentedControlResult = .{.node = -1, .clicked_index = -1, .selected_index = -1}
|
|
if props.has_selection { result.selected_index = props.selected_index }
|
|
result.height = SegmentedControlHeight(session, props, options)
|
|
bounds: Rectangle = props.bounds
|
|
if bounds.width <= 0.0 { bounds.width = TreeCurrentParentBounds(session).width }
|
|
if (cast(s32)options.count) <= 0 || bounds.width <= 0.0 ||
|
|
result.height <= 0 { return result }
|
|
bounds.height = cast(float32)result.height
|
|
key: u64 = props.key
|
|
if key == cast(u64)0 && props.id > 0 { key = cast(u64)cast(u32)props.id }
|
|
if key == cast(u64)0 { key = TreeNextKey(session) }
|
|
if TreeIsBuilding(session) {
|
|
previous_control: s32 = TreePrevious(session,
|
|
TreeCurrentParent(session), key, WidgetKindSegmentedControl)
|
|
index: s32 = 0
|
|
while index < (cast(s32)options.count) {
|
|
if !props.disabled && !options[index].disabled {
|
|
previous: s32 = TreePreviousCommitted(session,
|
|
previous_control,
|
|
SegmentedOptionKey(options[index], index),
|
|
WidgetKindButton)
|
|
if TreeTakeActivationAt(session, previous) {
|
|
result.clicked_index = index
|
|
}
|
|
}
|
|
index += 1
|
|
}
|
|
}
|
|
selection: SegmentedSelectionResult = SegmentedSelectionFor(
|
|
result.selected_index, result.clicked_index,
|
|
props.has_selection)
|
|
result.selected_index = selection.selected_index
|
|
result.changed = selection.changed
|
|
if TreeIsBuilding(session) {
|
|
result.node = TreeSubmitCurrent(session, key,
|
|
WidgetKindSegmentedControl, bounds)
|
|
if result.node < 0 { return result }
|
|
TreeSetStyleClass(session, result.node, props.class_name)
|
|
TreeSetStyleId(session, result.node, props.id)
|
|
bounds = TreeSubmittedNodeAt(session, result.node).bounds
|
|
}
|
|
metrics: SegmentedMetrics = SegmentedControlMetricsFor(props)
|
|
// A single-row control is one outlined group, as the theme styles it;
|
|
// the outline is painted after the segments so the selection sits in it.
|
|
control: StyleFrame = SegmentedControlFaceFor(props,
|
|
StyleKindSegmentedControl())
|
|
control_opacity: float32 = StyleOpacityValue(control.value.fields,
|
|
control.value.opacity)
|
|
outlined: bool = !props.wrap && (control.value.fields &
|
|
cast(u32)StyleBorderWidth) != cast(u32)0 &&
|
|
control.value.border_width > 0.0
|
|
if !props.wrap && (control.value.fields & cast(u32)StyleBackground) != cast(u32)0 {
|
|
PaintRoundedFill(session, result.node, bounds, control.value.radius, 12,
|
|
ColorFromPacked(Opacity(control.value.background, control_opacity)))
|
|
}
|
|
segment: StyleFrame = SegmentedControlFaceFor(props,
|
|
StyleKindSegment())
|
|
font: s32 = ResolveFont(0,
|
|
StyleFontValue(segment.value.fields,
|
|
segment.value.font_size), 14)
|
|
row_start: s32 = 0
|
|
row_width: s32 = 0
|
|
row_count: s32 = 0
|
|
row_y: s32 = cast(s32)bounds.y
|
|
index: s32 = 0
|
|
while index <= (cast(s32)options.count) {
|
|
end_row: bool = index == (cast(s32)options.count)
|
|
advance: SegmentedRowAdvance
|
|
if !end_row {
|
|
item_width: s32 = SegmentedItemWidth(
|
|
GlyphWidth(options[index].label, font,
|
|
segment.value.typeface), metrics)
|
|
advance = SegmentedRowAdvanceFor(props.wrap, row_width,
|
|
row_count, item_width, cast(s32)bounds.width, metrics)
|
|
if !advance.wrap_before {
|
|
row_width = advance.row_width
|
|
row_count = advance.row_count
|
|
index += 1
|
|
continue
|
|
}
|
|
}
|
|
if row_count > 0 {
|
|
row: SegmentedRow = SegmentedRowFor(bounds.x,
|
|
bounds.width, row_y, row_start, row_count,
|
|
row_width, props.wrap, metrics)
|
|
item_x: s32 = row.x
|
|
row_item: s32 = 0
|
|
while row_item < row_count {
|
|
item_index: s32 = row_start + row_item
|
|
option: SegmentedControl.SegmentOption = options[item_index]
|
|
button: Button.Props
|
|
button.key = SegmentedOptionKey(option, item_index)
|
|
button.bounds = Rectangle.{cast(float32)item_x,
|
|
cast(float32)row_y, cast(float32)row.button_width,
|
|
cast(float32)metrics.row_height}
|
|
button.label = option.label
|
|
button.id = SegmentedFocusIdFor(props.id, item_index)
|
|
button.class_name = props.class_name
|
|
button.style_kind = StyleKindSegment()
|
|
button.tone = cast(ButtonTone)ButtonToneNeutral
|
|
if item_index == result.selected_index {
|
|
button.tone = cast(ButtonTone)ButtonToneAccent
|
|
}
|
|
button.emphasis = cast(ButtonEmphasis)ButtonEmphasisSoft
|
|
button.selected = item_index == result.selected_index
|
|
button.disabled = props.disabled || option.disabled
|
|
Button.SubmitButton(session, button, result.node)
|
|
item_x += row.button_width + metrics.gap
|
|
row_item += 1
|
|
}
|
|
row_y += metrics.row_height + metrics.gap
|
|
}
|
|
if end_row {
|
|
row_width = 0
|
|
row_count = 0
|
|
} else {
|
|
row_start = index
|
|
row_width = advance.row_width
|
|
row_count = advance.row_count
|
|
}
|
|
index += 1
|
|
}
|
|
if outlined {
|
|
PaintRoundedOutline(session, result.node, bounds, control.value.radius,
|
|
12, control.value.border_width,
|
|
ColorFromPacked(Opacity(control.value.border, control_opacity)))
|
|
}
|
|
return result
|
|
}
|
|
}
|