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

Co-Authored-By: vqn <85911372+vqns@users.noreply.github.com>
This commit is contained in:
takase1121 2023-08-19 12:36:39 +08:00
parent 017711b369
commit 218ba3ebac
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
2 changed files with 39 additions and 31 deletions

View File

@ -4,6 +4,7 @@ local command = require "core.command"
local keymap = require "core.keymap" local keymap = require "core.keymap"
local ContextMenu = require "core.contextmenu" local ContextMenu = require "core.contextmenu"
local RootView = require "core.rootview" local RootView = require "core.rootview"
local config = require "core.config"
local menu = ContextMenu() local menu = ContextMenu()
local on_view_mouse_pressed = RootView.on_view_mouse_pressed 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 { ["down"] = "context:focus-next" }
keymap.add { ["escape"] = "context:hide" } keymap.add { ["escape"] = "context:hide" }
if require("plugins.scale") then
menu:register("core.docview", { local cmds = {
{ text = "Cut", command = "doc:cut" }, { text = "Cut", command = "doc:cut" },
{ text = "Copy", command = "doc:copy" }, { text = "Copy", command = "doc:copy" },
{ text = "Paste", command = "doc:paste" }, { text = "Paste", command = "doc:paste" },
{ text = "Font +", command = "scale:increase" }, ContextMenu.DIVIDER,
{ text = "Font -", command = "scale:decrease" }, { text = "Find", command = "find-replace:find" },
{ text = "Font Reset", command = "scale:reset" }, { text = "Replace", command = "find-replace:replace" }
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 end
menu:register("core.docview", cmds)
return menu return menu

View File

@ -518,15 +518,19 @@ local function is_primary_project_folder(path)
return core.project_dir == path return core.project_dir == path
end 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" }, { text = "Open in System", command = "treeview:open-in-system" },
ContextMenu.DIVIDER ContextMenu.DIVIDER
}) })
menu:register( menu:register(
function() function()
return view.hovered_item local item = treeitem()
and not is_project_folder(view.hovered_item.abs_filename) return core.active_view:is(TreeView) and item and not is_project_folder(item.abs_filename)
end, end,
{ {
{ text = "Rename", command = "treeview:rename" }, { text = "Rename", command = "treeview:rename" },
@ -536,7 +540,8 @@ menu:register(
menu:register( menu:register(
function() 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, end,
{ {
{ text = "New File", command = "treeview:new-file" }, { text = "New File", command = "treeview:new-file" },
@ -546,9 +551,10 @@ menu:register(
menu:register( menu:register(
function() function()
return view.hovered_item local item = treeitem()
and not is_primary_project_folder(view.hovered_item.abs_filename) return core.active_view:is(TreeView) and item
and is_project_folder(view.hovered_item.abs_filename) and not is_primary_project_folder(item.abs_filename)
and is_project_folder(item.abs_filename)
end, end,
{ {
{ text = "Remove directory", command = "treeview:remove-project-directory" }, { text = "Remove directory", command = "treeview:remove-project-directory" },
@ -589,7 +595,10 @@ command.add(nil, {
end end
}) })
command.add(TreeView, { command.add(
function()
return not menu.show_context_menu and core.active_view:extends(TreeView), TreeView
end, {
["treeview:next"] = function() ["treeview:next"] = function()
local item, _, item_y = view:get_next(view.selected_item) local item, _, item_y = view:get_next(view.selected_item)
view:set_selection(item, item_y) view:set_selection(item, item_y)
@ -660,19 +669,10 @@ command.add(TreeView, {
}) })
local function treeitem() return view.hovered_item or view.selected_item end
command.add( command.add(
function() function()
local item = treeitem() local item = treeitem()
return item ~= nil return item ~= nil and (core.active_view == view or menu.show_context_menu), item
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
end, { end, {
["treeview:delete"] = function(item) ["treeview:delete"] = function(item)
local filename = item.abs_filename local filename = item.abs_filename
@ -800,7 +800,8 @@ command.add(
local projectsearch = pcall(require, "plugins.projectsearch") local projectsearch = pcall(require, "plugins.projectsearch")
if projectsearch then if projectsearch then
menu:register(function() menu:register(function()
return view.hovered_item and view.hovered_item.type == "dir" local item = treeitem()
return item and item.type == "dir"
end, { end, {
{ text = "Find in directory", command = "treeview:search-in-directory" } { text = "Find in directory", command = "treeview:search-in-directory" }
}) })