Files
kryon/runtime/text_input.kry
T

1557 lines
45 KiB
Plaintext

#module "text_input"
#import "control_props"
#import "drawing_props"
#import "style_sheet"
TextInputMetrics :: struct {
font: i32
padding_x: i32
padding_y: i32
line_gap: i32
line_height: i32
}
TextInputResolvedStyle :: struct {
fields: u32
padding_x: i32
padding_y: i32
line_gap: i32
radius: float
}
TextInputFactsFor :: (style_kind: i32, class_name: i32) -> StyleFacts #export {
return StyleControlFacts(style_kind, 0, class_name, ButtonToneNeutral,
ButtonEmphasisSoft, ControlSizeMedium, ButtonStateNormal)
}
TextFieldScroll :: struct {
scroll: i32
max_scroll: i32
clip_width: i32
text_origin_x: i32
}
TextFieldPaint :: struct {
clip_bounds: Rectangle
text_x: i32
cursor_y: i32
cursor_height: i32
}
TextAreaPaint :: struct {
clip_bounds: Rectangle
wrap_width: i32
viewport_height: i32
max_scroll: i32
scroll_y: i32
placeholder_x: i32
placeholder_y: i32
}
TextAreaGutterMetrics :: struct {
top_inset: i32
active_y_inset: i32
label_x_inset: i32
label_font: i32
extra_rows: i32
}
TextNavigationDecision :: struct {
consumed: bool
collapse_selection_start: bool
collapse_selection_end: bool
char_direction: i32
word_direction: i32
document_edge: i32
line_edge: i32
vertical_direction: i32
page_direction: i32
extend_selection: bool
}
TextDeleteDecision :: struct {
consumed: bool
char_direction: i32
word_direction: i32
document_edge: i32
}
TextInsertDecision :: struct {
accept: bool
skip: bool
stop: bool
}
TextBufferInsertDecision :: struct {
can_insert: bool
tail_count: i32
}
TextBufferDeleteDecision :: struct {
can_delete: bool
tail_count: i32
}
TextBufferDeleteRangeDecision :: struct {
can_delete: bool
start: i32
end: i32
cursor: i32
tail_count: i32
}
TextBufferBracketDecision :: struct {
valid: bool
pos: i32
open: i32
close: i32
direction: i32
}
TextContextMenuState :: struct {
cut_enabled: bool
copy_enabled: bool
paste_enabled: bool
select_all_enabled: bool
}
TextContextCommandDecision :: struct {
copy_selection: bool
copy_all: bool
delete_selection: bool
clear_all: bool
paste: bool
select_all: bool
collapse_selection: bool
}
TextInputDoubleClickDecision :: struct {
double_click: bool
}
TextCompositionInputDecision :: struct {
accept_events: bool
cancel: bool
drain_events: bool
}
TextCompositionSessionDecision :: struct {
visible: bool
cancel: bool
}
TextCompositionPhaseDecision :: struct {
store_preedit: bool
commit: bool
cancel: bool
}
TextCompositionApplyDecision :: struct {
text_changed: bool
presentation_changed: bool
selection_changed: bool
}
TextCompositionViewRange :: struct {
replace_start: i32
replace_end: i32
cursor: i32
selection_start: i32
selection_end: i32
composition_start: i32
composition_end: i32
}
TextCompositionPaintSpan :: struct {
visible: bool
start: i32
end: i32
}
TextSelectionPaintSpan :: struct {
visible: bool
start: i32
end: i32
continues_past_line: bool
}
TextFieldPanDecision :: struct {
pan: bool
}
TextFocusClaimDecision :: struct {
claim: bool
displace_previous: bool
cancel_previous: bool
close_context: bool
clear_peer_selection: bool
}
TextFocusReleaseDecision :: struct {
release: bool
cancel_self: bool
clear_owner: bool
clear_frame_owner: bool
clear_active_focus: bool
close_context: bool
clear_field_drag: bool
clear_area_drag: bool
}
TextFocusOwnerDecision :: struct {
focused: bool
adopt_owner: bool
mark_frame_owner: bool
clear_target: bool
}
TextPlatformInputSyncDecision :: struct {
active: bool
call_callback: bool
callback_active: bool
clear_show_request: bool
}
TextFocusClaimDecisionFor :: (has_target: bool, has_previous_owner: bool,
same_owner: bool) -> TextFocusClaimDecision #export {
decision: TextFocusClaimDecision
decision.claim = has_target
if !has_target {
return decision
}
decision.displace_previous = has_previous_owner && !same_owner
decision.cancel_previous = !same_owner
decision.close_context = !same_owner
decision.clear_peer_selection = !same_owner
return decision
}
TextFocusOwnerDecisionFor :: (has_target: bool, owns_focus: bool,
has_frame_owner: bool, target_flag: bool,
has_focus_owner: bool) -> TextFocusOwnerDecision #export {
decision: TextFocusOwnerDecision
if !has_target {
return decision
}
if owns_focus {
decision.focused = true
decision.mark_frame_owner = true
return decision
}
if has_frame_owner {
decision.clear_target = true
return decision
}
if target_flag && !has_focus_owner {
decision.focused = true
decision.adopt_owner = true
decision.mark_frame_owner = true
return decision
}
decision.clear_target = true
return decision
}
TextFocusOwnerIsStale :: (has_owner: bool, owner_frame: i64,
current_frame: i64) -> bool #export {
return has_owner && owner_frame != current_frame - 1 &&
owner_frame != current_frame
}
TextPlatformInputActiveFor :: (requested: bool, has_owner: bool,
owner_rendered_this_frame: bool) -> bool #export {
return requested || (has_owner && owner_rendered_this_frame)
}
TextPlatformInputSyncDecisionFor :: (show_requested: bool,
has_callback: bool, platform_active: bool,
text_input_active: bool) -> TextPlatformInputSyncDecision #export {
decision: TextPlatformInputSyncDecision
decision.clear_show_request = true
if show_requested && has_callback {
decision.active = true
decision.call_callback = true
decision.callback_active = true
return decision
}
decision.active = text_input_active
if platform_active != text_input_active {
decision.call_callback = has_callback
decision.callback_active = text_input_active
}
return decision
}
TextFocusReleaseDecisionFor :: (has_target: bool, owns_focus: bool,
owns_frame_focus: bool, active_focus_matches: bool,
owns_field_drag: bool, owns_area_drag: bool) -> TextFocusReleaseDecision #export {
decision: TextFocusReleaseDecision
decision.release = has_target
if !has_target {
return decision
}
decision.cancel_self = owns_focus
decision.clear_owner = owns_focus
decision.clear_frame_owner = owns_frame_focus
decision.clear_active_focus = active_focus_matches
decision.close_context = true
decision.clear_field_drag = owns_field_drag
decision.clear_area_drag = owns_area_drag
return decision
}
TextCompositionInputDecisionFor :: (focused: bool, read_only: bool) -> TextCompositionInputDecision #export {
decision: TextCompositionInputDecision
decision.accept_events = focused && !read_only
decision.cancel = !decision.accept_events
decision.drain_events = focused && read_only
return decision
}
TextCompositionCancelDecisionFor :: (has_owner: bool, scoped: bool,
same_owner: bool) -> TextCompositionSessionDecision #export {
decision: TextCompositionSessionDecision
decision.cancel = has_owner && (!scoped || same_owner)
return decision
}
TextCompositionGetDecisionFor :: (has_owner: bool, same_owner: bool,
has_text: bool) -> TextCompositionSessionDecision #export {
decision: TextCompositionSessionDecision
decision.visible = has_owner && same_owner && has_text
return decision
}
TextCompositionPhaseDecisionFor :: (phase: i32) -> TextCompositionPhaseDecision #export {
decision: TextCompositionPhaseDecision
decision.store_preedit = phase == 1 || phase == 2
decision.commit = phase == 3
decision.cancel = phase == 4
return decision
}
TextCompositionApplyDecisionFor :: (phase: i32,
text_changed: bool) -> TextCompositionApplyDecision #export {
event: TextCompositionPhaseDecision = TextCompositionPhaseDecisionFor(phase)
decision: TextCompositionApplyDecision
if event.store_preedit || event.cancel {
decision.presentation_changed = true
return decision
}
if event.commit {
decision.text_changed = text_changed
decision.presentation_changed = true
decision.selection_changed = true
}
return decision
}
TextCompositionSelectionLength :: (preedit_len: i32, cursor: i32,
selection_length: i32) -> i32 #export {
if preedit_len < 0 { preedit_len = 0 }
if cursor < 0 { cursor = 0 }
if cursor > preedit_len { cursor = preedit_len }
if selection_length < 0 { selection_length = 0 }
max_length: i32 = preedit_len - cursor
if selection_length > max_length { return max_length }
return selection_length
}
TextCompositionViewRangeFor :: (selection_start: i32, selection_end: i32,
preedit_len: i32, preedit_cursor: i32,
preedit_selection_end: i32) -> TextCompositionViewRange #export {
view_range: TextCompositionViewRange
if selection_start < 0 { selection_start = 0 }
if selection_end < 0 { selection_end = 0 }
if selection_start <= selection_end {
view_range.replace_start = selection_start
view_range.replace_end = selection_end
} else {
view_range.replace_start = selection_end
view_range.replace_end = selection_start
}
if preedit_len < 0 { preedit_len = 0 }
if preedit_cursor < 0 { preedit_cursor = 0 }
if preedit_cursor > preedit_len { preedit_cursor = preedit_len }
if preedit_selection_end < preedit_cursor {
preedit_selection_end = preedit_cursor
}
if preedit_selection_end > preedit_len {
preedit_selection_end = preedit_len
}
view_range.cursor = view_range.replace_start + preedit_cursor
view_range.selection_start = view_range.cursor
view_range.selection_end = view_range.replace_start + preedit_selection_end
view_range.composition_start = view_range.replace_start
view_range.composition_end = view_range.replace_start + preedit_len
return view_range
}
TextCompositionPaintSpanForText :: (composition_start: i32,
composition_end: i32, text_len: i32) -> TextCompositionPaintSpan #export {
span: TextCompositionPaintSpan
if text_len < 0 { text_len = 0 }
if composition_start < 0 { composition_start = 0 }
if composition_end < 0 { composition_end = 0 }
if composition_start > text_len { composition_start = text_len }
if composition_end > text_len { composition_end = text_len }
if composition_end <= composition_start { return span }
span.visible = true
span.start = composition_start
span.end = composition_end
return span
}
TextCompositionPaintSpanForLine :: (composition_start: i32,
composition_end: i32, line_start: i32,
line_end: i32) -> TextCompositionPaintSpan #export {
span: TextCompositionPaintSpan
if composition_end <= composition_start { return span }
if line_end < line_start { line_end = line_start }
span.start = composition_start
if span.start < line_start { span.start = line_start }
span.end = composition_end
if span.end > line_end { span.end = line_end }
if span.end <= span.start { return span }
span.visible = true
return span
}
TextCompositionUnderlineEndX :: (start_x: i32, end_x: i32,
stroke_width: i32) -> i32 #export {
if stroke_width < 1 { stroke_width = 1 }
if end_x <= start_x { return start_x + stroke_width }
return end_x
}
TextCompositionUnderlineY :: (text_y: i32, line_height: i32,
stroke_width: i32) -> i32 #export {
if stroke_width < 1 { stroke_width = 1 }
return text_y + line_height - stroke_width
}
TextSelectionPaintSpanForLine :: (selection_start: i32,
selection_end: i32, line_start: i32,
line_end: i32) -> TextSelectionPaintSpan #export {
span: TextSelectionPaintSpan
if selection_end <= selection_start { return span }
if line_end < line_start { line_end = line_start }
span.start = selection_start
if span.start < line_start { span.start = line_start }
span.end = selection_end
if span.end > line_end { span.end = line_end }
span.continues_past_line = selection_start <= line_start &&
selection_end > line_end
if span.end < span.start { return span }
if span.end == span.start && !span.continues_past_line {
return span
}
span.visible = true
return span
}
TextSelectionPaintSpanForText :: (selection_start: i32,
selection_end: i32, text_len: i32) -> TextSelectionPaintSpan #export {
span: TextSelectionPaintSpan
if text_len < 0 { text_len = 0 }
if selection_start < 0 { selection_start = 0 }
if selection_end < 0 { selection_end = 0 }
if selection_start > text_len { selection_start = text_len }
if selection_end > text_len { selection_end = text_len }
if selection_end <= selection_start { return span }
span.visible = true
span.start = selection_start
span.end = selection_end
span.continues_past_line = false
return span
}
TextSelectionRange :: struct {
start: i32
end: i32
has_selection: bool
}
TextSelectionState :: struct {
anchor: i32
cursor: i32
has_selection: bool
}
TextSelectionOwnerMatches :: (has_owner: bool, same_owner: bool,
selection_id: i32, requested_id: i32) -> bool #export {
if has_owner && same_owner {
return true
}
return requested_id > 0 && selection_id == requested_id
}
TextContextMenuStateFor :: (has_selection: bool,
copy_all_when_empty: bool, has_text: bool, read_only: bool,
clipboard_has_text: bool) -> TextContextMenuState #export {
state: TextContextMenuState
can_copy_content: bool = has_selection ||
(copy_all_when_empty && has_text)
state.cut_enabled = can_copy_content && !read_only
state.copy_enabled = can_copy_content
state.paste_enabled = clipboard_has_text && !read_only
state.select_all_enabled = has_text
return state
}
TextContextCommandCut :: () -> i32 #export {
return 1
}
TextContextCommandCopy :: () -> i32 #export {
return 2
}
TextContextCommandPaste :: () -> i32 #export {
return 3
}
TextContextCommandSelectAll :: () -> i32 #export {
return 4
}
TextEditCommandDecisionFor :: (command: i32, has_selection: bool,
copy_all_when_empty: bool, clear_all_when_empty: bool, has_text: bool,
allow_copy: bool, allow_delete: bool,
allow_paste: bool) -> TextContextCommandDecision #export {
decision: TextContextCommandDecision
if command == TextContextCommandCut() {
if has_selection {
decision.copy_selection = allow_copy
decision.delete_selection = allow_copy && allow_delete
} else if copy_all_when_empty && has_text {
decision.copy_all = allow_copy
decision.clear_all = allow_copy && allow_delete && clear_all_when_empty
}
decision.collapse_selection = decision.copy_selection || decision.copy_all ||
decision.delete_selection || decision.clear_all
return decision
}
if command == TextContextCommandCopy() {
if has_selection {
decision.copy_selection = allow_copy
} else if allow_copy && copy_all_when_empty && has_text {
decision.copy_all = true
}
return decision
}
if command == TextContextCommandPaste() {
if allow_paste {
decision.delete_selection = allow_delete && has_selection
decision.paste = true
decision.collapse_selection = true
}
return decision
}
if command == TextContextCommandSelectAll() {
decision.select_all = true
return decision
}
return decision
}
TextContextCommandDecisionFor :: (command: i32, has_selection: bool,
copy_all_when_empty: bool, has_text: bool,
read_only: bool) -> TextContextCommandDecision #export {
return TextEditCommandDecisionFor(command, has_selection,
copy_all_when_empty, copy_all_when_empty, has_text,
true, !read_only, !read_only)
}
TextShortcutInput :: struct {
select_all: bool
copy: bool
cut: bool
paste: bool
}
TextShortcutInputFor :: (modifier: bool, select_all: bool, copy: bool,
cut: bool, paste: bool) -> TextShortcutInput #export {
input: TextShortcutInput
input.select_all = modifier && select_all
input.copy = modifier && copy
input.cut = modifier && cut
input.paste = modifier && paste
return input
}
TextShortcutShouldClaimSelectionFocus :: (has_selection: bool, focused: bool,
keyboard_enabled: bool, modifier: bool, copy_pressed: bool,
cut_pressed: bool, paste_pressed: bool) -> bool #export {
return has_selection && !focused && keyboard_enabled && modifier &&
(copy_pressed || cut_pressed || paste_pressed)
}
TextDeleteShortcutShouldRun :: (read_only: bool, backspace_pressed: bool,
delete_pressed: bool, queued_backspace_count: i32) -> bool #export {
return !read_only && (backspace_pressed || delete_pressed ||
queued_backspace_count > 0)
}
TextSelectionReplacementShouldRun :: (read_only: bool, handled: bool,
has_selection: bool, modifier: bool) -> bool #export {
return !read_only && !handled && has_selection && !modifier
}
TextNativeEditShouldRun :: (read_only: bool, handled: bool) -> bool #export {
return !read_only && !handled
}
TextCommitAfterHandledShouldRun :: (read_only: bool, handled: bool,
enter_requested: bool) -> bool #export {
return !read_only && handled && enter_requested
}
TextNavigationShouldRun :: (handled: bool) -> bool #export {
return !handled
}
TextAreaEnterNewlineShouldRun :: (read_only: bool,
enter_requested: bool) -> bool #export {
return !read_only && enter_requested
}
TextAreaChangedShouldCollapseSelection :: (changed: bool,
handled: bool) -> bool #export {
return changed && !handled
}
TextFieldChangedShouldCollapseSelection :: (changed: bool, left_pressed: bool,
right_pressed: bool, home_pressed: bool, end_pressed: bool) -> bool #export {
return changed || left_pressed || right_pressed || home_pressed || end_pressed
}
TextKeyboardShouldRun :: (focused: bool, keyboard_enabled: bool) -> bool #export {
return focused && keyboard_enabled
}
TextEscapeShouldBlur :: (focused: bool, keyboard_enabled: bool,
escape_pressed: bool) -> bool #export {
return focused && keyboard_enabled && escape_pressed
}
TextOutsideClickShouldBlur :: (focused: bool, context_active: bool,
scroll_gesture_pending: bool) -> bool #export {
return focused && !context_active && !scroll_gesture_pending
}
TextSelectionRangeShouldResolve :: (focused: bool, context_active: bool,
has_selection: bool) -> bool #export {
return (focused || context_active) && has_selection
}
TextCompositionDisplayShouldRun :: (secure: bool) -> bool #export {
return !secure
}
TextEditCommitShouldRun :: (enter_pressed: bool,
queued_enter_count: i32) -> bool #export {
return enter_pressed || queued_enter_count > 0
}
TextAreaScrollbarShouldShow :: (has_scroll_storage: bool,
max_scroll: i32) -> bool #export {
return has_scroll_storage && max_scroll > 0
}
TextAreaWheelShouldScroll :: (mouse_inside: bool, captured: bool,
modifier: bool) -> bool #export {
return mouse_inside && !captured && !modifier
}
TextAreaRevealCursorShouldRun :: (focused: bool, changed: bool,
navigation_pressed: bool) -> bool #export {
return focused && (changed || navigation_pressed)
}
TextAreaRevealScroll :: (scroll: i32, cursor_y: i32, cursor_height: i32,
viewport_height: i32) -> i32 #export {
if cursor_y < scroll {
return cursor_y
}
if cursor_y + cursor_height > scroll + viewport_height {
return cursor_y + cursor_height - viewport_height
}
return scroll
}
TextAreaScrollFor :: (scroll: i32, max_scroll: i32) -> i32 #export {
if max_scroll < 0 {
max_scroll = 0
}
if scroll < 0 {
return 0
}
if scroll > max_scroll {
return max_scroll
}
return scroll
}
TextFieldRevealCursorShouldRun :: (focused: bool) -> bool #export {
return focused
}
TextFieldContextShouldRegister :: (secure: bool) -> bool #export {
return !secure
}
TextNavNone :: () -> i32 #export {
return 0
}
TextNavLeft :: () -> i32 #export {
return 1
}
TextNavRight :: () -> i32 #export {
return 2
}
TextNavHome :: () -> i32 #export {
return 3
}
TextNavEnd :: () -> i32 #export {
return 4
}
TextNavUp :: () -> i32 #export {
return 5
}
TextNavDown :: () -> i32 #export {
return 6
}
TextNavPageUp :: () -> i32 #export {
return 7
}
TextNavPageDown :: () -> i32 #export {
return 8
}
TextNavigationKeyFor :: (multiline: bool, left: bool, right: bool,
home: bool, end: bool, up: bool, down: bool,
page_up: bool, page_down: bool) -> i32 #export {
if left { return TextNavLeft() }
if right { return TextNavRight() }
if home { return TextNavHome() }
if end { return TextNavEnd() }
if multiline && up { return TextNavUp() }
if multiline && down { return TextNavDown() }
if multiline && page_up { return TextNavPageUp() }
if multiline && page_down { return TextNavPageDown() }
return TextNavNone()
}
TextBackspaceRepeat :: struct {
count: i32
next_repeat_at: f64
}
TextBackspaceRepeatFor :: (pressed: bool, down: bool, now: f64,
next_repeat_at: f64) -> TextBackspaceRepeat #export {
result: TextBackspaceRepeat
if pressed {
result.count = 1
result.next_repeat_at = now + 0.34
return result
}
if !down {
result.next_repeat_at = 0.0
return result
}
if next_repeat_at <= 0.0 {
result.next_repeat_at = now + 0.34
return result
}
count: i32 = 0
cursor: f64 = next_repeat_at
while now >= cursor && count < 8 {
count += 1
cursor += 0.045
}
result.count = count
result.next_repeat_at = cursor
return result
}
TextDeleteNone :: () -> i32 #export {
return 0
}
TextDeleteBackspace :: () -> i32 #export {
return 1
}
TextDeleteForward :: () -> i32 #export {
return 2
}
TextInputMetric :: (fields: u32, field: u32, value: i32,
fallback: i32) -> i32 {
if (fields & field) == (u32)0 || value < 0 {
return fallback
}
return value
}
TextInputScaledMetric :: (value: float, scale: float) -> i32 {
if scale <= 0.0 { scale = 1.0 }
metric: i32 = (i32)(value * scale)
if metric < 0 { return 0 }
return metric
}
TextInputRoundScaledMetric :: (value: float, scale: float) -> i32 {
if scale <= 0.0 { scale = 1.0 }
metric: i32 = (i32)(((float)((i32)(value + 0.5))) * scale + 0.5)
if metric < 0 { return 0 }
return metric
}
TextInputRequestedFieldsFor :: (fields: u32, padding_x: i32,
padding_y: i32, line_gap: i32) -> u32 {
requested: u32 = fields
if padding_x > 0 {
requested = requested | (u32)StylePaddingX
}
if padding_y > 0 {
requested = requested | (u32)StylePaddingY
}
if line_gap >= 0 {
requested = requested | (u32)StyleGap
}
return requested
}
TextInputResolvedMetric :: (requested_fields: u32, field: u32,
requested_value: i32, resolved_fields: u32, resolved_value: float,
scale: float, unset_value: i32) -> i32 {
if (requested_fields & field) != (u32)0 {
if requested_value < 0 { return 0 }
return requested_value
}
if (resolved_fields & field) != (u32)0 && resolved_value >= 0.0 {
return TextInputRoundScaledMetric(resolved_value, scale)
}
return unset_value
}
TextInputResolvedStyleFor :: (fields: u32, padding_x: i32, padding_y: i32,
line_gap: i32, radius: float, resolved_fields: u32,
resolved_padding_x: float, resolved_padding_y: float,
resolved_gap: float, resolved_radius: float,
scale: float) -> TextInputResolvedStyle #export {
style: TextInputResolvedStyle
requested_fields: u32 = TextInputRequestedFieldsFor(fields, padding_x,
padding_y, line_gap)
style.fields = requested_fields | resolved_fields
style.padding_x = TextInputResolvedMetric(requested_fields,
(u32)StylePaddingX, padding_x, resolved_fields, resolved_padding_x,
scale, padding_x)
style.padding_y = TextInputResolvedMetric(requested_fields,
(u32)StylePaddingY, padding_y, resolved_fields, resolved_padding_y,
scale, padding_y)
style.line_gap = TextInputResolvedMetric(requested_fields,
(u32)StyleGap, line_gap, resolved_fields, resolved_gap, scale, -1)
style.radius = radius
if (fields & (u32)StyleRadius) == (u32)0 || radius < 0.0 {
style.radius = resolved_radius
}
return style
}
TextInputDefaultPaddingX :: (scale: float) -> i32 #export {
return TextInputScaledMetric(10.0, scale)
}
TextInputDefaultPaddingY :: (scale: float) -> i32 #export {
return TextInputScaledMetric(8.0, scale)
}
TextAreaMinWrapWidth :: (scale: float) -> i32 #export {
return TextInputScaledMetric(24.0, scale)
}
TextAreaScrollbarWidthFor :: (scale: float) -> i32 #export {
return TextInputScaledMetric(12.0, scale)
}
TextAreaMaxScrollFor :: (content_height: i32, bounds_height: float,
padding_y: i32) -> i32 #export {
viewport_height: i32 = (i32)bounds_height - padding_y * 2
max_scroll: i32 = content_height - viewport_height
if max_scroll < 0 {
return 0
}
return max_scroll
}
TextAreaWheelScrollFor :: (scroll: i32, wheel: float,
line_height: i32) -> i32 #export {
return scroll - (i32)(wheel * (float)line_height * 3.0)
}
TextAreaDragScrollFor :: (scroll: i32, mouse_y: i32, bounds_y: float,
bounds_height: float, padding_y: i32, line_height: i32) -> i32 #export {
next: i32 = scroll
if mouse_y < (i32)bounds_y + padding_y {
next -= line_height
}
if mouse_y > (i32)(bounds_y + bounds_height) - padding_y {
next += line_height
}
if next < 0 {
return 0
}
return next
}
TextAreaGutterMetricsFor :: (scale: float) -> TextAreaGutterMetrics #export {
metrics: TextAreaGutterMetrics
metrics.top_inset = TextInputScaledMetric(10.0, scale)
metrics.active_y_inset = TextInputScaledMetric(2.0, scale)
metrics.label_x_inset = TextInputScaledMetric(6.0, scale)
metrics.label_font = TextInputScaledMetric(10.0, scale)
metrics.extra_rows = 3
return metrics
}
TextAreaGutterRowsFor :: (bounds_height: float, line_height: i32,
metrics: TextAreaGutterMetrics) -> i32 #export {
if line_height <= 0 { return 0 }
rows: i32 = ((i32)bounds_height / line_height) + metrics.extra_rows
if rows < 0 { return 0 }
return rows
}
TextAreaGutterFirstY :: (gutter_y: float, scroll_y: i32, line_height: i32,
metrics: TextAreaGutterMetrics) -> i32 #export {
if line_height <= 0 { return (i32)gutter_y + metrics.top_inset }
return (i32)gutter_y + metrics.top_inset - (scroll_y % line_height)
}
TextAreaGutterInactiveAlpha :: (alpha: u8) -> u8 #export {
return (u8)((float)alpha * 0.62)
}
TextFieldRevealMargin :: (scale: float) -> i32 #export {
return TextInputScaledMetric(8.0, scale)
}
TextInputDoubleClickSlopFor :: (scale: float) -> i32 #export {
return TextInputScaledMetric(6.0, scale)
}
TextInputDoubleClickMaxSeconds :: () -> float #export {
return 0.45
}
TextInputDoubleClickDecisionFor :: (same_owner: bool, same_id: bool,
elapsed_seconds: float, dx: i32, dy: i32,
slop: i32) -> TextInputDoubleClickDecision #export {
decision: TextInputDoubleClickDecision
if slop < 0 { slop = 0 }
if dx < 0 { dx = -dx }
if dy < 0 { dy = -dy }
decision.double_click = same_owner && same_id &&
elapsed_seconds <= TextInputDoubleClickMaxSeconds() &&
dx <= slop && dy <= slop
return decision
}
TextFieldPanDragThresholdFor :: (scale: float) -> i32 #export {
return TextInputScaledMetric(5.0, scale)
}
TextFieldPanDecisionFor :: (already_panning: bool, dx: i32, dy: i32,
threshold: i32) -> TextFieldPanDecision #export {
decision: TextFieldPanDecision
if threshold < 0 { threshold = 0 }
abs_dx: i32 = dx
abs_dy: i32 = dy
if abs_dx < 0 { abs_dx = -abs_dx }
if abs_dy < 0 { abs_dy = -abs_dy }
decision.pan = already_panning ||
(abs_dx > threshold && abs_dx >= abs_dy)
return decision
}
TextFieldMinCursorHeight :: (scale: float) -> i32 #export {
return TextInputScaledMetric(8.0, scale)
}
TextFieldCursorVerticalPadding :: (scale: float) -> i32 #export {
return TextInputScaledMetric(8.0, scale)
}
TextFieldClipGuard :: (scale: float) -> i32 #export {
clip_guard: i32 = TextInputScaledMetric(1.0, scale)
if clip_guard < 1 { return 1 }
return clip_guard
}
TextInputStrokeWidth :: (scale: float) -> i32 #export {
width: i32 = TextInputScaledMetric(2.0, scale)
if width < 1 { return 1 }
return width
}
TextInputMetricsFor :: (fields: u32, font: i32, padding_x: i32, padding_y: i32,
line_gap: i32, default_font: i32, default_padding_x: i32,
default_padding_y: i32, default_line_gap: i32) -> TextInputMetrics #export {
metrics: TextInputMetrics
metrics.font = font
if metrics.font <= 0 {
metrics.font = default_font
}
metrics.padding_x = TextInputMetric(fields, (u32)StylePaddingX,
padding_x, default_padding_x)
metrics.padding_y = TextInputMetric(fields, (u32)StylePaddingY,
padding_y, default_padding_y)
metrics.line_gap = TextInputMetric(fields, (u32)StyleGap,
line_gap, default_line_gap)
metrics.line_height = metrics.font + metrics.line_gap
if metrics.line_height <= 0 {
metrics.line_height = 1
}
return metrics
}
TextInputContentWidth :: (bounds_width: float, padding_x: i32) -> i32 #export {
width: i32 = (i32)bounds_width - padding_x * 2
if width < 0 {
return 0
}
return width
}
TextAreaPageRows :: (bounds_height: float, font: i32, line_gap: i32,
padding_y: i32) -> i32 #export {
line_height: i32 = font + line_gap
visible_height: i32 = (i32)bounds_height - padding_y * 2
if line_height <= 0 || visible_height < line_height {
return 1
}
rows: i32 = visible_height / line_height
if rows < 1 {
return 1
}
if rows < 2 {
return 2
}
return rows
}
TextAreaWrapWidthFor :: (bounds_width: float, padding_x: i32,
wrap: bool, min_wrap_width: i32) -> i32 #export {
if !wrap { return 0 }
width: i32 = (i32)bounds_width - padding_x * 2
if width < min_wrap_width { return 0 }
return width
}
TextAreaPaintFor :: (bounds: Rectangle, font: i32, line_gap: i32,
padding_x: i32, padding_y: i32, wrap: bool, content_height: i32,
scroll_y: i32, text_line_height: i32, min_wrap_width: i32) -> TextAreaPaint #export {
paint: TextAreaPaint
line_height: i32 = font + line_gap
paint.viewport_height = (i32)bounds.height - padding_y * 2
if paint.viewport_height < 0 { paint.viewport_height = 0 }
paint.max_scroll = TextAreaMaxScrollFor(content_height, bounds.height,
padding_y)
paint.scroll_y = TextAreaScrollFor(scroll_y, paint.max_scroll)
paint.wrap_width = TextAreaWrapWidthFor(bounds.width, padding_x, wrap,
min_wrap_width)
paint.clip_bounds.x = bounds.x + (float)padding_x
paint.clip_bounds.y = bounds.y + (float)padding_y
paint.clip_bounds.width = bounds.width - (float)(padding_x * 2)
paint.clip_bounds.height = bounds.height - (float)(padding_y * 2)
if paint.clip_bounds.width < 0.0 { paint.clip_bounds.width = 0.0 }
if paint.clip_bounds.height < 0.0 { paint.clip_bounds.height = 0.0 }
paint.placeholder_x = (i32)bounds.x + padding_x
paint.placeholder_y = (i32)bounds.y + padding_y +
(line_height - text_line_height) / 2
return paint
}
TextInputBufferLimit :: (text_size: i32, max_codepoints: i32) -> i32 #export {
limit: i32 = text_size - 1
if max_codepoints > 0 && max_codepoints < limit {
limit = max_codepoints
}
if limit < 0 {
return 0
}
return limit
}
TextFieldScrollFor :: (bounds_x: float, bounds_width: float, padding_x: i32,
text_width: i32, scroll: i32) -> TextFieldScroll #export {
result: TextFieldScroll
result.clip_width = TextInputContentWidth(bounds_width, padding_x)
result.max_scroll = text_width - result.clip_width
if result.max_scroll < 0 {
result.max_scroll = 0
}
if scroll < 0 {
result.scroll = 0
} else if scroll > result.max_scroll {
result.scroll = result.max_scroll
} else {
result.scroll = scroll
}
result.text_origin_x = (i32)bounds_x + padding_x - result.scroll
return result
}
TextFieldRevealScroll :: (scroll: i32, max_scroll: i32, clip_width: i32,
cursor_text_x: i32, margin: i32) -> i32 #export {
if margin < 0 {
margin = 0
}
if cursor_text_x < scroll + margin {
scroll = cursor_text_x - margin
}
if cursor_text_x > scroll + clip_width - margin {
scroll = cursor_text_x - clip_width + margin
}
if scroll < 0 {
return 0
}
if scroll > max_scroll {
return max_scroll
}
return scroll
}
TextFieldCursorHeightFor :: (font: i32, bounds_height: float,
text_line_height: i32, min_height: i32, vertical_padding: i32) -> i32 #export {
height: i32 = text_line_height
max_height: i32 = (i32)bounds_height - vertical_padding
if height < font { height = font }
if max_height < min_height { max_height = (i32)bounds_height }
if height > max_height { height = max_height }
if height < min_height { height = min_height }
return height
}
TextFieldPaintFor :: (bounds: Rectangle, padding_x: i32, scroll_x: i32,
font: i32, text_line_height: i32, min_cursor_height: i32,
cursor_vertical_padding: i32, clip_guard: i32) -> TextFieldPaint #export {
paint: TextFieldPaint
clip_width: i32 = TextInputContentWidth(bounds.width, padding_x)
paint.clip_bounds.x = bounds.x + (float)padding_x
paint.clip_bounds.y = bounds.y - (float)clip_guard
paint.clip_bounds.width = (float)clip_width
paint.clip_bounds.height = bounds.height + (float)(clip_guard * 2)
if paint.clip_bounds.height < 0.0 { paint.clip_bounds.height = 0.0 }
paint.text_x = (i32)bounds.x + padding_x - scroll_x
paint.cursor_height = TextFieldCursorHeightFor(
font, bounds.height, text_line_height, min_cursor_height,
cursor_vertical_padding)
paint.cursor_y = (i32)bounds.y +
((i32)bounds.height - paint.cursor_height) / 2
return paint
}
TextSelectionRangeFor :: (anchor: i32, cursor: i32) -> TextSelectionRange #export {
out: TextSelectionRange
if anchor < cursor {
out.start = anchor
out.end = cursor
} else {
out.start = cursor
out.end = anchor
}
out.has_selection = out.start != out.end
return out
}
TextSelectionRangeForLength :: (anchor: i32, cursor: i32,
length: i32) -> TextSelectionRange #export {
out: TextSelectionRange = TextSelectionRangeFor(anchor, cursor)
if length < 0 { length = 0 }
if out.start < 0 { out.start = 0 }
if out.end < 0 { out.end = 0 }
if out.start > length { out.start = length }
if out.end > length { out.end = length }
out.has_selection = out.start != out.end
return out
}
TextCursorForLength :: (cursor: i32, length: i32) -> i32 #export {
if length < 0 { length = 0 }
if cursor < 0 { return 0 }
if cursor > length { return length }
return cursor
}
TextSelectionAfterMove :: (anchor: i32, cursor: i32, target: i32,
extend: bool) -> TextSelectionState #export {
out: TextSelectionState
if extend {
if anchor == cursor {
out.anchor = cursor
} else {
out.anchor = anchor
}
out.cursor = target
} else {
out.anchor = target
out.cursor = target
}
out.has_selection = out.anchor != out.cursor
return out
}
TextSelectionCollapsed :: (cursor: i32) -> TextSelectionState #export {
out: TextSelectionState
out.anchor = cursor
out.cursor = cursor
out.has_selection = false
return out
}
TextSelectionAll :: (length: i32) -> TextSelectionState #export {
out: TextSelectionState
if length < 0 {
length = 0
}
out.anchor = 0
out.cursor = length
out.has_selection = length > 0
return out
}
TextBufferLineStartCursor :: (cursor: i32) -> i32 #export {
if cursor < 0 { return 0 }
return cursor
}
TextBufferInsertDecisionFor :: (text_size: i32, used: i32, at: i32,
insert_len: i32) -> TextBufferInsertDecision #export {
decision: TextBufferInsertDecision
if text_size <= 0 || used < 0 || at < 0 || at > used || insert_len <= 0 {
return decision
}
if used + insert_len >= text_size {
return decision
}
decision.can_insert = true
decision.tail_count = used - at + 1
return decision
}
TextBufferDeleteDecisionFor :: (used: i32, at: i32,
delete_len: i32) -> TextBufferDeleteDecision #export {
decision: TextBufferDeleteDecision
if used < 0 || at < 0 || delete_len <= 0 || at + delete_len > used {
return decision
}
decision.can_delete = true
decision.tail_count = used - at - delete_len + 1
return decision
}
TextBufferDeleteRangeDecisionFor :: (used: i32, start: i32,
end: i32) -> TextBufferDeleteRangeDecision #export {
decision: TextBufferDeleteRangeDecision
if used < 0 {
return decision
}
if start < 0 { start = 0 }
if end < 0 { end = 0 }
if start > used { start = used }
if end > used { end = used }
if end <= start {
return decision
}
decision.can_delete = true
decision.start = start
decision.end = end
decision.cursor = start
decision.tail_count = used - end + 1
return decision
}
TextBufferCursorAfterInsert :: (cursor: i32, at: i32, insert_len: i32) -> i32 #export {
if cursor >= at {
return cursor + insert_len
}
return cursor
}
TextBufferCursorAfterDelete :: (cursor: i32, at: i32,
delete_len: i32) -> i32 #export {
if cursor >= at + delete_len {
return cursor - delete_len
}
if cursor > at {
return at
}
return cursor
}
TextBufferBracketCursorFor :: (cursor: i32, length: i32) -> i32 #export {
if length <= 0 { return -1 }
if cursor < 0 { return 0 }
if cursor >= length { return length - 1 }
return cursor
}
TextBufferBracketCandidatePos :: (cursor: i32,
current_is_bracket: bool) -> i32 #export {
if cursor > 0 && !current_is_bracket {
return cursor - 1
}
return cursor
}
TextBufferBracketDecisionFor :: (pos: i32,
bracket_code: i32) -> TextBufferBracketDecision #export {
decision: TextBufferBracketDecision
decision.pos = pos
if bracket_code == 40 {
decision.valid = true
decision.open = 40
decision.close = 41
decision.direction = 1
return decision
}
if bracket_code == 91 {
decision.valid = true
decision.open = 91
decision.close = 93
decision.direction = 1
return decision
}
if bracket_code == 123 {
decision.valid = true
decision.open = 123
decision.close = 125
decision.direction = 1
return decision
}
if bracket_code == 41 {
decision.valid = true
decision.open = 40
decision.close = 41
decision.direction = -1
return decision
}
if bracket_code == 93 {
decision.valid = true
decision.open = 91
decision.close = 93
decision.direction = -1
return decision
}
if bracket_code == 125 {
decision.valid = true
decision.open = 123
decision.close = 125
decision.direction = -1
return decision
}
return decision
}
TextNavigationDecisionFor :: (key: i32, multiline: bool, shift: bool,
modifier: bool, secure: bool, has_selection: bool) -> TextNavigationDecision #export {
decision: TextNavigationDecision
decision.extend_selection = shift
if key == TextNavLeft() {
decision.consumed = true
if !shift && has_selection {
decision.collapse_selection_start = true
} else if modifier && secure {
decision.document_edge = -1
} else if modifier {
decision.word_direction = -1
} else {
decision.char_direction = -1
}
return decision
}
if key == TextNavRight() {
decision.consumed = true
if !shift && has_selection {
decision.collapse_selection_end = true
} else if modifier && secure {
decision.document_edge = 1
} else if modifier {
decision.word_direction = 1
} else {
decision.char_direction = 1
}
return decision
}
if key == TextNavHome() {
decision.consumed = true
if multiline && !modifier {
decision.line_edge = -1
} else {
decision.document_edge = -1
}
return decision
}
if key == TextNavEnd() {
decision.consumed = true
if multiline && !modifier {
decision.line_edge = 1
} else {
decision.document_edge = 1
}
return decision
}
if key == TextNavUp() && multiline {
decision.consumed = true
decision.vertical_direction = -1
return decision
}
if key == TextNavDown() && multiline {
decision.consumed = true
decision.vertical_direction = 1
return decision
}
if key == TextNavPageUp() && multiline {
decision.consumed = true
decision.page_direction = -1
return decision
}
if key == TextNavPageDown() && multiline {
decision.consumed = true
decision.page_direction = 1
return decision
}
return decision
}
TextDeleteDecisionFor :: (action: i32, modifier: bool, secure: bool,
has_selection: bool) -> TextDeleteDecision #export {
decision: TextDeleteDecision
if action == TextDeleteBackspace() {
decision.consumed = true
if has_selection {
return decision
}
if modifier && secure {
decision.document_edge = -1
} else if modifier {
decision.word_direction = -1
} else {
decision.char_direction = -1
}
return decision
}
if action == TextDeleteForward() {
decision.consumed = true
if has_selection {
return decision
}
if modifier && secure {
decision.document_edge = 1
} else if modifier {
decision.word_direction = 1
} else {
decision.char_direction = 1
}
return decision
}
return decision
}
TextInsertDecisionFor :: (codepoint: i32, encoded_len: i32,
text_len: i32, text_size: i32, current_codepoints: i32,
inserted_codepoints: i32, max_codepoints: i32,
allow_newlines: bool) -> TextInsertDecision #export {
decision: TextInsertDecision
if codepoint == 13 {
decision.skip = true
return decision
}
if codepoint == 10 && !allow_newlines {
decision.skip = true
return decision
}
if codepoint < 32 && codepoint != 10 {
decision.skip = true
return decision
}
if encoded_len <= 0 {
decision.skip = true
return decision
}
if text_size <= 0 || text_len + encoded_len + 1 > text_size {
decision.stop = true
return decision
}
if max_codepoints > 0 &&
current_codepoints + inserted_codepoints >= max_codepoints {
decision.stop = true
return decision
}
decision.accept = true
return decision
}
TextWordBlank :: (codepoint: i32) -> bool {
return codepoint == 32 || codepoint == 9 || codepoint == 0x3000
}
TextWordSeparator :: (codepoint: i32) -> bool {
return codepoint == 44 || codepoint == 0x3001 ||
codepoint == 46 || codepoint == 0x3002 ||
codepoint == 59 || codepoint == 0xff1b ||
codepoint == 40 || codepoint == 0xff08 ||
codepoint == 41 || codepoint == 0xff09 ||
codepoint == 123 || codepoint == 0xff5b ||
codepoint == 125 || codepoint == 0xff5d ||
codepoint == 91 || codepoint == 0x300c ||
codepoint == 93 || codepoint == 0x300d ||
codepoint == 124 || codepoint == 0xff5c ||
codepoint == 33 || codepoint == 0xff01 ||
codepoint == 92 || codepoint == 0xffe5 ||
codepoint == 47 || codepoint == 0x30fb || codepoint == 0xff0f ||
codepoint == 10 || codepoint == 13
}
TextWordBoundaryFor :: (previous: i32, current: i32) -> bool #export {
previous_blank := TextWordBlank(previous)
previous_separator := TextWordSeparator(previous)
current_blank := TextWordBlank(current)
current_separator := TextWordSeparator(current)
return ((previous_blank || previous_separator) &&
!(current_separator || current_blank)) ||
(current_separator && !previous_separator)
}