Steps to generalize toolbar and treeview. (#1088)

This commit is contained in:
Adam 2022-08-09 14:53:30 -04:00 committed by GitHub
parent 77cebd8341
commit 6ccc5f6dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 41 deletions

View File

@ -527,6 +527,23 @@ local commands = {
doc():set_selection(line2, col2, line1, col1)
end,
["doc:create-cursor-previous-line"] = function()
split_cursor(-1)
doc():merge_cursors()
end,
["doc:create-cursor-next-line"] = function()
split_cursor(1)
doc():merge_cursors()
end
}
command.add(function(x, y)
if x == nil or y == nil or not doc() then return false end
local x1,y1,x2,y2 = dv().position.x, dv().position.y, dv().position.x + dv().size.x, dv().position.y + dv().size.y
return x >= x1 + dv():get_gutter_width() and x < x2 and y >= y1 and y < y2
end, {
["doc:set-cursor"] = function(x, y)
set_cursor(x, y, "set")
end,
@ -553,20 +570,8 @@ local commands = {
doc():add_selection(line, col, line, col)
end
dv().mouse_selecting = { line, col, "set" }
end,
["doc:create-cursor-previous-line"] = function()
split_cursor(-1)
doc():merge_cursors()
end,
["doc:create-cursor-next-line"] = function()
split_cursor(1)
doc():merge_cursors()
end
}
})
local translations = {
["previous-char"] = translate,

View File

@ -1109,9 +1109,10 @@ function StatusView:draw()
if self.message and system.get_time() <= self.message_timeout then
self:draw_items(self.message, false, 0, self.size.y)
elseif self.tooltip_mode then
self:draw_items(self.tooltip)
else
if self.tooltip_mode then
self:draw_items(self.tooltip)
end
if #self.active_items > 0 then
--- draw left pane
core.push_clip_rect(
@ -1121,7 +1122,7 @@ function StatusView:draw()
for _, item in ipairs(self.active_items) do
local item_x = self.left_xoffset + item.x + style.padding.x
local hovered, item_bg = get_item_bg_color(self, item)
if item.alignment == StatusView.Item.LEFT then
if item.alignment == StatusView.Item.LEFT and not self.tooltip_mode then
if type(item_bg) == "table" then
renderer.draw_rect(
item_x, self.position.y,

View File

@ -7,31 +7,26 @@ local View = require "core.view"
local ToolbarView = View:extend()
local 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"},
}
local function toolbar_height()
return style.icon_big_font:get_height() + style.padding.y * 2
end
function ToolbarView:new()
ToolbarView.super.new(self)
self.visible = true
self.init_size = true
self.tooltip = false
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"},
}
end
function ToolbarView:update()
local dest_size = self.visible and toolbar_height() or 0
local dest_size = self.visible and (self.toolbar_font:get_height() + style.padding.y * 2) or 0
if self.init_size then
self.size.y = dest_size
self.init_size = nil
@ -46,19 +41,24 @@ function ToolbarView:toggle_visible()
self.visible = not self.visible
end
function ToolbarView:get_icon_width()
local max_width = 0
for i,v in ipairs(self.toolbar_commands) do max_width = math.max(max_width, self.toolbar_font:get_width(v.symbol)) end
return max_width
end
function ToolbarView:each_item()
local icon_h, icon_w = style.icon_big_font:get_height(), style.icon_big_font:get_width("D")
local icon_h, icon_w = self.toolbar_font:get_height(), self:get_icon_width()
local toolbar_spacing = icon_w / 2
local ox, oy = self:get_content_offset()
local index = 0
local iter = function()
index = index + 1
if index <= #toolbar_commands then
if index <= #self.toolbar_commands then
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
local dy = style.padding.y
if dx + icon_w > self.size.x then return end
return toolbar_commands[index], ox + dx, oy + dy, icon_w, icon_h
return self.toolbar_commands[index], ox + dx, oy + dy, icon_w, icon_h
end
end
return iter
@ -66,9 +66,9 @@ end
function ToolbarView:get_min_width()
local icon_w = style.icon_big_font:get_width("D")
local icon_w = self:get_icon_width()
local space = icon_w / 2
return 2 * style.padding.x + (icon_w + space) * #toolbar_commands - space
return 2 * style.padding.x + (icon_w + space) * #self.toolbar_commands - space
end
@ -76,8 +76,8 @@ function ToolbarView:draw()
self:draw_background(style.background2)
for item, x, y, w, h 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, h)
local color = item == self.hovered_item and command.is_valid(item.command) and style.text or style.dim
common.draw_text(self.toolbar_font, color, item.symbol, nil, x, y, 0, h)
end
end
@ -86,7 +86,7 @@ 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 caught end
core.set_active_view(core.last_active_view)
if self.hovered_item then
if self.hovered_item and command.is_valid(self.hovered_item.command) then
command.perform(self.hovered_item.command)
end
return true

View File

@ -458,7 +458,7 @@ end
-- init
local view = TreeView()
local node = core.root_view:get_active_node()
local treeview_node = node:split("left", view, {x = true}, true)
view.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.
@ -470,7 +470,7 @@ local toolbar_view = nil
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
if config.plugins.toolbarview ~= false and toolbar_plugin then
toolbar_view = ToolbarView()
treeview_node:split("down", toolbar_view, {y = true})
view.node:split("down", toolbar_view, {y = true})
local min_toolbar_width = toolbar_view:get_min_width()
view:set_target_size("x", math.max(config.plugins.treeview.size, min_toolbar_width))
command.add(nil, {