diff --git a/src/apprt/gtk-ng/class/command_palette.zig b/src/apprt/gtk-ng/class/command_palette.zig index ee10989b7..eb00f6df4 100644 --- a/src/apprt/gtk-ng/class/command_palette.zig +++ b/src/apprt/gtk-ng/class/command_palette.zig @@ -174,11 +174,14 @@ pub const CommandPalette = extern struct { } } + fn dialogClosed(_: adw.Dialog, self: *CommandPalette) callconv(.c) void { + self.unref(); + } + fn searchStopped(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void { // ESC was pressed - close the palette const priv = self.private(); _ = priv.dialog.close(); - self.unref(); } fn searchActivated(_: *gtk.SearchEntry, self: *CommandPalette) callconv(.c) void { @@ -198,11 +201,9 @@ pub const CommandPalette = extern struct { pub fn toggle(self: *CommandPalette, window: *Window) void { const priv = self.private(); - // If the dialog has been shown, close it and unref ourselves so all of - // our memory is reclaimed. + // If the dialog has been shown, close it. if (priv.dialog.as(gtk.Widget).getRealized() != 0) { _ = priv.dialog.close(); - self.unref(); return; } @@ -216,6 +217,10 @@ pub const CommandPalette = extern struct { /// Helper function to send a signal containing the action that should be /// performed. fn activated(self: *CommandPalette, pos: c_uint) void { + // add a reference to keep ourselves around until we're done + _ = self.ref(); + defer self.unref(); + const priv = self.private(); // Close before running the action in order to avoid being replaced by @@ -224,10 +229,6 @@ pub const CommandPalette = extern struct { // and cannot receive focus when reopened. _ = priv.dialog.close(); - // We are always done with the command palette when this finishes, even - // if there were errors. - defer self.unref(); - // Use priv.model and not priv.source here to use the list of *visible* results const object = priv.model.as(gio.ListModel).getObject(pos) orelse return; defer object.unref(); @@ -277,6 +278,7 @@ pub const CommandPalette = extern struct { class.bindTemplateChildPrivate("source", .{}); // Template Callbacks + class.bindTemplateCallback("closed", &dialogClosed); class.bindTemplateCallback("notify_config", &propConfig); class.bindTemplateCallback("search_stopped", &searchStopped); class.bindTemplateCallback("search_activated", &searchActivated); diff --git a/src/apprt/gtk-ng/class/surface.zig b/src/apprt/gtk-ng/class/surface.zig index 05393bd4f..2891840a0 100644 --- a/src/apprt/gtk-ng/class/surface.zig +++ b/src/apprt/gtk-ng/class/surface.zig @@ -361,19 +361,6 @@ pub const Surface = extern struct { void, ); }; - - /// Emitted when this surface requests that the command palette be - /// toggled. - pub const @"toggle-command-palette" = struct { - pub const name = "toggle-command-palette"; - pub const connect = impl.connect; - const impl = gobject.ext.defineSignal( - name, - Self, - &.{}, - void, - ); - }; }; const Private = struct { @@ -564,13 +551,7 @@ pub const Surface = extern struct { } pub fn toggleCommandPalette(self: *Self) bool { - signals.@"toggle-command-palette".impl.emit( - self, - null, - .{}, - null, - ); - return true; + return self.as(gtk.Widget).activateAction("win.toggle-command-palette", null) != 0; } /// Set the current progress report state. @@ -2423,7 +2404,6 @@ pub const Surface = extern struct { signals.@"present-request".impl.register(.{}); signals.@"toggle-fullscreen".impl.register(.{}); signals.@"toggle-maximize".impl.register(.{}); - signals.@"toggle-command-palette".impl.register(.{}); // Virtual methods gobject.Object.virtual_methods.dispose.implement(class, &dispose); diff --git a/src/apprt/gtk-ng/class/window.zig b/src/apprt/gtk-ng/class/window.zig index f3e8ee129..ee5753560 100644 --- a/src/apprt/gtk-ng/class/window.zig +++ b/src/apprt/gtk-ng/class/window.zig @@ -714,13 +714,6 @@ pub const Window = extern struct { self, .{}, ); - _ = Surface.signals.@"toggle-command-palette".connect( - surface, - *Self, - surfaceToggleCommandPalette, - self, - .{}, - ); // If we've never had a surface initialize yet, then we register // this signal. Its theoretically possible to launch multiple surfaces @@ -1529,15 +1522,6 @@ pub const Window = extern struct { // We react to the changes in the propMaximized callback } - /// React to a signal from a surface requesting that the command palette - /// be toggled. - fn surfaceToggleCommandPalette( - _: *Surface, - self: *Self, - ) callconv(.c) void { - self.toggleCommandPalette(); - } - fn surfaceInit( surface: *Surface, self: *Self, @@ -1722,7 +1706,7 @@ pub const Window = extern struct { fn toggleCommandPalette(self: *Window) void { const priv = self.private(); // Get a reference to a command palette. First check the weak reference - // that we save to see if we already have stored. If we don't then + // that we save to see if we already have one stored. If we don't then // create a new one. const command_palette = gobject.ext.cast(CommandPalette, priv.command_palette.get()) orelse command_palette: { // Create a fresh command palette. @@ -1747,14 +1731,14 @@ pub const Window = extern struct { .{}, ); + // Save a weak reference to the command palette. We use a weak reference to avoid + // reference counting cycles that might cause problems later. + priv.command_palette.set(command_palette.as(gobject.Object)); + break :command_palette command_palette; }; defer command_palette.unref(); - // Save a weak reference to the command palette. We use a weak reference to avoid - // reference counting cycles that might cause problems later. - priv.command_palette.set(command_palette.as(gobject.Object)); - // Tell the command palette to toggle itself. If the dialog gets // presented (instead of hidden) it will be modal over our window. command_palette.toggle(self); diff --git a/src/apprt/gtk-ng/ui/1.5/command-palette.blp b/src/apprt/gtk-ng/ui/1.5/command-palette.blp index 0ccae1f0a..473fb1f06 100644 --- a/src/apprt/gtk-ng/ui/1.5/command-palette.blp +++ b/src/apprt/gtk-ng/ui/1.5/command-palette.blp @@ -4,6 +4,7 @@ using Adw 1; Adw.Dialog dialog { content-width: 700; + closed => $closed(); Adw.ToolbarView { top-bar-style: flat;