diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2024-05-24 11:51:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-24 11:51:09 +0100 |
| commit | 0658778a307fdfbfa522040d793df6dbb1da0211 (patch) | |
| tree | 786d3e2bb1a9d2fee4329bf1becf3c4f3619bdf1 /core | |
| parent | 1081e9ac09816601b7cc055a7d677b5d928f16a7 (diff) | |
| parent | 4328562e2c376d4828a8ccbe24d451a0c112ed1c (diff) | |
Merge pull request #3611 from hstormo/textbox
vendor/microui: nicer textbox
Diffstat (limited to 'core')
| -rw-r--r-- | core/text/edit/text_edit.odin | 43 |
1 files changed, 30 insertions, 13 deletions
diff --git a/core/text/edit/text_edit.odin b/core/text/edit/text_edit.odin index 6f21c9860..a4f8c06b9 100644 --- a/core/text/edit/text_edit.odin +++ b/core/text/edit/text_edit.odin @@ -183,16 +183,17 @@ undo_check :: proc(s: ^State) { } // insert text into the edit state - deletes the current selection -input_text :: proc(s: ^State, text: string) { +input_text :: proc(s: ^State, text: string) -> int { if len(text) == 0 { - return + return 0 } if has_selection(s) { selection_delete(s) } - insert(s, s.selection[0], text) - offset := s.selection[0] + len(text) + n := insert(s, s.selection[0], text) + offset := s.selection[0] + n s.selection = {offset, offset} + return n } // insert slice of runes into the edit state - deletes the current selection @@ -206,8 +207,11 @@ input_runes :: proc(s: ^State, text: []rune) { offset := s.selection[0] for r in text { b, w := utf8.encode_rune(r) - insert(s, offset, string(b[:w])) - offset += w + n := insert(s, offset, string(b[:w])) + offset += n + if n != w { + break + } } s.selection = {offset, offset} } @@ -219,17 +223,29 @@ input_rune :: proc(s: ^State, r: rune) { } offset := s.selection[0] b, w := utf8.encode_rune(r) - insert(s, offset, string(b[:w])) - offset += w + n := insert(s, offset, string(b[:w])) + offset += n s.selection = {offset, offset} } // insert a single rune into the edit state - deletes the current selection -insert :: proc(s: ^State, at: int, text: string) { +insert :: proc(s: ^State, at: int, text: string) -> int { undo_check(s) if s.builder != nil { - inject_at(&s.builder.buf, at, text) + if ok, _ := inject_at(&s.builder.buf, at, text); !ok { + n := cap(s.builder.buf) - len(s.builder.buf) + assert(n < len(text)) + for is_continuation_byte(text[n]) { + n -= 1 + } + if ok2, _ := inject_at(&s.builder.buf, at, text[:n]); !ok2 { + n = 0 + } + return n + } + return len(text) } + return 0 } // remove the wanted range withing, usually the selection within byte indices @@ -263,11 +279,12 @@ selection_delete :: proc(s: ^State) { s.selection = {lo, lo} } +is_continuation_byte :: proc(b: byte) -> bool { + return b >= 0x80 && b < 0xc0 +} + // translates the caret position translate_position :: proc(s: ^State, t: Translation) -> int { - is_continuation_byte :: proc(b: byte) -> bool { - return b >= 0x80 && b < 0xc0 - } is_space :: proc(b: byte) -> bool { return b == ' ' || b == '\t' || b == '\n' } |