diff --git a/frontend/ui/bidi.lua b/frontend/ui/bidi.lua index 29b4557ac..387ee093e 100644 --- a/frontend/ui/bidi.lua +++ b/frontend/ui/bidi.lua @@ -37,6 +37,7 @@ https://material.io/design/usability/bidirectionality.html ]] local Language = require("ui/language") +local util = require("util") local _ = require("gettext") local Bidi = { @@ -78,13 +79,25 @@ function Bidi.setup(lang) xtext.setDefaultLang(alt_lang) end end - -- Optimise Bidi.default and Bidi.wrap by aliasing them to the right wrappers + -- Optimise some wrappers by aliasing them to the right wrappers if Bidi._rtl_ui_text then Bidi.default = Bidi.rtl Bidi.wrap = Bidi.rtl + Bidi.filename = Bidi._filename_rtl + Bidi.filepath = Bidi.ltr -- see if we need to split and _filename_rtl() the filename part + Bidi.directory = Bidi.ltr + Bidi.dirpath = Bidi.ltr + Bidi.path = Bidi.ltr + Bidi.url = Bidi.ltr else Bidi.default = Bidi.ltr - Bidi.wrap = Bidi.noop + Bidi.wrap = Bidi.nowrap + Bidi.filename = Bidi.nowrap + Bidi.filepath = Bidi.nowrap + Bidi.directory = Bidi.nowrap + Bidi.dirpath = Bidi.nowrap + Bidi.path = Bidi.nowrap + Bidi.url = Bidi.nowrap end end @@ -160,14 +173,14 @@ function Bidi.default(text) -- default direction return Bidi._rtl_ui_text and Bidi.rtl(text) or Bidi.ltr(text) end -function Bidi.noop(text) -- no wrap +function Bidi.nowrap(text) return text end -- Helper for concatenated string bits of numbers an symbols (like -- our reader footer) to keep them ordered in RTL UI (to not have -- a letter B for battery make the whole string considered LTR). --- Note: it will be replaced and aliased to Bidi.noop or Bidi.rtl +-- Note: it will be replaced and aliased to Bidi.nowrap or Bidi.rtl -- by Bibi.setup() as an optimisation function Bidi.wrap(text) return Bidi._rtl_ui_text and Bidi.rtl(text) or text @@ -181,9 +194,21 @@ end -- shown as real RTL). -- Note: when the filename or path are standalone in a TextWidget, it's -- better to use "para_direction_rtl = false" without any wrapping. -Bidi.filename = Bidi.ltr -Bidi.directory = Bidi.ltr -Bidi.path = Bidi.ltr -Bidi.url = Bidi.ltr +Bidi.filename = Bidi.nowrap -- aliased to Bidi._filename_rtl if _rtl_ui_text +Bidi.filepath = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text +Bidi.directory = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text +Bidi.dirpath = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text +Bidi.path = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text +Bidi.url = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text + +function Bidi._filename_rtl(filename) + -- We always want to show the extension either on the left + -- or on the right - never in the middle (which could happen + -- with the bidi algo if we give it the filename as-is). + local name, suffix = util.splitFileNameSuffix(filename) + -- Let the first strong character of the filename decides + -- about the direction + return Bidi.auto(name .. "." .. Bidi.ltr(suffix)) +end return Bidi diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index eeaced86e..b277bce8b 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -135,6 +135,7 @@ Widget that displays an item for menu --]] local MenuItem = InputContainer:new{ text = nil, + bidi_wrap_func = nil, show_parent = nil, detail = nil, font = "cfont", @@ -257,6 +258,12 @@ function MenuItem:init() -- get rid of any \n (which could be found in highlighted text in bookmarks). local text = self.text:gsub("\n", " ") + -- Wrap text with provided bidi_wrap_func (only provided by FileChooser, + -- to correctly display filenames and directories) + if self.bidi_wrap_func then + text = self.bidi_wrap_func(text) + end + if self.single_line then -- items only in single line -- No font size change: text will be truncated if it overflows item_name = TextWidget:new{ @@ -1002,6 +1009,7 @@ function Menu:updateItems(select_number) state = self.item_table[i].state, state_size = self.state_size or {}, text = Menu.getMenuText(self.item_table[i]), + bidi_wrap_func = self.item_table[i].bidi_wrap_func, mandatory = self.item_table[i].mandatory, bold = self.item_table.current == i or self.item_table[i].bold == true, dim = self.item_table[i].dim, @@ -1308,9 +1316,6 @@ function Menu.getMenuText(item) else text = item.text end - if item.bidi_wrap_func then - text = item.bidi_wrap_func(text) - end if item.sub_item_table ~= nil or item.sub_item_table_func then text = string.format(sub_item_format, text) end