Adding final details for toolbarview plugin

This commit is contained in:
Francesco Abbate 2021-02-15 15:45:56 +01:00
parent ebba21310b
commit c20aff2307
2 changed files with 47 additions and 25 deletions

View File

@ -5,54 +5,72 @@ local style = require "core.style"
local View = require "core.view"
local icon_h, icon_w = style.icon_big_font:get_height(), style.icon_big_font:get_width("D")
local spacing = {
-- FIXME: simplifies as y is not really needed.
margin = {x = icon_w / 2},
padding = {x = icon_w / 4},
}
local toolbar_spacing = icon_w / 2
local toolbar_height = icon_h + style.padding.y * 2
local ToolbarView = View:extend()
local toolbar_commands = {
{symbol = "f", command = "core:open-file"},
{symbol = "f", command = "core:new-doc"},
{symbol = "D", command = "core:open-file"},
{symbol = "S", command = "doc:save"},
{symbol = "L", command = "core:find-file"},
}
function ToolbarView:new()
ToolbarView.super.new(self)
self.visible = true
self.init_size = toolbar_height
end
function ToolbarView:update()
-- FIXME: remove size.x variable if not really needed.
-- self.size.x = (icon_w + spacing.padding.x) * #toolbar_commands - spacing.padding.x + 2 * spacing.margin.x
self.size.y = style.icon_big_font:get_height() + style.padding.y * 2
if self.init_size then
self.size.y = self.init_size
self.init_size = nil
elseif self.goto_size then
if self.goto_size ~= self.size.y then
self:move_towards(self.size, "y", self.goto_size)
else
self.goto_size = nil
end
end
ToolbarView.super.update(self)
end
function ToolbarView:toggle_visible()
self.goto_size = self.visible and 0 or toolbar_height
self.visible = not self.visible
end
function ToolbarView:each_item()
local ox, oy = self:get_content_offset()
local index = 0
local iter = function()
index = index + 1
if index <= #toolbar_commands then
local dx = spacing.margin.x + (icon_w + spacing.padding.x) * (index - 1)
return toolbar_commands[index], ox + dx, oy, icon_w, icon_h
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
local dy = style.padding.y
return toolbar_commands[index], ox + dx, oy + dy, icon_w, icon_h
end
end
return iter
end
function ToolbarView:draw()
self:draw_background(style.background2)
for item, x, y in self:each_item() do
local color = item == self.hovered_item and style.text or style.dim
common.draw_text(style.icon_big_font, color, item.symbol, nil, x, y, 0, self.size.y)
common.draw_text(style.icon_big_font, color, item.symbol, nil, x, y, 0, icon_h)
end
end
function ToolbarView:on_mouse_pressed(button, x, y, clicks)
local caught = ToolbarView.super.on_mouse_pressed(self, button, x, y, clicks)
if caught then return end
@ -74,15 +92,7 @@ function ToolbarView:on_mouse_moved(px, py, ...)
end
end
-- register commands and keymap
--[[command.add(nil, {
["toolbar:toggle"] = function()
view.visible = not view.visible
end,
})
keymap.add { ["ctrl+\\"] = "treeview:toggle" }
]]
-- The toolbarview pane is not plugged here but it is added in the
-- treeview plugin.
return ToolbarView

View File

@ -256,9 +256,21 @@ local view = TreeView()
local node = core.root_view:get_active_node()
local treeview_node = node:split("left", view, {x = true}, true)
-- The toolbarview plugin is special because it is plugged inside
-- a treeview pane which is itelf provided in a plugin.
-- We therefore break the usual plugin's logic that would require each
-- plugin to be independent of each other. In addition it is not the
-- plugin module that plug itself in the active node but it is plugged here
-- in the treeview node.
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
if toolbar_plugin then
treeview_node:split("down", ToolbarView(), {y = true})
if config.toolbarview ~= false and toolbar_plugin then
local toolbar_view = ToolbarView()
treeview_node:split("down", toolbar_view, {y = true})
command.add(nil, {
["toolbar:toggle"] = function()
toolbar_view:toggle_visible()
end,
})
end