Files
kryon/runtime/text_input.kry
T
2026-09-13 05:27:25 -03:00

496 lines
13 KiB
Plaintext

#module "text_input"
#import "drawing_props"
TextInputMetrics :: struct {
font: i32
padding_x: i32
padding_y: i32
line_gap: i32
line_height: i32
}
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
}
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
}
TextSelectionRange :: struct {
start: i32
end: i32
has_selection: bool
}
TextSelectionState :: struct {
anchor: i32
cursor: i32
has_selection: bool
}
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
}
TextDeleteNone :: () -> i32 #export {
return 0
}
TextDeleteBackspace :: () -> i32 #export {
return 1
}
TextDeleteForward :: () -> i32 #export {
return 2
}
TextInputMetricsFor :: (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 = padding_x
if metrics.padding_x <= 0 {
metrics.padding_x = default_padding_x
}
metrics.padding_y = padding_y
if metrics.padding_y <= 0 {
metrics.padding_y = default_padding_y
}
metrics.line_gap = line_gap
if metrics.line_gap < 0 {
metrics.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 = content_height - paint.viewport_height
if paint.max_scroll < 0 { paint.max_scroll = 0 }
paint.scroll_y = scroll_y
if paint.scroll_y < 0 { paint.scroll_y = 0 }
if paint.scroll_y > paint.max_scroll { paint.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
}
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
}
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
}