More work in ReplView

This commit is contained in:
Francesco Abbate 2020-08-27 17:10:11 +02:00
parent 84509904b1
commit ff6fffb8c0
1 changed files with 41 additions and 56 deletions

View File

@ -101,9 +101,19 @@ function ReplNode:get_node_for_view(view)
local current_node = self local current_node = self
while current_node do while current_node do
if current_node.view == view then if current_node.view == view then
return self return current_node
end end
current_node = self.next current_node = current_node.next
end
end
function ReplNode:get_last_node()
local current_node = self
while current_node do
if current_node.next == nil then
return current_node
end
current_node = current_node.next
end end
end end
@ -117,7 +127,17 @@ end
-- function ReplNode:get_children(t) -- function ReplNode:get_children(t)
-- function ReplNode:get_divider_overlapping_point(px, py) -- function ReplNode:get_divider_overlapping_point(px, py)
-- function ReplNode:get_tab_overlapping_point(px, py) -- function ReplNode:get_tab_overlapping_point(px, py)
-- function ReplNode:get_child_overlapping_point(x, y)
function ReplNode:get_child_overlapping_point(x, y)
if (y >= self.position.y) and (y <= self.position.y + self.size.y) then
return self
else
if self.next then
return self.next:get_child_overlapping_point(x, y)
end
end
end
-- function ReplNode:get_tab_rect(idx) -- function ReplNode:get_tab_rect(idx)
-- function ReplNode:get_divider_rect() -- function ReplNode:get_divider_rect()
-- function ReplNode:get_locked_size() -- function ReplNode:get_locked_size()
@ -171,9 +191,6 @@ function ReplNode:draw()
end end
-- CONTINUE FROM HERE
local ReplView = View:extend() local ReplView = View:extend()
function ReplView:new() function ReplView:new()
@ -188,25 +205,16 @@ function ReplView:defer_draw(fn, ...)
table.insert(self.deferred_draws, 1, { fn = fn, ... }) table.insert(self.deferred_draws, 1, { fn = fn, ... })
end end
-- FIXME: this function should be remove because it is needed only
-- for a RootView.
function ReplView:get_active_node() function ReplView:get_active_node()
return self.root_node:get_node_for_view(core.active_view) return self.root_node:get_node_for_view(core.active_view)
end end
function ReplView:open_doc(doc) -- Previously named "open_doc"
local node = self:get_active_node() function ReplView:append_doc(doc)
if node.locked and core.last_active_view then local node = self.root_node:get_last_node()
core.set_active_view(core.last_active_view)
node = self:get_active_node()
end
assert(not node.locked, "Cannot open doc on locked node")
for i, view in ipairs(node.views) do
if view.doc == doc then
node:set_active_view(node.views[i])
return view
end
end
local view = DocView(doc) local view = DocView(doc)
node:add_view(view) node:add_view(view)
self.root_node:update_layout() self.root_node:update_layout()
@ -216,57 +224,34 @@ end
function ReplView:on_mouse_pressed(button, x, y, clicks) function ReplView:on_mouse_pressed(button, x, y, clicks)
local div = self.root_node:get_divider_overlapping_point(x, y)
if div then
self.dragged_divider = div
return
end
local node = self.root_node:get_child_overlapping_point(x, y) local node = self.root_node:get_child_overlapping_point(x, y)
local idx = node:get_tab_overlapping_point(x, y)
if idx then -- FIXME: Should not be called, it is only for RootView.
node:set_active_view(node.views[idx]) -- Verify is set_active_view below whould be removed.
if button == "middle" then core.set_active_view(node.view)
node:close_active_view(self.root_node)
end node.view:on_mouse_pressed(button, x, y, clicks)
else
core.set_active_view(node.active_view)
node.active_view:on_mouse_pressed(button, x, y, clicks)
end
end end
function ReplView:on_mouse_released(...) function ReplView:on_mouse_released(...)
if self.dragged_divider then
self.dragged_divider = nil
end
self.root_node:on_mouse_released(...) self.root_node:on_mouse_released(...)
end end
function ReplView:on_mouse_moved(x, y, dx, dy) -- CONTINUE FROM HERE:
if self.dragged_divider then
local node = self.dragged_divider
if node.type == "hsplit" then
node.divider = node.divider + dx / node.size.x
else
node.divider = node.divider + dy / node.size.y
end
node.divider = common.clamp(node.divider, 0.01, 0.99)
return
end
function ReplView:on_mouse_moved(x, y, dx, dy)
self.mouse.x, self.mouse.y = x, y self.mouse.x, self.mouse.y = x, y
-- CONTINUE HERE: implement the on_mouse_moved for ReplNode
self.root_node:on_mouse_moved(x, y, dx, dy) self.root_node:on_mouse_moved(x, y, dx, dy)
local node = self.root_node:get_child_overlapping_point(x, y) local node = self.root_node:get_child_overlapping_point(x, y)
local div = self.root_node:get_divider_overlapping_point(x, y) if node then
if div then
system.set_cursor(div.type == "hsplit" and "sizeh" or "sizev")
elseif node:get_tab_overlapping_point(x, y) then
system.set_cursor("arrow")
else
system.set_cursor(node.active_view.cursor) system.set_cursor(node.active_view.cursor)
end else
system.set_cursor("arrow")
endif
end end