Balance padding uses the explicit padding value for grid calculations

This fixes window resize not working properly when
`window-padding-balance` is set to true.
This commit is contained in:
Mitchell Hashimoto
2024-11-15 15:15:11 -08:00
parent 7dbcde7286
commit 7605472922
2 changed files with 27 additions and 7 deletions

View File

@ -411,11 +411,17 @@ pub fn init(
},
.cell = font_grid.cellSize(),
.padding = derived_config.scaledPadding(x_dpi, y_dpi),
.padding = .{},
};
const explicit: renderer.Padding = derived_config.scaledPadding(
x_dpi,
y_dpi,
);
if (derived_config.window_padding_balance) {
size.balancePadding();
size.balancePadding(explicit);
} else {
size.padding = explicit;
}
break :size size;
@ -1358,7 +1364,7 @@ fn setSelection(self: *Surface, sel_: ?terminal.Selection) !void {
fn setCellSize(self: *Surface, size: renderer.CellSize) !void {
// Update our cell size within our size struct
self.size.cell = size;
if (self.config.window_padding_balance) self.size.balancePadding();
self.balancePaddingIfNeeded();
// Notify the terminal
self.io.queueMessage(.{ .resize = self.size }, .unlocked);
@ -1440,7 +1446,7 @@ pub fn sizeCallback(self: *Surface, size: apprt.SurfaceSize) !void {
fn resize(self: *Surface, size: renderer.ScreenSize) !void {
// Save our screen size
self.size.screen = size;
if (self.config.window_padding_balance) self.size.balancePadding();
self.balancePaddingIfNeeded();
// Recalculate our grid size. Because Ghostty supports fluid resizing,
// its possible the grid doesn't change at all even if the screen size changes.
@ -1460,6 +1466,15 @@ fn resize(self: *Surface, size: renderer.ScreenSize) !void {
self.io.queueMessage(.{ .resize = self.size }, .unlocked);
}
/// Recalculate the balanced padding if needed.
fn balancePaddingIfNeeded(self: *Surface) void {
if (!self.config.window_padding_balance) return;
const content_scale = try self.rt_surface.getContentScale();
const x_dpi = content_scale.x * font.face.default_dpi;
const y_dpi = content_scale.y * font.face.default_dpi;
self.size.balancePadding(self.config.scaledPadding(x_dpi, y_dpi));
}
/// Called to set the preedit state for character input. Preedit is used
/// with dead key states, for example, when typing an accent character.
/// This should be called with null to reset the preedit state.

View File

@ -31,9 +31,14 @@ pub const Size = struct {
return self.screen.subPadding(self.padding);
}
/// Set the padding to be balanced around the grid. Overwrites the current
/// padding.
pub fn balancePadding(self: *Size) void {
/// Set the padding to be balanced around the grid. The balanced
/// padding is calculated AFTER the explicit padding is taken
/// into account.
pub fn balancePadding(self: *Size, explicit: Padding) void {
// This ensure grid() does the right thing
self.padding = explicit;
// Now we can calculate the balanced padding
self.padding = Padding.balanced(
self.screen,
self.grid(),