Don't crash on huge padding, warn users if padding is absurd

This commit is contained in:
Mitchell Hashimoto
2022-11-14 17:41:15 -08:00
parent e6c7fd0214
commit 334743e8a7
3 changed files with 17 additions and 4 deletions

View File

@ -581,6 +581,14 @@ fn sizeCallback(window: glfw.Window, width: i32, height: i32) void {
// Recalculate our grid size // Recalculate our grid size
win.grid_size = win.renderer.gridSize(screen_size); win.grid_size = win.renderer.gridSize(screen_size);
if (win.grid_size.columns < 5 and (win.padding.left > 0 or win.padding.right > 0)) {
log.warn("WARNING: very small terminal grid detected with padding " ++
"set. Is your padding reasonable?", .{});
}
if (win.grid_size.rows < 2 and (win.padding.top > 0 or win.padding.bottom > 0)) {
log.warn("WARNING: very small terminal grid detected with padding " ++
"set. Is your padding reasonable?", .{});
}
// Mail the IO thread // Mail the IO thread
_ = win.io_thread.mailbox.push(.{ _ = win.io_thread.mailbox.push(.{

View File

@ -93,6 +93,11 @@ pub const Config = struct {
/// the window border. The "x" option applies to the left and right /// the window border. The "x" option applies to the left and right
/// padding and the "y" option is top and bottom. The value is in points, /// padding and the "y" option is top and bottom. The value is in points,
/// meaning that it will be scaled appropriately for screen DPI. /// meaning that it will be scaled appropriately for screen DPI.
///
/// If this value is set too large, the screen will render nothing, because
/// the grid will be completely squished by the padding. It is up to you
/// as the user to pick a reasonable value. If you pick an unreasonable
/// value, a warning will appear in the logs.
@"window-padding-x": u32 = 0, @"window-padding-x": u32 = 0,
@"window-padding-y": u32 = 0, @"window-padding-y": u32 = 0,

View File

@ -48,8 +48,8 @@ pub const ScreenSize = struct {
/// Subtract padding from the screen size. /// Subtract padding from the screen size.
pub fn subPadding(self: ScreenSize, padding: Padding) ScreenSize { pub fn subPadding(self: ScreenSize, padding: Padding) ScreenSize {
return .{ return .{
.width = self.width - @floatToInt(u32, padding.left + padding.right), .width = self.width -| @floatToInt(u32, padding.left + padding.right),
.height = self.height - @floatToInt(u32, padding.top + padding.bottom), .height = self.height -| @floatToInt(u32, padding.top + padding.bottom),
}; };
} }
}; };
@ -71,8 +71,8 @@ pub const GridSize = struct {
/// Update the columns/rows for the grid based on the given screen and /// Update the columns/rows for the grid based on the given screen and
/// cell size. /// cell size.
pub fn update(self: *GridSize, screen: ScreenSize, cell: CellSize) void { pub fn update(self: *GridSize, screen: ScreenSize, cell: CellSize) void {
self.columns = @floatToInt(Unit, @intToFloat(f32, screen.width) / cell.width); self.columns = @max(1, @floatToInt(Unit, @intToFloat(f32, screen.width) / cell.width));
self.rows = @floatToInt(Unit, @intToFloat(f32, screen.height) / cell.height); self.rows = @max(1, @floatToInt(Unit, @intToFloat(f32, screen.height) / cell.height));
} }
}; };