ConfirmBox: avoid overflowing screen height when long text

Just like what's been done previously for InfoMessage.
Also tweak 'other_buttons' option (not yet used anywhere)
to allow multiple rows of such other buttons.
reviewable/pr5898/r1
poire-z 4 years ago
parent 74efb24b9d
commit 7d7781b768

@ -73,6 +73,11 @@ function ConfirmBox:init()
} }
end end
end end
local text_widget = TextBoxWidget:new{
text = self.text,
face = self.face,
width = Screen:getWidth()*2/3,
}
local content = HorizontalGroup:new{ local content = HorizontalGroup:new{
align = "center", align = "center",
ImageWidget:new{ ImageWidget:new{
@ -80,11 +85,7 @@ function ConfirmBox:init()
scale_for_dpi = true, scale_for_dpi = true,
}, },
HorizontalSpan:new{ width = Size.span.horizontal_default }, HorizontalSpan:new{ width = Size.span.horizontal_default },
TextBoxWidget:new{ text_widget,
text = self.text,
face = self.face,
width = Screen:getWidth()*2/3,
}
} }
local buttons = {{ local buttons = {{
@ -100,20 +101,26 @@ function ConfirmBox:init()
UIManager:close(self) UIManager:close(self)
end, end,
},} },}
buttons = { buttons } -- single row
if self.other_buttons ~= nil then if self.other_buttons ~= nil then
for __, button in ipairs(self.other_buttons) do -- additional rows
assert(type(button.text) == "string") for __, buttons_row in ipairs(self.other_buttons) do
assert(button.callback == nil or type(button.callback) == "function") local row = {}
table.insert(buttons, { table.insert(buttons, row)
text = button.text, for ___, button in ipairs(buttons_row) do
callback = function() assert(type(button.text) == "string")
if button.callback ~= nil then assert(button.callback == nil or type(button.callback) == "function")
button.callback() table.insert(row, {
end text = button.text,
UIManager:close(self) callback = function()
end, if button.callback ~= nil then
}) button.callback()
end
UIManager:close(self)
end,
})
end
end end
end end
@ -121,29 +128,54 @@ function ConfirmBox:init()
width = content:getSize().w, width = content:getSize().w,
button_font_face = "cfont", button_font_face = "cfont",
button_font_size = 20, button_font_size = 20,
buttons = { buttons }, buttons = buttons,
zero_sep = true, zero_sep = true,
show_parent = self, show_parent = self,
} }
local frame = FrameContainer:new{
background = Blitbuffer.COLOR_WHITE,
margin = self.margin,
padding = self.padding,
padding_bottom = 0, -- no padding below buttontable
VerticalGroup:new{
align = "left",
content,
-- Add same vertical space after than before content
VerticalSpan:new{ width = self.margin + self.padding },
button_table,
}
}
self.movable = MovableContainer:new{
frame,
}
self[1] = CenterContainer:new{ self[1] = CenterContainer:new{
dimen = Screen:getSize(), dimen = Screen:getSize(),
MovableContainer:new{ self.movable,
FrameContainer:new{
background = Blitbuffer.COLOR_WHITE,
margin = self.margin,
padding = self.padding,
padding_bottom = 0, -- no padding below buttontable
VerticalGroup:new{
align = "left",
content,
-- Add same vertical space after than before content
VerticalSpan:new{ width = self.margin + self.padding },
button_table,
}
}
}
} }
-- Reduce font size until widget fit screen height if needed
local cur_size = frame:getSize()
if cur_size and cur_size.h > 0.95 * Screen:getHeight() then
local orig_font = text_widget.face.orig_font
local orig_size = text_widget.face.orig_size
local real_size = text_widget.face.size
if orig_size > 10 then -- don't go too small
while true do
orig_size = orig_size - 1
self.face = Font:getFace(orig_font, orig_size)
-- scaleBySize() in Font:getFace() may give the same
-- real font size even if we decreased orig_size,
-- so check we really got a smaller real font size
if self.face.size < real_size then
break
end
end
-- re-init this widget
self:free()
self:init()
end
end
end end
function ConfirmBox:onShow() function ConfirmBox:onShow()

Loading…
Cancel
Save