2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/screenshoter.lua
NiLuJe fadee1f5dc
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 02:14:48 +02:00

121 lines
4.3 KiB
Lua

local BD = require("ui/bidi")
local ConfirmBox = require("ui/widget/confirmbox")
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
local DataStorage = require("datastorage")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
local UIManager = require("ui/uimanager")
local Screen = require("device").screen
local T = require("ffi/util").template
local _ = require("gettext")
local Screenshoter = InputContainer:extend{
prefix = 'Screenshot',
}
function Screenshoter:init()
local diagonal = math.sqrt(Screen:getWidth()^2 + Screen:getHeight()^2)
self.ges_events = {
TapDiagonal = {
GestureRange:new{
ges = "two_finger_tap",
scale = {diagonal - Screen:scaleBySize(200), diagonal},
rate = 1.0,
}
},
SwipeDiagonal = {
GestureRange:new{
ges = "swipe",
scale = {diagonal - Screen:scaleBySize(200), diagonal},
rate = 1.0,
}
},
}
end
function Screenshoter:onScreenshot(filename, when_done_func)
local screenshots_dir = G_reader_settings:readSetting("screenshot_dir") or DataStorage:getDataDir() .. "/screenshots/"
self.screenshot_fn_fmt = screenshots_dir .. self.prefix .. "_%Y-%m-%d_%H%M%S.png"
local screenshot_name = filename or os.date(self.screenshot_fn_fmt)
Screen:shot(screenshot_name)
local confirm_box
confirm_box = ConfirmBox:new{
text = T( _("Screenshot saved to:\n%1"), BD.filepath(screenshot_name)),
keep_dialog_open = true,
flush_events_on_show = true, -- may be invoked with 2-fingers tap, accidental additional events can happen
cancel_text = _("Close"),
cancel_callback = function()
if when_done_func then when_done_func() end
end,
ok_text = _("Set as screensaver"),
ok_callback = function()
G_reader_settings:saveSetting("screensaver_type", "image_file")
G_reader_settings:saveSetting("screensaver_image", screenshot_name)
UIManager:close(confirm_box)
if when_done_func then when_done_func() end
end,
other_buttons_first = true,
other_buttons = {{
{
text = _("Delete"),
callback = function()
local __ = os.remove(screenshot_name)
UIManager:close(confirm_box)
if when_done_func then when_done_func() end
end,
},
{
text = _("View"),
callback = function()
local image_viewer = require("ui/widget/imageviewer"):new{
file = screenshot_name,
modal = true,
with_title_bar = false,
buttons_visible = true,
}
UIManager:show(image_viewer)
end,
},
}},
}
UIManager:show(confirm_box)
-- trigger full refresh
UIManager:setDirty(nil, "full")
return true
end
function Screenshoter:chooseFolder()
local screenshot_dir_default = DataStorage:getFullDataDir() .. "/screenshots/"
local screenshot_dir = G_reader_settings:readSetting("screenshot_dir") or screenshot_dir_default
local confirm_box = MultiConfirmBox:new{
text = T(_("Screenshot folder is set to:\n%1\n\nChoose a new folder for screenshots?"), screenshot_dir),
choice1_text = _("Use default"),
choice1_callback = function()
G_reader_settings:saveSetting("screenshot_dir", screenshot_dir_default)
end,
choice2_text = _("Choose folder"),
choice2_callback = function()
local path_chooser = require("ui/widget/pathchooser"):new{
select_file = false,
show_files = false,
path = screenshot_dir,
onConfirm = function(new_path)
G_reader_settings:saveSetting("screenshot_dir", new_path .. "/")
end
}
UIManager:show(path_chooser)
end,
}
UIManager:show(confirm_box)
end
function Screenshoter:onTapDiagonal()
return self:onScreenshot()
end
function Screenshoter:onSwipeDiagonal()
return self:onScreenshot()
end
return Screenshoter