terminal/kitty-gfx: get rid of selection

This commit is contained in:
Mitchell Hashimoto
2023-08-23 11:41:49 -07:00
parent de3ef0f78c
commit 135250018e
3 changed files with 8 additions and 52 deletions

View File

@ -896,13 +896,14 @@ fn prepKittyGraphics(
};
// If the selection isn't within our viewport then skip it.
const image_sel = p.selection(image, t);
if (!image_sel.within(top, bot)) continue;
const rect = p.rect(image, t);
if (rect.top_left.y > bot.y) continue;
if (rect.bottom_right.y < top.y) continue;
// If the top left is outside the viewport we need to calc an offset
// so that we render (0, 0) with some offset for the texture.
const offset_y: u32 = if (image_sel.start.y < t.screen.viewport) offset_y: {
const offset_cells = t.screen.viewport - image_sel.start.y;
const offset_y: u32 = if (rect.top_left.y < t.screen.viewport) offset_y: {
const offset_cells = t.screen.viewport - rect.top_left.y;
const offset_pixels = offset_cells * self.cell_size.height;
break :offset_y @intCast(offset_pixels);
} else 0;

View File

@ -189,11 +189,11 @@ fn display(
switch (d.cursor_movement) {
.none => {},
.after => {
const p_sel = p.selection(img, terminal);
const rect = p.rect(img, terminal);
// We can do better by doing this with pure internal screen state
// but this handles scroll regions.
const height = p_sel.end.y - p_sel.start.y + 1;
const height = rect.bottom_right.y - rect.top_left.y + 1;
for (0..height) |_| terminal.index() catch |err| {
log.warn("failed to move cursor: {}", .{err});
break;
@ -201,7 +201,7 @@ fn display(
terminal.setCursorPos(
terminal.screen.cursor.y + 1,
p_sel.end.x + 1,
rect.bottom_right.x + 1,
);
},
}

View File

@ -351,51 +351,6 @@ pub const ImageStorage = struct {
},
};
}
/// Returns a selection of the entire rectangle this placement
/// occupies within the screen.
pub fn selection(
self: Placement,
image: Image,
t: *const terminal.Terminal,
) terminal.Selection {
// If we have columns/rows specified we can simplify this whole thing.
if (self.columns > 0 and self.rows > 0) {
return terminal.Selection{
.start = self.point,
.end = .{
.x = @min(self.point.x + self.columns, t.cols),
.y = @min(self.point.y + self.rows, t.rows),
},
};
}
// Calculate our cell size.
const terminal_width_f64: f64 = @floatFromInt(t.width_px);
const terminal_height_f64: f64 = @floatFromInt(t.height_px);
const grid_columns_f64: f64 = @floatFromInt(t.cols);
const grid_rows_f64: f64 = @floatFromInt(t.rows);
const cell_width_f64 = terminal_width_f64 / grid_columns_f64;
const cell_height_f64 = terminal_height_f64 / grid_rows_f64;
// Our image width
const width_px = if (self.source_width > 0) self.source_width else image.width;
const height_px = if (self.source_height > 0) self.source_height else image.height;
// Calculate our image size in grid cells
const width_f64: f64 = @floatFromInt(width_px);
const height_f64: f64 = @floatFromInt(height_px);
const width_cells: u32 = @intFromFloat(@ceil(width_f64 / cell_width_f64));
const height_cells: u32 = @intFromFloat(@ceil(height_f64 / cell_height_f64));
return .{
.start = self.point,
.end = .{
.x = @min(t.cols - 1, self.point.x + width_cells),
.y = self.point.y + height_cells,
},
};
}
};
};