From 6e5bc62726bf9c5520682a2b4698535ab7694f17 Mon Sep 17 00:00:00 2001 From: karei Date: Sat, 20 Jul 2024 23:02:17 +0300 Subject: [PATCH] apprt/gtk: disable copy in context menu while without selection Left a FIXME where the "Copy" button action is disabled. Though very hackish this was the best way I found to do this currently. Disable sensitivity on the button didn't do anything and trying to remove the button altogether like on macOS, causes the menu to become really buggy. Either by the context menu turning into a scrollable list or by it becoming really janky and showing the user pre-update UI. --- src/apprt/gtk/App.zig | 22 +++++++++++++++------- src/apprt/gtk/Surface.zig | 3 +++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index cc7af2758..60fefc6de 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -797,13 +797,7 @@ fn initContextMenu(self: *App) void { const menu = c.g_menu_new(); errdefer c.g_object_unref(menu); - { - 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"); - } + createContextMenuCopyPasteSection(menu, false); { const section = c.g_menu_new(); @@ -823,6 +817,20 @@ 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); +} + fn isValidAppId(app_id: [:0]const u8) bool { if (app_id.len > 255 or app_id.len == 0) return false; if (app_id[0] == '.') return false; diff --git a/src/apprt/gtk/Surface.zig b/src/apprt/gtk/Surface.zig index 4d7ff688c..390cf8c90 100644 --- a/src/apprt/gtk/Surface.zig +++ b/src/apprt/gtk/Surface.zig @@ -1199,6 +1199,9 @@ 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()); + c.gtk_popover_popup(@ptrCast(@alignCast(window.context_menu))); }