Travel by contextMenu using keyboard

This commit is contained in:
AlexSol 2022-01-28 20:08:54 +02:00
parent 3d40725b8f
commit efedbae663
2 changed files with 56 additions and 7 deletions

View File

@ -91,6 +91,7 @@ function ContextMenu:show(x, y)
self.position.x, self.position.y = x, y
self.show_context_menu = true
core.request_cursor("arrow")
return true
end
return false
@ -101,6 +102,7 @@ function ContextMenu:hide()
self.items = nil
self.selected = -1
self.height = 0
core.request_cursor(core.active_view.cursor)
end
function ContextMenu:each_item()
@ -126,9 +128,6 @@ function ContextMenu:on_mouse_moved(px, py)
break
end
end
if self.selected >= 0 then
core.request_cursor("arrow")
end
return true
end
@ -140,8 +139,38 @@ function ContextMenu:on_selected(item)
end
end
function ContextMenu:on_mouse_pressed(button, x, y, clicks)
local selected = (self.items or {})[self.selected]
local function change_value(value, change)
return value + change
end
function ContextMenu:focus_previous()
self.selected = (self.selected == -1 or self.selected == 1) and #self.items or change_value(self.selected, -1)
if self:get_item_selected() == DIVIDER then
self.selected = change_value(self.selected, -1)
end
end
function ContextMenu:focus_next()
self.selected = (self.selected == -1 or self.selected == #self.items) and 1 or change_value(self.selected, 1)
if self:get_item_selected() == DIVIDER then
self.selected = change_value(self.selected, 1)
end
end
function ContextMenu:get_item_selected()
return (self.items or {})[self.selected]
end
function ContextMenu:call_selected_item()
local selected = self:get_item_selected()
self:hide()
if selected then
self:on_selected(selected)
end
end
function ContextMenu:on_mouse_pressed(button, px, py, clicks)
local selected = self:get_item_selected()
local caught = false
self:hide()
@ -153,7 +182,7 @@ function ContextMenu:on_mouse_pressed(button, x, y, clicks)
end
if button == "right" then
caught = self:show(x, y)
caught = self:show(px, py)
end
return caught
end

View File

@ -3,6 +3,7 @@ local core = require "core"
local command = require "core.command"
local keymap = require "core.keymap"
local ContextMenu = require "core.contextmenu"
local DocView = require "core.docview"
local RootView = require "core.rootview"
local menu = ContextMenu()
@ -32,7 +33,7 @@ function RootView:draw(...)
menu:draw()
end
command.add(nil, {
command.add(function() return getmetatable(core.active_view) == DocView end, {
["context:show"] = function()
menu:show(core.active_view.position.x, core.active_view.position.y)
end
@ -42,6 +43,25 @@ keymap.add {
["menu"] = "context:show"
}
command.add(function() return menu.show_context_menu == true end, {
["context:focus-previous"] = function()
menu:focus_previous()
end,
["context:focus-next"] = function()
menu:focus_next()
end,
["context:hide"] = function()
menu:hide()
end,
["context:on-selected"] = function()
menu:call_selected_item()
end,
})
keymap.add { ["return"] = "context:on-selected" }
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" },