Steps to generalize toolbar and treeview. (#1088)
This commit is contained in:
parent
77cebd8341
commit
6ccc5f6dde
|
@ -527,6 +527,23 @@ local commands = {
|
||||||
doc():set_selection(line2, col2, line1, col1)
|
doc():set_selection(line2, col2, line1, col1)
|
||||||
end,
|
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)
|
["doc:set-cursor"] = function(x, y)
|
||||||
set_cursor(x, y, "set")
|
set_cursor(x, y, "set")
|
||||||
end,
|
end,
|
||||||
|
@ -553,20 +570,8 @@ local commands = {
|
||||||
doc():add_selection(line, col, line, col)
|
doc():add_selection(line, col, line, col)
|
||||||
end
|
end
|
||||||
dv().mouse_selecting = { line, col, "set" }
|
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
|
end
|
||||||
|
})
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
local translations = {
|
local translations = {
|
||||||
["previous-char"] = translate,
|
["previous-char"] = translate,
|
||||||
|
|
|
@ -1109,9 +1109,10 @@ function StatusView:draw()
|
||||||
|
|
||||||
if self.message and system.get_time() <= self.message_timeout then
|
if self.message and system.get_time() <= self.message_timeout then
|
||||||
self:draw_items(self.message, false, 0, self.size.y)
|
self:draw_items(self.message, false, 0, self.size.y)
|
||||||
elseif self.tooltip_mode then
|
|
||||||
self:draw_items(self.tooltip)
|
|
||||||
else
|
else
|
||||||
|
if self.tooltip_mode then
|
||||||
|
self:draw_items(self.tooltip)
|
||||||
|
end
|
||||||
if #self.active_items > 0 then
|
if #self.active_items > 0 then
|
||||||
--- draw left pane
|
--- draw left pane
|
||||||
core.push_clip_rect(
|
core.push_clip_rect(
|
||||||
|
@ -1121,7 +1122,7 @@ function StatusView:draw()
|
||||||
for _, item in ipairs(self.active_items) do
|
for _, item in ipairs(self.active_items) do
|
||||||
local item_x = self.left_xoffset + item.x + style.padding.x
|
local item_x = self.left_xoffset + item.x + style.padding.x
|
||||||
local hovered, item_bg = get_item_bg_color(self, item)
|
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
|
if type(item_bg) == "table" then
|
||||||
renderer.draw_rect(
|
renderer.draw_rect(
|
||||||
item_x, self.position.y,
|
item_x, self.position.y,
|
||||||
|
|
|
@ -7,7 +7,14 @@ local View = require "core.view"
|
||||||
|
|
||||||
local ToolbarView = View:extend()
|
local ToolbarView = View:extend()
|
||||||
|
|
||||||
local toolbar_commands = {
|
|
||||||
|
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 = "f", command = "core:new-doc"},
|
||||||
{symbol = "D", command = "core:open-file"},
|
{symbol = "D", command = "core:open-file"},
|
||||||
{symbol = "S", command = "doc:save"},
|
{symbol = "S", command = "doc:save"},
|
||||||
|
@ -15,23 +22,11 @@ local toolbar_commands = {
|
||||||
{symbol = "B", command = "core:find-command"},
|
{symbol = "B", command = "core:find-command"},
|
||||||
{symbol = "P", command = "core:open-user-module"},
|
{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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:update()
|
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
|
if self.init_size then
|
||||||
self.size.y = dest_size
|
self.size.y = dest_size
|
||||||
self.init_size = nil
|
self.init_size = nil
|
||||||
|
@ -46,19 +41,24 @@ function ToolbarView:toggle_visible()
|
||||||
self.visible = not self.visible
|
self.visible = not self.visible
|
||||||
end
|
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()
|
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 toolbar_spacing = icon_w / 2
|
||||||
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 <= #self.toolbar_commands then
|
||||||
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
|
local dx = style.padding.x + (icon_w + toolbar_spacing) * (index - 1)
|
||||||
local dy = style.padding.y
|
local dy = style.padding.y
|
||||||
if dx + icon_w > self.size.x then return end
|
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
|
||||||
end
|
end
|
||||||
return iter
|
return iter
|
||||||
|
@ -66,9 +66,9 @@ end
|
||||||
|
|
||||||
|
|
||||||
function ToolbarView:get_min_width()
|
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
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,8 +76,8 @@ function ToolbarView:draw()
|
||||||
self:draw_background(style.background2)
|
self:draw_background(style.background2)
|
||||||
|
|
||||||
for item, x, y, w, h in self:each_item() do
|
for item, x, y, w, h in self:each_item() do
|
||||||
local color = item == self.hovered_item and style.text or style.dim
|
local color = item == self.hovered_item and command.is_valid(item.command) and style.text or style.dim
|
||||||
common.draw_text(style.icon_big_font, color, item.symbol, nil, x, y, 0, h)
|
common.draw_text(self.toolbar_font, color, item.symbol, nil, x, y, 0, h)
|
||||||
end
|
end
|
||||||
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)
|
local caught = ToolbarView.super.on_mouse_pressed(self, button, x, y, clicks)
|
||||||
if caught then return caught end
|
if caught then return caught end
|
||||||
core.set_active_view(core.last_active_view)
|
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)
|
command.perform(self.hovered_item.command)
|
||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -458,7 +458,7 @@ end
|
||||||
-- init
|
-- init
|
||||||
local view = TreeView()
|
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)
|
view.node = node:split("left", view, {x = true}, true)
|
||||||
|
|
||||||
-- The toolbarview plugin is special because it is plugged inside
|
-- The toolbarview plugin is special because it is plugged inside
|
||||||
-- a treeview pane which is itelf provided in a plugin.
|
-- 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")
|
local toolbar_plugin, ToolbarView = core.try(require, "plugins.toolbarview")
|
||||||
if config.plugins.toolbarview ~= false and toolbar_plugin then
|
if config.plugins.toolbarview ~= false and toolbar_plugin then
|
||||||
toolbar_view = ToolbarView()
|
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()
|
local min_toolbar_width = toolbar_view:get_min_width()
|
||||||
view:set_target_size("x", math.max(config.plugins.treeview.size, min_toolbar_width))
|
view:set_target_size("x", math.max(config.plugins.treeview.size, min_toolbar_width))
|
||||||
command.add(nil, {
|
command.add(nil, {
|
||||||
|
|
Loading…
Reference in New Issue