apprt/gtk-ng: proper memory management for config errors dialog

This commit is contained in:
Mitchell Hashimoto
2025-07-17 06:32:53 -07:00
parent b1aab1e7bf
commit b253e2efe2

View File

@ -35,16 +35,27 @@ pub const ConfigErrorsDialog = extern struct {
.nick = "config", .nick = "config",
.blurb = "The configuration that this dialog is showing errors for.", .blurb = "The configuration that this dialog is showing errors for.",
.default = null, .default = null,
.accessor = gobject.ext.privateFieldAccessor( .accessor = .{
Self, .getter = Self.getConfig,
Private, .setter = Self.setConfig,
&Private.offset, },
"config",
),
}, },
); );
}; };
pub const signals = struct {
pub const @"reload-config" = struct {
pub const name = "reload-config";
pub const connect = impl.connect;
const impl = gobject.ext.defineSignal(
name,
Self,
&.{},
void,
);
};
};
const Private = struct { const Private = struct {
config: ?*Config, config: ?*Config,
var offset: c_int = 0; var offset: c_int = 0;
@ -67,10 +78,57 @@ pub const ConfigErrorsDialog = extern struct {
} }
} }
fn response(
self: *Self,
response_id: [*:0]const u8,
) callconv(.C) void {
if (std.mem.orderZ(u8, response_id, "reload") != .eq) return;
signals.@"reload-config".impl.emit(
self,
null,
.{},
null,
);
}
fn dispose(self: *Self) callconv(.C) void {
gtk.Widget.disposeTemplate(self.as(gtk.Widget), getGObjectType());
const priv = self.private();
if (priv.config) |v| {
v.unref();
priv.config = null;
}
gobject.Object.virtual_methods.dispose.call(
Class.parent,
self.as(Parent),
);
}
fn getConfig(self: *Self) ?*Config {
return self.private().config;
}
fn setConfig(self: *Self, config: ?*Config) void {
const priv = self.private();
if (priv.config) |old| old.unref();
if (config) |newv| _ = newv.ref();
priv.config = config;
}
pub fn as(win: *Self, comptime T: type) *T { pub fn as(win: *Self, comptime T: type) *T {
return gobject.ext.as(T, win); return gobject.ext.as(T, win);
} }
fn private(self: *Self) *Private {
return gobject.ext.impl_helpers.getPrivate(
self,
Private,
Private.offset,
);
}
pub const Class = extern struct { pub const Class = extern struct {
parent_class: Parent.Class, parent_class: Parent.Class,
var parent: *Parent.Class = undefined; var parent: *Parent.Class = undefined;
@ -90,9 +148,17 @@ pub const ConfigErrorsDialog = extern struct {
}, },
); );
// Properties
gobject.ext.registerProperties(class, &.{ gobject.ext.registerProperties(class, &.{
properties.config, properties.config,
}); });
// Signals
signals.@"reload-config".impl.register(.{});
// Virtual methods
gobject.Object.virtual_methods.dispose.implement(class, &dispose);
Parent.virtual_methods.response.implement(class, &response);
} }
pub fn as(class: *Class, comptime T: type) *T { pub fn as(class: *Class, comptime T: type) *T {