From 1037265cd90cfa9412cb6abe58fb2833857386ae Mon Sep 17 00:00:00 2001 From: Paul Berg Date: Fri, 13 Sep 2024 11:03:40 +0200 Subject: [PATCH 1/2] apprt/gtk: make Notebook.create not depend on parent box --- src/apprt/gtk/Window.zig | 26 ++++++++++++++++++++++++- src/apprt/gtk/notebook.zig | 39 ++++++++++++-------------------------- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 49541ce82..591520c2f 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -197,7 +197,8 @@ pub fn init(self: *Window, app: *App) !void { } // Setup our notebook - self.notebook = Notebook.create(self, box); + self.notebook = Notebook.create(self); + c.gtk_box_append(@ptrCast(box), self.notebook.asWidget()); // If we have a tab overview then we can set it on our notebook. if (tab_overview_) |tab_overview| { @@ -270,6 +271,29 @@ pub fn init(self: *Window, app: *App) !void { ); } } else { + switch (self.notebook) { + .adw_tab_view => |tab_view| if (comptime adwaita.versionAtLeast(0, 0, 0)) { + // 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().?; + switch (app.config.@"gtk-tabs-location") { + // left and right is not supported in libadwaita. + .top, + .left, + .right, + => c.gtk_box_prepend(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), + + .bottom => c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), + } + c.adw_tab_bar_set_view(tab_bar, tab_view); + + if (!app.config.@"gtk-wide-tabs") { + c.adw_tab_bar_set_expand_tabs(tab_bar, 0); + } + }, + else => {}, + } + // The box is our main child c.gtk_window_set_child(gtk_window, box); if (self.header) |h| c.gtk_window_set_titlebar(gtk_window, @ptrCast(@alignCast(h))); diff --git a/src/apprt/gtk/notebook.zig b/src/apprt/gtk/notebook.zig index 30c6178eb..1d126658c 100644 --- a/src/apprt/gtk/notebook.zig +++ b/src/apprt/gtk/notebook.zig @@ -16,13 +16,13 @@ pub const Notebook = union(enum) { adw_tab_view: *AdwTabView, gtk_notebook: *c.GtkNotebook, - pub fn create(window: *Window, box: *c.GtkWidget) @This() { + pub fn create(window: *Window) @This() { const app = window.app; - if (adwaita.enabled(&app.config)) return initAdw(window, box); - return initGtk(window, box); + if (adwaita.enabled(&app.config)) return initAdw(window); + return initGtk(window); } - fn initGtk(window: *Window, box: *c.GtkWidget) Notebook { + fn initGtk(window: *Window) Notebook { const app = window.app; // Create a notebook to hold our tabs. @@ -57,36 +57,21 @@ pub const Notebook = union(enum) { _ = c.g_signal_connect_data(notebook, "switch-page", c.G_CALLBACK(>kSwitchPage), window, null, c.G_CONNECT_DEFAULT); _ = c.g_signal_connect_data(notebook, "create-window", c.G_CALLBACK(>kNotebookCreateWindow), window, null, c.G_CONNECT_DEFAULT); - c.gtk_box_append(@ptrCast(box), notebook_widget); return .{ .gtk_notebook = notebook }; } - fn initAdw(window: *Window, box: *c.GtkWidget) Notebook { + pub fn asWidget(self: Notebook) *c.GtkWidget { + return switch (self) { + .adw_tab_view => |tab_view| @ptrCast(@alignCast(tab_view)), + .gtk_notebook => |notebook| @ptrCast(@alignCast(notebook)), + }; + } + + fn initAdw(window: *Window) Notebook { const app = window.app; assert(adwaita.enabled(&app.config)); const tab_view: *c.AdwTabView = c.adw_tab_view_new().?; - c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_view))); - if ((comptime !adwaita.versionAtLeast(1, 4, 0)) or - !adwaita.versionAtLeast(1, 4, 0) or - !app.config.@"gtk-titlebar") - { - const tab_bar: *c.AdwTabBar = c.adw_tab_bar_new().?; - switch (app.config.@"gtk-tabs-location") { - // left and right is not supported in libadwaita. - .top, - .left, - .right, - => c.gtk_box_prepend(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), - - .bottom => c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), - } - c.adw_tab_bar_set_view(tab_bar, tab_view); - - if (!app.config.@"gtk-wide-tabs") { - c.adw_tab_bar_set_expand_tabs(tab_bar, 0); - } - } _ = c.g_signal_connect_data(tab_view, "page-attached", c.G_CALLBACK(&adwPageAttached), window, null, c.G_CONNECT_DEFAULT); _ = c.g_signal_connect_data(tab_view, "create-window", c.G_CALLBACK(&adwTabViewCreateWindow), window, null, c.G_CONNECT_DEFAULT); From 4a539cf4c978aa1c4dcf769226a71f79131676a9 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 13 Sep 2024 09:51:34 -0700 Subject: [PATCH 2/2] apprt/gtk: style tweaks --- src/apprt/gtk/Window.zig | 13 ++++++++++--- src/apprt/gtk/notebook.zig | 16 ++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 591520c2f..59539f377 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -281,9 +281,15 @@ pub fn init(self: *Window, app: *App) !void { .top, .left, .right, - => c.gtk_box_prepend(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), + => c.gtk_box_prepend( + @ptrCast(box), + @ptrCast(@alignCast(tab_bar)), + ), - .bottom => c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar))), + .bottom => c.gtk_box_append( + @ptrCast(box), + @ptrCast(@alignCast(tab_bar)), + ), } c.adw_tab_bar_set_view(tab_bar, tab_view); @@ -291,7 +297,8 @@ pub fn init(self: *Window, app: *App) !void { c.adw_tab_bar_set_expand_tabs(tab_bar, 0); } }, - else => {}, + + .gtk_notebook => {}, } // The box is our main child diff --git a/src/apprt/gtk/notebook.zig b/src/apprt/gtk/notebook.zig index 1d126658c..44c46795d 100644 --- a/src/apprt/gtk/notebook.zig +++ b/src/apprt/gtk/notebook.zig @@ -16,7 +16,7 @@ pub const Notebook = union(enum) { adw_tab_view: *AdwTabView, gtk_notebook: *c.GtkNotebook, - pub fn create(window: *Window) @This() { + pub fn create(window: *Window) Notebook { const app = window.app; if (adwaita.enabled(&app.config)) return initAdw(window); return initGtk(window); @@ -60,13 +60,6 @@ pub const Notebook = union(enum) { return .{ .gtk_notebook = notebook }; } - pub fn asWidget(self: Notebook) *c.GtkWidget { - return switch (self) { - .adw_tab_view => |tab_view| @ptrCast(@alignCast(tab_view)), - .gtk_notebook => |notebook| @ptrCast(@alignCast(notebook)), - }; - } - fn initAdw(window: *Window) Notebook { const app = window.app; assert(adwaita.enabled(&app.config)); @@ -80,6 +73,13 @@ pub const Notebook = union(enum) { return .{ .adw_tab_view = tab_view }; } + pub fn asWidget(self: Notebook) *c.GtkWidget { + return switch (self) { + .adw_tab_view => |tab_view| @ptrCast(@alignCast(tab_view)), + .gtk_notebook => |notebook| @ptrCast(@alignCast(notebook)), + }; + } + pub fn nPages(self: Notebook) c_int { return switch (self) { .gtk_notebook => |notebook| c.gtk_notebook_get_n_pages(notebook),