From 3c4bd47de384f2fe54357921556c31d6b00e5322 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 6 Dec 2023 20:53:32 -0800 Subject: [PATCH] apprt/gtk: stylistic changes --- src/apprt/gtk/Split.zig | 27 ++++++++++++++++++++++----- src/apprt/gtk/Surface.zig | 12 +++++++----- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/apprt/gtk/Split.zig b/src/apprt/gtk/Split.zig index 1971f865c..b4cecefbd 100644 --- a/src/apprt/gtk/Split.zig +++ b/src/apprt/gtk/Split.zig @@ -15,7 +15,25 @@ const c = @import("c.zig"); const log = std.log.scoped(.gtk); -pub const Orientation = enum { horizontal, vertical }; +/// The split orientation. +pub const Orientation = enum { + horizontal, + vertical, + + pub fn fromDirection(direction: input.SplitDirection) Orientation { + return switch (direction) { + .right => .horizontal, + .down => .vertical, + }; + } + + pub fn fromResizeDirection(direction: input.SplitResizeDirection) Orientation { + return switch (direction) { + .up, .down => .vertical, + .left, .right => .horizontal, + }; + } +}; /// Our actual GtkPaned widget paned: *c.GtkPaned, @@ -83,10 +101,7 @@ pub fn init( .container = container, .top_left = .{ .surface = sibling }, .bottom_right = .{ .surface = surface }, - .orientation = (switch (direction) { - .right => .horizontal, - .down => .vertical, - }), + .orientation = Orientation.fromDirection(direction), }; // Replace the previous containers element with our split. @@ -146,12 +161,14 @@ fn removeChild( alloc.destroy(self); } +/// Move the divider in the given direction by the given amount. pub fn moveDivider(self: *Split, direction: input.SplitResizeDirection, amount: u16) void { const pos = c.gtk_paned_get_position(self.paned); const new = switch (direction) { .up, .left => pos - amount, .down, .right => pos + amount, }; + c.gtk_paned_set_position(self.paned, new); } diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 51e6a5146..5535c380d 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -153,7 +153,10 @@ pub const Container = union(enum) { /// Returns the first split with the given orientation, walking upwards in /// the tree. - pub fn firstSplitWithOrientation(self: Container, orientation: Split.Orientation) ?*Split { + pub fn firstSplitWithOrientation( + self: Container, + orientation: Split.Orientation, + ) ?*Split { return switch (self) { .none, .tab_ => null, .split_tl, .split_br => split: { @@ -573,10 +576,9 @@ pub fn gotoSplit(self: *const Surface, direction: input.SplitFocusDirection) voi } pub fn resizeSplit(self: *const Surface, direction: input.SplitResizeDirection, amount: u16) void { - const s = self.container.firstSplitWithOrientation(switch (direction) { - .up, .down => .vertical, - .left, .right => .horizontal, - }) orelse return; + const s = self.container.firstSplitWithOrientation( + Split.Orientation.fromResizeDirection(direction), + ) orelse return; s.moveDivider(direction, amount); }