aboutsummaryrefslogtreecommitdiff
path: root/vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig
diff options
context:
space:
mode:
authorCan Celik <ogulcancelik@gmail.com>2026-04-04 19:22:04 +0300
committerCan Celik <ogulcancelik@gmail.com>2026-04-05 01:23:06 +0300
commit41dd733ca7123c9ac4bfc6c781697a660e95b052 (patch)
tree3fc91815ca139ef2e0b281bba0abda4ef15175f4 /vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig
parent55c138263e190a1e79d4b997bfb9397306c0ec1f (diff)
feat: finish ghostty pane backend integration
Diffstat (limited to 'vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig')
-rw-r--r--vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig b/vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig
new file mode 100644
index 00000000..f689e45f
--- /dev/null
+++ b/vendor/libghostty-vt/src/apprt/gtk/weak_ref.zig
@@ -0,0 +1,44 @@
+const std = @import("std");
+const gtk = @import("gtk");
+const gobject = @import("gobject");
+
+/// A lightweight wrapper around gobject.WeakRef to make it type-safe
+/// to hold a single type of value.
+pub fn WeakRef(comptime T: type) type {
+ return struct {
+ const Self = @This();
+
+ ref: gobject.WeakRef = std.mem.zeroes(gobject.WeakRef),
+
+ pub const empty: Self = .{};
+
+ /// Set the weak reference to the given object. This will not
+ /// increase the reference count of the object.
+ pub fn set(self: *Self, v_: ?*T) void {
+ if (v_) |v| {
+ self.ref.set(v.as(gobject.Object));
+ } else {
+ self.ref.set(null);
+ }
+ }
+
+ /// Get a strong reference to the object, or null if the object
+ /// has been finalized. This increases the reference count by one.
+ pub fn get(self: *Self) ?*T {
+ // We can't use `as` because `as` guarantees conversion and
+ // that can't be statically guaranteed.
+ return gobject.ext.cast(T, self.ref.get() orelse return null);
+ }
+ };
+}
+
+test WeakRef {
+ const testing = std.testing;
+
+ var ref: WeakRef(gtk.TextBuffer) = .empty;
+ const obj: *gtk.TextBuffer = .new(null);
+ ref.set(obj);
+ ref.get().?.unref(); // The "?" asserts non-null
+ obj.unref();
+ try testing.expect(ref.get() == null);
+}