diff --git a/macos/Sources/Features/Terminal/TerminalController.swift b/macos/Sources/Features/Terminal/TerminalController.swift index 1aa2d0c6f..0d5aa5a9f 100644 --- a/macos/Sources/Features/Terminal/TerminalController.swift +++ b/macos/Sources/Features/Terminal/TerminalController.swift @@ -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) diff --git a/src/apprt/glfw.zig b/src/apprt/glfw.zig index 89448c354..f38214e32 100644 --- a/src/apprt/glfw.zig +++ b/src/apprt/glfw.zig @@ -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. diff --git a/src/termio/Exec.zig b/src/termio/Exec.zig index 7961ea4a9..55d47defa 100644 --- a/src/termio/Exec.zig +++ b/src/termio/Exec.zig @@ -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), }); } }