Skip to content

Commit

Permalink
Text editor: rotate (koreader#12658)
Browse files Browse the repository at this point in the history
  • Loading branch information
hius07 authored Oct 29, 2024
1 parent b42b9a8 commit 97a7ebe
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 153 deletions.
40 changes: 21 additions & 19 deletions frontend/apps/filemanager/filemanager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,21 @@ local function isFile(file)
return lfs.attributes(file, "mode") == "file"
end

function FileManager:onSetRotationMode(rotation)
if rotation ~= nil and rotation ~= Screen:getRotationMode() then
Screen:setRotationMode(rotation)
if FileManager.instance then
self:reinit(self.path, self.focused_file)
end
end
return true
end

function FileManager:onPhysicalKeyboardConnected()
-- So that the key navigation shortcuts apply right away.
-- This will also naturally call registerKeyEvents
self:reinit(self.path, self.focused_file)
end
FileManager.onPhysicalKeyboardDisconnected = FileManager.onPhysicalKeyboardConnected

function FileManager:setRotationMode()
local locked = G_reader_settings:isTrue("lock_rotation")
if not locked then
local rotation_mode = G_reader_settings:readSetting("fm_rotation_mode") or Screen.DEVICE_ROTATED_UPRIGHT
self:onSetRotationMode(rotation_mode)
local mode = G_reader_settings:readSetting("fm_rotation_mode") or Screen.DEVICE_ROTATED_UPRIGHT
self:onSetRotationMode(mode)
end
end

function FileManager:onSetRotationMode(mode)
local old_mode = Screen:getRotationMode()
if mode ~= nil and mode ~= old_mode then
Screen:setRotationMode(mode)
if FileManager.instance then
self:rotate()
end
end
end

Expand Down Expand Up @@ -728,6 +721,8 @@ function FileManager:tapPlus()
end

function FileManager:reinit(path, focused_file)
path = path or self.path
focused_file = focused_file or self.focused_file
UIManager:flushSettings()
self.dimen = Screen:getSize()
-- backup the root path and path items
Expand All @@ -748,6 +743,13 @@ function FileManager:reinit(path, focused_file)
-- self:onRefresh()
end

FileManager.rotate = FileManager.reinit

-- So that the key navigation shortcuts apply right away.
-- This will also naturally call registerKeyEvents
FileManager.onPhysicalKeyboardConnected = FileManager.reinit
FileManager.onPhysicalKeyboardDisconnected = FileManager.reinit

function FileManager:getCurrentDir()
return FileManager.instance and FileManager.instance.file_chooser.path
end
Expand Down
4 changes: 2 additions & 2 deletions frontend/apps/reader/modules/readerfooter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2454,11 +2454,11 @@ function ReaderFooter:onSwapPageTurnButtons()
end
ReaderFooter.onToggleReadingOrder = ReaderFooter.onSwapPageTurnButtons

function ReaderFooter:onSetRotationMode()
function ReaderFooter:onSetDimensions()
self:updateFooterContainer()
self:resetLayout(true)
end
ReaderFooter.onScreenResize = ReaderFooter.onSetRotationMode
ReaderFooter.onScreenResize = ReaderFooter.onSetDimensions

function ReaderFooter:onSetPageHorizMargins(h_margins)
if self.settings.progress_margin then
Expand Down
47 changes: 0 additions & 47 deletions frontend/apps/reader/modules/readerrotation.lua

This file was deleted.

46 changes: 20 additions & 26 deletions frontend/apps/reader/modules/readerview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -836,40 +836,34 @@ function ReaderView:restoreViewContext(ctx)
return false
end

function ReaderView:onSetRotationMode(rotation)
if rotation ~= nil then
local old_rotation = Screen:getRotationMode()
if rotation == old_rotation then
return
end
function ReaderView:onSetRotationMode(mode)
local old_mode = Screen:getRotationMode()
if mode ~= nil and mode ~= old_mode then
Screen:setRotationMode(mode)
self:rotate(mode, old_mode)
end
end

function ReaderView:rotate(mode, old_mode)
-- NOTE: We cannot rely on getScreenMode, as it actually checks the screen dimensions, instead of the rotation mode.
-- (i.e., it returns how the screen *looks* like, not how it's oriented relative to its native layout).
-- This would horribly break if you started in Portrait (both rotation and visually),
-- then resized your window to a Landscape layout *without* changing the rotation.
-- If you then attempted to switch to a Landscape *rotation*, it would mistakenly think the layout hadn't changed!
-- So, instead, as we're concerned with *rotation* layouts, just compare the two.
-- We use LinuxFB-style constants, so, Portraits are even, Landscapes are odds, making this trivial.
local matching_orientation = bit.band(rotation, 1) == bit.band(old_rotation, 1)

if rotation ~= old_rotation and matching_orientation then
-- No layout change, just rotate & repaint with a flash
Screen:setRotationMode(rotation)
UIManager:setDirty(self.dialog, "full")
Notification:notify(T(_("Rotation mode set to: %1"), optionsutil:getOptionText("SetRotationMode", rotation)))
return
end

Screen:setRotationMode(rotation)
local matching_orientation = bit.band(mode, 1) == bit.band(old_mode, 1)
if matching_orientation then
-- No layout change, just rotate & repaint with a flash
UIManager:setDirty(self.dialog, "full")
else
UIManager:setDirty(nil, "full") -- SetDimensions will only request a partial, we want a flash
local new_screen_size = Screen:getSize()
self.ui:handleEvent(Event:new("SetDimensions", new_screen_size))
self.ui:onScreenResize(new_screen_size)
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end

UIManager:setDirty(nil, "full") -- SetDimensions will only request a partial, we want a flash
local new_screen_size = Screen:getSize()
self.ui:handleEvent(Event:new("SetDimensions", new_screen_size))
self.ui:onScreenResize(new_screen_size)
self.ui:handleEvent(Event:new("InitScrollPageStates"))
Notification:notify(T(_("Rotation mode set to: %1"), optionsutil:getOptionText("SetRotationMode", rotation)))
return
Notification:notify(T(_("Rotation mode set to: %1"), optionsutil:getOptionText("SetRotationMode", mode)))
end

function ReaderView:onSetDimensions(dimensions)
Expand Down Expand Up @@ -975,7 +969,7 @@ function ReaderView:onBBoxUpdate(bbox)
self.use_bbox = bbox and true or false
end

--- @note: From ReaderRotation, which is broken and disabled.
--- @note: From ReaderRotation, which was broken, and has been removed in #12658
function ReaderView:onRotationUpdate(rotation)
self.state.rotation = rotation
self:recalculate()
Expand Down
2 changes: 1 addition & 1 deletion frontend/apps/reader/modules/readerzooming.lua
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ function ReaderZooming:onRestoreDimensions(dimensions)
self:setZoom()
end

--- @note: From ReaderRotation, which is broken and disabled.
--- @note: From ReaderRotation, which was broken, and has been removed in #12658
function ReaderZooming:onRotationUpdate(rotation)
self.rotation = rotation
self:setZoom()
Expand Down
10 changes: 0 additions & 10 deletions frontend/apps/reader/readerui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ local ReaderLink = require("apps/reader/modules/readerlink")
local ReaderMenu = require("apps/reader/modules/readermenu")
local ReaderPageMap = require("apps/reader/modules/readerpagemap")
local ReaderPanning = require("apps/reader/modules/readerpanning")
--local ReaderRotation = require("apps/reader/modules/readerrotation")
local ReaderPaging = require("apps/reader/modules/readerpaging")
local ReaderRolling = require("apps/reader/modules/readerrolling")
local ReaderSearch = require("apps/reader/modules/readersearch")
Expand Down Expand Up @@ -159,15 +158,6 @@ function ReaderUI:init()
view = self.view,
ui = self
})
-- (legacy, and defunct) rotation controller
--- @fixme: Tripping this would break rendering, c.f., `Document:renderPage`
--[[
self:registerModule("rotation", ReaderRotation:new{
dialog = self.dialog,
view = self.view,
ui = self
})
--]]
-- Handmade/custom ToC and hidden flows
self:registerModule("handmade", ReaderHandMade:new{
dialog = self.dialog,
Expand Down
6 changes: 3 additions & 3 deletions frontend/document/document.lua
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ function Document:renderPage(pageno, rect, zoom, rotation, gamma, hinting)
-- Make the context match the rotation,
-- by pointing at the rotated origin via coordinates offsets.
-- NOTE: We rotate our *Screen* bb on rotation (SetRotationMode), not the document,
-- so we hardly ever exercise this codepath...
-- AFAICT, the only thing that will *ever* (attempt to) rotate the document is ReaderRotation's key bindings (RotationUpdate).
--- @fixme: And whaddayano, it's broken ;). The aptly named key binds in question are J/K, I shit you not.
-- so we hardly ever exercize this codepath...
-- AFAICT, the only thing that *ever* (attempted to) rotate the document was ReaderRotation's key bindings (RotationUpdate).
--- @note: It was broken as all hell (it had likely never worked outside of its original implementation in KPV), and has been removed in #12658
if rotation == 90 then
dc:setOffset(page_size.w, 0)
elseif rotation == 180 then
Expand Down
61 changes: 37 additions & 24 deletions frontend/ui/widget/inputdialog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ function InputDialog:init()
title_multilines = true,
bottom_v_padding = self.bottom_v_padding,
info_text = self.description,
left_icon = self.title_bar_left_icon,
left_icon_tap_callback = self.title_bar_left_icon_tap_callback,
show_parent = self,
}

Expand Down Expand Up @@ -475,6 +477,31 @@ function InputDialog:init()
end
end

function InputDialog:reinit()
local visible = self:isKeyboardVisible()
self.input = self:getInputText() -- re-init with up-to-date text
self:onClose() -- will close keyboard and save view position
self._input_widget:onCloseWidget() -- proper cleanup of InputText and its keyboard
if self._added_widgets then
-- prevent these externally added widgets from being freed as :init() will re-add them
for i = 1, #self._added_widgets do
table.remove(self.vgroup, #self.vgroup-2)
end
end
self:free()
-- Restore original text_height (or reset it if none to force recomputing it)
self.text_height = self.orig_text_height or nil

-- Same deal as in toggleKeyboard...
self.keyboard_visible = visible and true or false
self:init()
if self.keyboard_visible then
self:onShowKeyboard()
end
-- Our position on screen has probably changed, so have the full screen refreshed
UIManager:setDirty("all", "flashui")
end

function InputDialog:addWidget(widget, re_init)
table.insert(self.layout, #self.layout, {widget})
if not re_init then -- backup widget for re-init
Expand Down Expand Up @@ -677,30 +704,7 @@ function InputDialog:onKeyboardClosed()
end
end

function InputDialog:onKeyboardHeightChanged()
local visible = self:isKeyboardVisible()
self.input = self:getInputText() -- re-init with up-to-date text
self:onClose() -- will close keyboard and save view position
self._input_widget:onCloseWidget() -- proper cleanup of InputText and its keyboard
if self._added_widgets then
-- prevent these externally added widgets from being freed as :init() will re-add them
for i = 1, #self._added_widgets do
table.remove(self.vgroup, #self.vgroup-2)
end
end
self:free()
-- Restore original text_height (or reset it if none to force recomputing it)
self.text_height = self.orig_text_height or nil

-- Same deal as in toggleKeyboard...
self.keyboard_visible = visible
self:init()
if self.keyboard_visible then
self:onShowKeyboard()
end
-- Our position on screen has probably changed, so have the full screen refreshed
UIManager:setDirty("all", "flashui")
end
InputDialog.onKeyboardHeightChanged = InputDialog.reinit

function InputDialog:onCloseDialog()
local close_button = self.button_table:getButtonById("close")
Expand All @@ -724,6 +728,15 @@ function InputDialog:onClose()
self:onCloseKeyboard()
end

function InputDialog:onSetRotationMode(mode)
if self.rotation_enabled and mode ~= nil then -- Text editor only
self.rotation_mode_backup = self.rotation_mode_backup or Screen:getRotationMode() -- backup only initial mode
Screen:setRotationMode(mode)
self:reinit()
return true -- we are the upper widget, stop event propagation
end
end

function InputDialog:refreshButtons()
-- Using what ought to be enough:
-- return "ui", self.button_table.dimen
Expand Down
Loading

0 comments on commit 97a7ebe

Please sign in to comment.