2017-09-11 08:32:39 +00:00
|
|
|
local BBoxWidget = require("ui/widget/bboxwidget")
|
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2020-11-20 23:19:24 +00:00
|
|
|
local ButtonTable = require("ui/widget/buttontable")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Event = require("ui/event")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2013-10-23 14:37:55 +00:00
|
|
|
local Math = require("optmath")
|
2017-09-11 08:32:39 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
2017-09-11 08:32:39 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2022-04-14 06:59:36 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = Device.screen
|
2020-04-25 07:03:19 +00:00
|
|
|
local _ = require("gettext")
|
2013-02-03 04:29:30 +00:00
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local ReaderCropping = WidgetContainer:extend{}
|
2013-02-02 20:42:59 +00:00
|
|
|
|
|
|
|
function ReaderCropping:onPageCrop(mode)
|
2014-10-10 10:14:16 +00:00
|
|
|
self.ui:handleEvent(Event:new("CloseConfigMenu"))
|
2020-11-20 23:19:24 +00:00
|
|
|
|
2014-11-06 02:20:55 +00:00
|
|
|
-- backup original zoom mode as cropping use "page" zoom mode
|
|
|
|
self.orig_zoom_mode = self.view.zoom_mode
|
|
|
|
if mode == "auto" then
|
2021-03-30 16:47:52 +00:00
|
|
|
--- @fixme: This is weird. "auto" crop happens to be the default, yet the default zoom mode/genus is "page", not "content".
|
|
|
|
--- This effectively yields different results whether auto is enabled by default, or toggled at runtime...
|
2014-12-03 08:32:38 +00:00
|
|
|
if self.document.configurable.text_wrap ~= 1 then
|
|
|
|
self:setCropZoomMode(true)
|
|
|
|
end
|
2014-11-06 02:20:55 +00:00
|
|
|
return
|
2019-03-03 07:57:20 +00:00
|
|
|
elseif mode == "none" then
|
|
|
|
return
|
2014-11-06 02:20:55 +00:00
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
-- backup original view dimen
|
|
|
|
self.orig_view_dimen = Geom:new{w = self.view.dimen.w, h = self.view.dimen.h}
|
|
|
|
-- backup original view bgcolor
|
|
|
|
self.orig_view_bgcolor = self.view.outer_page_color
|
2019-03-14 19:58:45 +00:00
|
|
|
self.view.outer_page_color = Blitbuffer.COLOR_DARK_GRAY
|
2020-11-20 23:19:24 +00:00
|
|
|
-- backup original footer visibility
|
|
|
|
self.orig_view_footer_visibility = self.view.footer_visible
|
|
|
|
self.view.footer_visible = false
|
2014-03-13 13:52:43 +00:00
|
|
|
-- backup original page scroll
|
|
|
|
self.orig_page_scroll = self.view.page_scroll
|
|
|
|
self.view.page_scroll = false
|
|
|
|
-- backup and disable original hinting state
|
|
|
|
self.ui:handleEvent(Event:new("DisableHinting"))
|
|
|
|
-- backup original reflow mode as cropping use non-reflow mode
|
|
|
|
self.orig_reflow_mode = self.document.configurable.text_wrap
|
|
|
|
if self.orig_reflow_mode == 1 then
|
|
|
|
self.document.configurable.text_wrap = 0
|
|
|
|
-- if we are in reflow mode, then we are already in page
|
|
|
|
-- mode, just force readerview to recalculate visible_area
|
|
|
|
self.view:recalculate()
|
|
|
|
else
|
2020-11-28 16:18:57 +00:00
|
|
|
self.ui:handleEvent(Event:new("SetZoomMode", "page"))
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2020-11-20 23:19:24 +00:00
|
|
|
|
|
|
|
-- prepare bottom buttons so we know the size available for the page above it
|
|
|
|
local button_table = ButtonTable:new{
|
|
|
|
width = Screen:getWidth(),
|
|
|
|
buttons = {{
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
|
|
|
callback = function() self:onCancelPageCrop() end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Apply crop"),
|
|
|
|
callback = function() self:onConfirmPageCrop() end,
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
zero_sep = true,
|
|
|
|
show_parent = self,
|
|
|
|
}
|
|
|
|
local button_container = FrameContainer:new{
|
|
|
|
margin = 0,
|
|
|
|
bordersize = 0,
|
|
|
|
padding = 0,
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = button_table:getSize().h,
|
|
|
|
},
|
|
|
|
button_table,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
-- height available for page
|
2022-04-14 06:59:36 +00:00
|
|
|
local page_container_h = Screen:getHeight()
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
-- non-touch devices do not need cancel and apply buttons
|
|
|
|
page_container_h = page_container_h - button_table:getSize().h
|
|
|
|
end
|
2020-11-20 23:19:24 +00:00
|
|
|
local page_dimen = Geom:new{
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = page_container_h,
|
|
|
|
}
|
|
|
|
-- resize document view to the available size
|
|
|
|
self.ui:handleEvent(Event:new("SetDimensions", page_dimen))
|
|
|
|
|
|
|
|
-- finalize crop dialog
|
2014-03-13 13:52:43 +00:00
|
|
|
self.bbox_widget = BBoxWidget:new{
|
|
|
|
ui = self.ui,
|
|
|
|
view = self.view,
|
|
|
|
document = self.document,
|
|
|
|
}
|
2020-11-20 23:19:24 +00:00
|
|
|
self.crop_dialog = VerticalGroup:new{
|
|
|
|
align = "left",
|
2014-03-13 13:52:43 +00:00
|
|
|
self.bbox_widget,
|
2022-04-14 06:59:36 +00:00
|
|
|
(Device:isTouchDevice() and button_container) or nil, -- button bar only availble for touch devices
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
UIManager:show(self.crop_dialog)
|
|
|
|
return true
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
|
|
|
|
2013-02-23 05:10:53 +00:00
|
|
|
function ReaderCropping:onConfirmPageCrop()
|
2014-03-13 13:52:43 +00:00
|
|
|
--DEBUG("new bbox", new_bbox)
|
|
|
|
UIManager:close(self.crop_dialog)
|
|
|
|
local new_bbox = self.bbox_widget:getModifiedPageBBox()
|
|
|
|
self.ui:handleEvent(Event:new("BBoxUpdate", new_bbox))
|
|
|
|
local pageno = self.view.state.page
|
|
|
|
self.document.bbox[pageno] = new_bbox
|
|
|
|
self.document.bbox[Math.oddEven(pageno)] = new_bbox
|
|
|
|
self:exitPageCrop(true)
|
|
|
|
return true
|
2013-02-20 11:39:09 +00:00
|
|
|
end
|
|
|
|
|
2013-02-23 05:10:53 +00:00
|
|
|
function ReaderCropping:onCancelPageCrop()
|
2014-03-13 13:52:43 +00:00
|
|
|
UIManager:close(self.crop_dialog)
|
|
|
|
self:exitPageCrop(false)
|
|
|
|
return true
|
2013-02-20 11:39:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderCropping:exitPageCrop(confirmed)
|
2014-03-13 13:52:43 +00:00
|
|
|
-- restore hinting state
|
|
|
|
self.ui:handleEvent(Event:new("RestoreHinting"))
|
|
|
|
-- restore page scroll
|
|
|
|
self.view.page_scroll = self.orig_page_scroll
|
2020-11-20 23:19:24 +00:00
|
|
|
-- restore footer visibility
|
|
|
|
self.view.footer_visible = self.orig_view_footer_visibility
|
2014-03-13 13:52:43 +00:00
|
|
|
-- restore view bgcolor
|
|
|
|
self.view.outer_page_color = self.orig_view_bgcolor
|
|
|
|
-- restore reflow mode
|
|
|
|
self.document.configurable.text_wrap = self.orig_reflow_mode
|
|
|
|
-- restore view dimens
|
|
|
|
self.ui:handleEvent(Event:new("RestoreDimensions", self.orig_view_dimen))
|
|
|
|
self.view:recalculate()
|
|
|
|
-- Exiting should have the same look and feel with entering.
|
|
|
|
if self.orig_reflow_mode == 1 then
|
|
|
|
self.ui:handleEvent(Event:new("RestoreZoomMode"))
|
|
|
|
else
|
2014-11-06 02:20:55 +00:00
|
|
|
self:setCropZoomMode(confirmed)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
|
|
|
|
2014-11-06 02:20:55 +00:00
|
|
|
function ReaderCropping:setCropZoomMode(confirmed)
|
|
|
|
if confirmed then
|
2020-11-28 16:18:57 +00:00
|
|
|
-- if original zoom mode is "page???", set zoom mode to "content???"
|
|
|
|
local zoom_mode_type = self.orig_zoom_mode:match("page(.*)")
|
|
|
|
self:setZoomMode(zoom_mode_type
|
|
|
|
and "content"..zoom_mode_type
|
|
|
|
or self.orig_zoom_mode)
|
2014-11-06 02:20:55 +00:00
|
|
|
self.ui:handleEvent(Event:new("InitScrollPageStates"))
|
|
|
|
else
|
2016-04-26 02:45:55 +00:00
|
|
|
self:setZoomMode(self.orig_zoom_mode)
|
2014-11-06 02:20:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 02:45:55 +00:00
|
|
|
function ReaderCropping:setZoomMode(mode)
|
|
|
|
self.ui:handleEvent(Event:new("SetZoomMode", mode))
|
|
|
|
end
|
|
|
|
|
2013-02-02 20:42:59 +00:00
|
|
|
function ReaderCropping:onReadSettings(config)
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
if config:has("bbox") then
|
|
|
|
self.document.bbox = config:readSetting("bbox")
|
|
|
|
end
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
|
|
|
|
2013-12-27 15:18:16 +00:00
|
|
|
function ReaderCropping:onSaveSettings()
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ui.doc_settings:saveSetting("bbox", self.document.bbox)
|
2013-02-02 20:42:59 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return ReaderCropping
|