Implement proper layout of toolbar

This commit is contained in:
Francesco Abbate 2021-02-15 00:36:39 +01:00
parent 5851a04098
commit b836c2e1e6
4 changed files with 62 additions and 32 deletions

View File

@ -401,12 +401,8 @@ function core.init()
local cur_node = core.root_view.root_node
cur_node.is_primary_node = true
cur_node = cur_node:split("down", core.command_view, true)
local got_toolbar, ToolbarView = core.try(require, "plugins.toolbarview")
if got_toolbar then
cur_node = cur_node:split("up", ToolbarView(), true)
end
cur_node = cur_node:split("down", core.status_view, true)
cur_node = cur_node:split("down", core.command_view, {y = true})
cur_node = cur_node:split("down", core.status_view, {y = true})
core.project_scan_thread_id = core.add_thread(project_scan_thread)
command.add_defaults()

View File

@ -93,6 +93,7 @@ end
local type_map = { up="vsplit", down="vsplit", left="hsplit", right="hsplit" }
function Node:split(dir, view, locked, resizable)
assert(type(locked) == 'table')
assert(self.type == "leaf", "Tried to split non-leaf node")
local type = assert(type_map[dir], "Invalid direction")
local last_active = core.active_view
@ -255,20 +256,36 @@ function Node:get_divider_rect()
end
-- Return two values for x and y axis and each of them is either falsy or a number.
-- A falsy value indicate no fixed size along the corresponding direction.
function Node:get_locked_size()
if self.type == "leaf" then
if self.locked then
local size = self.active_view.size
return size.x, size.y
-- The values below should be either a falsy value or a number
local sx = (self.locked and self.locked.x) and size.x
local sy = (self.locked and self.locked.y) and size.y
return sx, sy
end
else
local x1, y1 = self.a:get_locked_size()
local x2, y2 = self.b:get_locked_size()
-- The values below should be either a falsy value or a number
local sx, sy
if self.type == 'hsplit' then
if x1 and x2 then
local dsx = (x1 < 1 or x2 < 1) and 0 or style.divider_size
local dsy = (y1 < 1 or y2 < 1) and 0 or style.divider_size
return x1 + x2 + dsx, y1 + y2 + dsy
sx = x1 + x2 + dsx
end
sy = y1 or y2
else
if y1 and y2 then
local dsy = (y1 < 1 or y2 < 1) and 0 or style.divider_size
sy = y1 + y2 + dsy
end
sx = x1 or x2
end
return sx, sy
end
end
@ -281,9 +298,9 @@ end
-- calculating the sizes is the same for hsplits and vsplits, except the x/y
-- axis are swapped; this function lets us use the same code for both
local function calc_split_sizes(self, x, y, x1, x2)
local function calc_split_sizes(self, x, y, x1, x2, y1, y2)
local n
local ds = (x1 and x1 < 1 or x2 and x2 < 1) and 0 or style.divider_size
local ds = ((x1 and x1 < 1) or (x2 and x2 < 1)) and 0 or style.divider_size
if x1 then
n = x1 + ds
elseif x2 then
@ -421,17 +438,36 @@ function Node:close_all_docviews()
end
function Node:is_resizable()
function Node:is_resizable(axis)
if self.type == 'leaf' then
return not self.locked or self.resizable
return not self.locked or not self.locked[axis] or self.resizable
else
local a_resizable = self.a:is_resizable()
local b_resizable = self.b:is_resizable()
local a_resizable = self.a:is_resizable(axis)
local b_resizable = self.b:is_resizable(axis)
return a_resizable and b_resizable
end
end
function Node:resize(axis, value)
if self.type == 'leaf' then
-- FIXME: repeated logic with Node:is_resizable()
if not self.locked or not self.locked[axis] or self.resizable then
local view = self.active_view
view.size[axis] = value
return true
end
else
local a_resizable = self.a:is_resizable(axis)
local b_resizable = self.b:is_resizable(axis)
if a_resizable and b_resizable then
self.a:resize(axis, value)
self.b:resize(axis, value)
end
end
end
local RootView = View:extend()
function RootView:new()
@ -529,13 +565,8 @@ end
local function resize_child_node(node, axis, value, delta)
if node.a.resizable then
local view = node.a.active_view
view.size[axis] = value
elseif node.b.resizable then
local view = node.b.active_view
view.size[axis] = node.size[axis] - value
else
local accept_resize = node.a:resize(axis, value) or node.b:resize(axis, node.size[axis] - value)
if not accept_resize then
node.divider = node.divider + delta / node.size[axis]
end
end
@ -559,7 +590,8 @@ function RootView:on_mouse_moved(x, y, dx, dy)
local node = self.root_node:get_child_overlapping_point(x, y)
local div = self.root_node:get_divider_overlapping_point(x, y)
if div then
if div.a:is_resizable() and div.b:is_resizable() then
local axis = (div.type == "hsplit" and "x" or "y")
if div.a:is_resizable(axis) and div.b:is_resizable(axis) then
system.set_cursor(div.type == "hsplit" and "sizeh" or "sizev")
end
elseif node:get_tab_overlapping_point(x, y) then

View File

@ -74,12 +74,6 @@ function ToolbarView:on_mouse_moved(px, py, ...)
end
end
-- init
if false then
local view = ToolbarView()
local node = core.root_view:get_active_node()
node:split("up", view, true)
end
-- register commands and keymap
--[[command.add(nil, {

View File

@ -188,6 +188,9 @@ function TreeView:update()
if self.init_size then
self.size.x = dest
self.init_size = false
-- FIXME: bring back the visibility toggle and animation
-- else
-- self:move_towards(self.size, "x", dest)
end
TreeView.super.update(self)
@ -249,7 +252,12 @@ end
-- init
local view = TreeView()
local node = core.root_view:get_active_node()
local treeview_node = node:split("left", view, true, true)
local treeview_node = node:split("left", view, {x = true}, true)
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
if toolbar_plugin then
treeview_node:split("down", ToolbarView(), {y = true})
end
-- register commands and keymap