Skip to content

Commit c8bbd5a

Browse files
committed
refactor scroll_into_view logic
1 parent fa4dff7 commit c8bbd5a

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

view_text.v

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,24 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
183183
}
184184
ev := event_relative_to(layout.shape, e)
185185
input_state := w.view_state.input_state[layout.shape.id_focus]
186-
start_cursor_pos := u32(w.view_state.mouse_lock.cursor_pos)
187-
mouse_cursor_pos := u32(tv.mouse_cursor_pos(layout.shape, ev, mut w))
186+
start_cursor_pos := w.view_state.mouse_lock.cursor_pos
187+
mouse_cursor_pos := tv.mouse_cursor_pos(layout.shape, ev, mut w)
188188

189189
w.view_state.input_state[layout.shape.id_focus] = InputState{
190190
...input_state
191-
cursor_pos: int(mouse_cursor_pos)
191+
cursor_pos: mouse_cursor_pos
192192
cursor_offset: -1
193193
select_beg: match start_cursor_pos < mouse_cursor_pos {
194-
true { start_cursor_pos }
195-
else { mouse_cursor_pos }
194+
true { u32(start_cursor_pos) }
195+
else { u32(mouse_cursor_pos) }
196196
}
197197
select_end: match start_cursor_pos < mouse_cursor_pos {
198-
true { mouse_cursor_pos }
199-
else { start_cursor_pos }
198+
true { u32(mouse_cursor_pos) }
199+
else { u32(start_cursor_pos) }
200200
}
201201
}
202202

203-
scroll_cursor_into_view(int(mouse_cursor_pos), layout, ev, mut w)
203+
scroll_cursor_into_view(mouse_cursor_pos, layout, mut w)
204204
e.is_handled = true
205205
}
206206
}
@@ -224,22 +224,19 @@ fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
224224
cursor_offset: -1
225225
}
226226

227-
scroll_cursor_into_view(int(mouse_cursor_pos), layout, ev, mut w)
227+
scroll_cursor_into_view(mouse_cursor_pos, layout, mut w)
228228
e.is_handled = true
229229
}
230230
}
231231

232-
// scroll_cursor_into_view ensures that the text cursor is visible within the
233-
// scroll container. It calculates the line position of the cursor and
234-
// adjusts the scroll offset if the cursor is outside the current visible
235-
// area.
236-
fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Window) {
232+
// scroll_container_cursor_y
233+
fn scroll_container_cursor_y(cursor_pos int, layout &Layout, mut w Window) f32 {
237234
id_scroll_container := layout.shape.id_scroll_container
238235

239236
// Find the scroll container and calculate height. (need to start at the root layout)
240237
scroll_container := w.layout.find_layout(fn [id_scroll_container] (ly Layout) bool {
241238
return ly.shape.id_scroll == id_scroll_container
242-
}) or { return }
239+
}) or { return -1 }
243240

244241
// Determine the height of the scrollable region
245242
mut padding_height := scroll_container.shape.padding.height()
@@ -277,7 +274,15 @@ fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Windo
277274
cursor_y < current_scroll_h_y { cursor_h_y }
278275
else { current_scroll_y }
279276
}
280-
w.scroll_vertical_to(id_scroll_container, new_scroll_y)
277+
278+
return new_scroll_y
279+
}
280+
281+
// scroll_cursor_into_view ensures that the text cursor is visible within the
282+
// scroll container.
283+
fn scroll_cursor_into_view(cursor_pos int, layout &Layout, mut w Window) {
284+
new_scroll_y := scroll_container_cursor_y(cursor_pos, layout, mut w)
285+
w.scroll_vertical_to(layout.shape.id_scroll_container, new_scroll_y)
281286
}
282287

283288
// mouse_cursor_pos determines the character index (cursor position) within
@@ -435,7 +440,7 @@ fn (tv &TextView) on_key_down(layout &Layout, mut e Event, mut window Window) {
435440
}
436441

437442
// Ensure the new cursor position is visible
438-
scroll_cursor_into_view(position, layout, e, mut window)
443+
scroll_cursor_into_view(position, layout, mut window)
439444
e.is_handled = true
440445
}
441446
}

0 commit comments

Comments
 (0)