@@ -130,8 +130,10 @@ fn (tv &TextView) on_click(layout &Layout, mut e Event, mut w Window) {
130130 }
131131 if e.mouse_button == .left && w.is_focus (layout.shape.id_focus) {
132132 id_focus := layout.shape.id_focus
133+ cursor_pos := tv.mouse_cursor_pos (layout.shape, e, mut w)
133134 // Init mouse lock to handle dragging selection (mouse move) and finishing selection (mouse up)
134135 w.mouse_lock (
136+ cursor_pos: cursor_pos
135137 mouse_move: fn [tv, id_focus] (layout & Layout, mut e Event, mut w Window) {
136138 // The layout in mouse locks is always the root layout.
137139 if ly := layout.find_layout (fn [id_focus] (ly Layout) bool {
@@ -153,7 +155,6 @@ fn (tv &TextView) on_click(layout &Layout, mut e Event, mut w Window) {
153155 }
154156 )
155157 // Set cursor position and reset text selection
156- cursor_pos := tv.mouse_cursor_pos (layout.shape, e, mut w)
157158 cursor_offset := offset_from_cursor_position (layout.shape, cursor_pos, w)
158159 input_state := w.view_state.input_state[layout.shape.id_focus]
159160 w.view_state.input_state[layout.shape.id_focus] = InputState{
@@ -181,17 +182,23 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
181182 }
182183 ev := event_relative_to (layout.shape, e)
183184 input_state := w.view_state.input_state[layout.shape.id_focus]
184- cursor_pos := u32 (input_state .cursor_pos)
185+ start_cursor_pos := u32 (w.view_state.mouse_lock .cursor_pos)
185186 mouse_cursor_pos := u32 (tv.mouse_cursor_pos (layout.shape, ev, mut w))
186187
187- // Update selection range: start is the original cursor pos, end is the current mouse pos
188188 w.view_state.input_state[layout.shape.id_focus] = InputState{
189189 ...input_state
190- select_beg: if cursor_pos < mouse_cursor_pos { cursor_pos } else { mouse_cursor_pos }
191- select_end: if cursor_pos < mouse_cursor_pos { mouse_cursor_pos } else { cursor_pos }
190+ cursor_pos: int (mouse_cursor_pos)
191+ cursor_offset: - 1
192+ select_beg: match start_cursor_pos < mouse_cursor_pos {
193+ true { start_cursor_pos }
194+ else { mouse_cursor_pos }
195+ }
196+ select_end: match start_cursor_pos < mouse_cursor_pos {
197+ true { mouse_cursor_pos }
198+ else { start_cursor_pos }
199+ }
192200 }
193201
194- // Ensure the cursor being dragged to is visible
195202 scroll_cursor_into_view (int (mouse_cursor_pos), layout, ev, mut w)
196203 e.is_handled = true
197204 }
0 commit comments