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 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

View File

@ -518,15 +518,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" },
@ -536,7 +540,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" },
@ -546,9 +551,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" },
@ -589,7 +595,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)
@ -660,19 +669,10 @@ command.add(TreeView, {
})
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
@ -800,7 +800,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" }
})