Skip to content

Commit

Permalink
[RTL UI] update plugins and apps to support mirroring
Browse files Browse the repository at this point in the history
  • Loading branch information
poire-z committed Dec 3, 2019
1 parent 6b4cb05 commit eee1adc
Show file tree
Hide file tree
Showing 53 changed files with 846 additions and 217 deletions.
7 changes: 5 additions & 2 deletions frontend/apps/filemanager/filemanager.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local Button = require("ui/widget/button")
local ButtonDialogTitle = require("ui/widget/buttondialogtitle")
Expand Down Expand Up @@ -109,6 +110,7 @@ function FileManager:init()
self.path_text = TextWidget:new{
face = Font:getFace("xx_smallinfofont"),
text = filemanagerutil.abbreviate(self.root_path),
para_direction_rtl = false, -- force LTR
max_width = Screen:getWidth() - 2*Size.padding.small,
truncate_left = true,
}
Expand Down Expand Up @@ -456,9 +458,10 @@ function FileManager:onShowPlusMenu()
end

function FileManager:onSwipeFM(ges)
if ges.direction == "west" then
local direction = BD.mirrorDirectionIfMirroredUILayout(ges.direction)
if direction == "west" then
self.file_chooser:onNextPage()
elseif ges.direction == "east" then
elseif direction == "east" then
self.file_chooser:onPrevPage()
end
return true
Expand Down
36 changes: 34 additions & 2 deletions frontend/apps/filemanager/filemanagermenu.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local CenterContainer = require("ui/widget/container/centercontainer")
local CloudStorage = require("apps/cloudstorage/cloudstorage")
local ConfirmBox = require("ui/widget/confirmbox")
Expand Down Expand Up @@ -399,6 +400,37 @@ function FileManagerMenu:setUpdateItemTable()
})
end,
})
table.insert(self.menu_items.developer_options.sub_item_table, {
text = "UI layout mirroring and text direction",
sub_item_table = {
{
text = _("Reverse language natural UI layout mirroring"),
checked_func = function()
return G_reader_settings:isTrue("reverse_lang_ui_layout_mirroring")
end,
callback = function()
G_reader_settings:flipNilOrFalse("reverse_lang_ui_layout_mirroring")
local InfoMessage = require("ui/widget/infomessage")
UIManager:show(InfoMessage:new{
text = _("This will take effect on next restart."),
})
end
},
{
text = _("Reverse language natural UI text direction"),
checked_func = function()
return G_reader_settings:isTrue("reverse_lang_ui_text_direction")
end,
callback = function()
G_reader_settings:flipNilOrFalse("reverse_lang_ui_text_direction")
local InfoMessage = require("ui/widget/infomessage")
UIManager:show(InfoMessage:new{
text = _("This will take effect on next restart."),
})
end
}
}
})

self.menu_items.cloud_storage = {
text = _("Cloud storage"),
Expand Down Expand Up @@ -580,10 +612,10 @@ function FileManagerMenu:_getTabIndexFromLocation(ges)
return last_tab_index
-- if the start position is far right
elseif ges.pos.x > 2 * Screen:getWidth() / 3 then
return #self.tab_item_table
return BD.mirroredUILayout() and 1 or #self.tab_item_table
-- if the start position is far left
elseif ges.pos.x < Screen:getWidth() / 3 then
return 1
return BD.mirroredUILayout() and #self.tab_item_table or 1
-- if center return the last index
else
return last_tab_index
Expand Down
3 changes: 2 additions & 1 deletion frontend/apps/reader/modules/readerbookmark.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local CenterContainer = require("ui/widget/container/centercontainer")
local ConfirmBox = require("ui/widget/confirmbox")
local Device = require("device")
Expand Down Expand Up @@ -231,7 +232,7 @@ function ReaderBookmark:onShowBookmark()
w = Screen:getWidth(),
h = Screen:getHeight(),
},
direction = "east"
direction = BD.mirrorDirectionIfMirroredUILayout("east")
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/apps/reader/modules/readerdogear.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local Device = require("device")
local Geom = require("ui/geometry")
local ImageWidget = require("ui/widget/imagewidget")
Expand Down Expand Up @@ -33,6 +34,7 @@ function ReaderDogear:setupDogear(new_dogear_size)
dimen = Geom:new{w = Screen:getWidth(), h = self.dogear_size},
ImageWidget:new{
file = "resources/icons/dogear.png",
rotation_angle = BD.mirroredUILayout() and 90 or 0,
width = self.dogear_size,
height = self.dogear_size,
}
Expand Down
52 changes: 38 additions & 14 deletions frontend/apps/reader/modules/readerfooter.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local BottomContainer = require("ui/widget/container/bottomcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
Expand All @@ -21,6 +22,7 @@ local WidgetContainer = require("ui/widget/container/widgetcontainer")
local util = require("util")
local T = require("ffi/util").template
local _ = require("gettext")
local C_ = _.pgettext
local Screen = Device.screen

local MODE = {
Expand All @@ -41,26 +43,45 @@ local symbol_prefix = {
letters = {
time = nil,
pages_left = "=>",
battery = "B:",
percentage = "R:",
book_time_to_read = "TB:",
chapter_time_to_read = "TC:",
frontlight = "L:",
mem_usage = "M:",
wifi_status = "W:",
battery = C_("FooterLetterPrefix", "B:"),
percentage = C_("FooterLetterPrefix", "R:"),
book_time_to_read = C_("FooterLetterPrefix", "TB:"),
chapter_time_to_read = C_("FooterLetterPrefix", "TC:"),
frontlight = C_("FooterLetterPrefix", "L:"),
mem_usage = C_("FooterLetterPrefix", "M:"),
wifi_status = C_("FooterLetterPrefix", "W:"),
},
icons = {
time = "",
pages_left = "",
pages_left = BD.mirroredUILayout() and "" or "",
battery = "",
percentage = "",
percentage = BD.mirroredUILayout() and "" or "",
book_time_to_read = "",
chapter_time_to_read = "",
chapter_time_to_read = BD.mirroredUILayout() and "" or "",
frontlight = "",
mem_usage = "",
wifi_status = "",
}
}
if BD.mirroredUILayout() then
-- We need to RTL-wrap these letters and symbols for proper layout
for k, v in pairs(symbol_prefix.letters) do
local colon = v:find(":")
local wrapped
if colon then
local pre = v:sub(1, colon-1)
local post = v:sub(colon)
wrapped = BD.wrap(pre) .. BD.wrap(post)
else
wrapped = BD.wrap(v)
end
symbol_prefix.letters[k] = wrapped
end
for k, v in pairs(symbol_prefix.icons) do
symbol_prefix.icons[k] = BD.wrap(v)
end
end

local PROGRESS_BAR_STYLE_THICK_DEFAULT_HEIGHT = 7
local PROGRESS_BAR_STYLE_THIN_DEFAULT_HEIGHT = 3

Expand Down Expand Up @@ -115,9 +136,9 @@ local footerTextGeneratorMap = {
prefix = ""
end
end
return prefix .. batt_lvl .. "%"
return BD.wrap(prefix) .. batt_lvl .. "%"
else
return prefix .. " " .. (powerd:isCharging() and "+" or "") .. batt_lvl .. "%"
return BD.wrap(prefix) .. " " .. (powerd:isCharging() and "+" or "") .. batt_lvl .. "%"
end
end,
time = function(footer)
Expand Down Expand Up @@ -1230,10 +1251,13 @@ function ReaderFooter:genAllFooterText()
elseif self.settings.items_separator == "bullet" then
separator = ""
end
-- We need to BD.wrap() all items and separators, so we're
-- sure they are laid out in our order (reversed in RTL),
-- without ordering by the RTL Bidi algorithm.
for _, gen in ipairs(self.footerTextGenerators) do
table.insert(info, gen(self))
table.insert(info, BD.wrap(gen(self)))
end
return table.concat(info, separator)
return table.concat(info, BD.wrap(separator))
end

function ReaderFooter:setTocMarkers(reset)
Expand Down
45 changes: 44 additions & 1 deletion frontend/apps/reader/modules/readergesture.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local ConfirmBox = require("ui/widget/confirmbox")
local DataStorage = require("datastorage")
local Device = require("device")
Expand Down Expand Up @@ -225,6 +226,44 @@ function ReaderGesture:init()
}
local gm = G_reader_settings:readSetting(self.ges_mode)
if gm == nil then G_reader_settings:saveSetting(self.ges_mode, {}) end

-- Some of these defaults need to be reversed in RTL mirrored UI,
-- and as we set them in the saved gestures, we need to reset them
-- to the defaults in case of UI language's direction change.
local mirrored_if_rtl = {
tap_top_left_corner = "tap_top_right_corner",
tap_right_bottom_corner = "tap_left_bottom_corner",
}
local is_rtl = BD.mirroredUILayout()
if is_rtl then
for k, v in pairs(mirrored_if_rtl) do
self.default_gesture[k], self.default_gesture[v] = self.default_gesture[v], self.default_gesture[k]
end
end
-- We remember the last UI direction gestures were made on. If it changes,
-- reset the mirrored_if_rtl ones to the default for the new direction.
local ges_dir_setting = self.ges_mode.."ui_lang_direction_rtl"
local prev_lang_dir_rtl = G_reader_settings:isTrue(ges_dir_setting)
if (is_rtl and not prev_lang_dir_rtl) or (not is_rtl and prev_lang_dir_rtl) then
local reset = false
for k, v in pairs(mirrored_if_rtl) do
-- We only replace them if they are still the other direction's default.
-- If not, the user has changed them: let him deal with setting new ones if needed.
if gm[k] == self.default_gesture[v] then
gm[k] = self.default_gesture[k]
reset = true
end
if gm[v] == self.default_gesture[k] then
gm[v] = self.default_gesture[v]
reset = true
end
end
if reset then
logger.info("UI language direction changed: resetting some gestures to direction default")
end
G_reader_settings:flipNilOrFalse(ges_dir_setting)
end

self.ui.menu:registerToMainMenu(self)
self:initGesture()
end
Expand Down Expand Up @@ -1577,8 +1616,12 @@ function ReaderGesture:onToggleReadingOrder()
local document_module = self.ui.document.info.has_pages and self.ui.paging or self.ui.rolling
document_module.inverse_reading_order = not document_module.inverse_reading_order
document_module:setupTouchZones()
local is_rtl = BD.mirroredUILayout()
if document_module.inverse_reading_order then
is_rtl = not is_rtl
end
UIManager:show(Notification:new{
text = document_module.inverse_reading_order and _("RTL page turning.") or _("LTR page turning."),
text = is_rtl and _("RTL page turning.") or _("LTR page turning."),
timeout = 2.5,
})
return true
Expand Down
33 changes: 29 additions & 4 deletions frontend/apps/reader/modules/readerhighlight.lua
@@ -1,3 +1,4 @@
local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local Device = require("device")
local Event = require("ui/event")
Expand Down Expand Up @@ -350,9 +351,19 @@ function ReaderHighlight:onShowHighlightDialog(page, index)
}

if not self.ui.document.info.has_pages then
local start_prev = "◁⇱"
local start_next = "⇱▷"
local end_prev = "◁⇲"
local end_next = "⇲▷"
if BD.mirroredUILayout() then
-- Sadly, there's only north west & south east arrow to corner,
-- north east and south west do not exist in Unicode.
start_prev, start_next = BD.ltr(start_next), BD.ltr(start_prev)
end_prev, end_next = BD.ltr(end_next), BD.ltr(end_prev)
end
table.insert(buttons, {
{
text = "◁⇱",
text = start_prev,
callback = function()
self:updateHighlight(page, index, 0, -1, false)
end,
Expand All @@ -362,7 +373,7 @@ function ReaderHighlight:onShowHighlightDialog(page, index)
end
},
{
text = "⇱▷",
text = start_next,
callback = function()
self:updateHighlight(page, index, 0, 1, false)
end,
Expand All @@ -372,7 +383,7 @@ function ReaderHighlight:onShowHighlightDialog(page, index)
end
},
{
text = "◁⇲",
text = end_prev,
callback = function()
self:updateHighlight(page, index, 1, -1, false)
end,
Expand All @@ -381,7 +392,7 @@ function ReaderHighlight:onShowHighlightDialog(page, index)
end
},
{
text = "⇲▷",
text = end_next,
callback = function()
self:updateHighlight(page, index, 1, 1, false)
end,
Expand Down Expand Up @@ -577,6 +588,14 @@ function ReaderHighlight:onHoldPan(_, ges)
and self.holdpan_pos.x < 1/8*Screen:getWidth()
local is_in_bottom_right_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
and self.holdpan_pos.x > 7/8*Screen:getWidth()
if BD.mirroredUILayout() then
-- Note: this might not be really usable, as crengine native selection
-- is not adapted to RTL text
is_in_top_left_corner = self.holdpan_pos.y < 1/8*Screen:getHeight()
and self.holdpan_pos.x > 7/8*Screen:getWidth()
is_in_bottom_right_corner = self.holdpan_pos.y > 7/8*Screen:getHeight()
and self.holdpan_pos.x < 1/8*Screen:getWidth()
end
if is_in_top_left_corner or is_in_bottom_right_corner then
if self.was_in_some_corner then
-- Do nothing, wait for the user to move his finger out of that corner
Expand Down Expand Up @@ -781,6 +800,8 @@ function ReaderHighlight:viewSelectionHTML(debug_view)
text = css_text or _("Failed getting CSS content"),
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
buttons_table = {
{{
text = _("Prettify"),
Expand All @@ -792,6 +813,8 @@ function ReaderHighlight:viewSelectionHTML(debug_view)
text = prettifyCss(css_text),
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
})
end,
}},
Expand Down Expand Up @@ -838,6 +861,8 @@ function ReaderHighlight:viewSelectionHTML(debug_view)
text = html,
text_face = Font:getFace("smallinfont"),
justified = false,
para_direction_rtl = false,
auto_para_direction = false,
buttons_table = buttons_table,
}
UIManager:show(textviewer)
Expand Down

0 comments on commit eee1adc

Please sign in to comment.