2022-05-31 22:34:14 +02:00
|
|
|
-- mod-version:3
|
2021-02-12 15:54:11 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local command = require "core.command"
|
|
|
|
local style = require "core.style"
|
|
|
|
local View = require "core.view"
|
|
|
|
|
|
|
|
local ToolbarView = View:extend()
|
|
|
|
|
2021-02-27 12:13:11 +01:00
|
|
|
|
2021-02-12 15:54:11 +01:00
|
|
|
function ToolbarView:new()
|
|
|
|
ToolbarView.super.new(self)
|
2021-02-15 15:45:56 +01:00
|
|
|
self.visible = true
|
2021-02-27 12:13:11 +01:00
|
|
|
self.init_size = true
|
2021-02-19 11:50:20 +01:00
|
|
|
self.tooltip = false
|
2022-08-09 20:53:30 +02:00
|
|
|
self.toolbar_font = style.icon_big_font
|
|
|
|
self.toolbar_commands = {
|
|
|
|
{symbol = "f", command = "core:new-doc"},
|
|
|
|
{symbol = "D", command = "core:open-file"},
|
|
|
|
{symbol = "S", command = "doc:save"},
|
|
|
|
{symbol = "L", command = "core:find-file"},
|
|
|
|
{symbol = "B", command = "core:find-command"},
|
|
|
|
{symbol = "P", command = "core:open-user-module"},
|
|
|
|
}
|
2021-02-12 15:54:11 +01:00
|
|
|
end
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
|
2021-02-12 15:54:11 +01:00
|
|
|
function ToolbarView:update()
|
2022-08-09 20:53:30 +02:00
|
|
|
local dest_size = self.visible and (self.toolbar_font:get_height() + style.padding.y * 2) or 0
|
2021-02-15 15:45:56 +01:00
|
|
|
if self.init_size then
|
2021-02-27 12:13:11 +01:00
|
|
|
self.size.y = dest_size
|
2021-02-15 15:45:56 +01:00
|
|
|
self.init_size = nil
|
2021-02-27 12:13:11 +01:00
|
|
|
else
|
|
|
|
self:move_towards(self.size, "y", dest_size)
|
2021-02-15 15:45:56 +01:00
|
|
|
end
|
2021-02-12 15:54:11 +01:00
|
|
|
ToolbarView.super.update(self)
|
|
|
|
end
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
|
|
|
|
function ToolbarView:toggle_visible()
|
|
|
|
self.visible = not self.visible
|
2022-12-20 23:07:29 +01:00
|
|
|
if self.tooltip then
|
|
|
|
core.status_view:remove_tooltip()
|
|
|
|
self.tooltip = false
|
|
|
|
end
|
|
|
|
self.hovered_item = nil
|
2021-02-15 15:45:56 +01:00
|
|
|
end
|
|
|
|
|
2022-08-09 20:53:30 +02:00
|
|
|
function ToolbarView:get_icon_width()
|
|
|
|
local max_width = 0
|
2024-02-11 18:51:12 +01:00
|
|
|
for i,v in ipairs(self.toolbar_commands) do max_width = math.max(max_width, self.toolbar_font:get_width(v.symbol)) end
|
2022-08-09 20:53:30 +02:00
|
|
|
return max_width
|
|
|
|
end
|
2021-02-15 15:45:56 +01:00
|
|
|
|
2021-02-13 18:29:08 +01:00
|
|
|
function ToolbarView:each_item()
|
2022-08-09 20:53:30 +02:00
|
|
|
local icon_h, icon_w = self.toolbar_font:get_height(), self:get_icon_width()
|
2021-02-27 12:13:11 +01:00
|
|
|
local toolbar_spacing = icon_w / 2
|
2021-02-13 18:29:08 +01:00
|
|
|
local ox, oy = self:get_content_offset()
|
|
|
|
local index = 0
|
|
|
|
local iter = function()
|
|
|
|
index = index + 1
|
2022-08-09 20:53:30 +02:00
|
|
|
if index <= #self.toolbar_commands then
|
2021-02-15 15:45:56 +01:00
|
|
|
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
|
|
|
|
local dy = style.padding.y
|
2021-02-21 09:43:47 +01:00
|
|
|
if dx + icon_w > self.size.x then return end
|
2022-08-09 20:53:30 +02:00
|
|
|
return self.toolbar_commands[index], ox + dx, oy + dy, icon_w, icon_h
|
2021-02-13 18:29:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return iter
|
|
|
|
end
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
|
2021-02-27 19:39:56 +01:00
|
|
|
function ToolbarView:get_min_width()
|
2022-08-09 20:53:30 +02:00
|
|
|
local icon_w = self:get_icon_width()
|
2021-02-27 19:39:56 +01:00
|
|
|
local space = icon_w / 2
|
2022-08-09 20:53:30 +02:00
|
|
|
return 2 * style.padding.x + (icon_w + space) * #self.toolbar_commands - space
|
2021-02-27 19:39:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-12 15:54:11 +01:00
|
|
|
function ToolbarView:draw()
|
2022-12-20 23:07:29 +01:00
|
|
|
if not self.visible then return end
|
2021-02-12 15:54:11 +01:00
|
|
|
self:draw_background(style.background2)
|
|
|
|
|
2021-02-27 12:13:11 +01:00
|
|
|
for item, x, y, w, h in self:each_item() do
|
2022-08-09 20:53:30 +02:00
|
|
|
local color = item == self.hovered_item and command.is_valid(item.command) and style.text or style.dim
|
2024-02-11 18:51:12 +01:00
|
|
|
common.draw_text(self.toolbar_font, color, item.symbol, nil, x, y, 0, h)
|
2021-02-13 18:29:08 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
|
2021-02-13 18:29:08 +01:00
|
|
|
function ToolbarView:on_mouse_pressed(button, x, y, clicks)
|
2022-12-20 23:07:29 +01:00
|
|
|
if not self.visible then return end
|
2021-02-13 18:29:08 +01:00
|
|
|
local caught = ToolbarView.super.on_mouse_pressed(self, button, x, y, clicks)
|
2021-12-30 03:12:24 +01:00
|
|
|
if caught then return caught end
|
2021-02-13 18:29:08 +01:00
|
|
|
core.set_active_view(core.last_active_view)
|
2022-08-09 20:53:30 +02:00
|
|
|
if self.hovered_item and command.is_valid(self.hovered_item.command) then
|
2021-02-13 18:29:08 +01:00
|
|
|
command.perform(self.hovered_item.command)
|
|
|
|
end
|
2021-12-29 22:26:58 +01:00
|
|
|
return true
|
2021-02-13 18:29:08 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function ToolbarView:on_mouse_moved(px, py, ...)
|
2022-12-20 23:07:29 +01:00
|
|
|
if not self.visible then return end
|
2021-02-13 18:29:08 +01:00
|
|
|
ToolbarView.super.on_mouse_moved(self, px, py, ...)
|
|
|
|
self.hovered_item = nil
|
2021-02-21 11:09:51 +01:00
|
|
|
local x_min, x_max, y_min, y_max = self.size.x, 0, self.size.y, 0
|
2021-02-13 18:29:08 +01:00
|
|
|
for item, x, y, w, h in self:each_item() do
|
2021-02-21 11:09:51 +01:00
|
|
|
x_min, x_max = math.min(x, x_min), math.max(x + w, x_max)
|
|
|
|
y_min, y_max = y, y + h
|
2021-02-13 18:29:08 +01:00
|
|
|
if px > x and py > y and px <= x + w and py <= y + h then
|
|
|
|
self.hovered_item = item
|
2021-02-19 11:50:20 +01:00
|
|
|
core.status_view:show_tooltip(command.prettify_name(item.command))
|
|
|
|
self.tooltip = true
|
|
|
|
return
|
2021-02-13 18:29:08 +01:00
|
|
|
end
|
2021-02-12 15:54:11 +01:00
|
|
|
end
|
2021-02-21 12:57:42 +01:00
|
|
|
if self.tooltip and not (px > x_min and px <= x_max and py > y_min and py <= y_max) then
|
2021-02-19 11:50:20 +01:00
|
|
|
core.status_view:remove_tooltip()
|
|
|
|
self.tooltip = false
|
|
|
|
end
|
2021-02-12 15:54:11 +01:00
|
|
|
end
|
|
|
|
|
2021-02-15 15:45:56 +01:00
|
|
|
-- The toolbarview pane is not plugged here but it is added in the
|
|
|
|
-- treeview plugin.
|
2021-02-12 15:54:11 +01:00
|
|
|
|
|
|
|
return ToolbarView
|