TextWidget: small refactoring, better handle max_width (#5503)

Lots of code was doing some renderText calls to get the size
of some text string, and truncate it to some width if needed,
with or without an added ellipsis, before instantiating
a TextWidget with that tweaked text string.

This PR fixes/adds some properties and methods to TextWidget
so all that can be done by it. It makes the calling code
simpler, as they don't need to use RenderText directly.
(Additionally, when we go at using Harfbuzz for text rendering,
we'll just have to update or replace textwidget.lua without
the need to update any higher level code.)

Also:
- RenderText: removed the space added by truncateTextByWidth
  after the ellipsis, as it doesn't feel needed, and break
  right alignment of the ellipsis with other texts.
- KeyValuePage: fix some subtle size and alignment issues.
- NumberPickerWidget: fix font size (provided font size was
  not used)
This commit is contained in:
poire-z
2019-10-21 15:20:40 +02:00
committed by GitHub
parent ef22e85469
commit f05e62c1fb
17 changed files with 246 additions and 285 deletions

View File

@@ -12,7 +12,6 @@ local InputContainer = require("ui/widget/container/inputcontainer")
local LeftContainer = require("ui/widget/container/leftcontainer")
local LineWidget = require("ui/widget/linewidget")
local OverlapGroup = require("ui/widget/overlapgroup")
local RenderText = require("ui/rendertext")
local Size = require("ui/size")
local TextWidget = require("ui/widget/textwidget")
local UIManager = require("ui/uimanager")
@@ -33,20 +32,12 @@ local SortTitleWidget = VerticalGroup:new{
function SortTitleWidget:init()
self.close_button = CloseButton:new{ window = self }
local btn_width = self.close_button:getSize().w
local title_txt_width = RenderText:sizeUtf8Text(
0, self.width, self.tface, self.title).x
local show_title_txt
if self.width < (title_txt_width + btn_width) then
show_title_txt = RenderText:truncateTextByWidth(
self.title, self.tface, self.width-btn_width)
else
show_title_txt = self.title
end
-- title and close button
table.insert(self, OverlapGroup:new{
dimen = { w = self.width },
TextWidget:new{
text = show_title_txt,
text = self.title,
max_width = self.width - btn_width,
face = self.tface,
},
self.close_button,
@@ -124,10 +115,6 @@ function SortItemWidget:init()
local frame_padding = Size.padding.default
local frame_internal_width = self.width - frame_padding * 2
local text_rendered = RenderText:sizeUtf8Text(0, self.width, self.tface, self.text).x
if text_rendered > frame_internal_width then
self.text = RenderText:truncateTextByWidth(self.text, self.tface, frame_internal_width)
end
self[1] = FrameContainer:new{
padding = 0,
@@ -139,6 +126,7 @@ function SortItemWidget:init()
},
TextWidget:new{
text = self.text,
max_width = frame_internal_width,
face = self.tface,
}
},