Merge pull request #822 from AlexSol/update_contextMenu

Travel by contextMenu using keyboard
This commit is contained in:
Adam 2022-02-11 12:10:14 -05:00 committed by GitHub
commit a6f32ca0d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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.position.x, self.position.y = x, y
self.show_context_menu = true self.show_context_menu = true
core.request_cursor("arrow")
return true return true
end end
return false return false
@ -101,6 +102,7 @@ function ContextMenu:hide()
self.items = nil self.items = nil
self.selected = -1 self.selected = -1
self.height = 0 self.height = 0
core.request_cursor(core.active_view.cursor)
end end
function ContextMenu:each_item() function ContextMenu:each_item()
@ -126,9 +128,6 @@ function ContextMenu:on_mouse_moved(px, py)
break break
end end
end end
if self.selected >= 0 then
core.request_cursor("arrow")
end
return true return true
end end
@ -140,8 +139,38 @@ function ContextMenu:on_selected(item)
end end
end end
function ContextMenu:on_mouse_pressed(button, x, y, clicks) local function change_value(value, change)
local selected = (self.items or {})[self.selected] 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 local caught = false
self:hide() self:hide()
@ -153,7 +182,7 @@ function ContextMenu:on_mouse_pressed(button, x, y, clicks)
end end
if button == "right" then if button == "right" then
caught = self:show(x, y) caught = self:show(px, py)
end end
return caught return caught
end end

View File

@ -3,6 +3,7 @@ local core = require "core"
local command = require "core.command" 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 DocView = require "core.docview"
local RootView = require "core.rootview" local RootView = require "core.rootview"
local menu = ContextMenu() local menu = ContextMenu()
@ -32,7 +33,7 @@ function RootView:draw(...)
menu:draw() menu:draw()
end end
command.add(nil, { command.add(function() return getmetatable(core.active_view) == DocView end, {
["context:show"] = function() ["context:show"] = function()
menu:show(core.active_view.position.x, core.active_view.position.y) menu:show(core.active_view.position.x, core.active_view.position.y)
end end
@ -42,6 +43,25 @@ keymap.add {
["menu"] = "context:show" ["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 if require("plugins.scale") then
menu:register("core.docview", { menu:register("core.docview", {
{ text = "Cut", command = "doc:cut" }, { text = "Cut", command = "doc:cut" },