1 type TextBox 2 def TextEditControl.process_key (self 3 key : Key 4 action : KeyAction 5 mods : KeyModifiers) = 6 assert self.is_focused 7 let display_text = self.display_text 8 9 if action <> KeyAction/Press && action <> KeyAction/Repeat then 10 return 11 12 case key of 13 Key/Backspace -> 14 let selection = self.selection 15 if self.cursor_index > 0 || selection.is_some then 16 case selection of 17 None -> 18 display_text.remove_at (self.cursor_index - 1) 19 self.cursor_index = self.cursor_index - 1 20 21 Some (l, r) -> 22 display_text.remove_range l (r - l) 23 self.selection = None 24 cursor_index = l 25 26 self.update_offset 27 28 Key/Left | Key/Right | Key/Home | Key/End -> 29 let prev_cursor_index = self.cursor_index 30 let mut cursor_index = prev_cursor_index 31 case key of 32 Key/Left -> 33 if self.cursor_index > 0 then 34 cursor_index = self.cursor_index - 1 35 36 Key/Right -> 37 if self.cursor_index < display_text.length then 38 cursor_index = self.cursor_index + 1 39 40 Key/Home -> cursor_index = 0 41 Key/End -> cursor_index = display_text.length 42 else -> fail 43 44 set_cursor_index self prev_cursor_index cursor_index 45 is_mouse_move = false 46 is_move = true 47 48 Key/Delete -> 49 let selection = self.selection 50 if display_text.length > 0 51 && (self.cursor_index < display_text.length 52 || selection.is_some) 53 then 54 case selection of 55 None -> display_text.remove_at self.cursor_index 56 Some (l, r) -> 57 display_text.remove_range l (r - l) 58 self.selection = None 59 cursor_index = l 60 61 self.update_offset 62 63 Key/Enter | Key/NumPadEnter -> 64 if self.on_enter ? Some f then 65 f self.display_text.as_string 66 else 67 self.is_focused = false 68 69 Key/Escape -> 70 if self.on_escape ? Some f then 71 f () 72 else 73 self.is_focused = false 74 75 Key/C if mods.contains KeyModifiers/Control -> 76 let window = self.os_window 77 let maybe_selection = self.selection 78 if maybe_selection ? Some (start, end) then 79 let selected_text = 80 self.display_text.as_string.substring start end 81 82 window.set_clipboard selected_text 83 84 Key/X if mods.contains KeyModifiers/Control -> 85 let window = self.os_window 86 let maybe_selection = self.selection 87 if maybe_selection ? Some (start, end) then 88 let selected_text = display_text.as_string.substring start end 89 window.set_clipboard selected_text 90 display_text.remove_range start (end - start) 91 self.selection = None 92 cursor_index = start 93 94 Key/V if mods.contains KeyModifiers/Control -> 95 let window = self.os_window 96 let s = window.get_clipboard 97 if s.is_not_empty then 98 let maybe_selection = self.selection 99 case maybe_selection of 100 None -> 101 display_text.insert self.cursor_index s 102 self.cursor_index = self.cursor_index + s.length 103 104 Some (start, end) -> 105 display_text.replace start end s 106 self.selection = None 107 cursor_index = start + s.length 108 else -> () 109 110 def TextEditControl.process_char (char : Char) = 111 assert is_focused 112 if selection ? Some (l, r) then 113 display_text.remove_range l (r - l) 114 cursor_index = l 115 116 display_text.insert cursor_index char 117 set_cursor_index self cursor_index (cursor_index + 1) 118 is_mouse_move = false 119 is_move = false 120