refactor: add View.clam_offset

This commit is contained in:
CJ van den Berg 2025-12-26 22:47:51 +01:00
parent 3db934d6f9
commit 6b4549f400
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
2 changed files with 25 additions and 11 deletions

View file

@ -92,22 +92,23 @@ inline fn to_cursor_bottom(self: *const Self, root: Buffer.Root) Cursor {
return .{ .row = bottom, .col = 0 };
}
fn clamp_row(self: *Self, cursor: *const Cursor, abs: bool) void {
const min_border_distance: usize = if (abs) scroll_cursor_min_border_distance_mouse else scroll_cursor_min_border_distance;
if (cursor.row < min_border_distance) {
fn clamp_row(self: *Self, cursor: *const Cursor, abs: bool, bottom_offset: usize) void {
const top_min_border_distance: usize = if (abs) scroll_cursor_min_border_distance_mouse else scroll_cursor_min_border_distance;
const bottom_min_border_distance: usize = top_min_border_distance + bottom_offset;
if (cursor.row < top_min_border_distance) {
self.row = 0;
return;
}
if (self.row > 0 and cursor.row >= min_border_distance) {
if (cursor.row < self.row + min_border_distance) {
self.row = cursor.row - min_border_distance;
if (self.row > 0 and cursor.row >= top_min_border_distance) {
if (cursor.row < self.row + top_min_border_distance) {
self.row = cursor.row - top_min_border_distance;
return;
}
}
if (cursor.row < self.row) {
self.row = 0;
} else if (cursor.row > self.row + self.rows - min_border_distance) {
self.row = cursor.row + min_border_distance - self.rows;
} else if (cursor.row > self.row + self.rows - bottom_min_border_distance) {
self.row = cursor.row + bottom_min_border_distance - self.rows;
}
}
@ -120,7 +121,12 @@ fn clamp_col(self: *Self, cursor: *const Cursor, _: bool) void {
}
pub fn clamp(self: *Self, cursor: *const Cursor, abs: bool) void {
self.clamp_row(cursor, abs);
self.clamp_row(cursor, abs, 0);
self.clamp_col(cursor, abs);
}
pub fn clamp_offset(self: *Self, cursor: *const Cursor, abs: bool, offset: usize) void {
self.clamp_row(cursor, abs, offset);
self.clamp_col(cursor, abs);
}

View file

@ -1914,17 +1914,25 @@ pub const Editor = struct {
_ = try self.handlers.msg(.{ "E", "eol_mode", eol_mode, utf8_sanitized, indent_mode });
}
fn clamp_abs(self: *Self, abs: bool) void {
fn clamp_abs_offset(self: *Self, abs: bool, offset: usize) void {
var dest: View = self.view;
dest.clamp(&self.get_primary().cursor, abs);
dest.clamp_offset(&self.get_primary().cursor, abs, offset);
self.update_scroll_dest_abs(dest.row);
self.view.col = dest.col;
}
fn clamp_abs(self: *Self, abs: bool) void {
self.clamp_abs_offset(abs, 0);
}
pub inline fn clamp(self: *Self) void {
self.clamp_abs(false);
}
inline fn clamp_offset(self: *Self, offset: usize) void {
self.clamp_abs_offset(false, offset);
}
fn clamp_mouse(self: *Self) void {
self.clamp_abs(true);
}