Merge pull request #2232 from tristan957/tabview

apprt/gtk: Add support for AdwTabOverview
This commit is contained in:
Mitchell Hashimoto
2024-09-12 16:19:20 -07:00
committed by GitHub
3 changed files with 91 additions and 6 deletions

View File

@ -37,6 +37,11 @@ elem: Surface.Container.Elem,
// can easily re-focus that terminal.
focus_child: *Surface,
/// If the notebook implementation is AdwTabView, then this is the tab's
/// AdwTabPage. If we have libadwaita disabled then this will be undefined
/// memory and is unsafe to use.
adw_tab_page: *c.GObject,
pub fn create(alloc: Allocator, window: *Window, parent_: ?*CoreSurface) !*Tab {
var tab = try alloc.create(Tab);
errdefer alloc.destroy(tab);
@ -53,6 +58,7 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void {
.box = undefined,
.elem = undefined,
.focus_child = undefined,
.adw_tab_page = undefined,
};
// Create a Box in which we'll later keep either Surface or Split.
@ -76,7 +82,9 @@ pub fn init(self: *Tab, window: *Window, parent_: ?*CoreSurface) !void {
// Set the userdata of the box to point to this tab.
c.g_object_set_data(@ptrCast(box_widget), GHOSTTY_TAB, self);
try window.notebook.addTab(self, "Ghostty");
if (try window.notebook.addTab(self, "Ghostty")) |page| {
self.adw_tab_page = page;
}
// Attach all events
_ = c.g_signal_connect_data(box_widget, "destroy", c.G_CALLBACK(&gtkDestroy), self, null, c.G_CONNECT_DEFAULT);

View File

@ -97,6 +97,25 @@ pub fn init(self: *Window, app: *App) !void {
const display = c.gdk_display_get_default();
c.gtk_style_context_add_provider_for_display(display, @ptrCast(app.css_provider), c.GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
// Create our box which will hold our widgets in the main content area.
const box = c.gtk_box_new(c.GTK_ORIENTATION_VERTICAL, 0);
// If we are using an AdwWindow then we can support the tab overview.
const tab_overview_: ?*c.GtkWidget = if (self.isAdwWindow()) overview: {
const tab_overview = c.adw_tab_overview_new();
c.adw_tab_overview_set_enable_new_tab(@ptrCast(tab_overview), 1);
_ = c.g_signal_connect_data(
tab_overview,
"create-tab",
c.G_CALLBACK(&gtkNewTabFromOverview),
self,
null,
c.G_CONNECT_DEFAULT,
);
break :overview tab_overview;
} else null;
// gtk-titlebar can be used to disable the header bar (but keep
// the window manager's decorations). We create this no matter if we
// are decorated or not because we can have a keybind to toggle the
@ -117,6 +136,25 @@ pub fn init(self: *Window, app: *App) !void {
else
c.gtk_header_bar_pack_end(@ptrCast(header), btn);
}
// If we're using an AdwWindow then we can support the tab overview.
if (tab_overview_) |tab_overview| {
assert(self.isAdwWindow());
const btn = c.gtk_toggle_button_new();
c.gtk_widget_set_tooltip_text(btn, "Show Open Tabs");
c.gtk_button_set_icon_name(@ptrCast(btn), "view-grid-symbolic");
c.gtk_widget_set_focus_on_click(btn, c.FALSE);
c.adw_header_bar_pack_end(@ptrCast(header), btn);
_ = c.g_object_bind_property(
btn,
"active",
tab_overview,
"open",
c.G_BINDING_BIDIRECTIONAL | c.G_BINDING_SYNC_CREATE,
);
}
{
const btn = c.gtk_button_new_from_icon_name("tab-new-symbolic");
c.gtk_widget_set_tooltip_text(btn, "New Tab");
@ -135,9 +173,6 @@ pub fn init(self: *Window, app: *App) !void {
c.gtk_window_set_decorated(gtk_window, 0);
}
// Create our box which will hold our widgets.
const box = c.gtk_box_new(c.GTK_ORIENTATION_VERTICAL, 0);
// In debug we show a warning and apply the 'devel' class to the window.
// This is a really common issue where people build from source in debug and performance is really bad.
if (comptime std.debug.runtime_safety) {
@ -161,8 +196,15 @@ pub fn init(self: *Window, app: *App) !void {
c.gtk_box_append(@ptrCast(box), warning_box);
}
// Setup our notebook
self.notebook = Notebook.create(self, box);
// If we have a tab overview then we can set it on our notebook.
if (tab_overview_) |tab_overview| {
assert(self.notebook == .adw_tab_view);
c.adw_tab_overview_set_view(@ptrCast(tab_overview), self.notebook.adw_tab_view);
}
self.context_menu = c.gtk_popover_menu_new_from_model(@ptrCast(@alignCast(self.app.context_menu)));
c.gtk_widget_set_parent(self.context_menu, window);
c.gtk_popover_set_has_arrow(@ptrCast(@alignCast(self.context_menu)), c.False);
@ -210,7 +252,23 @@ pub fn init(self: *Window, app: *App) !void {
c.gtk_widget_set_visible(header_widget, 0);
}
c.adw_application_window_set_content(@ptrCast(gtk_window), @ptrCast(@alignCast(toolbar_view)));
// Set our application window content. The content depends on if
// we're using an AdwTabOverview or not.
if (tab_overview_) |tab_overview| {
c.adw_tab_overview_set_child(
@ptrCast(tab_overview),
@ptrCast(@alignCast(toolbar_view)),
);
c.adw_application_window_set_content(
@ptrCast(gtk_window),
@ptrCast(@alignCast(tab_overview)),
);
} else {
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);
@ -379,6 +437,18 @@ fn gtkTabNewClick(_: *c.GtkButton, ud: ?*anyopaque) callconv(.C) void {
};
}
/// Create a new tab from the AdwTabOverview. We can't copy gtkTabNewClick
/// because we need to return an AdwTabPage from this function.
fn gtkNewTabFromOverview(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) ?*c.GObject {
const self: *Window = @ptrCast(@alignCast(ud orelse return null));
assert(self.isAdwWindow());
const alloc = self.app.core_app.alloc;
const surface = self.actionSurface() orelse return null;
const tab = Tab.create(alloc, self, surface) catch return null;
return tab.adw_tab_page;
}
fn gtkRefocusTerm(v: *c.GtkWindow, ud: ?*anyopaque) callconv(.C) bool {
_ = v;
log.debug("refocus term request", .{});

View File

@ -198,7 +198,10 @@ pub const Notebook = union(enum) {
}
}
pub fn addTab(self: Notebook, tab: *Tab, title: [:0]const u8) !void {
/// Adds a new tab with the given title to the notebook. If the notebook
/// is an adwaita tab view, this will return an AdwTabPage. If the notebook
/// is a GTK notebook, this will return null.
pub fn addTab(self: Notebook, tab: *Tab, title: [:0]const u8) !?*c.GObject {
const box_widget: *c.GtkWidget = @ptrCast(tab.box);
switch (self) {
.adw_tab_view => |tab_view| {
@ -209,6 +212,8 @@ pub const Notebook = union(enum) {
// Switch to the new tab
c.adw_tab_view_set_selected_page(tab_view, page);
return @ptrCast(@alignCast(page));
},
.gtk_notebook => |notebook| {
// Build the tab label
@ -265,6 +270,8 @@ pub const Notebook = union(enum) {
// Switch to the new tab
c.gtk_notebook_set_current_page(notebook, page_idx);
return null;
},
}
}