From 9db74f1f01a12a2a34b3bdb0207da592dce8e3df Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Tue, 2 May 2023 08:25:34 +0300 Subject: [PATCH] ConfirmBox: add widgets (#10364) --- .../apps/reader/modules/readerbookmark.lua | 5 +- frontend/ui/widget/checkbutton.lua | 6 +- frontend/ui/widget/confirmbox.lua | 84 +++++++++++++------ frontend/ui/widget/inputdialog.lua | 4 + frontend/ui/widget/openwithdialog.lua | 1 - 5 files changed, 70 insertions(+), 30 deletions(-) diff --git a/frontend/apps/reader/modules/readerbookmark.lua b/frontend/apps/reader/modules/readerbookmark.lua index 2f9f8650f..8db2ddf48 100644 --- a/frontend/apps/reader/modules/readerbookmark.lua +++ b/frontend/apps/reader/modules/readerbookmark.lua @@ -1136,15 +1136,16 @@ function ReaderBookmark:onSearchBookmark(bm_menu) parent = input_dialog, } input_dialog:addWidget(check_button_case) + local separator_width = input_dialog:getAddedWidgetAvailableWidth() separator = CenterContainer:new{ dimen = Geom:new{ - w = input_dialog._input_widget.width, + w = separator_width, h = 2 * Size.span.vertical_large, }, LineWidget:new{ background = Blitbuffer.COLOR_DARK_GRAY, dimen = Geom:new{ - w = input_dialog._input_widget.width, + w = separator_width, h = Size.line.medium, } }, diff --git a/frontend/ui/widget/checkbutton.lua b/frontend/ui/widget/checkbutton.lua index ff45e7164..ef7e785aa 100644 --- a/frontend/ui/widget/checkbutton.lua +++ b/frontend/ui/widget/checkbutton.lua @@ -38,8 +38,8 @@ local CheckButton = InputContainer:extend{ background = Blitbuffer.COLOR_WHITE, text = nil, parent = nil, -- parent widget, must be set by the caller - width = nil, -- default value: parent widget's input widget width - -- If the parent widget has no input widget, the width must be set by the caller. + width = nil, -- default value: parent widget's added widgets available width + -- If the parent widget has no getAddedWidgetAvailableWidth() method, the width must be set by the caller. } function CheckButton:init() @@ -70,7 +70,7 @@ function CheckButton:initCheckButton(checked) self._textwidget = TextBoxWidget:new{ text = self.text, face = self.face, - width = (self.width or self.parent._input_widget.width) - self._checkmark.dimen.w, + width = (self.width or self.parent:getAddedWidgetAvailableWidth()) - self._checkmark.dimen.w, fgcolor = self.enabled and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_DARK_GRAY, } local textbox_shift = math.max(0, self._checkmark.baseline - self._textwidget:getBaseline()) diff --git a/frontend/ui/widget/confirmbox.lua b/frontend/ui/widget/confirmbox.lua index 656319187..7387b0b5b 100644 --- a/frontend/ui/widget/confirmbox.lua +++ b/frontend/ui/widget/confirmbox.lua @@ -76,11 +76,23 @@ function ConfirmBox:init() self.key_events.Close = { { Device.input.group.Back } } end end + + self.text_widget_width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 2/3) local text_widget = TextBoxWidget:new{ text = self.text, face = self.face, - width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 2/3), + width = self.text_widget_width, + } + self.text_group = VerticalGroup:new{ + align = "left", + text_widget, } + if self._added_widgets then + table.insert(self.text_group, VerticalSpan:new{ width = Size.padding.large }) + for _, widget in ipairs(self._added_widgets) do + table.insert(self.text_group, widget) + end + end local content = HorizontalGroup:new{ align = "center", IconWidget:new{ @@ -88,36 +100,36 @@ function ConfirmBox:init() alpha = true, }, HorizontalSpan:new{ width = Size.span.horizontal_default }, - text_widget, + self.text_group, } - local buttons = {{ - text = self.cancel_text, - callback = function() - self.cancel_callback() - UIManager:close(self) - end, - }, { - text = self.ok_text, - callback = function() - self.ok_callback() - if self.keep_dialog_open then return end - UIManager:close(self) - end, - },} - buttons = { buttons } -- single row + local buttons = {{ -- single row + { + text = self.cancel_text, + callback = function() + self.cancel_callback() + UIManager:close(self) + end, + }, + { + text = self.ok_text, + callback = function() + self.ok_callback() + if self.keep_dialog_open then return end + UIManager:close(self) + end, + }, + }} - if self.other_buttons ~= nil then - -- additional rows + if self.other_buttons then -- additional rows local rownum = self.other_buttons_first and 0 or 1 for i, buttons_row in ipairs(self.other_buttons) do local row = {} - table.insert(buttons, rownum + i, row) - for ___, button in ipairs(buttons_row) do + for _, button in ipairs(buttons_row) do table.insert(row, { text = button.text, callback = function() - if button.callback ~= nil then + if button.callback then button.callback() end if self.keep_dialog_open then return end @@ -125,13 +137,12 @@ function ConfirmBox:init() end, }) end + table.insert(buttons, rownum + i, row) end end local button_table = ButtonTable:new{ width = content:getSize().w, - button_font_face = "cfont", - button_font_size = 20, buttons = buttons, zero_sep = true, show_parent = self, @@ -177,12 +188,37 @@ function ConfirmBox:init() end end -- re-init this widget + if self._added_widgets then + self:_preserveAddedWidgets() + end self:free() self:init() end end end +function ConfirmBox:addWidget(widget) + if self._added_widgets then + self:_preserveAddedWidgets() + else + self._added_widgets = {} + end + table.insert(self._added_widgets, widget) + self:free() + self:init() +end + +function ConfirmBox:_preserveAddedWidgets() + -- remove added widgets to preserve their subwidgets from being free'ed + for i = 1, #self._added_widgets do + table.remove(self.text_group) + end +end + +function ConfirmBox:getAddedWidgetAvailableWidth() + return self.text_widget_width +end + function ConfirmBox:onShow() UIManager:setDirty(self, function() return "ui", self.movable.dimen diff --git a/frontend/ui/widget/inputdialog.lua b/frontend/ui/widget/inputdialog.lua index db98cd147..34ae7ea07 100644 --- a/frontend/ui/widget/inputdialog.lua +++ b/frontend/ui/widget/inputdialog.lua @@ -469,6 +469,10 @@ function InputDialog:addWidget(widget, re_init) table.insert(self.vgroup, #self.vgroup-1, widget) end +function InputDialog:getAddedWidgetAvailableWidth() + return self._input_widget.width +end + function InputDialog:onTap() if self.deny_keyboard_hiding then return diff --git a/frontend/ui/widget/openwithdialog.lua b/frontend/ui/widget/openwithdialog.lua index ca19749cd..47b34e54d 100644 --- a/frontend/ui/widget/openwithdialog.lua +++ b/frontend/ui/widget/openwithdialog.lua @@ -43,7 +43,6 @@ function OpenWithDialog:init() } self.layout = {self.layout[#self.layout]} -- keep bottom buttons self:mergeLayoutInVertical(self.radio_button_table, #self.layout) -- before bottom buttons - self._input_widget = self.radio_button_table local vertical_span = VerticalSpan:new{ width = Size.padding.large,