Adding final details for toolbarview plugin
This commit is contained in:
@@ -5,54 +5,72 @@ local style = require "core.style"
|
|||||||
local View = require "core.view"
|
local View = require "core.view"
|
||||||
|
|
||||||
local icon_h, icon_w = style.icon_big_font:get_height(), style.icon_big_font:get_width("D")
|
local icon_h, icon_w = style.icon_big_font:get_height(), style.icon_big_font:get_width("D")
|
||||||
|
local toolbar_spacing = icon_w / 2
|
||||||
local spacing = {
|
local toolbar_height = icon_h + style.padding.y * 2
|
||||||
-- FIXME: simplifies as y is not really needed.
|
|
||||||
margin = {x = icon_w / 2},
|
|
||||||
padding = {x = icon_w / 4},
|
|
||||||
}
|
|
||||||
|
|
||||||
local ToolbarView = View:extend()
|
local ToolbarView = View:extend()
|
||||||
|
|
||||||
local toolbar_commands = {
|
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 = "S", command = "doc:save"},
|
||||||
{symbol = "L", command = "core:find-file"},
|
{symbol = "L", command = "core:find-file"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:new()
|
function ToolbarView:new()
|
||||||
ToolbarView.super.new(self)
|
ToolbarView.super.new(self)
|
||||||
|
self.visible = true
|
||||||
|
self.init_size = toolbar_height
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:update()
|
function ToolbarView:update()
|
||||||
-- FIXME: remove size.x variable if not really needed.
|
if self.init_size then
|
||||||
-- self.size.x = (icon_w + spacing.padding.x) * #toolbar_commands - spacing.padding.x + 2 * spacing.margin.x
|
self.size.y = self.init_size
|
||||||
self.size.y = style.icon_big_font:get_height() + style.padding.y * 2
|
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)
|
ToolbarView.super.update(self)
|
||||||
end
|
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()
|
function ToolbarView:each_item()
|
||||||
local ox, oy = self:get_content_offset()
|
local ox, oy = self:get_content_offset()
|
||||||
local index = 0
|
local index = 0
|
||||||
local iter = function()
|
local iter = function()
|
||||||
index = index + 1
|
index = index + 1
|
||||||
if index <= #toolbar_commands then
|
if index <= #toolbar_commands then
|
||||||
local dx = spacing.margin.x + (icon_w + spacing.padding.x) * (index - 1)
|
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
|
||||||
return toolbar_commands[index], ox + dx, oy, icon_w, icon_h
|
local dy = style.padding.y
|
||||||
|
return toolbar_commands[index], ox + dx, oy + dy, icon_w, icon_h
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return iter
|
return iter
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:draw()
|
function ToolbarView:draw()
|
||||||
self:draw_background(style.background2)
|
self:draw_background(style.background2)
|
||||||
|
|
||||||
for item, x, y in self:each_item() do
|
for item, x, y in self:each_item() do
|
||||||
local color = item == self.hovered_item and style.text or style.dim
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:on_mouse_pressed(button, x, y, clicks)
|
function ToolbarView:on_mouse_pressed(button, x, y, clicks)
|
||||||
local caught = ToolbarView.super.on_mouse_pressed(self, button, x, y, clicks)
|
local caught = ToolbarView.super.on_mouse_pressed(self, button, x, y, clicks)
|
||||||
if caught then return end
|
if caught then return end
|
||||||
@@ -74,15 +92,7 @@ function ToolbarView:on_mouse_moved(px, py, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- The toolbarview pane is not plugged here but it is added in the
|
||||||
-- register commands and keymap
|
-- treeview plugin.
|
||||||
--[[command.add(nil, {
|
|
||||||
["toolbar:toggle"] = function()
|
|
||||||
view.visible = not view.visible
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
keymap.add { ["ctrl+\\"] = "treeview:toggle" }
|
|
||||||
]]
|
|
||||||
|
|
||||||
return ToolbarView
|
return ToolbarView
|
||||||
|
|||||||
@@ -256,9 +256,21 @@ local view = TreeView()
|
|||||||
local node = core.root_view:get_active_node()
|
local node = core.root_view:get_active_node()
|
||||||
local treeview_node = node:split("left", view, {x = true}, true)
|
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")
|
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
|
||||||
if toolbar_plugin then
|
if config.toolbarview ~= false and toolbar_plugin then
|
||||||
treeview_node:split("down", ToolbarView(), {y = true})
|
local toolbar_view = ToolbarView()
|
||||||
|
treeview_node:split("down", toolbar_view, {y = true})
|
||||||
|
command.add(nil, {
|
||||||
|
["toolbar:toggle"] = function()
|
||||||
|
toolbar_view:toggle_visible()
|
||||||
|
end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user