Skip to content

gtk: add option to always display the tab bar #5590

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions src/apprt/gtk/Window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;

const adw = @import("adw");
const gio = @import("gio");
const glib = @import("glib");
const gobject = @import("gobject");
Expand Down Expand Up @@ -52,6 +53,9 @@ window: *c.GtkWindow,
/// The header bar for the window.
headerbar: HeaderBar,

/// The tab bar for the window.
tab_bar: *adw.TabBar,

/// The tab overview for the window. This is possibly null since there is no
/// taboverview without a AdwApplicationWindow (libadwaita >= 1.4.0).
tab_overview: ?*c.GtkWidget,
Expand Down Expand Up @@ -80,6 +84,7 @@ pub const DerivedConfig = struct {
gtk_tabs_location: configpkg.Config.GtkTabsLocation,
gtk_wide_tabs: bool,
gtk_toolbar_style: configpkg.Config.GtkToolbarStyle,
window_show_tab_bar: configpkg.Config.WindowShowTabBar,

quick_terminal_position: configpkg.Config.QuickTerminalPosition,
quick_terminal_size: configpkg.Config.QuickTerminalSize,
Expand All @@ -99,6 +104,7 @@ pub const DerivedConfig = struct {
.gtk_tabs_location = config.@"gtk-tabs-location",
.gtk_wide_tabs = config.@"gtk-wide-tabs",
.gtk_toolbar_style = config.@"gtk-toolbar-style",
.window_show_tab_bar = config.@"window-show-tab-bar",

.quick_terminal_position = config.@"quick-terminal-position",
.quick_terminal_size = config.@"quick-terminal-size",
Expand Down Expand Up @@ -133,6 +139,7 @@ pub fn init(self: *Window, app: *App) !void {
.config = DerivedConfig.init(&app.config),
.window = undefined,
.headerbar = undefined,
.tab_bar = undefined,
.tab_overview = null,
.notebook = undefined,
.titlebar_menu = undefined,
Expand Down Expand Up @@ -215,8 +222,10 @@ pub fn init(self: *Window, app: *App) !void {
// If we're using an AdwWindow then we can support the tab overview.
if (self.tab_overview) |tab_overview| {
if (!adwaita.versionAtLeast(1, 4, 0)) unreachable;
const btn = switch (self.config.gtk_tabs_location) {
.top, .bottom => btn: {

// TODO: Move this to syncAppearance and make it reactive
const btn = switch (self.config.window_show_tab_bar) {
.always, .auto => btn: {
const btn = c.gtk_toggle_button_new();
c.gtk_widget_set_tooltip_text(btn, i18n._("View Open Tabs"));
c.gtk_button_set_icon_name(@ptrCast(btn), "view-grid-symbolic");
Expand All @@ -230,8 +239,7 @@ pub fn init(self: *Window, app: *App) !void {

break :btn btn;
},

.hidden => btn: {
.never => btn: {
const btn = c.adw_tab_button_new();
c.adw_tab_button_set_view(@ptrCast(btn), @ptrCast(@alignCast(self.notebook.tab_view)));
c.gtk_actionable_set_action_name(@ptrCast(btn), "overview.open");
Expand Down Expand Up @@ -310,23 +318,17 @@ pub fn init(self: *Window, app: *App) !void {
// Our actions for the menu
initActions(self);

self.tab_bar = adw.TabBar.new();
self.tab_bar.setView(self.notebook.tab_view);

if (adwaita.versionAtLeast(1, 4, 0)) {
const toolbar_view: *c.AdwToolbarView = @ptrCast(c.adw_toolbar_view_new());

c.adw_toolbar_view_add_top_bar(toolbar_view, self.headerbar.asWidget());

if (self.config.gtk_tabs_location != .hidden) {
const tab_bar = c.adw_tab_bar_new();
c.adw_tab_bar_set_view(tab_bar, @ptrCast(@alignCast(self.notebook.tab_view)));

if (!self.config.gtk_wide_tabs) c.adw_tab_bar_set_expand_tabs(tab_bar, 0);

const tab_bar_widget: *c.GtkWidget = @ptrCast(@alignCast(tab_bar));
switch (self.config.gtk_tabs_location) {
.top => c.adw_toolbar_view_add_top_bar(toolbar_view, tab_bar_widget),
.bottom => c.adw_toolbar_view_add_bottom_bar(toolbar_view, tab_bar_widget),
.hidden => unreachable,
}
switch (self.config.gtk_tabs_location) {
.top => c.adw_toolbar_view_add_top_bar(toolbar_view, @ptrCast(@alignCast(self.tab_bar))),
.bottom => c.adw_toolbar_view_add_bottom_bar(toolbar_view, @ptrCast(@alignCast(self.tab_bar))),
}
c.adw_toolbar_view_set_content(toolbar_view, box);

Expand All @@ -347,27 +349,22 @@ pub fn init(self: *Window, app: *App) !void {
@ptrCast(gtk_widget),
@ptrCast(@alignCast(self.tab_overview)),
);
} else tab_bar: {
if (self.config.gtk_tabs_location == .hidden) break :tab_bar;
} else {
// In earlier adwaita versions, we need to add the tabbar manually since we do not use
// an AdwToolbarView.
const tab_bar: *c.AdwTabBar = c.adw_tab_bar_new().?;
c.gtk_widget_add_css_class(@ptrCast(@alignCast(tab_bar)), "inline");
self.tab_bar.as(gtk.Widget).addCssClass("inline");

switch (self.config.gtk_tabs_location) {
.top => c.gtk_box_insert_child_after(
@ptrCast(box),
@ptrCast(@alignCast(tab_bar)),
@ptrCast(@alignCast(self.tab_bar)),
@ptrCast(@alignCast(self.headerbar.asWidget())),
),
.bottom => c.gtk_box_append(
@ptrCast(box),
@ptrCast(@alignCast(tab_bar)),
@ptrCast(@alignCast(self.tab_bar)),
),
.hidden => unreachable,
}
c.adw_tab_bar_set_view(tab_bar, @ptrCast(@alignCast(self.notebook.tab_view)));

if (!self.config.gtk_wide_tabs) c.adw_tab_bar_set_expand_tabs(tab_bar, 0);
}

// If we want the window to be maximized, we do that here.
Expand Down Expand Up @@ -480,6 +477,16 @@ pub fn syncAppearance(self: *Window) !void {
c.adw_toolbar_view_set_bottom_bar_style(toolbar_view, toolbar_style);
}

self.tab_bar.setExpandTabs(@intFromBool(self.config.gtk_wide_tabs));
self.tab_bar.setAutohide(switch (self.config.window_show_tab_bar) {
.auto, .never => @intFromBool(true),
.always => @intFromBool(false),
});
self.tab_bar.as(gtk.Widget).setVisible(switch (self.config.window_show_tab_bar) {
.always, .auto => @intFromBool(true),
.never => @intFromBool(false),
});

self.winproto.syncAppearance() catch |err| {
log.warn("failed to sync winproto appearance error={}", .{err});
};
Expand Down
29 changes: 28 additions & 1 deletion src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,27 @@ keybind: Keybinds = .{},
/// * `end` - Insert the new tab at the end of the tab list.
@"window-new-tab-position": WindowNewTabPosition = .current,

/// Whether to show the tab bar.
///
/// Valid values:
///
/// - `always`
///
/// Always display the tab bar, even when there's only one tab.
///
/// - `auto` *(default)*
///
/// Automatically show and hide the tab bar. The tab bar is only
/// shown when there are two or more tabs present.
///
/// - `never`
///
/// Never show the tab bar. Tabs are only accessible via the tab
/// overview or by keybind actions.
///
/// Currently only supported on Linux (GTK).
@"window-show-tab-bar": WindowShowTabBar = .auto,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just remove gtk-tabs-location=hidden?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO probably yeah, but then we need to have a migration mechanism for people who are using that config. (See, I did tell everyone that we might run into more complex config migrations in the future 😅)

Copy link

@lcgix0 lcgix0 Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After trying that out, I must revise my comment above and state that I find hidden tabs less practical than large tabs. Not sure why I thought it wouldn't be fully hidden or maybe auto-hidden. Sorry folks, my bad, thanks for listening to my opinion. I'll experiment with CSS.


/// Background color for the window titlebar. This only takes effect if
/// window-theme is set to ghostty. Currently only supported in the GTK app
/// runtime.
Expand Down Expand Up @@ -5662,7 +5683,6 @@ pub const GtkSingleInstance = enum {
pub const GtkTabsLocation = enum {
top,
bottom,
hidden,
};

/// See gtk-toolbar-style
Expand Down Expand Up @@ -5705,6 +5725,13 @@ pub const WindowNewTabPosition = enum {
end,
};

/// See window-show-tab-bar
pub const WindowShowTabBar = enum {
always,
auto,
never,
};

/// See resize-overlay
pub const ResizeOverlay = enum {
always,
Expand Down