apprt/gtk-ng: helper for getAncestor

This commit is contained in:
Mitchell Hashimoto
2025-07-29 10:58:25 -07:00
parent 179fa8e5aa
commit 4fb790ca4c
3 changed files with 33 additions and 20 deletions

View File

@@ -22,6 +22,7 @@ const xev = @import("../../../global.zig").xev;
const CoreConfig = configpkg.Config;
const CoreSurface = @import("../../../Surface.zig");
const ext = @import("../ext.zig");
const adw_version = @import("../adw_version.zig");
const gtk_version = @import("../gtk_version.zig");
const winprotopkg = @import("../winproto.zig");
@@ -1285,13 +1286,13 @@ const Action = struct {
// be aware they might be in windows but at the app level we
// can do this.
const surface = core.rt_surface.surface;
const window_widget = surface
.as(gtk.Widget)
.getAncestor(gobject.ext.typeFor(Window)) orelse {
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring new_tab", .{});
return false;
};
const window = gobject.ext.cast(Window, window_widget).?;
window.newTab(core);
return true;
},

View File

@@ -11,6 +11,7 @@ const i18n = @import("../../../os/main.zig").i18n;
const apprt = @import("../../../apprt.zig");
const input = @import("../../../input.zig");
const CoreSurface = @import("../../../Surface.zig");
const ext = @import("../ext.zig");
const gtk_version = @import("../gtk_version.zig");
const adw_version = @import("../adw_version.zig");
const gresource = @import("../build/gresource.zig");
@@ -719,17 +720,13 @@ pub const Window = extern struct {
_: *CloseConfirmationDialog,
page: *adw.TabPage,
) callconv(.c) void {
const tab_view_widget = page
.getChild()
.as(gtk.Widget)
.getAncestor(gobject.ext.typeFor(adw.TabView)) orelse {
const tab_view = ext.getAncestor(
adw.TabView,
page.getChild().as(gtk.Widget),
) orelse {
log.warn("close confirmation called for non-existent page", .{});
return;
};
const tab_view = gobject.ext.cast(
adw.TabView,
tab_view_widget,
).?;
tab_view.closePageFinish(page, @intFromBool(true));
}
@@ -737,17 +734,13 @@ pub const Window = extern struct {
_: *CloseConfirmationDialog,
page: *adw.TabPage,
) callconv(.c) void {
const tab_view_widget = page
.getChild()
.as(gtk.Widget)
.getAncestor(gobject.ext.typeFor(adw.TabView)) orelse {
const tab_view = ext.getAncestor(
adw.TabView,
page.getChild().as(gtk.Widget),
) orelse {
log.warn("close confirmation called for non-existent page", .{});
return;
};
const tab_view = gobject.ext.cast(
adw.TabView,
tab_view_widget,
).?;
tab_view.closePageFinish(page, @intFromBool(false));
}

19
src/apprt/gtk-ng/ext.zig Normal file
View File

@@ -0,0 +1,19 @@
//! Extensions/helpers for GTK objects, following a similar naming
//! style to zig-gobject. These should, wherever possible, be Zig-friendly
//! wrappers around existing GTK functionality, rather than complex new
//! helpers.
const std = @import("std");
const assert = std.debug.assert;
const gobject = @import("gobject");
const gtk = @import("gtk");
/// Wrapper around `gtk.Widget.getAncestor` to get the widget ancestor
/// of the given type `T`, or null if it doesn't exist.
pub fn getAncestor(comptime T: type, widget: *gtk.Widget) ?*T {
const ancestor_ = widget.getAncestor(gobject.ext.typeFor(T));
const ancestor = ancestor_ orelse return null;
// We can assert the unwrap because getAncestor above
return gobject.ext.cast(T, ancestor).?;
}