Compare commits

...

2 Commits

Author SHA1 Message Date
takase1121 e3c42d865e remove data/plugins/contextmenu.lua
moved the code throughout rootview, keymap and commands
2021-08-03 15:46:08 +08:00
takase1121 084012ed60 add item.info and item.disabled 2021-08-02 11:53:10 +08:00
6 changed files with 60 additions and 63 deletions

View File

@ -0,0 +1,9 @@
local core = require "core"
local command = require "core.command"
command.add(nil, {
["context-menu:show"] = function()
core.context_menu:show(core.active_view.position.x, core.active_view.position.y)
end
})

View File

@ -49,7 +49,7 @@ function ContextMenu:register(predicate, items)
local width, height = 0, 0 --precalculate the size of context menu
for i, item in ipairs(items) do
if item ~= DIVIDER then
item.info = keymap.reverse_map[item.command]
item.info = item.info or keymap.reverse_map[item.command]
end
local lw, lh = get_item_size(item)
width = math.max(width, lw)
@ -129,6 +129,7 @@ function ContextMenu:on_mouse_moved(px, py)
end
function ContextMenu:on_selected(item)
if item.disabled then return end
if type(item.command) == "string" then
command.perform(item.command)
else
@ -203,11 +204,12 @@ function ContextMenu:draw_context_menu()
if item == DIVIDER then
renderer.draw_rect(x, y, w, h, style.caret)
else
if i == self.selected then
if i == self.selected and not self.disabled then
renderer.draw_rect(x, y, w, h, style.selection)
end
common.draw_text(style.font, style.text, item.text, "left", x + style.padding.x, y, w, h)
local text_color = item.disabled and style.dim or style.text
common.draw_text(style.font, text_color, item.text, "left", x + style.padding.x, y, w, h)
if item.info then
common.draw_text(style.font, style.dim, item.info, "right", x, y, w - style.padding.x, h)
end

View File

@ -9,6 +9,7 @@ local RootView
local StatusView
local TitleView
local CommandView
local ContextMenu
local NagView
local DocView
local Doc
@ -417,6 +418,40 @@ local function reload_on_user_module_save()
end
local function find_selected_occurence()
local doc = core.active_view.doc
if not doc or not doc:has_selection() then return end
-- get selection. If #358 is merged we won't need this
local text = {}
for idx, line1, col1, line2, col2 in doc:get_selections() do
if line1 ~= line2 or col1 ~= col2 then
local t = doc:get_text(line1, col1, line2, col2)
if t ~= "" then text[#text + 1] = t end
end
end
text = table.concat(text, "\n")
command.perform "find-replace:find"
core.command_view:set_text(text)
core.command_view:submit()
end
local function setup_context_menu()
local info = keymap.reverse_map["find-replace:find"]
core.context_menu:register("core.docview", {
{ text = "Find Occurence...", command = find_selected_occurence, info = info },
ContextMenu.DIVIDER,
{ text = "Cut", command = "doc:cut" },
{ text = "Copy", command = "doc:copy" },
{ text = "Paste", command = "doc:paste" },
ContextMenu.DIVIDER,
{ text = "Command Palette...", command = "core:find-command" }
})
end
function core.init()
command = require "core.command"
keymap = require "core.keymap"
@ -424,6 +459,7 @@ function core.init()
StatusView = require "core.statusview"
TitleView = require "core.titleview"
CommandView = require "core.commandview"
ContextMenu = require "core.contextmenu"
NagView = require "core.nagview"
DocView = require "core.docview"
Doc = require "core.doc"
@ -498,6 +534,7 @@ function core.init()
core.root_view = RootView()
core.command_view = CommandView()
core.context_menu = ContextMenu()
core.status_view = StatusView()
core.nag_view = NagView()
core.title_view = TitleView()
@ -560,6 +597,7 @@ function core.init()
end)
end
setup_context_menu()
reload_on_user_module_save()
end

View File

@ -206,7 +206,9 @@ keymap.add_direct {
["shift+pageup"] = "doc:select-to-previous-page",
["shift+pagedown"] = "doc:select-to-next-page",
["ctrl+shift+up"] = "doc:create-cursor-previous-line",
["ctrl+shift+down"] = "doc:create-cursor-next-line"
["ctrl+shift+down"] = "doc:create-cursor-next-line",
["menu"] = "context-menu:show"
}
return keymap

View File

@ -741,6 +741,7 @@ end
-- Function to intercept mouse pressed events on the active view.
-- Do nothing by default.
function RootView.on_view_mouse_pressed(button, x, y, clicks)
return core.context_menu:on_mouse_pressed(button, x, y, clicks)
end
@ -801,6 +802,8 @@ function RootView:on_mouse_moved(x, y, dx, dy)
return
end
if core.context_menu:on_mouse_moved(x, y, dx, dy) then return end
if self.dragged_divider then
local node = self.dragged_divider
if node.type == "hsplit" then
@ -874,11 +877,13 @@ function RootView:update()
copy_position_and_size(self.root_node, self)
self.root_node:update()
self.root_node:update_layout()
core.context_menu:update()
end
function RootView:draw()
self.root_node:draw()
core.context_menu:draw()
while #self.deferred_draws > 0 do
local t = table.remove(self.deferred_draws)
t.fn(table.unpack(t))

View File

@ -1,59 +0,0 @@
-- mod-version:1 -- lite-xl 1.16
local core = require "core"
local command = require "core.command"
local keymap = require "core.keymap"
local ContextMenu = require "core.contextmenu"
local RootView = require "core.rootview"
local menu = ContextMenu()
local on_view_mouse_pressed = RootView.on_view_mouse_pressed
local on_mouse_moved = RootView.on_mouse_moved
local root_view_update = RootView.update
local root_view_draw = RootView.draw
function RootView:on_mouse_moved(...)
if menu:on_mouse_moved(...) then return end
on_mouse_moved(self, ...)
end
function RootView.on_view_mouse_pressed(button, x, y, clicks)
-- We give the priority to the menu to process mouse pressed events.
local handled = menu:on_mouse_pressed(button, x, y, clicks)
return handled or on_view_mouse_pressed(button, x, y, clicks)
end
function RootView:update(...)
root_view_update(self, ...)
menu:update()
end
function RootView:draw(...)
root_view_draw(self, ...)
menu:draw()
end
command.add(nil, {
["context:show"] = function()
menu:show(core.active_view.position.x, core.active_view.position.y)
end
})
keymap.add {
["menu"] = "context:show"
}
if require("plugins.scale") then
menu:register("core.docview", {
{ 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" },
ContextMenu.DIVIDER,
{ text = "Find Pattern", command = "find-replace:find-pattern" },
{ text = "Replace Pattern", command = "find-replace:replace-pattern" },
})
end
return menu