Button: handle 'width' as the final outer width

All our widgets are considering their provided 'width'
as the outer width, except Button which considered it
as some 'inner width', to which padding/border/margin
were added. Let's have them all consistent.
Some other widgets using Button had tweaks to account
for that odd behaviour: fix and simplify them.
Also fix Button layout when text is left aligned.
reviewable/pr10228/r4
poire-z 1 year ago
parent 1e6b22a60e
commit f0122cf457

@ -8,7 +8,6 @@ A button widget that shows text or an icon and handles callback when tapped.
enabled = false, -- defaults to true enabled = false, -- defaults to true
callback = some_callback_function, callback = some_callback_function,
width = Screen:scaleBySize(50), width = Screen:scaleBySize(50),
max_width = Screen:scaleBySize(100),
bordersize = Screen:scaleBySize(3), bordersize = Screen:scaleBySize(3),
margin = 0, margin = 0,
padding = Screen:scaleBySize(2), padding = Screen:scaleBySize(2),
@ -55,6 +54,8 @@ local Button = InputContainer:extend{
padding = Size.padding.button, padding = Size.padding.button,
padding_h = nil, padding_h = nil,
padding_v = nil, padding_v = nil,
-- Provide only one of these two: 'width' to get a fixed width,
-- 'max_width' to allow it to be smaller if text or icon is smaller.
width = nil, width = nil,
max_width = nil, max_width = nil,
avoid_text_truncation = true, avoid_text_truncation = true,
@ -82,12 +83,13 @@ function Button:init()
self.padding_v = self.padding self.padding_v = self.padding
end end
local is_left_aligned = self.align == "left" local outer_pad_width = 2*self.padding_h + 2*self.margin + 2*self.bordersize -- unscaled_size_check: ignore
local right_margin = is_left_aligned and (2 * Size.padding.large) or 0
if self.text then if self.text then
local max_width = self.max_width local max_width = self.max_width or self.width
and self.max_width - 2*self.padding_h - 2*self.margin - 2*self.bordersize - right_margin or nil if max_width then
max_width = max_width - outer_pad_width
end
self.label_widget = TextWidget:new{ self.label_widget = TextWidget:new{
text = self.text, text = self.text,
max_width = max_width, max_width = max_width,
@ -146,14 +148,17 @@ function Button:init()
} }
end end
local widget_size = self.label_widget:getSize() local widget_size = self.label_widget:getSize()
if self.width == nil then local inner_width
self.width = widget_size.w if self.width then
inner_width = self.width - outer_pad_width
else
inner_width = widget_size.w
end end
-- set FrameContainer content -- set FrameContainer content
if is_left_aligned then if self.align == "left" then
self.label_container = LeftContainer:new{ self.label_container = LeftContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width - 4 * Size.padding.large, w = inner_width,
h = widget_size.h h = widget_size.h
}, },
self.label_widget, self.label_widget,
@ -161,7 +166,7 @@ function Button:init()
else else
self.label_container = CenterContainer:new{ self.label_container = CenterContainer:new{
dimen = Geom:new{ dimen = Geom:new{
w = self.width, w = inner_width,
h = widget_size.h h = widget_size.h
}, },
self.label_widget, self.label_widget,

@ -73,7 +73,7 @@ function ButtonProgressWidget:update()
buttons_count = buttons_count + 1 buttons_count = buttons_count + 1
span_count = span_count + 1 span_count = span_count + 1
end end
local button_width_real = (self.width - span_count * self.horizontal_span_width) / buttons_count - 2*button_padding - 2*button_margin - 2*button_bordersize local button_width_real = (self.width - span_count * self.horizontal_span_width) / buttons_count
local button_width = math.floor(button_width_real) local button_width = math.floor(button_width_real)
local button_width_adjust = button_width_real - button_width local button_width_adjust = button_width_real - button_width
local button_width_to_add = 0 local button_width_to_add = 0
@ -81,16 +81,14 @@ function ButtonProgressWidget:update()
-- Minus button on the left -- Minus button on the left
if self.fine_tune then if self.fine_tune then
button_width_to_add = button_width_to_add + button_width_adjust button_width_to_add = button_width_to_add + button_width_adjust
local margin = button_margin
local extra_border_size = 0
local button = Button:new{ local button = Button:new{
text = "", text = "",
radius = 0, radius = 0,
margin = margin, margin = button_margin,
padding = button_padding, padding = button_padding,
bordersize = button_bordersize + extra_border_size, bordersize = button_bordersize,
enabled = true, enabled = true,
width = button_width - 2*extra_border_size, width = button_width,
preselect = false, preselect = false,
text_font_face = self.font_face, text_font_face = self.font_face,
text_font_size = self.font_size, text_font_size = self.font_size,
@ -122,6 +120,7 @@ function ButtonProgressWidget:update()
local margin = button_margin local margin = button_margin
if self.thin_grey_style and highlighted then if self.thin_grey_style and highlighted then
margin = 0 -- moved outside button so it's not inverted margin = 0 -- moved outside button so it's not inverted
real_button_width = real_button_width - 2*button_margin
end end
local extra_border_size = 0 local extra_border_size = 0
if not self.thin_grey_style and is_default then if not self.thin_grey_style and is_default then
@ -135,7 +134,7 @@ function ButtonProgressWidget:update()
padding = button_padding, padding = button_padding,
bordersize = button_bordersize + extra_border_size, bordersize = button_bordersize + extra_border_size,
enabled = true, enabled = true,
width = real_button_width - 2*extra_border_size, width = real_button_width,
preselect = highlighted, preselect = highlighted,
text_font_face = self.font_face, text_font_face = self.font_face,
text_font_size = self.font_size, text_font_size = self.font_size,
@ -183,16 +182,14 @@ function ButtonProgressWidget:update()
real_button_width = button_width + math.floor(button_width_to_add) real_button_width = button_width + math.floor(button_width_to_add)
button_width_to_add = button_width_to_add - math.floor(button_width_to_add) button_width_to_add = button_width_to_add - math.floor(button_width_to_add)
end end
local margin = button_margin
local extra_border_size = 0
local button = Button:new{ local button = Button:new{
text = "", text = "",
radius = 0, radius = 0,
margin = margin, margin = button_margin,
padding = button_padding, padding = button_padding,
bordersize = button_bordersize + extra_border_size, bordersize = button_bordersize,
enabled = true, enabled = true,
width = real_button_width - 2*extra_border_size, width = real_button_width,
preselect = false, preselect = false,
text_font_face = self.font_face, text_font_face = self.font_face,
text_font_size = self.font_size, text_font_size = self.font_size,
@ -218,16 +215,14 @@ function ButtonProgressWidget:update()
-- One pixel wider to better align the entire widget -- One pixel wider to better align the entire widget
real_button_width = button_width + math.floor(button_width_to_add) real_button_width = button_width + math.floor(button_width_to_add)
end end
local margin = button_margin
local extra_border_size = 0
local button = Button:new{ local button = Button:new{
text = "", text = "",
radius = 0, radius = 0,
margin = margin, margin = button_margin,
padding = button_padding, padding = button_padding,
bordersize = button_bordersize + extra_border_size, bordersize = button_bordersize,
enabled = true, enabled = true,
width = real_button_width - 2*extra_border_size, width = real_button_width,
preselect = false, preselect = false,
text_font_face = self.font_face, text_font_face = self.font_face,
text_font_size = self.font_size, text_font_size = self.font_size,

@ -19,8 +19,6 @@ local ButtonTable = FocusManager:extend{
}, },
}, },
sep_width = Size.line.medium, sep_width = Size.line.medium,
padding = Size.padding.default,
zero_sep = false, zero_sep = false,
} }
@ -65,11 +63,11 @@ function ButtonTable:init()
allow_hold_when_disabled = btn_entry.allow_hold_when_disabled, allow_hold_when_disabled = btn_entry.allow_hold_when_disabled,
vsync = btn_entry.vsync, vsync = btn_entry.vsync,
width = math.ceil((self.width - sizer_space)/column_cnt), width = math.ceil((self.width - sizer_space)/column_cnt),
max_width = math.ceil((self.width - sizer_space)/column_cnt - 2*self.sep_width - 2*self.padding),
bordersize = 0, bordersize = 0,
margin = 0, margin = 0,
padding = Size.padding.buttontable, -- a bit taller than standalone buttons, for easier tap padding = Size.padding.buttontable, -- a bit taller than standalone buttons, for easier tap
padding_h = 0, -- allow text to take more of the horizontal space padding_h = btn_entry.align == "left" and Size.padding.large or 0,
-- allow text to take more of the horizontal space if centered
text_font_face = btn_entry.font_face, text_font_face = btn_entry.font_face,
text_font_size = btn_entry.font_size, text_font_size = btn_entry.font_size,
text_font_bold = btn_entry.font_bold, text_font_bold = btn_entry.font_bold,

@ -288,8 +288,8 @@ function ConfigOption:init()
local name_widget_width = math.floor(name_align * Screen:getWidth()) local name_widget_width = math.floor(name_align * Screen:getWidth())
-- We don't remove default_option_hpadding from name_text_max_width -- We don't remove default_option_hpadding from name_text_max_width
-- to give more to text and avoid truncation: as it is right aligned, -- to give more to text and avoid truncation: as it is right aligned,
-- the text can grow on the left, padding_small is enough. -- the text can grow on the left.
local name_text_max_width = name_widget_width - 2*padding_small local name_text_max_width = name_widget_width
local face = Font:getFace(name_font_face, name_font_size) local face = Font:getFace(name_font_face, name_font_size)
local option_name_container = RightContainer:new{ local option_name_container = RightContainer:new{
dimen = Geom:new{ w = name_widget_width, h = option_height}, dimen = Geom:new{ w = name_widget_width, h = option_height},

@ -236,7 +236,6 @@ function NumberPickerWidget:init()
text_font_face = self.spinner_face.font, text_font_face = self.spinner_face.font,
text_font_size = self.spinner_face.orig_size, text_font_size = self.spinner_face.orig_size,
width = self.width, width = self.width,
max_width = self.width,
show_parent = self.show_parent, show_parent = self.show_parent,
callback = callback_input, callback = callback_input,
} }

@ -55,7 +55,6 @@ function SkimToWidget:init()
local larger_span_units = 3 -- 3 x small span width local larger_span_units = 3 -- 3 x small span width
local nb_span_units = 2 + 2*larger_span_units local nb_span_units = 2 + 2*larger_span_units
local button_width = math.floor( (inner_width - nb_span_units * button_span_unit_width) * (1/5)) local button_width = math.floor( (inner_width - nb_span_units * button_span_unit_width) * (1/5))
local button_inner_width = button_width - 2 * (Size.border.button + Size.padding.button)
-- Update inner_width (possibly smaller because of math.floor()) -- Update inner_width (possibly smaller because of math.floor())
inner_width = button_width * 5 + nb_span_units * button_span_unit_width inner_width = button_width * 5 + nb_span_units * button_span_unit_width
@ -79,7 +78,7 @@ function SkimToWidget:init()
text = "-1", text = "-1",
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -90,7 +89,7 @@ function SkimToWidget:init()
text = "-10", text = "-10",
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -101,7 +100,7 @@ function SkimToWidget:init()
text = "+1", text = "+1",
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -112,7 +111,7 @@ function SkimToWidget:init()
text = "+10", text = "+10",
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -131,7 +130,7 @@ function SkimToWidget:init()
padding = 0, padding = 0,
bordersize = 0, bordersize = 0,
enabled = true, enabled = true,
width = button_width, -- no border/padding: use outer button width width = button_width,
show_parent = self, show_parent = self,
callback = function() callback = function()
self.callback_switch_to_goto() self.callback_switch_to_goto()
@ -153,7 +152,7 @@ function SkimToWidget:init()
text = chapter_next_text, text = chapter_next_text,
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -170,7 +169,7 @@ function SkimToWidget:init()
text = chapter_prev_text, text = chapter_prev_text,
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -187,7 +186,7 @@ function SkimToWidget:init()
text = bookmark_next_text, text = bookmark_next_text,
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -202,7 +201,7 @@ function SkimToWidget:init()
text = bookmark_prev_text, text = bookmark_prev_text,
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
vsync = true, vsync = true,
callback = function() callback = function()
@ -219,7 +218,7 @@ function SkimToWidget:init()
end, end,
radius = 0, radius = 0,
enabled = true, enabled = true,
width = button_inner_width, width = button_width,
show_parent = self, show_parent = self,
callback = function() callback = function()
self.ui:handleEvent(Event:new("ToggleBookmark")) self.ui:handleEvent(Event:new("ToggleBookmark"))

@ -532,7 +532,6 @@ function WordInfoDialog:init()
self.book_title_button = Button:new{ self.book_title_button = Button:new{
text = self.book_title .. book_title_triangle, text = self.book_title .. book_title_triangle,
width = width, width = width,
max_width = width,
text_font_face = "NotoSans-Italic.ttf", text_font_face = "NotoSans-Italic.ttf",
text_font_size = 14, text_font_size = 14,
text_font_bold = false, text_font_bold = false,
@ -755,7 +754,6 @@ function VocabItemWidget:initItemWidget()
self.forgot_button = Button:new{ self.forgot_button = Button:new{
text = _("Forgot"), text = _("Forgot"),
width = self.review_button_width, width = self.review_button_width,
max_width = self.review_button_width,
radius = Size.radius.button, radius = Size.radius.button,
callback = function() callback = function()
self:onForgot() self:onForgot()
@ -770,7 +768,6 @@ function VocabItemWidget:initItemWidget()
self:onGotIt() self:onGotIt()
end, end,
width = self.review_button_width, width = self.review_button_width,
max_width = self.review_button_width,
show_parent = self, show_parent = self,
} }
right_widget = HorizontalGroup:new{ right_widget = HorizontalGroup:new{
@ -1359,7 +1356,7 @@ function VocabularyBuilderWidget:refreshFooter()
local sync_size = TextWidget:getFontSizeToFitHeight("cfont", footer_height, Size.padding.buttontable*2) local sync_size = TextWidget:getFontSizeToFitHeight("cfont", footer_height, Size.padding.buttontable*2)
self.footer_sync = Button:new{ self.footer_sync = Button:new{
text = "", text = "",
width = self.footer_left_corner_width - Size.padding.large * 2, width = self.footer_left_corner_width,
text_font_size = sync_size, text_font_size = sync_size,
text_font_bold = false, text_font_bold = false,
bordersize = 0, bordersize = 0,

Loading…
Cancel
Save