feat: auto hide tabs widget if there are less than {n} tabs

This can be configured via an integer argument in the bar widget descriptor, or
via the tab styles configuration file. A value of 0 will effectively disable
auto hide.
This commit is contained in:
CJ van den Berg 2025-08-03 17:08:53 +02:00
parent dfcc825e4b
commit b97b8e8fb7
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -11,7 +11,11 @@ const Widget = @import("../Widget.zig");
const WidgetList = @import("../WidgetList.zig"); const WidgetList = @import("../WidgetList.zig");
const Button = @import("../Button.zig"); const Button = @import("../Button.zig");
const default_min_tabs = 2;
const @"style.config" = struct { const @"style.config" = struct {
default_minimum_tabs_shown: usize = 2,
dirty_indicator: []const u8 = "", dirty_indicator: []const u8 = "",
spacer: []const u8 = "|", spacer: []const u8 = "|",
@ -52,10 +56,11 @@ const @"style.config" = struct {
}; };
pub const Style = @"style.config"; pub const Style = @"style.config";
pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, _: ?[]const u8) @import("widget.zig").CreateError!Widget { pub fn create(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, arg: ?[]const u8) @import("widget.zig").CreateError!Widget {
const min_tabs = if (arg) |str_size| std.fmt.parseInt(usize, str_size, 10) catch null else null;
const self = try allocator.create(TabBar); const self = try allocator.create(TabBar);
errdefer allocator.destroy(self); errdefer allocator.destroy(self);
self.* = try TabBar.init(allocator, parent, event_handler); self.* = try TabBar.init(allocator, parent, event_handler, min_tabs);
return Widget.to(self); return Widget.to(self);
} }
@ -67,6 +72,7 @@ const TabBar = struct {
event_handler: ?EventHandler, event_handler: ?EventHandler,
tabs: []TabBarTab = &[_]TabBarTab{}, tabs: []TabBarTab = &[_]TabBarTab{},
active_buffer_ref: ?usize = null, active_buffer_ref: ?usize = null,
minimum_tabs_shown: usize,
tab_style: Style, tab_style: Style,
tab_style_bufs: [][]const u8, tab_style_bufs: [][]const u8,
@ -78,7 +84,7 @@ const TabBar = struct {
widget: Widget, widget: Widget,
}; };
fn init(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler) !Self { fn init(allocator: std.mem.Allocator, parent: Plane, event_handler: ?EventHandler, min_tabs: ?usize) !Self {
var w = try WidgetList.createH(allocator, parent, "tabs", .dynamic); var w = try WidgetList.createH(allocator, parent, "tabs", .dynamic);
w.ctx = w; w.ctx = w;
const tab_style, const tab_style_bufs = root.read_config(Style, allocator); const tab_style, const tab_style_bufs = root.read_config(Style, allocator);
@ -90,6 +96,7 @@ const TabBar = struct {
.event_handler = event_handler, .event_handler = event_handler,
.tab_style = tab_style, .tab_style = tab_style,
.tab_style_bufs = tab_style_bufs, .tab_style_bufs = tab_style_bufs,
.minimum_tabs_shown = min_tabs orelse tab_style.default_minimum_tabs_shown,
}; };
} }
@ -101,7 +108,10 @@ const TabBar = struct {
} }
pub fn layout(self: *Self) Widget.Layout { pub fn layout(self: *Self) Widget.Layout {
return self.widget_list_widget.layout(); return if (self.tabs.len >= self.minimum_tabs_shown)
self.widget_list_widget.layout()
else
.{ .static = 0 };
} }
pub fn update(self: *Self) void { pub fn update(self: *Self) void {