|
|
@@ -70,6 +70,32 @@ void Window::set_viewport_size(int width, int height) {
|
|
|
adjust_scroll();
|
|
|
}
|
|
|
|
|
|
+void Window::scroll_lines(int lines) {
|
|
|
+ int max_lines = static_cast<int>(buffer_->line_count());
|
|
|
+ int new_offset = viewport_.scroll_offset + lines;
|
|
|
+
|
|
|
+ // Clamp offset
|
|
|
+ if (new_offset < 0) new_offset = 0;
|
|
|
+ // Don't scroll past end of buffer (allow some empty space though)
|
|
|
+ if (new_offset >= max_lines) new_offset = max_lines - 1;
|
|
|
+ if (new_offset < 0) new_offset = 0;
|
|
|
+
|
|
|
+ viewport_.scroll_offset = new_offset;
|
|
|
+
|
|
|
+ // If cursor is now out of view, move it into view
|
|
|
+ int cursor_line = static_cast<int>(cursor_.line);
|
|
|
+ int view_top = viewport_.scroll_offset;
|
|
|
+ int view_bottom = view_top + viewport_.height; // Exclusive
|
|
|
+
|
|
|
+ if (cursor_line < view_top) {
|
|
|
+ cursor_.line = static_cast<size_t>(view_top);
|
|
|
+ } else if (cursor_line >= view_bottom) {
|
|
|
+ cursor_.line = static_cast<size_t>(std::max(0, view_bottom - 1));
|
|
|
+ }
|
|
|
+
|
|
|
+ clamp_cursor();
|
|
|
+}
|
|
|
+
|
|
|
void Window::adjust_scroll() {
|
|
|
viewport_.height = std::max(10, viewport_.height);
|
|
|
viewport_.width = std::max(10, viewport_.width);
|