Do not duplicate RootView method in contextmenu

The method RootView:on_mouse_pressed was copied in the contextmenu plugin with
a small modification to intercept the mouse clicks of the active view.

This approach is problematic because a relatively large portion of code is
duplicated.

We introduced a function named RootView.on_view_mouse_pressed to let plugins
like contextmenu intercepts mouse clicks in the active area without duplicating
the function RootView:on_mouse_pressed.
This commit is contained in:
Francesco Abbate 2021-06-17 10:13:04 +02:00
parent 2fc245eb69
commit b39db791f9
2 changed files with 16 additions and 31 deletions

View File

@ -738,6 +738,12 @@ function RootView:close_all_docviews()
end 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)
end
function RootView:on_mouse_pressed(button, x, y, clicks) function RootView:on_mouse_pressed(button, x, y, clicks)
local div = self.root_node:get_divider_overlapping_point(x, y) local div = self.root_node:get_divider_overlapping_point(x, y)
if div then if div then
@ -759,9 +765,11 @@ function RootView:on_mouse_pressed(button, x, y, clicks)
end end
else else
core.set_active_view(node.active_view) core.set_active_view(node.active_view)
if not self.on_view_mouse_pressed(button, x, y, clicks) then
node.active_view:on_mouse_pressed(button, x, y, clicks) node.active_view:on_mouse_pressed(button, x, y, clicks)
end end
end end
end
function RootView:on_mouse_released(...) function RootView:on_mouse_released(...)

View File

@ -219,43 +219,20 @@ end
local menu = ContextMenu() local menu = ContextMenu()
local root_view_on_mouse_pressed = RootView.on_mouse_pressed local on_view_mouse_pressed = RootView.on_view_mouse_pressed
local root_view_on_mouse_moved = RootView.on_mouse_moved local on_mouse_moved = RootView.on_mouse_moved
local root_view_update = RootView.update local root_view_update = RootView.update
local root_view_draw = RootView.draw local root_view_draw = RootView.draw
function RootView:on_mouse_moved(...) function RootView:on_mouse_moved(...)
if menu:on_mouse_moved(...) then return end if menu:on_mouse_moved(...) then return end
root_view_on_mouse_moved(self, ...) on_mouse_moved(self, ...)
end end
-- this function is mostly copied from lite-xl's source function RootView.on_view_mouse_pressed(button, x, y, clicks)
function RootView:on_mouse_pressed(button, x,y, clicks) -- We give the priority to the menu to process mouse pressed events.
local div = self.root_node:get_divider_overlapping_point(x, y) local handled = menu:on_mouse_pressed(button, x, y, clicks)
if div then return handled or on_view_mouse_pressed(button, x, y, clicks)
self.dragged_divider = div
return
end
local node = self.root_node:get_child_overlapping_point(x, y)
if node.hovered_scroll_button > 0 then
node:scroll_tabs(node.hovered_scroll_button)
return
end
local idx = node:get_tab_overlapping_point(x, y)
if idx then
if button == "middle" or node.hovered_close == idx then
node:close_view(self.root_node, node.views[idx])
else
self.dragged_node = { node, idx or #node.views }
node:set_active_view(node.views[idx])
end
else
core.set_active_view(node.active_view)
-- send to context menu first
if not menu:on_mouse_pressed(button, x, y, clicks) then
node.active_view:on_mouse_pressed(button, x, y, clicks)
end
end
end end
function RootView:update(...) function RootView:update(...)