2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2018-07-28 18:18:31 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
2021-09-25 08:21:18 +00:00
|
|
|
local MultiConfirmBox = require("ui/widget/multiconfirmbox")
|
2015-10-03 06:18:47 +00:00
|
|
|
local DataStorage = require("datastorage")
|
2018-07-28 18:18:31 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2015-10-03 06:18:47 +00:00
|
|
|
local Screen = require("device").screen
|
2014-11-28 13:10:37 +00:00
|
|
|
local T = require("ffi/util").template
|
|
|
|
local _ = require("gettext")
|
2013-03-06 14:36:51 +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 Screenshoter = InputContainer:extend{
|
2016-01-03 08:47:01 +00:00
|
|
|
prefix = 'Screenshot',
|
2014-11-05 08:58:09 +00:00
|
|
|
}
|
2013-03-06 14:36:51 +00:00
|
|
|
|
2016-01-03 08:47:01 +00:00
|
|
|
function Screenshoter:init()
|
2022-09-21 21:26:22 +00:00
|
|
|
local diagonal = math.sqrt(Screen:getWidth()^2 + Screen:getHeight()^2)
|
2014-03-13 13:52:43 +00:00
|
|
|
self.ges_events = {
|
2014-06-17 13:12:42 +00:00
|
|
|
TapDiagonal = {
|
2014-03-13 13:52:43 +00:00
|
|
|
GestureRange:new{
|
|
|
|
ges = "two_finger_tap",
|
2014-11-20 22:07:39 +00:00
|
|
|
scale = {diagonal - Screen:scaleBySize(200), diagonal},
|
2014-03-13 13:52:43 +00:00
|
|
|
rate = 1.0,
|
|
|
|
}
|
|
|
|
},
|
2014-06-17 13:12:42 +00:00
|
|
|
SwipeDiagonal = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "swipe",
|
2014-11-20 22:07:39 +00:00
|
|
|
scale = {diagonal - Screen:scaleBySize(200), diagonal},
|
2014-06-17 13:12:42 +00:00
|
|
|
rate = 1.0,
|
|
|
|
}
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2013-03-06 14:36:51 +00:00
|
|
|
end
|
|
|
|
|
2021-08-14 15:52:29 +00:00
|
|
|
function Screenshoter:onScreenshot(filename, when_done_func)
|
2019-09-17 11:01:40 +00:00
|
|
|
local screenshots_dir = G_reader_settings:readSetting("screenshot_dir") or DataStorage:getDataDir() .. "/screenshots/"
|
2020-04-22 07:07:48 +00:00
|
|
|
self.screenshot_fn_fmt = screenshots_dir .. self.prefix .. "_%Y-%m-%d_%H%M%S.png"
|
2016-01-03 08:47:01 +00:00
|
|
|
local screenshot_name = filename or os.date(self.screenshot_fn_fmt)
|
2018-08-19 18:21:03 +00:00
|
|
|
Screen:shot(screenshot_name)
|
2021-08-14 15:52:29 +00:00
|
|
|
local confirm_box
|
|
|
|
confirm_box = ConfirmBox:new{
|
|
|
|
text = T( _("Screenshot saved to:\n%1"), BD.filepath(screenshot_name)),
|
|
|
|
keep_dialog_open = true,
|
2022-01-10 18:14:47 +00:00
|
|
|
flush_events_on_show = true, -- may be invoked with 2-fingers tap, accidental additional events can happen
|
2021-08-14 15:52:29 +00:00
|
|
|
cancel_text = _("Close"),
|
|
|
|
cancel_callback = function()
|
|
|
|
if when_done_func then when_done_func() end
|
|
|
|
end,
|
|
|
|
ok_text = _("Set as screensaver"),
|
2018-07-28 18:18:31 +00:00
|
|
|
ok_callback = function()
|
|
|
|
G_reader_settings:saveSetting("screensaver_type", "image_file")
|
|
|
|
G_reader_settings:saveSetting("screensaver_image", screenshot_name)
|
2021-08-14 15:52:29 +00:00
|
|
|
UIManager:close(confirm_box)
|
|
|
|
if when_done_func then when_done_func() end
|
2018-07-28 18:18:31 +00:00
|
|
|
end,
|
2021-08-14 15:52:29 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}},
|
2018-07-28 18:18:31 +00:00
|
|
|
}
|
2021-08-14 15:52:29 +00:00
|
|
|
UIManager:show(confirm_box)
|
2014-11-30 00:12:00 +00:00
|
|
|
-- trigger full refresh
|
|
|
|
UIManager:setDirty(nil, "full")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2013-03-06 14:36:51 +00:00
|
|
|
end
|
|
|
|
|
2019-09-17 11:01:40 +00:00
|
|
|
function Screenshoter:chooseFolder()
|
2021-09-25 08:21:18 +00:00
|
|
|
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{
|
2021-08-14 15:52:29 +00:00
|
|
|
text = T(_("Screenshot folder is set to:\n%1\n\nChoose a new folder for screenshots?"), screenshot_dir),
|
2021-09-25 08:21:18 +00:00
|
|
|
choice1_text = _("Use default"),
|
|
|
|
choice1_callback = function()
|
|
|
|
G_reader_settings:saveSetting("screenshot_dir", screenshot_dir_default)
|
|
|
|
end,
|
|
|
|
choice2_text = _("Choose folder"),
|
|
|
|
choice2_callback = function()
|
2021-08-14 15:52:29 +00:00
|
|
|
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,
|
2019-09-17 11:01:40 +00:00
|
|
|
}
|
2021-08-14 15:52:29 +00:00
|
|
|
UIManager:show(confirm_box)
|
2019-09-17 11:01:40 +00:00
|
|
|
end
|
|
|
|
|
2016-01-03 08:47:01 +00:00
|
|
|
function Screenshoter:onTapDiagonal()
|
2014-06-17 13:12:42 +00:00
|
|
|
return self:onScreenshot()
|
|
|
|
end
|
|
|
|
|
2016-01-03 08:47:01 +00:00
|
|
|
function Screenshoter:onSwipeDiagonal()
|
2014-06-17 13:12:42 +00:00
|
|
|
return self:onScreenshot()
|
|
|
|
end
|
|
|
|
|
2016-01-03 08:47:01 +00:00
|
|
|
return Screenshoter
|