1 type TextBox 2 local update_offset self = 3 let text_box = self 4 let text_block = self.text_block 5 let display_text = text_box.display_text 6 measure_clamped text_box Control.max_size Control.max_size 7 text_box.arrange 8 let content_width = text_block.width as i32 9 let text_box_width = text_box.width as i32 10 let prev_offset = text_box.offset 11 12 let font = text_block.font 13 let cursor_width = font.rectangles['a'].right - font.rectangles['a'].left 14 let space_width = font.space_width as i32 15 let overflows = content_width + space_width > text_box_width 16 if overflows || prev_offset <> 0 then 17 let offset = if overflows then 18 let cursor_index = text_box.cursor_index as u32 19 let text_length = display_text.length 20 let offsets = text_block.offsets 21 let right_offset = content_width - offsets[offsets.size - 2].right as i32 22 let max_cursor_left = text_box_width - right_offset - cursor_width as i32 23 let current_cursor_left = offsets[cursor_index].left 24 25 if cursor_index == text_length then 26 max_cursor_left - current_cursor_left as i32 27 else 28 let left_offset = offsets[0].left 29 let cursor_left = offsets[cursor_index].left - left_offset as u32 30 let cursor_right = offsets[cursor_index].right 31 32 if cursor_left as i32 + prev_offset >= 0 33 && cursor_right as i32 + prev_offset < text_box_width 34 then 35 let max_index_left = offsets[offsets.size - 2].left 36 let min_offset = max_cursor_left - max_index_left as i32 |> min 0 37 let offset = prev_offset.max min_offset 38 39 if current_cursor_left as i32 - max_cursor_left < -offset 40 then offset 41 else max_cursor_left - current_cursor_left as i32 42 else 43 let left_diff = if cursor_left as i32 > -prev_offset 44 then 0 45 else -cursor_left.as<i32> - prev_offset 46 47 let right_diff = 48 if cursor_right as i32 + prev_offset < text_box_width 49 then 0 50 else max_cursor_left - current_cursor_left as i32 - prev_offset 51 52 assert left_diff == 0 || right_diff == 0 53 prev_offset + left_diff + right_diff 54 else 55 0 56 57 text_box.offset = offset 58 let diff = offset - prev_offset 59 for control in text_box.offset_controls do 60 let position = control.position 61 control.position = position.with_x (position.x + diff as f32) 62 63 subscribe { _, event -> if event is SizeEvent/Width then update_offset } 64