From d207adc230f4bc1a7d1324c2cd4cc55d4582116e Mon Sep 17 00:00:00 2001 From: vqn <85911372+vqns@users.noreply.github.com> Date: Mon, 30 Jan 2023 18:36:17 +0100 Subject: [PATCH] Context menu fixes and keyboard navigation (#1338) * fix Doc contextmenu not registering commands if scale plugin is not found * fix TreeView contextmenu commands not working if the mouse hovers DocView * add keyboard navigation to TreeView contextmenu * fix incorrect contextmenu predicate --- data/plugins/contextmenu.lua | 31 ++++++++----- data/plugins/treeview.lua | 87 ++++++++++++++++++++++++++++-------- 2 files changed, 87 insertions(+), 31 deletions(-) diff --git a/data/plugins/contextmenu.lua b/data/plugins/contextmenu.lua index db6d28db..26afbb57 100644 --- a/data/plugins/contextmenu.lua +++ b/data/plugins/contextmenu.lua @@ -4,6 +4,7 @@ local command = require "core.command" local keymap = require "core.keymap" local ContextMenu = require "core.contextmenu" local RootView = require "core.rootview" +local config = require "core.config" local menu = ContextMenu() local on_view_mouse_pressed = RootView.on_view_mouse_pressed @@ -61,18 +62,24 @@ keymap.add { ["up"] = "context:focus-previous" } keymap.add { ["down"] = "context:focus-next" } keymap.add { ["escape"] = "context:hide" } -if require("plugins.scale") then - menu:register("core.docview", { - { text = "Cut", command = "doc:cut" }, - { text = "Copy", command = "doc:copy" }, - { text = "Paste", command = "doc:paste" }, - { text = "Font +", command = "scale:increase" }, - { text = "Font -", command = "scale:decrease" }, - { text = "Font Reset", command = "scale:reset" }, - ContextMenu.DIVIDER, - { text = "Find", command = "find-replace:find" }, - { text = "Replace", command = "find-replace:replace" } - }) + +local cmds = { + { text = "Cut", command = "doc:cut" }, + { text = "Copy", command = "doc:copy" }, + { text = "Paste", command = "doc:paste" }, + ContextMenu.DIVIDER, + { text = "Find", command = "find-replace:find" }, + { text = "Replace", command = "find-replace:replace" } +} + +if config.plugins.scale ~= false and require("plugins.scale") then + table.move(cmds, 4, 6, 7) + cmds[4] = { text = "Font +", command = "scale:increase" } + cmds[5] = { text = "Font -", command = "scale:decrease" } + cmds[6] = { text = "Font Reset", command = "scale:reset" } end +menu:register("core.docview", cmds) + + return menu diff --git a/data/plugins/treeview.lua b/data/plugins/treeview.lua index 9aa39ca2..78015f2c 100644 --- a/data/plugins/treeview.lua +++ b/data/plugins/treeview.lua @@ -523,15 +523,19 @@ local function is_primary_project_folder(path) return core.project_dir == path end -menu:register(function() return view.hovered_item end, { + +local function treeitem() return view.hovered_item or view.selected_item end + + +menu:register(function() return core.active_view:is(TreeView) and treeitem() end, { { text = "Open in System", command = "treeview:open-in-system" }, ContextMenu.DIVIDER }) menu:register( function() - return view.hovered_item - and not is_project_folder(view.hovered_item.abs_filename) + local item = treeitem() + return core.active_view:is(TreeView) and item and not is_project_folder(item.abs_filename) end, { { text = "Rename", command = "treeview:rename" }, @@ -541,7 +545,8 @@ menu:register( menu:register( function() - return view.hovered_item and view.hovered_item.type == "dir" + local item = treeitem() + return core.active_view:is(TreeView) and item and item.type == "dir" end, { { text = "New File", command = "treeview:new-file" }, @@ -551,9 +556,10 @@ menu:register( menu:register( function() - return view.hovered_item - and not is_primary_project_folder(view.hovered_item.abs_filename) - and is_project_folder(view.hovered_item.abs_filename) + local item = treeitem() + return core.active_view:is(TreeView) and item + and not is_primary_project_folder(item.abs_filename) + and is_project_folder(item.abs_filename) end, { { text = "Remove directory", command = "treeview:remove-project-directory" }, @@ -594,7 +600,10 @@ command.add(nil, { end }) -command.add(TreeView, { +command.add( + function() + return not menu.show_context_menu and core.active_view:extends(TreeView), TreeView + end, { ["treeview:next"] = function() local item, _, item_y = view:get_next(view.selected_item) view:set_selection(item, item_y) @@ -661,22 +670,33 @@ command.add(TreeView, { view:toggle_expand(true) end end, + + ["treeview-context:show"] = function() + if view.hovered_item then + menu:show(view.cursor_pos.x, view.cursor_pos.y) + return + end + + local item = view.selected_item + if not item then return end + + local x, y + for _i, _x, _y, _w, _h in view:each_item() do + if _i == item then + x = _x + _w / 2 + y = _y + _h / 2 + break + end + end + menu:show(x, y) + end }) -local function treeitem() return view.hovered_item or view.selected_item end - - command.add( function() local item = treeitem() - return item ~= nil - and ( - core.active_view == view or core.active_view == menu - or (view.toolbar and core.active_view == view.toolbar) - -- sometimes the context menu is shown on top of statusbar - or core.active_view == core.status_view - ), item + return item ~= nil and (core.active_view == view or menu.show_context_menu), item end, { ["treeview:delete"] = function(item) local filename = item.abs_filename @@ -804,7 +824,8 @@ command.add( local projectsearch = pcall(require, "plugins.projectsearch") if projectsearch then menu:register(function() - return view.hovered_item and view.hovered_item.type == "dir" + local item = treeitem() + return item and item.type == "dir" end, { { text = "Find in directory", command = "treeview:search-in-directory" } }) @@ -829,6 +850,25 @@ command.add(function() }) +command.add( + function() + return menu.show_context_menu == true and core.active_view:is(TreeView) + end, { + ["treeview-context:focus-previous"] = function() + menu:focus_previous() + end, + ["treeview-context:focus-next"] = function() + menu:focus_next() + end, + ["treeview-context:hide"] = function() + menu:hide() + end, + ["treeview-context:on-selected"] = function() + menu:call_selected_item() + end, +}) + + keymap.add { ["ctrl+\\"] = "treeview:toggle", ["up"] = "treeview:previous", @@ -844,6 +884,15 @@ keymap.add { ["ctrl+lclick"] = "treeview:new-folder" } +keymap.add { + ["menu"] = "treeview-context:show", + ["return"] = "treeview-context:on-selected", + ["up"] = "treeview-context:focus-previous", + ["down"] = "treeview-context:focus-next", + ["escape"] = "treeview-context:hide" +} + + -- The config specification used by gui generators config.plugins.treeview.config_spec = { name = "Treeview",