fix: calculate cell size before presenting gtk window

This commit is contained in:
AJ Bucci
2026-01-26 16:00:01 -05:00
committed by Jeffrey C. Ollie
parent 4581392625
commit d9d65fdb9f
2 changed files with 61 additions and 0 deletions

View File

@@ -2182,6 +2182,18 @@ const Action = struct {
// Create a new tab with window context (first tab in new window)
win.newTabForWindow(parent);
// Compute the initial window size before presenting so the window
// manager can position it correctly.
if (win.getActiveSurface()) |surface| {
surface.computeInitialSize();
if (surface.getDefaultSize()) |size| {
win.as(gtk.Window).setDefaultSize(
@intCast(size.width),
@intCast(size.height),
);
}
}
// Show the window
gtk.Window.present(win.as(gtk.Window));
}

View File

@@ -2005,6 +2005,55 @@ pub const Surface = extern struct {
self.as(gobject.Object).notifyByPspec(properties.@"default-size".impl.param_spec);
}
/// Compute and set the initial window size from config and font metrics.
/// This can be called before the core surface exists to set up the window
/// size before presenting.
pub fn computeInitialSize(self: *Self) void {
const priv = self.private();
const config_obj = priv.config orelse return;
const config = config_obj.get();
// Both dimensions must be configured
if (config.@"window-height" <= 0 or config.@"window-width" <= 0) return;
const app = Application.default();
const alloc = app.allocator();
// Get content scale and compute DPI
const content_scale = self.getContentScale();
const x_dpi = content_scale.x * font.face.default_dpi;
const y_dpi = content_scale.y * font.face.default_dpi;
const font_size: font.face.DesiredSize = .{
.points = config.@"font-size",
.xdpi = @intFromFloat(x_dpi),
.ydpi = @intFromFloat(y_dpi),
};
// Get font grid for cell metrics
var derived_config = font.SharedGridSet.DerivedConfig.init(alloc, config) catch return;
defer derived_config.deinit();
const font_grid_key, const font_grid = app.core().font_grid_set.ref(
&derived_config,
font_size,
) catch return;
defer app.core().font_grid_set.deref(font_grid_key);
const cell = font_grid.cellSize();
// Calculate size (matching recomputeInitialSize logic)
const width = @max(config.@"window-width", 10) * cell.width;
const height = @max(config.@"window-height", 4) * cell.height;
const width_f32: f32 = @floatFromInt(width);
const height_f32: f32 = @floatFromInt(height);
const final_width: u32 = @intFromFloat(@ceil(width_f32 / content_scale.x));
const final_height: u32 = @intFromFloat(@ceil(height_f32 / content_scale.y));
self.setDefaultSize(.{ .width = final_width, .height = final_height });
}
/// Get the key sequence list. Full transfer.
fn getKeySequence(self: *Self) ?*ext.StringList {
const priv = self.private();