Skip to content

Commit 5230cb6

Browse files
committed
Refactor scroll offset calculation logic and rename related functions for clarity and consistency
1 parent c8bbd5a commit 5230cb6

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

view_text.v

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,23 @@ fn (tv &TextView) mouse_move_locked(layout &Layout, mut e Event, mut w Window) {
181181
if tv.placeholder_active {
182182
return
183183
}
184+
184185
ev := event_relative_to(layout.shape, e)
185-
input_state := w.view_state.input_state[layout.shape.id_focus]
186186
start_cursor_pos := w.view_state.mouse_lock.cursor_pos
187187
mouse_cursor_pos := tv.mouse_cursor_pos(layout.shape, ev, mut w)
188188

189+
// scroll_offset_y := cursor_pos_to_scroll_offset_y(mouse_cursor_pos, layout, mut
190+
// w)
191+
// current_scroll_offset_y := w.view_state.scroll_y[layout.shape.id_scroll_container]
192+
//
193+
// if scroll_offset_y > current_scroll_offset_y {
194+
// mouse_cursor_pos = cursor_up(layout.shape, mouse_cursor_pos, -1, w)
195+
// } else if scroll_offset_y < current_scroll_offset_y {
196+
// mouse_cursor_pos = cursor_down(layout.shape, mouse_cursor_pos, -1, w)
197+
// }
198+
189199
w.view_state.input_state[layout.shape.id_focus] = InputState{
190-
...input_state
200+
...w.view_state.input_state[layout.shape.id_focus]
191201
cursor_pos: mouse_cursor_pos
192202
cursor_offset: -1
193203
select_beg: match start_cursor_pos < mouse_cursor_pos {
@@ -229,8 +239,8 @@ fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
229239
}
230240
}
231241

232-
// scroll_container_cursor_y
233-
fn scroll_container_cursor_y(cursor_pos int, layout &Layout, mut w Window) f32 {
242+
// cursor_pos_to_scroll_offset_y
243+
fn cursor_pos_to_scroll_offset_y(cursor_pos int, layout &Layout, mut w Window) f32 {
234244
id_scroll_container := layout.shape.id_scroll_container
235245

236246
// Find the scroll container and calculate height. (need to start at the root layout)
@@ -259,29 +269,29 @@ fn scroll_container_cursor_y(cursor_pos int, layout &Layout, mut w Window) f32 {
259269
// Calculate the y offset of the cursor line.
260270
// Since scroll offsets are often negative (content moves up), use -lh.
261271
lh := line_height(layout.shape)
262-
cursor_y := line_idx * -lh
263-
cursor_h_y := cursor_y + scroll_view_height - lh
272+
cursor_offset_top_y := line_idx * -lh
273+
cursor_offset_bottom_y := cursor_offset_top_y + scroll_view_height - lh
264274

265275
// Calculate scroll offsets for current visible region
266-
current_scroll_y := w.view_state.scroll_y[id_scroll_container]
267-
current_scroll_h_y := current_scroll_y - scroll_view_height + lh
276+
scroll_offset_top_y := w.view_state.scroll_y[id_scroll_container]
277+
scroll_offset_bottom_y := scroll_offset_top_y - scroll_view_height + lh
268278

269279
// Determine if we need to scroll:
270280
// 1. If cursor is above the current view
271281
// 2. If cursor is below the current view
272-
new_scroll_y := match true {
273-
cursor_y > current_scroll_y { cursor_y - 1 }
274-
cursor_y < current_scroll_h_y { cursor_h_y }
275-
else { current_scroll_y }
282+
scroll_offset_y := match true {
283+
cursor_offset_top_y > scroll_offset_top_y { cursor_offset_top_y - 1 }
284+
cursor_offset_top_y < scroll_offset_bottom_y { cursor_offset_bottom_y }
285+
else { scroll_offset_top_y }
276286
}
277287

278-
return new_scroll_y
288+
return scroll_offset_y
279289
}
280290

281291
// scroll_cursor_into_view ensures that the text cursor is visible within the
282292
// scroll container.
283293
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)
294+
new_scroll_y := cursor_pos_to_scroll_offset_y(cursor_pos, layout, mut w)
285295
w.scroll_vertical_to(layout.shape.id_scroll_container, new_scroll_y)
286296
}
287297

0 commit comments

Comments
 (0)