-
Notifications
You must be signed in to change notification settings - Fork 697
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement custom split size #2564
Conversation
src/input/Binding.zig
Outdated
@@ -454,6 +454,13 @@ pub const Action = union(enum) { | |||
auto, // splits along the larger direction | |||
}; | |||
|
|||
pub const Percentage = []const u8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should really be a numeric parameter. I'd suggest a f32 that is clamped to 0.0 <= pct <= 1.0. That way the percentage doesn't have to be re-parsed every time a window is split.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree, thanks for pointing that out @jcollie. I tried initially to set is as u16
but then was having issues parcing the suggested syntax "new_split:right,30%" to invalidate something like "new_split:right,30" just for new splits while keeping valid for other actions.
After your comment, I tried a couple of other things, and got this so far. How about setting Percentage as a Struct instead, such as:
pub const Percentage = struct {
value: u16,
pub fn init(value: u16) Percentage {
return .{ .value = value };
}
};
and then in the parseParemeter
logic add a Struct in the switch statement, such as:
.Struct => if (field_.type == Percentage) try parsePercentage(field_.type, next),
The parcePercentage
function would be something like:
fn parsePercentage(comptime T: type, value: []const u8) !T {
if (value.len < 2) return Error.InvalidFormat;
if (value[value.len - 1] != '%') return Error.InvalidFormat;
const percent = value[0 .. value.len - 1];
const val = std.fmt.parseInt(u16, percent, 10) catch return Error.InvalidFormat;
return Percentage.init(val);
}
I found that this is working and passing the corresponding tests, as well as differentiating actions such as resize_split:up,10
that require an int, from new_split:left,30%
that should enforce an int ending with %
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed Percentage to f32, which is clamped between 0 and 1 in the latest commit, and parameter parsing logic is being handled by parseFloat
instead, so the newly added parsePercentage
has been removed.
In the last commits I changed from f32 to u16 to avoid some extra hashing when parsing the flags (which somehow was needed when using a float.). I think u16 leads to a cleaner approach, imho. At the end, obviously the design choice is entirely yours and I could see this not being merged as it involves a lot of changes and design decisions. However it was a fun and nice learning experience for me. |
Hi @mitchellh,
This is my take on custom split sizes, as referred in #2227, for your review and consideration. Bare in mind that this is my first time working in Zig 😅.
Known issue:
sibling.size
insrc/apprt/gtk/Split.zig
, I triedsibling.core_surface.screen_size
or playing around withgrid_size
andcell_size
but still got the same issue. Update: this has been fixed now withgetContentScale
.Unresolved problem:
Tests:
src/input/Binding.zig
("parse: action with enum"
) for parsing the new action flag and tests are passing.Update:
Custom size splits work in gtk and macOS (with the known issue for gtk above but not for macOS).