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 Language = require("ui/language")
|
||||||
|
local util = require("util")
|
||||||
local _ = require("gettext")
|
local _ = require("gettext")
|
||||||
|
|
||||||
local Bidi = {
|
local Bidi = {
|
||||||
@@ -78,13 +79,25 @@ function Bidi.setup(lang)
|
|||||||
xtext.setDefaultLang(alt_lang)
|
xtext.setDefaultLang(alt_lang)
|
||||||
end
|
end
|
||||||
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
|
if Bidi._rtl_ui_text then
|
||||||
Bidi.default = Bidi.rtl
|
Bidi.default = Bidi.rtl
|
||||||
Bidi.wrap = 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
|
else
|
||||||
Bidi.default = Bidi.ltr
|
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
|
||||||
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)
|
return Bidi._rtl_ui_text and Bidi.rtl(text) or Bidi.ltr(text)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Bidi.noop(text) -- no wrap
|
function Bidi.nowrap(text)
|
||||||
return text
|
return text
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Helper for concatenated string bits of numbers an symbols (like
|
-- Helper for concatenated string bits of numbers an symbols (like
|
||||||
-- our reader footer) to keep them ordered in RTL UI (to not have
|
-- our reader footer) to keep them ordered in RTL UI (to not have
|
||||||
-- a letter B for battery make the whole string considered LTR).
|
-- 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
|
-- by Bibi.setup() as an optimisation
|
||||||
function Bidi.wrap(text)
|
function Bidi.wrap(text)
|
||||||
return Bidi._rtl_ui_text and Bidi.rtl(text) or text
|
return Bidi._rtl_ui_text and Bidi.rtl(text) or text
|
||||||
@@ -181,9 +194,21 @@ end
|
|||||||
-- shown as real RTL).
|
-- shown as real RTL).
|
||||||
-- Note: when the filename or path are standalone in a TextWidget, it's
|
-- Note: when the filename or path are standalone in a TextWidget, it's
|
||||||
-- better to use "para_direction_rtl = false" without any wrapping.
|
-- better to use "para_direction_rtl = false" without any wrapping.
|
||||||
Bidi.filename = Bidi.ltr
|
Bidi.filename = Bidi.nowrap -- aliased to Bidi._filename_rtl if _rtl_ui_text
|
||||||
Bidi.directory = Bidi.ltr
|
Bidi.filepath = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text
|
||||||
Bidi.path = Bidi.ltr
|
Bidi.directory = Bidi.nowrap -- aliased to Bidi.ltr if _rtl_ui_text
|
||||||
Bidi.url = Bidi.ltr
|
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
|
return Bidi
|
||||||
|
|||||||
@@ -135,6 +135,7 @@ Widget that displays an item for menu
|
|||||||
--]]
|
--]]
|
||||||
local MenuItem = InputContainer:new{
|
local MenuItem = InputContainer:new{
|
||||||
text = nil,
|
text = nil,
|
||||||
|
bidi_wrap_func = nil,
|
||||||
show_parent = nil,
|
show_parent = nil,
|
||||||
detail = nil,
|
detail = nil,
|
||||||
font = "cfont",
|
font = "cfont",
|
||||||
@@ -257,6 +258,12 @@ function MenuItem:init()
|
|||||||
-- get rid of any \n (which could be found in highlighted text in bookmarks).
|
-- get rid of any \n (which could be found in highlighted text in bookmarks).
|
||||||
local text = self.text:gsub("\n", " ")
|
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
|
if self.single_line then -- items only in single line
|
||||||
-- No font size change: text will be truncated if it overflows
|
-- No font size change: text will be truncated if it overflows
|
||||||
item_name = TextWidget:new{
|
item_name = TextWidget:new{
|
||||||
@@ -1002,6 +1009,7 @@ function Menu:updateItems(select_number)
|
|||||||
state = self.item_table[i].state,
|
state = self.item_table[i].state,
|
||||||
state_size = self.state_size or {},
|
state_size = self.state_size or {},
|
||||||
text = Menu.getMenuText(self.item_table[i]),
|
text = Menu.getMenuText(self.item_table[i]),
|
||||||
|
bidi_wrap_func = self.item_table[i].bidi_wrap_func,
|
||||||
mandatory = self.item_table[i].mandatory,
|
mandatory = self.item_table[i].mandatory,
|
||||||
bold = self.item_table.current == i or self.item_table[i].bold == true,
|
bold = self.item_table.current == i or self.item_table[i].bold == true,
|
||||||
dim = self.item_table[i].dim,
|
dim = self.item_table[i].dim,
|
||||||
@@ -1308,9 +1316,6 @@ function Menu.getMenuText(item)
|
|||||||
else
|
else
|
||||||
text = item.text
|
text = item.text
|
||||||
end
|
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
|
if item.sub_item_table ~= nil or item.sub_item_table_func then
|
||||||
text = string.format(sub_item_format, text)
|
text = string.format(sub_item_format, text)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user