apprt/embedded: fix new size struct

This commit is contained in:
Mitchell Hashimoto
2024-11-14 13:58:03 -08:00
parent 3b856f6269
commit bdf3d1cb5f
3 changed files with 69 additions and 84 deletions

View File

@ -324,6 +324,32 @@ const DerivedConfig = struct {
for (self.links) |*link| link.regex.deinit(); for (self.links) |*link| link.regex.deinit();
self.arena.deinit(); self.arena.deinit();
} }
fn scaledPadding(self: *const DerivedConfig, x_dpi: f32, y_dpi: f32) renderer.Padding {
const padding_top: u32 = padding_top: {
const padding_top: f32 = @floatFromInt(self.window_padding_top);
break :padding_top @intFromFloat(@floor(padding_top * y_dpi / 72));
};
const padding_bottom: u32 = padding_bottom: {
const padding_bottom: f32 = @floatFromInt(self.window_padding_bottom);
break :padding_bottom @intFromFloat(@floor(padding_bottom * y_dpi / 72));
};
const padding_left: u32 = padding_left: {
const padding_left: f32 = @floatFromInt(self.window_padding_left);
break :padding_left @intFromFloat(@floor(padding_left * x_dpi / 72));
};
const padding_right: u32 = padding_right: {
const padding_right: f32 = @floatFromInt(self.window_padding_right);
break :padding_right @intFromFloat(@floor(padding_right * x_dpi / 72));
};
return .{
.top = padding_top,
.bottom = padding_bottom,
.left = padding_left,
.right = padding_right,
};
}
}; };
/// Create a new surface. This must be called from the main thread. The /// Create a new surface. This must be called from the main thread. The
@ -373,30 +399,6 @@ pub fn init(
// Pre-calculate our initial cell size ourselves. // Pre-calculate our initial cell size ourselves.
const cell_size = font_grid.cellSize(); const cell_size = font_grid.cellSize();
// Convert our padding from points to pixels
const padding_top: u32 = padding_top: {
const padding_top: f32 = @floatFromInt(derived_config.window_padding_top);
break :padding_top @intFromFloat(@floor(padding_top * y_dpi / 72));
};
const padding_bottom: u32 = padding_bottom: {
const padding_bottom: f32 = @floatFromInt(derived_config.window_padding_bottom);
break :padding_bottom @intFromFloat(@floor(padding_bottom * y_dpi / 72));
};
const padding_left: u32 = padding_left: {
const padding_left: f32 = @floatFromInt(derived_config.window_padding_left);
break :padding_left @intFromFloat(@floor(padding_left * x_dpi / 72));
};
const padding_right: u32 = padding_right: {
const padding_right: f32 = @floatFromInt(derived_config.window_padding_right);
break :padding_right @intFromFloat(@floor(padding_right * x_dpi / 72));
};
const padding: renderer.Padding = .{
.top = padding_top,
.bottom = padding_bottom,
.left = padding_left,
.right = padding_right,
};
// Build our size struct which has all the sizes we need. // Build our size struct which has all the sizes we need.
const size: renderer.Size = size: { const size: renderer.Size = size: {
var size: renderer.Size = .{ var size: renderer.Size = .{
@ -409,10 +411,10 @@ pub fn init(
}, },
.cell = font_grid.cellSize(), .cell = font_grid.cellSize(),
.padding = padding, .padding = derived_config.scaledPadding(x_dpi, y_dpi),
}; };
if (config.@"window-padding-balance") { if (derived_config.window_padding_balance) {
size.balancePadding(); size.balancePadding();
} }
@ -591,12 +593,12 @@ pub fn init(
// account for the padding so we get the exact correct grid size. // account for the padding so we get the exact correct grid size.
const final_width: u32 = const final_width: u32 =
@as(u32, @intFromFloat(@ceil(width_f32 / scale.x))) + @as(u32, @intFromFloat(@ceil(width_f32 / scale.x))) +
padding.left + size.padding.left +
padding.right; size.padding.right;
const final_height: u32 = const final_height: u32 =
@as(u32, @intFromFloat(@ceil(height_f32 / scale.y))) + @as(u32, @intFromFloat(@ceil(height_f32 / scale.y))) +
padding.top + size.padding.top +
padding.bottom; size.padding.bottom;
rt_app.performAction( rt_app.performAction(
.{ .surface = self }, .{ .surface = self },
@ -1151,7 +1153,7 @@ pub fn selectionInfo(self: *const Surface) ?apprt.Selection {
const x: f64 = x: { const x: f64 = x: {
// Simple x * cell width gives the left // Simple x * cell width gives the left
var x: f64 = @floatFromInt(tl_coord.x * self.cell_size.width); var x: f64 = @floatFromInt(tl_coord.x * self.size.cell.width);
// Add padding // Add padding
x += @floatFromInt(self.size.padding.left); x += @floatFromInt(self.size.padding.left);
@ -1164,10 +1166,10 @@ pub fn selectionInfo(self: *const Surface) ?apprt.Selection {
const y: f64 = y: { const y: f64 = y: {
// Simple y * cell height gives the top // Simple y * cell height gives the top
var y: f64 = @floatFromInt(tl_coord.y * self.cell_size.height); var y: f64 = @floatFromInt(tl_coord.y * self.size.cell.height);
// We want the text baseline // We want the text baseline
y += @floatFromInt(self.cell_size.height); y += @floatFromInt(self.size.cell.height);
y -= @floatFromInt(self.font_metrics.cell_baseline); y -= @floatFromInt(self.font_metrics.cell_baseline);
// Add padding // Add padding
@ -1212,10 +1214,10 @@ pub fn imePoint(self: *const Surface) apprt.IMEPos {
const x: f64 = x: { const x: f64 = x: {
// Simple x * cell width gives the top-left corner // Simple x * cell width gives the top-left corner
var x: f64 = @floatFromInt(cursor.x * self.cell_size.width); var x: f64 = @floatFromInt(cursor.x * self.size.cell.width);
// We want the midpoint // We want the midpoint
x += @as(f64, @floatFromInt(self.cell_size.width)) / 2; x += @as(f64, @floatFromInt(self.size.cell.width)) / 2;
// And scale it // And scale it
x /= content_scale.x; x /= content_scale.x;
@ -1225,10 +1227,10 @@ pub fn imePoint(self: *const Surface) apprt.IMEPos {
const y: f64 = y: { const y: f64 = y: {
// Simple x * cell width gives the top-left corner // Simple x * cell width gives the top-left corner
var y: f64 = @floatFromInt(cursor.y * self.cell_size.height); var y: f64 = @floatFromInt(cursor.y * self.size.cell.height);
// We want the bottom // We want the bottom
y += @floatFromInt(self.cell_size.height); y += @floatFromInt(self.size.cell.height);
// And scale it // And scale it
y /= content_scale.y; y /= content_scale.y;
@ -2299,34 +2301,8 @@ pub fn contentScaleCallback(self: *Surface, content_scale: apprt.ContentScale) !
// Update our padding which is dependent on DPI. We only do this for // Update our padding which is dependent on DPI. We only do this for
// unbalanced padding since balanced padding is not dependent on DPI. // unbalanced padding since balanced padding is not dependent on DPI.
if (!self.config.window_padding_balanced) { if (!self.config.window_padding_balance) {
self.size.padding = padding: { self.size.padding = self.config.scaledPadding(x_dpi, y_dpi);
const padding_top: u32 = padding_top: {
const padding_top: f32 = @floatFromInt(self.config.window_padding_top);
break :padding_top @intFromFloat(@floor(padding_top * y_dpi / 72));
};
const padding_bottom: u32 = padding_bottom: {
const padding_bottom: f32 = @floatFromInt(self.config.window_padding_bottom);
break :padding_bottom @intFromFloat(@floor(padding_bottom * y_dpi / 72));
};
const padding_left: u32 = padding_left: {
const padding_left: f32 = @floatFromInt(self.config.window_padding_left);
break :padding_left @intFromFloat(@floor(padding_left * x_dpi / 72));
};
const padding_right: u32 = padding_right: {
const padding_right: f32 = @floatFromInt(self.config.window_padding_right);
break :padding_right @intFromFloat(@floor(padding_right * x_dpi / 72));
};
break :padding .{
.top = padding_top,
.bottom = padding_bottom,
.left = padding_left,
.right = padding_right,
};
};
self.padding = self.size.padding;
} }
// Force a resize event because the change in padding will affect // Force a resize event because the change in padding will affect

View File

@ -1447,13 +1447,14 @@ pub const CAPI = struct {
/// Return the size information a surface has. /// Return the size information a surface has.
export fn ghostty_surface_size(surface: *Surface) SurfaceSize { export fn ghostty_surface_size(surface: *Surface) SurfaceSize {
const grid_size = surface.core_surface.size.grid();
return .{ return .{
.columns = surface.core_surface.grid_size.columns, .columns = grid_size.columns,
.rows = surface.core_surface.grid_size.rows, .rows = grid_size.rows,
.width_px = surface.core_surface.screen_size.width, .width_px = surface.core_surface.size.screen.width,
.height_px = surface.core_surface.screen_size.height, .height_px = surface.core_surface.size.screen.height,
.cell_width_px = surface.core_surface.cell_size.width, .cell_width_px = surface.core_surface.size.cell.width,
.cell_height_px = surface.core_surface.cell_size.height, .cell_height_px = surface.core_surface.size.cell.height,
}; };
} }

View File

@ -11,6 +11,7 @@ const cimgui = @import("cimgui");
const Surface = @import("../Surface.zig"); const Surface = @import("../Surface.zig");
const font = @import("../font/main.zig"); const font = @import("../font/main.zig");
const input = @import("../input.zig"); const input = @import("../input.zig");
const renderer = @import("../renderer.zig");
const terminal = @import("../terminal/main.zig"); const terminal = @import("../terminal/main.zig");
const inspector = @import("main.zig"); const inspector = @import("main.zig");
@ -641,8 +642,8 @@ fn renderSizeWindow(self: *Inspector) void {
_ = cimgui.c.igTableSetColumnIndex(1); _ = cimgui.c.igTableSetColumnIndex(1);
cimgui.c.igText( cimgui.c.igText(
"%dpx x %dpx", "%dpx x %dpx",
self.surface.screen_size.width, self.surface.size.screen.width,
self.surface.screen_size.height, self.surface.size.screen.height,
); );
} }
} }
@ -656,10 +657,11 @@ fn renderSizeWindow(self: *Inspector) void {
} }
{ {
_ = cimgui.c.igTableSetColumnIndex(1); _ = cimgui.c.igTableSetColumnIndex(1);
const grid_size = self.surface.size.grid();
cimgui.c.igText( cimgui.c.igText(
"%dc x %dr", "%dc x %dr",
self.surface.grid_size.columns, grid_size.columns,
self.surface.grid_size.rows, grid_size.rows,
); );
} }
} }
@ -675,8 +677,8 @@ fn renderSizeWindow(self: *Inspector) void {
_ = cimgui.c.igTableSetColumnIndex(1); _ = cimgui.c.igTableSetColumnIndex(1);
cimgui.c.igText( cimgui.c.igText(
"%dpx x %dpx", "%dpx x %dpx",
self.surface.cell_size.width, self.surface.size.cell.width,
self.surface.cell_size.height, self.surface.size.cell.height,
); );
} }
} }
@ -692,10 +694,10 @@ fn renderSizeWindow(self: *Inspector) void {
_ = cimgui.c.igTableSetColumnIndex(1); _ = cimgui.c.igTableSetColumnIndex(1);
cimgui.c.igText( cimgui.c.igText(
"T=%d B=%d L=%d R=%d px", "T=%d B=%d L=%d R=%d px",
self.surface.padding.top, self.surface.size.padding.top,
self.surface.padding.bottom, self.surface.size.padding.bottom,
self.surface.padding.left, self.surface.size.padding.left,
self.surface.padding.right, self.surface.size.padding.right,
); );
} }
} }
@ -785,7 +787,13 @@ fn renderSizeWindow(self: *Inspector) void {
} }
{ {
const adjusted = self.surface.posAdjusted(self.mouse.last_xpos, self.mouse.last_ypos); const coord: renderer.Coordinate.Terminal = (renderer.Coordinate{
.surface = .{
.x = self.mouse.last_xpos,
.y = self.mouse.last_ypos,
},
}).convert(.terminal, self.surface.size).terminal;
cimgui.c.igTableNextRow(cimgui.c.ImGuiTableRowFlags_None, 0); cimgui.c.igTableNextRow(cimgui.c.ImGuiTableRowFlags_None, 0);
{ {
_ = cimgui.c.igTableSetColumnIndex(0); _ = cimgui.c.igTableSetColumnIndex(0);
@ -795,8 +803,8 @@ fn renderSizeWindow(self: *Inspector) void {
_ = cimgui.c.igTableSetColumnIndex(1); _ = cimgui.c.igTableSetColumnIndex(1);
cimgui.c.igText( cimgui.c.igText(
"(%dpx, %dpx)", "(%dpx, %dpx)",
@as(i64, @intFromFloat(adjusted.x)), @as(i64, @intFromFloat(coord.x)),
@as(i64, @intFromFloat(adjusted.y)), @as(i64, @intFromFloat(coord.y)),
); );
} }
} }