Merge pull request #755 from Jipok/draw_tab_rework

rootview.lua: Refactor Node:draw_tab
This commit is contained in:
Jefferson González 2022-03-18 05:05:55 -04:00 committed by GitHub
commit 30de42f4ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 35 deletions

View File

@ -260,8 +260,8 @@ end
local function close_button_location(x, w) local function close_button_location(x, w)
local cw = style.icon_font:get_width("C") local cw = style.icon_font:get_width("C")
local pad = style.padding.y local pad = style.padding.x / 2
return x + w - pad - cw, cw, pad return x + w - cw - pad, cw, pad
end end
@ -476,51 +476,59 @@ function Node:update()
end end
end end
function Node:draw_tab(text, is_active, is_hovered, is_close_hovered, x, y, w, h, standalone) function Node:draw_tab_title(view, font, is_active, is_hovered, x, y, w, h)
local text = view and view:get_name() or ""
local dots_width = font:get_width("")
local align = "center"
if font:get_width(text) > w then
align = "left"
for i = 1, #text do
local reduced_text = text:sub(1, #text - i)
if font:get_width(reduced_text) + dots_width <= w then
text = reduced_text .. ""
break
end
end
end
local color = style.dim
if is_active then color = style.text end
if is_hovered then color = style.text end
common.draw_text(font, color, text, align, x, y, w, h)
end
function Node:draw_tab_borders(view, is_active, is_hovered, x, y, w, h, standalone)
-- Tabs deviders
local ds = style.divider_size local ds = style.divider_size
local dots_width = style.font:get_width("")
local color = style.dim local color = style.dim
local padding_y = style.padding.y local padding_y = style.padding.y
renderer.draw_rect(x + w, y + padding_y, ds, h - padding_y*2, style.dim) renderer.draw_rect(x + w, y + padding_y, ds, h - padding_y*2, style.dim)
if standalone then if standalone then
renderer.draw_rect(x-1, y-1, w+2, h+2, style.background2) renderer.draw_rect(x-1, y-1, w+2, h+2, style.background2)
end end
-- Full border
if is_active then if is_active then
color = style.text color = style.text
renderer.draw_rect(x, y, w, h, style.background) renderer.draw_rect(x, y, w, h, style.background)
renderer.draw_rect(x + w, y, ds, h, style.divider) renderer.draw_rect(x + w, y, ds, h, style.divider)
renderer.draw_rect(x - ds, y, ds, h, style.divider) renderer.draw_rect(x - ds, y, ds, h, style.divider)
end end
local cx, cw, cspace = close_button_location(x, w) return x + ds, y, w - ds*2, h
end
function Node:draw_tab(view, is_active, is_hovered, is_close_hovered, x, y, w, h, standalone)
x, y, w, h = self:draw_tab_borders(view, is_active, is_hovered, x, y, w, h, standalone)
-- Close button
local cx, cw, cpad = close_button_location(x, w)
local show_close_button = ((is_active or is_hovered) and not standalone and config.tab_close_button) local show_close_button = ((is_active or is_hovered) and not standalone and config.tab_close_button)
if show_close_button then if show_close_button then
local close_style = is_close_hovered and style.text or style.dim local close_style = is_close_hovered and style.text or style.dim
common.draw_text(style.icon_font, close_style, "C", nil, cx, y, 0, h) common.draw_text(style.icon_font, close_style, "C", nil, cx, y, cw, h)
end end
if is_hovered then -- Title
color = style.text x = x + cpad
end w = cx - x
local padx = style.padding.x core.push_clip_rect(x, y, w, h)
-- Normally we should substract "cspace" from text_avail_width and from the self:draw_tab_title(view, style.font, is_active, is_hovered, x, y, w, h)
-- clipping width. It is the padding space we give to the left and right of the
-- close button. However, since we are using dots to terminate filenames, we
-- choose to ignore "cspace" accepting that the text can possibly "touch" the
-- close button.
local text_avail_width = cx - x - padx
core.push_clip_rect(x, y, cx - x, h)
x, w = x + padx, w - padx * 2
local align = "center"
if style.font:get_width(text) > text_avail_width then
align = "left"
for i = 1, #text do
local reduced_text = text:sub(1, #text - i)
if style.font:get_width(reduced_text) + dots_width <= text_avail_width then
text = reduced_text .. ""
break
end
end
end
common.draw_text(style.font, color, text, align, x, y, w, h)
core.pop_clip_rect() core.pop_clip_rect()
end end
@ -547,7 +555,7 @@ function Node:draw_tabs()
for i = self.tab_offset, self.tab_offset + tabs_number - 1 do for i = self.tab_offset, self.tab_offset + tabs_number - 1 do
local view = self.views[i] local view = self.views[i]
local x, y, w, h = self:get_tab_rect(i) local x, y, w, h = self:get_tab_rect(i)
self:draw_tab(view:get_name(), view == self.active_view, self:draw_tab(view, view == self.active_view,
i == self.hovered_tab, i == self.hovered_close, i == self.hovered_tab, i == self.hovered_close,
x, y, w, h) x, y, w, h)
end end

View File

@ -390,8 +390,8 @@ function RootView:draw_grabbed_tab()
local _,_, w, h = dn.node:get_tab_rect(dn.idx) local _,_, w, h = dn.node:get_tab_rect(dn.idx)
local x = self.mouse.x - w / 2 local x = self.mouse.x - w / 2
local y = self.mouse.y - h / 2 local y = self.mouse.y - h / 2
local text = dn.node.views[dn.idx] and dn.node.views[dn.idx]:get_name() or "" local view = dn.node.views[dn.idx]
self.root_node:draw_tab(text, true, true, false, x, y, w, h, true) self.root_node:draw_tab(view, true, true, false, x, y, w, h, true)
end end