From c64bcabb6ef458648c9550e96944aeabb2e3f93f Mon Sep 17 00:00:00 2001 From: "Jeffrey C. Ollie" Date: Tue, 29 Oct 2024 12:05:31 -0500 Subject: [PATCH] gtk: clean up context menu creation and refresh The preferred method to enable/disable menu options is to enable/disable the associated actions. --- src/apprt/gtk/App.zig | 23 ++++++++++------------- src/apprt/gtk/Surface.zig | 2 +- src/apprt/gtk/notebook.zig | 2 +- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index b00d65bce..424a97c3a 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -1531,7 +1531,13 @@ fn initContextMenu(self: *App) void { const menu = c.g_menu_new(); errdefer c.g_object_unref(menu); - createContextMenuCopyPasteSection(menu, false); + { + const section = c.g_menu_new(); + defer c.g_object_unref(section); + c.g_menu_append_section(menu, null, @ptrCast(@alignCast(section))); + c.g_menu_append(section, "Copy", "win.copy"); + c.g_menu_append(section, "Paste", "win.paste"); + } { const section = c.g_menu_new(); @@ -1552,18 +1558,9 @@ fn initContextMenu(self: *App) void { self.context_menu = menu; } -fn createContextMenuCopyPasteSection(menu: ?*c.GMenu, has_selection: bool) void { - const section = c.g_menu_new(); - defer c.g_object_unref(section); - c.g_menu_prepend_section(menu, null, @ptrCast(@alignCast(section))); - // FIXME: Feels really hackish, but disabling sensitivity on this doesn't seems to work(?) - c.g_menu_append(section, "Copy", if (has_selection) "win.copy" else "noop"); - c.g_menu_append(section, "Paste", "win.paste"); -} - -pub fn refreshContextMenu(self: *App, has_selection: bool) void { - c.g_menu_remove(self.context_menu, 0); - createContextMenuCopyPasteSection(self.context_menu, has_selection); +pub fn refreshContextMenu(_: *App, window: ?*c.GtkWindow, has_selection: bool) void { + const action: ?*c.GSimpleAction = @ptrCast(c.g_action_map_lookup_action(@ptrCast(window), "copy")); + c.g_simple_action_set_enabled(action, if (has_selection) 1 else 0); } fn isValidAppId(app_id: [:0]const u8) bool { diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 22124becd..c422f55ce 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -1156,7 +1156,7 @@ fn showContextMenu(self: *Surface, x: f32, y: f32) void { }; c.gtk_popover_set_pointing_to(@ptrCast(@alignCast(window.context_menu)), &rect); - self.app.refreshContextMenu(self.core_surface.hasSelection()); + self.app.refreshContextMenu(window.window, self.core_surface.hasSelection()); c.gtk_popover_popup(@ptrCast(@alignCast(window.context_menu))); } diff --git a/src/apprt/gtk/notebook.zig b/src/apprt/gtk/notebook.zig index baca32a30..0a23a2c37 100644 --- a/src/apprt/gtk/notebook.zig +++ b/src/apprt/gtk/notebook.zig @@ -504,7 +504,7 @@ fn adwTabViewSetupMenu(tab_view: *AdwTabView, page: *AdwTabPage, ud: ?*anyopaque const tab: *Tab = @ptrCast(@alignCast( c.g_object_get_data(@ptrCast(child), Tab.GHOSTTY_TAB) orelse return, )); - window.app.refreshContextMenu(if (tab.focus_child) |focus_child| focus_child.core_surface.hasSelection() else false); + window.app.refreshContextMenu(window.window, if (tab.focus_child) |focus_child| focus_child.core_surface.hasSelection() else false); c.adw_tab_view_set_menu_model(tab_view, @ptrCast(@alignCast(window.app.context_menu))); }