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
This commit is contained in:
vqn 2023-01-30 18:36:17 +01:00 committed by GitHub
parent 0a0754d1c3
commit d207adc230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 87 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

@ -523,15 +523,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" },
@ -541,7 +545,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" },
@ -551,9 +556,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" },
@ -594,7 +600,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)
@ -661,22 +670,33 @@ command.add(TreeView, {
view:toggle_expand(true) view:toggle_expand(true)
end end
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( 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
@ -804,7 +824,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" }
}) })
@ -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 { keymap.add {
["ctrl+\\"] = "treeview:toggle", ["ctrl+\\"] = "treeview:toggle",
["up"] = "treeview:previous", ["up"] = "treeview:previous",
@ -844,6 +884,15 @@ keymap.add {
["ctrl+lclick"] = "treeview:new-folder" ["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 -- The config specification used by gui generators
config.plugins.treeview.config_spec = { config.plugins.treeview.config_spec = {
name = "Treeview", name = "Treeview",