mirror of
https://github.com/koreader/koreader.git
synced 2025-12-13 20:36:53 +01:00
[RTL UI] Bidi wrapping tweaks
- Alias everything to Bidi.nowrap() when in LTR UI, as using LTR isolates seems uneeded when already LTR. - Better wrapping of RTL filename by using auto direction and LTR-isolating only the suffix so it's always on a side. - menu.lua: handle bidi_wrap_func outside getMenuText(), which may be used in other contexts. - Add BD.filepath() and BD.dirpath()
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user