@@ -450,10 +450,11 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
453− let scrolloff = PADDING; // min(user pref, half win width/height)
453+ let scrolloff = PADDING.min(view.area.height as usize / 2); // TODO: user pref
455 // cursor visual offset
455 // cursor visual offset
456+ // TODO: only if dragging via mouse?
456− let cursor_off = cursor.row - view.first_line;
457+ // let cursor_off = cursor.row - view.first_line;
458 view.first_line = match direction {
459 view.first_line = match direction {
459 Forward => view.first_line + offset,
460 Forward => view.first_line + offset,
@@ -461,14 +462,19 @@ fn scroll(cx: &mut Context, offset: usize, direction: Direction) {
462 .min(doc_last_line);
463 .min(doc_last_line);
465+ // recalculate last line
466+ let last_line = view.last_line(doc);
464 // clamp into viewport
468 // clamp into viewport
465− let line = (view.first_line + cursor_off)
469+ let line = cursor.row.clamp(
466− .max(view.first_line + scrolloff)
470+ view.first_line + scrolloff,
467− .min(last_line.saturating_sub(scrolloff));
471+ last_line.saturating_sub(scrolloff),
469 let text = doc.text().slice(..);
474 let text = doc.text().slice(..);
470 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
475 let pos = pos_at_coords(text, Position::new(line, cursor.col)); // this func will properly truncate to line end
477+ // TODO: only manipulate main selection
472 doc.set_selection(view.id, Selection::point(pos));
478 doc.set_selection(view.id, Selection::point(pos));