diff --git a/frontend/apps/reader/modules/readerfooter.lua b/frontend/apps/reader/modules/readerfooter.lua index d68801035..26b94297e 100644 --- a/frontend/apps/reader/modules/readerfooter.lua +++ b/frontend/apps/reader/modules/readerfooter.lua @@ -6,6 +6,7 @@ local CenterContainer = require("ui/widget/container/centercontainer") local Device = require("device") local Event = require("ui/event") local Font = require("ui/font") +local FontChooser = require("ui/widget/fontchooser") local FrameContainer = require("ui/widget/container/framecontainer") local Geom = require("ui/geometry") local HorizontalGroup = require("ui/widget/horizontalgroup") @@ -483,7 +484,6 @@ local ReaderFooter = WidgetContainer:extend{ pageno = nil, pages = nil, footer_text = nil, - text_font_face = "ffont", height = Screen:scaleBySize(G_defaults:readSetting("DMINIBAR_CONTAINER_HEIGHT")), horizontal_margin = Size.span.horizontal_default, bottom_padding = Size.padding.tiny, @@ -523,6 +523,7 @@ ReaderFooter.default_settings = { toc_markers_width = 2, -- unscaled_size_check: ignore text_font_size = 14, -- unscaled_size_check: ignore text_font_bold = false, + text_font_face = "./fonts/noto/NotoSans-Regular.ttf", container_height = G_defaults:readSetting("DMINIBAR_CONTAINER_HEIGHT"), container_bottom_padding = 1, -- unscaled_size_check: ignore progress_margin_width = Device:isAndroid() and Screen:scaleByDPI(16) or 10, -- android: guidelines for rounded corner margins @@ -589,7 +590,10 @@ function ReaderFooter:init() self:set_has_no_mode() self.reclaim_height = self.settings.reclaim_height - self.footer_text_face = Font:getFace(self.text_font_face, self.settings.text_font_size) + if not FontChooser.isFontRegistered(self.settings.text_font_face) then + self.settings.text_font_face = self.default_settings.text_font_face + end + self.footer_text_face = Font:getFace(self.settings.text_font_face, self.settings.text_font_size) self.footer_text = TextWidget:new{ text = "", face = self.footer_text_face, @@ -1555,19 +1559,22 @@ With this feature enabled, the current page is factored in, resulting in the cou }, { text_func = function() - local font_weight = "" - if self.settings.text_font_bold == true then - font_weight = ", " .. _("bold") + local text = self.settings.text_font_face == self.default_settings.text_font_face + and _("default") or FontChooser.getFontNameText(self.settings.text_font_face) + text = text .. ", " .. self.settings.text_font_size + if self.settings.text_font_bold then + text = text .. ", " .. _("bold") end - return T(_("Item font: %1%2"), self.settings.text_font_size, font_weight) + return T(_("Item font: %1"), text) end, sub_item_table = { { text_func = function() return T(_("Item font size: %1"), self.settings.text_font_size) end, + keep_menu_open = true, callback = function(touchmenu_instance) - local items_font = SpinWidget:new{ + UIManager:show(SpinWidget:new{ title_text = _("Item font size"), value = self.settings.text_font_size, value_min = 8, @@ -1580,10 +1587,40 @@ With this feature enabled, the current page is factored in, resulting in the cou self:refreshFooter(true, true) touchmenu_instance:updateItems() end, - } - UIManager:show(items_font) + }) + end, + }, + { + text_func = function() + local text = self.settings.text_font_face == self.default_settings.text_font_face + and _("default") or FontChooser.getFontNameText(self.settings.text_font_face) + return T(_("Item font: %1"), text) end, keep_menu_open = true, + callback = function(touchmenu_instance) + UIManager:show(FontChooser:new{ + title = _("Item font"), + font_file = self.settings.text_font_face, + default_font_file = self.default_settings.text_font_face, + keep_shown_on_apply = true, + callback = function(file) + if self.settings.text_font_face ~= file then + self.settings.text_font_face = file + self:updateFooterFont() + self:refreshFooter(true, true) + touchmenu_instance:updateItems() + end + end, + }) + end, + hold_callback = function(touchmenu_instance) + if self.settings.text_font_face ~= self.default_settings.text_font_face then + self.settings.text_font_face = self.default_settings.text_font_face + self:updateFooterFont() + self:refreshFooter(true, true) + touchmenu_instance:updateItems() + end + end, }, { text = _("Items in bold"), @@ -1981,6 +2018,7 @@ end function ReaderFooter:loadPreset(preset) local old_text_font_size = self.settings.text_font_size + local old_text_font_face = self.settings.text_font_face local old_text_font_bold = self.settings.text_font_bold G_reader_settings:saveSetting("footer", util.tableDeepCopy(preset.footer)) G_reader_settings:saveSetting("reader_footer_mode", preset.reader_footer_mode) @@ -1998,7 +2036,8 @@ function ReaderFooter:loadPreset(preset) end self:applyFooterMode(preset.reader_footer_mode) self:updateFooterTextGenerator() - if old_text_font_size ~= self.settings.text_font_size or old_text_font_bold ~= self.settings.text_font_bold then + if old_text_font_size ~= self.settings.text_font_size or old_text_font_face ~= self.settings.text_font_face + or old_text_font_bold ~= self.settings.text_font_bold then self:updateFooterFont() else self.separator_width = nil @@ -2167,7 +2206,10 @@ end function ReaderFooter:updateFooterFont() self.separator_width = nil self.filler_space_width = nil - self.footer_text_face = Font:getFace(self.text_font_face, self.settings.text_font_size) + if not FontChooser.isFontRegistered(self.settings.text_font_face) then + self.settings.text_font_face = self.default_settings.text_font_face + end + self.footer_text_face = Font:getFace(self.settings.text_font_face, self.settings.text_font_size) self.footer_text:free() self.footer_text = TextWidget:new{ text = self.footer_text.text, diff --git a/frontend/ui/widget/checkbutton.lua b/frontend/ui/widget/checkbutton.lua index 4346cff25..5030e5c6e 100644 --- a/frontend/ui/widget/checkbutton.lua +++ b/frontend/ui/widget/checkbutton.lua @@ -18,11 +18,14 @@ local Blitbuffer = require("ffi/blitbuffer") local CheckMark = require("ui/widget/checkmark") local Font = require("ui/font") local FrameContainer = require("ui/widget/container/framecontainer") +local Geom = require("ui/geometry") local GestureRange = require("ui/gesturerange") local HorizontalGroup = require("ui/widget/horizontalgroup") local InputContainer = require("ui/widget/container/inputcontainer") +local LeftContainer = require("ui/widget/container/leftcontainer") local RadioMark = require("ui/widget/radiomark") local TextBoxWidget = require("ui/widget/textboxwidget") +local TextWidget = require("ui/widget/textwidget") local UIManager = require("ui/uimanager") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") @@ -35,8 +38,10 @@ local CheckButton = InputContainer:extend{ enabled = true, radio = false, -- radio mark when true face = Font:getFace("smallinfofont"), + checkmark_face = Font:getFace("smallinfofont"), background = Blitbuffer.COLOR_WHITE, text = nil, + single_line = nil, parent = nil, -- parent widget, 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. @@ -53,7 +58,7 @@ function CheckButton:initCheckButton(checked) checkable = self.checkable, checked = self.checked, enabled = self.enabled, - face = self.face, + face = self.checkmark_face, parent = self.parent or self, show_parent = self.show_parent or self, } @@ -62,32 +67,54 @@ function CheckButton:initCheckButton(checked) checkable = self.checkable, checked = self.checked, enabled = self.enabled, - face = self.face, + face = self.checkmark_face, parent = self.parent or self, show_parent = self.show_parent or self, } end + local fgcolor = self.fgcolor or Blitbuffer.COLOR_BLACK - self._textwidget = TextBoxWidget:new{ - text = self.text, - face = self.face, - width = (self.width or self.parent:getAddedWidgetAvailableWidth()) - self._checkmark.dimen.w, - bold = self.bold, - fgcolor = self.enabled and fgcolor or Blitbuffer.COLOR_DARK_GRAY, - bgcolor = self.bgcolor, - } - local textbox_shift = math.max(0, self._checkmark.baseline - self._textwidget:getBaseline()) - self._verticalgroup = VerticalGroup:new{ - align = "left", - VerticalSpan:new{ - width = textbox_shift, - }, - self._textwidget, - } + local max_width = (self.width or self.parent:getAddedWidgetAvailableWidth()) - self._checkmark.dimen.w + local textwidget + if self.single_line then + local checkmark_h = self._checkmark:getSize().h + textwidget = TextWidget:new{ + text = self.text, + face = self.face, + max_width = max_width, + bold = self.bold, + fgcolor = self.enabled and fgcolor or Blitbuffer.COLOR_DARK_GRAY, + forced_baseline = self._checkmark.baseline, + forced_height = checkmark_h, + } + textwidget = LeftContainer:new{ + dimen = Geom:new{ + w = max_width, + h = checkmark_h, + }, + textwidget, + } + else + textwidget = TextBoxWidget:new{ + text = self.text, + face = self.face, + width = max_width, + bold = self.bold, + fgcolor = self.enabled and fgcolor or Blitbuffer.COLOR_DARK_GRAY, + bgcolor = self.bgcolor, + } + textwidget = VerticalGroup:new{ + align = "left", + VerticalSpan:new{ + width = math.max(0, self._checkmark.baseline - textwidget:getBaseline()), + }, + textwidget, + } + end self._horizontalgroup = HorizontalGroup:new{ align = "top", self._checkmark, - self._verticalgroup, + textwidget, } self._frame = FrameContainer:new{ bordersize = 0, diff --git a/frontend/ui/widget/fontchooser.lua b/frontend/ui/widget/fontchooser.lua new file mode 100644 index 000000000..e68379b11 --- /dev/null +++ b/frontend/ui/widget/fontchooser.lua @@ -0,0 +1,254 @@ +local Blitbuffer = require("ffi/blitbuffer") +local ButtonTable = require("ui/widget/buttontable") +local CenterContainer = require("ui/widget/container/centercontainer") +local FocusManager = require("ui/widget/focusmanager") +local Font = require("ui/font") +local FontList = require("fontlist") +local FrameContainer = require("ui/widget/container/framecontainer") +local Geom = require("ui/geometry") +local GestureRange = require("ui/gesturerange") +local InfoMessage = require("ui/widget/infomessage") +local MovableContainer = require("ui/widget/container/movablecontainer") +local RadioButtonTable = require("ui/widget/radiobuttontable") +local ScrollableContainer = require("ui/widget/container/scrollablecontainer") +local Size = require("ui/size") +local TitleBar = require("ui/widget/titlebar") +local UIManager = require("ui/uimanager") +local VerticalGroup = require("ui/widget/verticalgroup") +local VerticalSpan = require("ui/widget/verticalspan") +local ffiUtil = require("ffi/util") +local _ = require("gettext") +local Device = require("device") +local Screen = Device.screen + +local FontChooser = FocusManager:extend{ + title = "", + font_file = nil, -- current + default_font_file = nil, -- is marked with a star + keep_shown_on_apply = false, + callback = nil, -- must be provided by the caller + close_callback = nil, -- can be nil +} + +function FontChooser:init() + local s = Screen:getSize() + local screen_w, screen_h = s.w, s.h + + self.ges_events.TapClose = { + GestureRange:new{ + ges = "tap", + range = Geom:new{ + w = screen_w, + h = screen_h, + }, + }, + } + if Device:hasKeys() then + self.key_events.Close = { { Device.input.group.Back } } + end + + local width = math.floor(math.min(screen_w, screen_h) * 0.8) + self.layout = {} + + local title_bar = TitleBar:new{ + width = width, + title = self.title, + align = "left", + with_bottom_line = true, + bottom_v_padding = 0, + show_parent = self, + } + + local radio_buttons = {} -- one column + for font_file, font_info in pairs(FontList.fontinfo) do + local name_text, name = self.getFontNameText(font_file) + if self.default_font_file and self.default_font_file == font_file then + name_text = name_text .. " ★" + end + table.insert(radio_buttons, {{ + text = name_text, + name = name, + checked = font_file == self.font_file, + provider = font_file, + face = Font:getFace(font_file, 22), + hold_callback = function() + UIManager:show(InfoMessage:new{ text = font_file, show_icon = false }) + end, + }}) + end + if #radio_buttons > 1 then + table.sort(radio_buttons, function(a, b) + if a[1].name ~= b[1].name then + return ffiUtil.strcoll(a[1].name, b[1].name) + end + return ffiUtil.strcoll(a[1].text, b[1].text) + end) + end + + local scroll_container_inner_width = width - ScrollableContainer:getScrollbarWidth() + local radio_button_table = RadioButtonTable:new{ + radio_buttons = radio_buttons, + width = scroll_container_inner_width - 2*Size.padding.large, + button_single_line = true, + no_sep = true, + focused = true, + parent = self, + show_parent = self, + } + self:mergeLayoutInVertical(radio_button_table) + + local buttons = {{ + { + text = _("Close"), + id = "close", + callback = function() + UIManager:close(self) + end, + }, + { + text = _("Set font"), + is_enter_default = true, + callback = function() + if not self.keep_shown_on_apply then + UIManager:close(self) + end + self.callback(radio_button_table.checked_button.provider) + end, + }, + }} + local button_table = ButtonTable:new{ + width = width - 2*Size.padding.default, + buttons = buttons, + zero_sep = true, + show_parent = self, + } + self:mergeLayoutInVertical(button_table) + + self.radio_button_table_height = radio_button_table:getSize().h + local max_radio_button_container_height = math.floor(screen_h*0.8 + - title_bar:getHeight() + - Size.span.vertical_large*4 - button_table:getSize().h) + if self.radio_button_table_height > max_radio_button_container_height then + self.is_scrollable = true + -- adjust scrollable container height to fit integer number of buttons + local radio_button_height = radio_button_table.checked_button:getSize().h -- all buttons of the same height + self.radio_buttons_per_page = math.floor(max_radio_button_container_height / radio_button_height) + self.radio_button_container_height = self.radio_buttons_per_page * radio_button_height + else + self.radio_button_container_height = self.radio_button_table_height + end + + self.cropping_widget = ScrollableContainer:new{ + dimen = Geom:new{ + w = width, + h = self.radio_button_container_height, + }, + show_parent = self, + CenterContainer:new{ + dimen = Geom:new{ + w = scroll_container_inner_width, + h = self.radio_button_table_height, + }, + radio_button_table, + }, + } + self:scrollToCheckedButton() + + local dialog_frame = FrameContainer:new{ + radius = Size.radius.window, + bordersize = Size.border.window, + padding = 0, + margin = 0, + background = Blitbuffer.COLOR_WHITE, + VerticalGroup:new{ + align = "center", + title_bar, + VerticalSpan:new{ + width = Size.span.vertical_large*2, + }, + self.cropping_widget, + VerticalSpan:new{ + width = Size.span.vertical_large*2, + }, + CenterContainer:new{ + dimen = Geom:new{ + w = width, + h = button_table:getSize().h, + }, + button_table, + }, + }, + } + + self.movable = MovableContainer:new{ + dialog_frame, + } + self[1] = CenterContainer:new{ + dimen = Geom:new{ + w = screen_w, + h = screen_h, + }, + self.movable, + } +end + +function FontChooser:scrollToCheckedButton() + if not self.is_scrollable then return end + -- keep scrollable container "pagination" + local radio_button_table = self.cropping_widget[1][1] + local checked_button_previous_page_number = + math.floor((radio_button_table.checked_button.row - 1) / self.radio_buttons_per_page) + local checked_button_page_offset = checked_button_previous_page_number * self.radio_button_container_height + local max_offset = self.radio_button_table_height - self.radio_button_container_height + self.cropping_widget:setScrolledOffset({ x = 0, y = math.min(checked_button_page_offset, max_offset) }) +end + +function FontChooser.isFontRegistered(file) + return file and FontList.fontinfo[file] and true or false +end + +function FontChooser.getFontNameText(file) + local font_info = FontList.fontinfo[file] + local info = font_info and font_info[1] + if info then + local name = FontList:getLocalizedFontName(file, 0) or info.name + local name_text = name + if info.bold then + name_text = name_text .. " " .. _("bold") + end + if info.italic then + name_text = name_text .. " " .. _("italic") + end + return name_text, name + end +end + +function FontChooser:onShow() + UIManager:setDirty(self, function() + return "ui", self.movable.dimen + end) + return true +end + +function FontChooser:onCloseWidget() + UIManager:setDirty(nil, function() + return "ui", self.movable.dimen + end) +end + +function FontChooser:onTapClose(arg, ges_ev) + if ges_ev.pos:notIntersectWith(self.movable.dimen) then + self:onClose() + end + return true +end + +function FontChooser:onClose() + UIManager:close(self) + if self.close_callback then + self.close_callback() + end + return true +end + +return FontChooser diff --git a/frontend/ui/widget/radiobuttontable.lua b/frontend/ui/widget/radiobuttontable.lua index 635f262da..280c11eef 100644 --- a/frontend/ui/widget/radiobuttontable.lua +++ b/frontend/ui/widget/radiobuttontable.lua @@ -4,7 +4,6 @@ A button table to be used in dialogs and widgets. local Blitbuffer = require("ffi/blitbuffer") local CheckButton = require("ui/widget/checkbutton") -local Device = require("device") local FocusManager = require("ui/widget/focusmanager") local Font = require("ui/font") local Geom = require("ui/geometry") @@ -14,21 +13,19 @@ local Size = require("ui/size") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") local dbg = require("dbg") +local Device = require("device") local Screen = Device.screen local RadioButtonTable = FocusManager:extend{ width = Screen:getWidth(), - radio_buttons = { - { - {text="Cancel", enabled=false, callback=nil}, - {text="OK", enabled=true, callback=nil}, - }, - }, + radio_buttons = nil, -- must be provided by the caller sep_width = Size.line.medium, - padding = Size.padding.button, + padding = Size.padding.button, -- between buttons in a row zero_sep = false, - face = Font:getFace("cfont", 22), + no_sep = false, -- if true: no horizontal separator/span before and after the table + face = Font:getFace("cfont", 22), -- can be overriden by a button face + button_single_line = nil, _first_button = nil, checked_button = nil, button_select_callback = nil, @@ -48,6 +45,7 @@ function RadioButtonTable:init() self:addHorizontalSep(false, false, true) end + local one_sizer_space = self.sep_width + 2 * self.padding local row_cnt = #self.radio_buttons for i = 1, row_cnt do @@ -55,7 +53,7 @@ function RadioButtonTable:init() local horizontal_group = HorizontalGroup:new{} local row = self.radio_buttons[i] local column_cnt = #row - local sizer_space = (self.sep_width + 2 * self.padding) * (column_cnt - 1) + local button_width = math.floor((self.width - one_sizer_space * (column_cnt - 1)) / column_cnt) for j = 1, column_cnt do local btn_entry = row[j] local button = CheckButton:new{ @@ -65,32 +63,31 @@ function RadioButtonTable:init() enabled = btn_entry.enabled, radio = true, provider = btn_entry.provider, + hold_callback = btn_entry.hold_callback, bold = btn_entry.bold, fgcolor = btn_entry.fgcolor, bgcolor = btn_entry.bgcolor, - width = (self.width - sizer_space) / column_cnt, + row = i, + width = button_width, bordersize = 0, margin = 0, padding = 0, - face = self.face, + face = btn_entry.face or self.face, + single_line = self.button_single_line, show_parent = self.show_parent or self, parent = self.parent or self, } local button_callback = function() - self:_checkButton(button) + self:checkButton(button) if self.button_select_callback then self.button_select_callback(btn_entry) end end button.callback = button_callback - if i == 1 and j == 1 then - self._first_button = button - end - if button.checked and not self.checked_button then self.checked_button = button elseif dbg.is_on and @@ -98,17 +95,16 @@ function RadioButtonTable:init() error("RadioButtonGroup: multiple checked RadioButtons") end - local button_dim = button:getSize() - local vertical_sep = LineWidget:new{ - background = Blitbuffer.COLOR_DARK_GRAY, - dimen = Geom:new{ - w = self.sep_width, - h = button_dim.h, - } - } self.radio_buttons_layout[i][j] = button table.insert(horizontal_group, button) if j < column_cnt then + local vertical_sep = LineWidget:new{ + background = Blitbuffer.COLOR_DARK_GRAY, + dimen = Geom:new{ + w = self.sep_width, + h = button:getSize().h, + } + } table.insert(horizontal_group, vertical_sep) end end -- end for each button @@ -120,6 +116,7 @@ function RadioButtonTable:init() self:addHorizontalSep(true, false, false) -- check first entry unless otherwise specified + self._first_button = self.radio_buttons_layout[1][1] if not self.checked_button then self._first_button:toggleCheck() self.checked_button = self._first_button @@ -134,6 +131,7 @@ function RadioButtonTable:init() end function RadioButtonTable:addHorizontalSep(vspan_before, add_line, vspan_after, black_line) + if self.no_sep then return end if vspan_before then table.insert(self.container, VerticalSpan:new{ width = Size.span.vertical_default }) @@ -153,13 +151,12 @@ function RadioButtonTable:addHorizontalSep(vspan_before, add_line, vspan_after, end end -function RadioButtonTable:_checkButton(button) - -- nothing to do - if button.checked then return end - - self.checked_button:toggleCheck() - button:toggleCheck() - self.checked_button = button +function RadioButtonTable:checkButton(button) + if not button.checked then + self.checked_button:toggleCheck() + button:toggleCheck() + self.checked_button = button + end end return RadioButtonTable