git.lucas.co / cce-compositor
Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git

commitb4a26b4350741d32c158c9ba2548f52ccf20b53e
parentaba248d59a
authorIsaac Freund <[email protected]>
date2023-01-06 17:47
rivertile: fix code to disallow 0 main count

Also document this limit

 doc/rivertile.1.scd |  1 +
 rivertile/main.zig  | 19 +++++++++----------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/doc/rivertile.1.scd b/doc/rivertile.1.scd
index 7b845a5..a91994b 100644
--- a/doc/rivertile.1.scd
+++ b/doc/rivertile.1.scd
@@ -54,6 +54,7 @@ These commands may be sent to rivertile at runtime with the help of
 	Set or modify the number of views in the main area of the layout. If
 	_value_ is prefixed by a +/- sign, _value_ is added/subtracted from the
 	current count. If there is no sign, the main count is set to _value_.
+	Note that the main count cannot be decreased below 1.
 
 *main-ratio* _value_
 	Set or modify the ratio of the main area to total layout area. If
diff --git a/rivertile/main.zig b/rivertile/main.zig
index dee3892..293ffeb 100644
--- a/rivertile/main.zig
+++ b/rivertile/main.zig
@@ -173,9 +173,11 @@ const Output = struct {
                             '+' => output.main_count +|= @intCast(u31, arg),
                             '-' => {
                                 const result = output.main_count +| arg;
-                                if (result >= 0) output.main_count = @intCast(u31, result);
+                                if (result >= 1) output.main_count = @intCast(u31, result);
+                            },
+                            else => {
+                                if (arg >= 1) output.main_count = @intCast(u31, arg);
                             },
-                            else => output.main_count = @intCast(u31, arg),
                         }
                     },
                     .@"main-ratio" => {
@@ -194,7 +196,9 @@ const Output = struct {
             },
 
             .layout_demand => |ev| {
-                const main_count = math.clamp(output.main_count, 1, @truncate(u31, ev.view_count));
+                assert(ev.view_count > 0);
+
+                const main_count = math.min(output.main_count, @truncate(u31, ev.view_count));
                 const secondary_count = @truncate(u31, ev.view_count) -| main_count;
 
                 const usable_width = switch (output.main_location) {
@@ -216,7 +220,7 @@ const Output = struct {
                 var secondary_height: u31 = undefined;
                 var secondary_height_rem: u31 = undefined;
 
-                if (main_count > 0 and secondary_count > 0) {
+                if (secondary_count > 0) {
                     main_width = @floatToInt(u31, output.main_ratio * @intToFloat(f64, usable_width));
                     main_height = usable_height / main_count;
                     main_height_rem = usable_height % main_count;
@@ -224,15 +228,10 @@ const Output = struct {
                     secondary_width = usable_width - main_width;
                     secondary_height = usable_height / secondary_count;
                     secondary_height_rem = usable_height % secondary_count;
-                } else if (main_count > 0) {
+                } else {
                     main_width = usable_width;
                     main_height = usable_height / main_count;
                     main_height_rem = usable_height % main_count;
-                } else if (secondary_width > 0) {
-                    main_width = 0;
-                    secondary_width = usable_width;
-                    secondary_height = usable_height / secondary_count;
-                    secondary_height_rem = usable_height % secondary_count;
                 }
 
                 var i: u31 = 0;