You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
koreader/frontend/ui/widget/screenshoter.lua

129 lines
4.3 KiB
Lua

local BD = require("ui/bidi")
local ButtonDialog = require("ui/widget/buttondialog")
local DataStorage = require("datastorage")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
local UIManager = require("ui/uimanager")
local filemanagerutil = require("apps/filemanager/filemanagerutil")
local Screen = require("device").screen
local _ = require("gettext")
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.
2 years ago
local Screenshoter = InputContainer:extend{
prefix = "Screenshot",
default_dir = DataStorage:getFullDataDir() .. "/screenshots",
}
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:getScreenshotDir()
local screenshot_dir = G_reader_settings:readSetting("screenshot_dir")
return screenshot_dir and screenshot_dir:gsub("/$", "") or self.default_dir
end
function Screenshoter:onScreenshot(screenshot_name, caller_callback)
if not screenshot_name then
screenshot_name = os.date(self:getScreenshotDir() .. "/" .. self.prefix .. "_%Y-%m-%d_%H%M%S.png")
end
Screen:shot(screenshot_name)
local file = self.ui and self.ui.document and self.ui.document.file -- currently opened book
local dialog
local buttons = {
{
{
text = _("Delete"),
callback = function()
os.remove(screenshot_name)
dialog:onClose()
end,
},
{
text = _("Set as book cover"),
enabled = file and true or false,
callback = function()
self.ui.bookinfo:setCustomCoverFromImage(file, screenshot_name)
os.remove(screenshot_name)
dialog:onClose()
end,
},
},
{
{
text = _("View"),
callback = function()
local ImageViewer = require("ui/widget/imageviewer")
local image_viewer = ImageViewer:new{
file = screenshot_name,
modal = true,
with_title_bar = false,
buttons_visible = true,
}
UIManager:show(image_viewer)
end,
},
{
text = _("Set as screensaver"),
callback = function()
G_reader_settings:saveSetting("screensaver_type", "image_file")
G_reader_settings:saveSetting("screensaver_image", screenshot_name)
dialog:onClose()
end,
},
},
}
dialog = ButtonDialog:new{
title = _("Screenshot saved to:") .. "\n\n" .. BD.filepath(screenshot_name) .. "\n",
buttons = buttons,
tap_close_callback = function()
if caller_callback then
caller_callback()
end
local current_path = self.ui and self.ui.file_chooser and self.ui.file_chooser.path
if current_path and current_path .. "/" == screenshot_name:match(".*/") then
self.ui.file_chooser:refreshPath()
end
end,
}
UIManager:show(dialog)
-- trigger full refresh
UIManager:setDirty(nil, "full")
return true
end
function Screenshoter:chooseFolder()
local title_header = _("Current screenshot folder:")
local current_path = G_reader_settings:readSetting("screenshot_dir")
local default_path = self.default_dir
local caller_callback = function(path)
G_reader_settings:saveSetting("screenshot_dir", path)
end
filemanagerutil.showChooseDialog(title_header, caller_callback, current_path, default_path)
end
function Screenshoter:onTapDiagonal()
return self:onScreenshot()
end
function Screenshoter:onSwipeDiagonal()
return self:onScreenshot()
end
return Screenshoter