gtk: adw toolbar view

gate toolbar view on ADWAITA_MINOR >= 4

use the right destructor on window creation errors
This commit is contained in:
Paul
2024-06-12 20:43:41 +02:00
committed by Paul Berg
parent 618a7a3fea
commit bdf618d7af
3 changed files with 41 additions and 18 deletions

View File

@ -124,10 +124,7 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void {
// c.gtk_notebook_set_show_tabs(notebook, 1); // c.gtk_notebook_set_show_tabs(notebook, 1);
// } // }
// Attach all events
//
// // Attach all events
_ = c.g_signal_connect_data(box_widget, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT); _ = c.g_signal_connect_data(box_widget, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT);
// We need to grab focus after Surface and Tab is added to the window. When // We need to grab focus after Surface and Tab is added to the window. When
@ -174,7 +171,7 @@ pub fn gtkTabCloseClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {
fn gtkDestroy(v: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void { fn gtkDestroy(v: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
_ = v; _ = v;
log.debug("tab box destroy", .{}); log.info("tab box destroy", .{});
// When our box is destroyed, we want to destroy our tab, too. // When our box is destroyed, we want to destroy our tab, too.
const tab: *Tab = @ptrCast(@alignCast(ud)); const tab: *Tab = @ptrCast(@alignCast(ud));

View File

@ -59,10 +59,17 @@ pub fn init(self: *Window, app: *App) !void {
.context_menu = undefined, .context_menu = undefined,
}; };
const adwaita = build_options.libadwaita and app.config.@"gtk-adwaita";
// Create the window // Create the window
const window = c.gtk_application_window_new(app.app); const adw_window = adwaita and app.config.@"gtk-titlebar" and c.ADW_MINOR_VERSION >= 4;
const window: *c.GtkWidget = if (adw_window)
c.adw_application_window_new(app.app)
else
c.gtk_application_window_new(app.app);
const gtk_window: *c.GtkWindow = @ptrCast(window); const gtk_window: *c.GtkWindow = @ptrCast(window);
errdefer c.gtk_window_destroy(gtk_window); errdefer if (adw_window) c.adw_application_window_destroy(window) else c.gtk_application_window_destroy(gtk_window);
self.window = gtk_window; self.window = gtk_window;
c.gtk_window_set_title(gtk_window, "Ghostty"); c.gtk_window_set_title(gtk_window, "Ghostty");
c.gtk_window_set_default_size(gtk_window, 1000, 600); c.gtk_window_set_default_size(gtk_window, 1000, 600);
@ -78,6 +85,8 @@ pub fn init(self: *Window, app: *App) !void {
c.gtk_widget_set_opacity(@ptrCast(window), app.config.@"background-opacity"); c.gtk_widget_set_opacity(@ptrCast(window), app.config.@"background-opacity");
} }
var header: ?*c.GtkHeaderBar = null;
// Internally, GTK ensures that only one instance of this provider exists in the provider list // Internally, GTK ensures that only one instance of this provider exists in the provider list
// for the display. // for the display.
const display = c.gdk_display_get_default(); const display = c.gdk_display_get_default();
@ -88,8 +97,7 @@ pub fn init(self: *Window, app: *App) !void {
// are decorated or not because we can have a keybind to toggle the // are decorated or not because we can have a keybind to toggle the
// decorations. // decorations.
if (app.config.@"gtk-titlebar") { if (app.config.@"gtk-titlebar") {
const header = c.gtk_header_bar_new(); header = @ptrCast(c.gtk_header_bar_new());
c.gtk_window_set_titlebar(gtk_window, header);
{ {
const btn = c.gtk_menu_button_new(); const btn = c.gtk_menu_button_new();
c.gtk_widget_set_tooltip_text(btn, "Main Menu"); c.gtk_widget_set_tooltip_text(btn, "Main Menu");
@ -142,8 +150,24 @@ pub fn init(self: *Window, app: *App) !void {
// Our actions for the menu // Our actions for the menu
initActions(self); initActions(self);
// The box is our main child if (build_options.libadwaita and app.config.@"gtk-adwaita" and app.config.@"gtk-titlebar" and header != null and c.ADW_MINOR_VERSION >= 4) {
c.gtk_window_set_child(gtk_window, box); const toolbar_view: *c.AdwToolbarView = @ptrCast(c.adw_toolbar_view_new());
c.adw_toolbar_view_add_top_bar(toolbar_view, @ptrCast(@alignCast(header.?)));
const tab_bar = c.adw_tab_bar_new();
c.adw_tab_bar_set_view(tab_bar, self.notebook.adw_tab_view);
if (!app.config.@"gtk-wide-tabs") c.adw_tab_bar_set_expand_tabs(tab_bar, 0);
c.adw_toolbar_view_add_top_bar(toolbar_view, @ptrCast(@alignCast(tab_bar)));
c.adw_toolbar_view_set_content(toolbar_view, box);
c.adw_application_window_set_content(@ptrCast(gtk_window), @ptrCast(@alignCast(toolbar_view)));
} else {
// The box is our main child
c.gtk_window_set_child(gtk_window, box);
if (header) |h| c.gtk_window_set_titlebar(gtk_window, @ptrCast(@alignCast(h)));
}
// Show the window // Show the window
c.gtk_widget_show(window); c.gtk_widget_show(window);

View File

@ -20,14 +20,16 @@ pub const Notebook = union(enum) {
const adwaita = build_options.libadwaita and app.config.@"gtk-adwaita"; const adwaita = build_options.libadwaita and app.config.@"gtk-adwaita";
if (adwaita) { if (adwaita) {
log.warn("using adwaita", .{});
const tab_view = c.adw_tab_view_new(); const tab_view = c.adw_tab_view_new();
const tab_bar = c.adw_tab_bar_new();
c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar)));
c.adw_tab_bar_set_view(tab_bar, tab_view);
if (!window.app.config.@"gtk-wide-tabs") if (!window.app.config.@"gtk-titlebar" or c.ADW_MINOR_VERSION < 4) {
c.adw_tab_bar_set_expand_tabs(tab_bar, 0); const tab_bar = c.adw_tab_bar_new();
c.gtk_box_append(@ptrCast(box), @ptrCast(@alignCast(tab_bar)));
c.adw_tab_bar_set_view(tab_bar, tab_view);
if (!window.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, "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); _ = c.g_signal_connect_data(tab_view, "create-window", c.G_CALLBACK(&adwTabViewCreateWindow), window, null, c.G_CONNECT_DEFAULT);
@ -357,7 +359,7 @@ fn gtkPageAdded(
fn adwSelectPage(_: *c.GObject, _: *c.GParamSpec, ud: ?*anyopaque) void { fn adwSelectPage(_: *c.GObject, _: *c.GParamSpec, ud: ?*anyopaque) void {
const window = userdataSelf(ud.?); const window = userdataSelf(ud.?);
const page = c.adw_tab_view_get_selected_page(window.notebook.adw_tab_view); const page = c.adw_tab_view_get_selected_page(window.notebook.adw_tab_view) orelse return;
const title = c.adw_tab_page_get_title(page); const title = c.adw_tab_page_get_title(page);
c.gtk_window_set_title(window.window, title); c.gtk_window_set_title(window.window, title);
} }