From 12dd99ddd91655c8cdf4fe42dc3d790bc39391b3 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Dec 2024 20:34:24 -0800 Subject: [PATCH] apprt/gtk: prevent a new split from being smaller than 2x2 Fixes #2092 This isn't perfect because it only prevents _new_ splits from being too small. You can still resize the window to make them smaller. This just helps prevent the very-easy-to-trigger crash of #2092. We don't need to do this to macOS because it doesn't crash in the same way with zero-sized splits. Long term we should really chase down what breaks in GTK at a root level when we have zero-sized splits. But this is a quick fix for now to prevent the easy crash I feel like people might stress test and run into with the 1.0 release. --- src/apprt/gtk/Split.zig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/apprt/gtk/Split.zig b/src/apprt/gtk/Split.zig index 54fa30e1c..83ba04da0 100644 --- a/src/apprt/gtk/Split.zig +++ b/src/apprt/gtk/Split.zig @@ -70,6 +70,27 @@ pub fn init( sibling: *Surface, direction: apprt.action.SplitDirection, ) !void { + // If our sibling is too small to be split in half then we don't + // allow the split to happen. This avoids a situation where the + // split becomes too small. + // + // This is kind of a hack. Ideally we'd use gtk_widget_set_size_request + // properly along the path to ensure minimum sizes. I don't know if + // GTK even respects that all but any way GTK does this for us seems + // better than this. + { + // This is the min size of the sibling split. This means the + // smallest split is half of this. + const multiplier = 4; + + const size = &sibling.core_surface.size; + const small = switch (direction) { + .right, .left => size.screen.width < size.cell.width * multiplier, + .down, .up => size.screen.height < size.cell.height * multiplier, + }; + if (small) return error.SplitTooSmall; + } + // Create the new child surface for the other direction. const alloc = sibling.app.core_app.alloc; var surface = try Surface.create(alloc, sibling.app, .{