Merge pull request #2151 from ghostty-org/clamp-win

Clamp initial window size configurations to screen size
This commit is contained in:
Mitchell Hashimoto
2024-08-26 10:13:32 -07:00
committed by GitHub
3 changed files with 22 additions and 8 deletions

View File

@ -281,14 +281,15 @@ class TerminalController: NSWindowController, NSWindowDelegate,
// If we have only a single surface (no splits) and that surface requested
// an initial size then we set it here now.
if case let .leaf(leaf) = surfaceTree {
if let initialSize = leaf.surface.initialSize {
if let initialSize = leaf.surface.initialSize,
let screen = window.screen ?? NSScreen.main {
// Setup our frame. We need to first subtract the views frame so that we can
// just get the chrome frame so that we only affect the surface view size.
var frame = window.frame
frame.size.width -= leaf.surface.frame.size.width
frame.size.height -= leaf.surface.frame.size.height
frame.size.width += initialSize.width
frame.size.height += initialSize.height
frame.size.width += min(initialSize.width, screen.frame.width)
frame.size.height += min(initialSize.height, screen.frame.height)
// We have no tabs and we are not a split, so set the initial size of the window.
window.setFrame(frame, display: true)

View File

@ -551,7 +551,16 @@ pub const Surface = struct {
/// surface initialization time. This may be called before "self"
/// is fully initialized.
pub fn setInitialWindowSize(self: *const Surface, width: u32, height: u32) !void {
self.window.setSize(.{ .width = width, .height = height });
const monitor = self.window.getMonitor() orelse glfw.Monitor.getPrimary() orelse {
log.warn("window is not on a monitor, not setting initial size", .{});
return;
};
const workarea = monitor.getWorkarea();
self.window.setSize(.{
.width = @min(width, workarea.width),
.height = @min(height, workarea.height),
});
}
/// Set the cell size. Unused by GLFW.

View File

@ -1066,11 +1066,15 @@ const Subprocess = struct {
self.screen_size = screen_size;
if (self.pty) |*pty| {
// It is theoretically possible for the grid or screen size to
// exceed u16, although the terminal in that case isn't very
// usable. This should be protected upstream but we still clamp
// in case there is a bad caller which has happened before.
try pty.setSize(.{
.ws_row = @intCast(grid_size.rows),
.ws_col = @intCast(grid_size.columns),
.ws_xpixel = @intCast(screen_size.width),
.ws_ypixel = @intCast(screen_size.height),
.ws_row = std.math.cast(u16, grid_size.rows) orelse std.math.maxInt(u16),
.ws_col = std.math.cast(u16, grid_size.columns) orelse std.math.maxInt(u16),
.ws_xpixel = std.math.cast(u16, screen_size.width) orelse std.math.maxInt(u16),
.ws_ypixel = std.math.cast(u16, screen_size.height) orelse std.math.maxInt(u16),
});
}
}