@@ -228,11 +228,18 @@ fn (tv &TextView) mouse_up_locked(layout &Layout, mut e Event, mut w Window) {
228228// adjusts the scroll offset if the cursor is outside the current visible
229229// area.
230230fn scroll_cursor_into_view (cursor_pos int , layout & Layout, _ & Event, mut w Window) {
231+ id_scroll_container := layout.shape.id_scroll_container
232+
231233 // Find the scroll container and calculate height. (need to start at the root layout)
232- scroll_container := w.layout.find_layout (fn [layout ] (ly Layout) bool {
233- return ly.shape.id_scroll == layout.shape. id_scroll_container
234+ scroll_container := w.layout.find_layout (fn [id_scroll_container ] (ly Layout) bool {
235+ return ly.shape.id_scroll == id_scroll_container
234236 }) or { return }
235- scroll_view_height := scroll_container.shape.height - scroll_container.shape.padding.height ()
237+
238+ mut padding_height := scroll_container.shape.padding.height ()
239+ if scroll_container.children.len > 0 {
240+ padding_height + = scroll_container.children[0 ].shape.padding.height ()
241+ }
242+ scroll_view_height := scroll_container.shape.height - padding_height
236243
237244 // Find the index of the line where the cursor is located.
238245 mut line_idx := 0
@@ -252,18 +259,18 @@ fn scroll_cursor_into_view(cursor_pos int, layout &Layout, _ &Event, mut w Windo
252259 cursor_h_y := cursor_y + scroll_view_height - lh
253260
254261 // Calculate scroll offsets for current visible region
255- current_scroll_y := w.view_state.scroll_y[layout.shape. id_scroll_container]
256- current_scroll_h_y := current_scroll_y - scroll_view_height
262+ current_scroll_y := w.view_state.scroll_y[id_scroll_container]
263+ current_scroll_h_y := current_scroll_y - scroll_view_height + lh
257264
258265 // Determine if we need to scroll:
259266 // 1. If cursor is above the current view
260267 // 2. If cursor is below the current view
261268 new_scroll_y := match true {
262- cursor_y > current_scroll_y { cursor_y }
263- cursor_y < = current_scroll_h_y { cursor_h_y }
269+ cursor_y > current_scroll_y { cursor_y - 1 }
270+ cursor_y < current_scroll_h_y { cursor_h_y }
264271 else { current_scroll_y }
265272 }
266- w.scroll_vertical_to (layout.shape. id_scroll_container, new_scroll_y)
273+ w.scroll_vertical_to (id_scroll_container, new_scroll_y)
267274}
268275
269276// mouse_cursor_pos determines the character index (cursor position) within
0 commit comments