2020-01-04 00:18:51 +00:00
local BD = require ( " ui/bidi " )
2017-12-17 17:27:24 +00:00
local Blitbuffer = require ( " ffi/blitbuffer " )
local ButtonDialogTitle = require ( " ui/widget/buttondialogtitle " )
local BookStatusWidget = require ( " ui/widget/bookstatuswidget " )
2020-02-22 08:07:28 +00:00
local BottomContainer = require ( " ui/widget/container/bottomcontainer " )
2018-07-23 08:37:24 +00:00
local DataStorage = require ( " datastorage " )
2016-04-18 05:30:46 +00:00
local Device = require ( " device " )
2014-09-17 10:24:33 +00:00
local DocSettings = require ( " docsettings " )
2017-09-05 07:01:46 +00:00
local DocumentRegistry = require ( " document/documentregistry " )
2020-02-22 08:07:28 +00:00
local Font = require ( " ui/font " )
local Geom = require ( " ui/geometry " )
2017-12-17 17:27:24 +00:00
local InfoMessage = require ( " ui/widget/infomessage " )
local ImageWidget = require ( " ui/widget/imagewidget " )
2019-07-19 03:52:12 +00:00
local Math = require ( " optmath " )
2020-06-06 17:23:16 +00:00
local OverlapGroup = require ( " ui/widget/overlapgroup " )
2017-12-17 17:27:24 +00:00
local ScreenSaverWidget = require ( " ui/widget/screensaverwidget " )
2020-02-22 08:07:28 +00:00
local TextBoxWidget = require ( " ui/widget/textboxwidget " )
local TopContainer = require ( " ui/widget/container/topcontainer " )
2017-09-05 07:01:46 +00:00
local UIManager = require ( " ui/uimanager " )
2017-12-17 17:27:24 +00:00
local lfs = require ( " libs/libkoreader-lfs " )
2016-12-29 08:10:38 +00:00
local logger = require ( " logger " )
2017-12-17 17:27:24 +00:00
local _ = require ( " gettext " )
2017-09-05 07:01:46 +00:00
local Screen = Device.screen
2017-12-17 17:27:24 +00:00
local T = require ( " ffi/util " ) . template
2014-08-22 07:05:00 +00:00
2021-04-04 01:28:45 +00:00
-- Default settings
if G_reader_settings : hasNot ( " screensaver_show_message " ) then
G_reader_settings : makeFalse ( " screensaver_show_message " )
end
if G_reader_settings : hasNot ( " screensaver_type " ) then
G_reader_settings : saveSetting ( " screensaver_type " , " disable " )
G_reader_settings : makeTrue ( " screensaver_show_message " )
end
if G_reader_settings : hasNot ( " screensaver_img_background " ) then
G_reader_settings : saveSetting ( " screensaver_img_background " , " black " )
end
if G_reader_settings : hasNot ( " screensaver_msg_background " ) then
G_reader_settings : saveSetting ( " screensaver_msg_background " , " none " )
end
if G_reader_settings : hasNot ( " screensaver_message_position " ) then
G_reader_settings : saveSetting ( " screensaver_message_position " , " middle " )
end
if G_reader_settings : hasNot ( " screensaver_stretch_images " ) then
G_reader_settings : makeFalse ( " screensaver_stretch_images " )
end
if G_reader_settings : hasNot ( " screensaver_delay " ) then
G_reader_settings : saveSetting ( " screensaver_delay " , " disable " )
end
2021-04-19 00:20:46 +00:00
if G_reader_settings : hasNot ( " screensaver_hide_fallback_msg " ) then
G_reader_settings : makeFalse ( " screensaver_hide_fallback_msg " )
end
2021-03-06 21:44:18 +00:00
local Screensaver = {
screensaver_provider = {
jpg = true ,
jpeg = true ,
png = true ,
gif = true ,
tif = true ,
tiff = true ,
} ,
default_screensaver_message = _ ( " Sleeping " ) ,
2018-09-30 18:29:43 +00:00
}
2016-04-15 00:42:54 +00:00
2021-03-06 21:44:18 +00:00
function Screensaver : _getRandomImage ( dir )
if not dir then
return nil
end
2016-04-15 00:42:54 +00:00
if string.sub ( dir , string.len ( dir ) ) ~= " / " then
dir = dir .. " / "
end
2014-08-22 07:05:00 +00:00
local pics = { }
local i = 0
2014-08-26 10:10:26 +00:00
math.randomseed ( os.time ( ) )
2018-06-16 21:05:20 +00:00
local ok , iter , dir_obj = pcall ( lfs.dir , dir )
if ok then
for entry in iter , dir_obj do
if lfs.attributes ( dir .. entry , " mode " ) == " file " then
2018-09-30 18:29:43 +00:00
local extension = string.lower ( string.match ( entry , " .+%.([^.]+) " ) or " " )
2021-03-06 21:44:18 +00:00
if self.screensaver_provider [ extension ] then
2018-06-16 21:05:20 +00:00
i = i + 1
pics [ i ] = entry
end
2014-08-22 07:05:00 +00:00
end
end
2018-06-16 21:05:20 +00:00
if i == 0 then
return nil
end
else
2016-11-17 13:12:31 +00:00
return nil
end
2017-12-17 17:27:24 +00:00
return dir .. pics [ math.random ( i ) ]
2016-04-15 20:21:39 +00:00
end
2021-03-06 21:44:18 +00:00
local function expandSpecial ( message , fallback )
-- Expand special character sequences in given message.
-- %p percentage read
-- %c current page
-- %t total pages
-- %T document title
-- %A document authors
-- %S document series
if G_reader_settings : hasNot ( " lastfile " ) then
return fallback
end
local ret = message
local lastfile = G_reader_settings : readSetting ( " lastfile " )
local totalpages = 0
local percent = 0
local currentpage = 0
local title = _ ( " N/A " )
local authors = _ ( " N/A " )
local series = _ ( " N/A " )
2021-03-07 22:08:23 +00:00
local ReaderUI = require ( " apps/reader/readerui " )
local ui = ReaderUI : _getRunningInstance ( )
2021-05-18 16:20:34 +00:00
if ui and ui.document then
2021-03-07 22:08:23 +00:00
-- If we have a ReaderUI instance, use it.
local doc = ui.document
currentpage = ui.view . state.page or currentpage
totalpages = doc : getPageCount ( ) or totalpages
percent = Math.round ( ( currentpage * 100 ) / totalpages )
local props = doc : getProps ( )
if props then
title = props.title and props.title ~= " " and props.title or title
authors = props.authors and props.authors ~= " " and props.authors or authors
series = props.series and props.series ~= " " and props.series or series
end
elseif DocSettings : hasSidecarFile ( lastfile ) then
2021-03-06 21:44:18 +00:00
-- If there's no ReaderUI instance, but the file has sidecar data, use that
local docinfo = DocSettings : open ( lastfile )
totalpages = docinfo.data . doc_pages or totalpages
percent = docinfo.data . percent_finished or percent
currentpage = Math.round ( percent * totalpages )
percent = Math.round ( percent * 100 )
if docinfo.data . doc_props then
title = docinfo.data . doc_props.title and docinfo.data . doc_props.title ~= " " and docinfo.data . doc_props.title or title
authors = docinfo.data . doc_props.authors and docinfo.data . doc_props.authors ~= " " and docinfo.data . doc_props.authors or authors
series = docinfo.data . doc_props.series and docinfo.data . doc_props.series ~= " " and docinfo.data . doc_props.series or series
end
end
ret = string.gsub ( ret , " %%c " , currentpage )
ret = string.gsub ( ret , " %%t " , totalpages )
ret = string.gsub ( ret , " %%p " , percent )
ret = string.gsub ( ret , " %%T " , title )
ret = string.gsub ( ret , " %%A " , authors )
ret = string.gsub ( ret , " %%S " , series )
return ret
end
local function addOverlayMessage ( widget , text )
local FrameContainer = require ( " ui/widget/container/framecontainer " )
local RightContainer = require ( " ui/widget/container/rightcontainer " )
local Size = require ( " ui/size " )
local TextWidget = require ( " ui/widget/textwidget " )
local face = Font : getFace ( " infofont " )
local screen_w , screen_h = Screen : getWidth ( ) , Screen : getHeight ( )
local textw = TextWidget : new {
text = text ,
face = face ,
}
-- Don't make our message reach full screen width
if textw : getWidth ( ) > screen_w * 0.9 then
-- Text too wide: use TextBoxWidget for multi lines display
textw = TextBoxWidget : new {
text = text ,
face = face ,
width = math.floor ( screen_w * 0.9 )
}
end
textw = FrameContainer : new {
background = Blitbuffer.COLOR_WHITE ,
bordersize = Size.border . default ,
margin = 0 ,
textw ,
}
textw = RightContainer : new {
dimen = {
w = screen_w ,
h = textw : getSize ( ) . h ,
} ,
textw ,
}
widget = OverlapGroup : new {
dimen = {
h = screen_w ,
w = screen_h ,
} ,
widget ,
textw ,
}
return widget
end
2017-12-17 17:27:24 +00:00
function Screensaver : chooseFolder ( )
local buttons = { }
table.insert ( buttons , {
{
2021-02-22 17:44:16 +00:00
text = _ ( " Choose screensaver folder " ) ,
2017-12-17 17:27:24 +00:00
callback = function ( )
UIManager : close ( self.choose_dialog )
require ( " ui/downloadmgr " ) : new {
onConfirm = function ( path )
logger.dbg ( " set screensaver directory to " , path )
G_reader_settings : saveSetting ( " screensaver_dir " , path )
UIManager : show ( InfoMessage : new {
2021-03-28 11:35:56 +00:00
text = T ( _ ( " Screensaver folder set to: \n %1 " ) , BD.dirpath ( path ) ) ,
2017-12-17 17:27:24 +00:00
timeout = 3 ,
} )
end ,
} : chooseDir ( )
end ,
}
} )
table.insert ( buttons , {
{
text = _ ( " Close " ) ,
callback = function ( )
UIManager : close ( self.choose_dialog )
end ,
}
} )
local screensaver_dir = G_reader_settings : readSetting ( " screensaver_dir " )
2021-03-06 21:44:18 +00:00
or DataStorage : getDataDir ( ) .. " /screenshots/ "
2017-12-17 17:27:24 +00:00
self.choose_dialog = ButtonDialogTitle : new {
2021-02-22 17:44:16 +00:00
title = T ( _ ( " Current screensaver image folder: \n %1 " ) , BD.dirpath ( screensaver_dir ) ) ,
2017-12-17 17:27:24 +00:00
buttons = buttons
}
UIManager : show ( self.choose_dialog )
2016-08-01 01:26:26 +00:00
end
2019-09-16 13:27:49 +00:00
function Screensaver : chooseFile ( document_cover )
local text = document_cover and _ ( " Choose document cover " ) or _ ( " Choose screensaver image " )
2018-07-23 08:37:24 +00:00
local buttons = { }
table.insert ( buttons , {
{
2019-09-16 13:27:49 +00:00
text = text ,
2018-07-23 08:37:24 +00:00
callback = function ( )
UIManager : close ( self.choose_dialog )
2019-03-15 13:14:53 +00:00
local util = require ( " util " )
local PathChooser = require ( " ui/widget/pathchooser " )
local path_chooser = PathChooser : new {
select_directory = false ,
select_file = true ,
2018-07-23 08:37:24 +00:00
file_filter = function ( filename )
local suffix = util.getFileNameSuffix ( filename )
2019-09-16 13:27:49 +00:00
if document_cover and DocumentRegistry : hasProvider ( filename ) then
return true
2021-03-06 21:44:18 +00:00
elseif self.screensaver_provider [ suffix ] then
2018-07-23 08:37:24 +00:00
return true
end
end ,
2019-03-15 13:14:53 +00:00
detailed_file_info = true ,
path = self.root_path ,
onConfirm = function ( file_path )
2019-09-16 13:27:49 +00:00
if document_cover then
G_reader_settings : saveSetting ( " screensaver_document_cover " , file_path )
UIManager : show ( InfoMessage : new {
2020-01-04 00:18:51 +00:00
text = T ( _ ( " Screensaver document cover set to: \n %1 " ) , BD.filepath ( file_path ) ) ,
2019-09-16 13:27:49 +00:00
timeout = 3 ,
} )
else
G_reader_settings : saveSetting ( " screensaver_image " , file_path )
UIManager : show ( InfoMessage : new {
2020-01-04 00:18:51 +00:00
text = T ( _ ( " Screensaver image set to: \n %1 " ) , BD.filepath ( file_path ) ) ,
2019-09-16 13:27:49 +00:00
timeout = 3 ,
} )
end
2019-03-15 13:14:53 +00:00
end
2018-07-23 08:37:24 +00:00
}
2019-03-15 13:14:53 +00:00
UIManager : show ( path_chooser )
2018-07-23 08:37:24 +00:00
end ,
}
} )
table.insert ( buttons , {
{
text = _ ( " Close " ) ,
callback = function ( )
UIManager : close ( self.choose_dialog )
end ,
}
} )
local screensaver_image = G_reader_settings : readSetting ( " screensaver_image " )
2021-03-06 21:44:18 +00:00
or DataStorage : getDataDir ( ) .. " /resources/koreader.png "
2019-09-16 13:27:49 +00:00
local screensaver_document_cover = G_reader_settings : readSetting ( " screensaver_document_cover " )
2020-01-04 00:18:51 +00:00
local title = document_cover and T ( _ ( " Current screensaver document cover: \n %1 " ) , BD.filepath ( screensaver_document_cover ) )
or T ( _ ( " Current screensaver image: \n %1 " ) , BD.filepath ( screensaver_image ) )
2018-07-23 08:37:24 +00:00
self.choose_dialog = ButtonDialogTitle : new {
2019-09-16 13:27:49 +00:00
title = title ,
2018-07-23 08:37:24 +00:00
buttons = buttons
}
UIManager : show ( self.choose_dialog )
end
2021-03-06 21:44:18 +00:00
function Screensaver : isExcluded ( )
2021-03-07 22:08:23 +00:00
local ReaderUI = require ( " apps/reader/readerui " )
local ui = ReaderUI : _getRunningInstance ( )
2021-05-18 16:20:34 +00:00
if ui and ui.doc_settings then
2021-03-07 22:08:23 +00:00
local doc_settings = ui.doc_settings
2021-03-06 21:44:18 +00:00
return doc_settings : isTrue ( " exclude_screensaver " )
else
2021-03-07 22:08:23 +00:00
if G_reader_settings : hasNot ( " lastfile " ) then
return false
end
local lastfile = G_reader_settings : readSetting ( " lastfile " )
if DocSettings : hasSidecarFile ( lastfile ) then
local doc_settings = DocSettings : open ( lastfile )
return doc_settings : isTrue ( " exclude_screensaver " )
else
-- No DocSetting, not excluded
return false
end
2017-12-17 17:27:24 +00:00
end
end
function Screensaver : setMessage ( )
local InputDialog = require ( " ui/widget/inputdialog " )
local screensaver_message = G_reader_settings : readSetting ( " screensaver_message " )
2021-03-06 21:44:18 +00:00
or self.default_screensaver_message
2017-12-17 17:27:24 +00:00
self.input_dialog = InputDialog : new {
title = " Screensaver message " ,
2021-01-06 03:01:40 +00:00
description = _ ( " Enter the message to be displayed by the screensaver. The following escape sequences can be used: \n %p percentage read \n %c current page number \n %t total number of pages \n %T title \n %A authors \n %S series " ) ,
2017-12-17 17:27:24 +00:00
input = screensaver_message ,
buttons = {
{
{
text = _ ( " Cancel " ) ,
callback = function ( )
UIManager : close ( self.input_dialog )
end ,
} ,
{
text = _ ( " Set message " ) ,
is_enter_default = true ,
callback = function ( )
G_reader_settings : saveSetting ( " screensaver_message " , self.input_dialog : getInputText ( ) )
UIManager : close ( self.input_dialog )
end ,
} ,
} ,
} ,
}
UIManager : show ( self.input_dialog )
2018-03-30 10:46:36 +00:00
self.input_dialog : onShowKeyboard ( )
2014-08-22 07:05:00 +00:00
end
2021-03-06 21:44:18 +00:00
-- When called after setup(), may not match the saved settings, because it accounts for fallbacks that might have kicked in.
function Screensaver : getMode ( )
return self.screensaver_type
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
function Screensaver : modeExpectsPortrait ( )
return self.screensaver_type ~= " message "
and self.screensaver_type ~= " disable "
and self.screensaver_type ~= " readingprogress "
and self.screensaver_type ~= " bookstatus "
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
function Screensaver : modeIsImage ( )
return self.screensaver_type == " cover "
or self.screensaver_type == " random_image "
or self.screensaver_type == " image_file "
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
function Screensaver : withBackground ( )
return self.screensaver_background ~= " none "
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
function Screensaver : setup ( event , fallback_message )
2021-04-04 01:28:45 +00:00
self.show_message = G_reader_settings : isTrue ( " screensaver_show_message " )
self.screensaver_type = G_reader_settings : readSetting ( " screensaver_type " )
local screensaver_img_background = G_reader_settings : readSetting ( " screensaver_img_background " )
local screensaver_msg_background = G_reader_settings : readSetting ( " screensaver_msg_background " )
2021-01-10 00:51:32 +00:00
2021-03-06 21:44:18 +00:00
-- These 2 (optional) parameters are to support poweroff and reboot actions on Kobo (c.f., UIManager)
self.prefix = event and event .. " _ " or " " -- "", "poweroff_" or "reboot_"
self.fallback_message = fallback_message
self.overlay_message = nil
if G_reader_settings : has ( self.prefix .. " screensaver_type " ) then
self.screensaver_type = G_reader_settings : readSetting ( self.prefix .. " screensaver_type " )
else
2021-04-19 00:20:46 +00:00
if event and G_reader_settings : isFalse ( " screensaver_hide_fallback_msg " ) then
2021-03-06 21:44:18 +00:00
-- Display the provided fallback_message over the screensaver,
-- so the user can distinguish between suspend (no overlay),
-- and reboot/poweroff (overlaid message).
self.overlay_message = self.fallback_message
2021-01-10 00:51:32 +00:00
end
2019-03-14 19:58:45 +00:00
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
-- Reset state
self.lastfile = nil
self.image = nil
self.image_file = nil
-- Check lastfile and setup the requested mode's resources, or a fallback mode if the required resources are unavailable.
2021-03-07 22:08:23 +00:00
local ReaderUI = require ( " apps/reader/readerui " )
local ui = ReaderUI : _getRunningInstance ( )
2021-03-06 21:44:18 +00:00
self.lastfile = G_reader_settings : readSetting ( " lastfile " )
if self.screensaver_type == " document_cover " then
2019-09-16 13:27:49 +00:00
-- Set lastfile to the document of which we want to show the cover.
2021-03-06 21:44:18 +00:00
self.lastfile = G_reader_settings : readSetting ( " screensaver_document_cover " )
self.screensaver_type = " cover "
2019-09-16 13:27:49 +00:00
end
2021-03-06 21:44:18 +00:00
if self.screensaver_type == " cover " then
self.lastfile = self.lastfile ~= nil and self.lastfile or G_reader_settings : readSetting ( " lastfile " )
local excluded
if DocSettings : hasSidecarFile ( self.lastfile ) then
2021-03-07 22:08:23 +00:00
local doc_settings
2021-05-18 16:20:34 +00:00
if ui and ui.doc_settings then
2021-03-07 22:08:23 +00:00
doc_settings = ui.doc_settings
else
doc_settings = DocSettings : open ( self.lastfile )
end
2021-03-06 21:44:18 +00:00
excluded = doc_settings : isTrue ( " exclude_screensaver " )
else
-- No DocSetting, not excluded
excluded = false
2014-09-17 10:24:33 +00:00
end
2021-03-06 21:44:18 +00:00
if not excluded then
if self.lastfile and lfs.attributes ( self.lastfile , " mode " ) == " file " then
2021-05-18 16:20:34 +00:00
if ui and ui.document then
2021-03-07 22:08:23 +00:00
local doc = ui.document
self.image = doc : getCoverPageImage ( )
else
local doc = DocumentRegistry : openDocument ( self.lastfile )
if doc.loadDocument then -- CreDocument
doc : loadDocument ( false ) -- load only metadata
end
self.image = doc : getCoverPageImage ( )
doc : close ( )
2018-11-20 20:07:59 +00:00
end
2021-03-06 21:44:18 +00:00
if self.image == nil then
self.screensaver_type = " random_image "
2016-11-16 10:49:34 +00:00
end
2017-12-17 17:27:24 +00:00
else
2021-03-06 21:44:18 +00:00
self.screensaver_type = " random_image "
2014-08-26 10:10:26 +00:00
end
2021-03-06 21:44:18 +00:00
else
-- Fallback to random images if this book cover is excluded
self.screensaver_type = " random_image "
2014-08-26 10:10:26 +00:00
end
end
2021-03-06 21:44:18 +00:00
if self.screensaver_type == " bookstatus " then
if self.lastfile and lfs.attributes ( self.lastfile , " mode " ) == " file " then
2021-03-07 22:08:23 +00:00
if not ui then
2021-03-06 21:44:18 +00:00
self.screensaver_type = " disable "
self.show_message = true
2017-12-17 17:27:24 +00:00
end
else
2021-03-06 21:44:18 +00:00
self.screensaver_type = " disable "
self.show_message = true
2017-04-08 21:21:03 +00:00
end
2017-12-17 17:27:24 +00:00
end
2021-03-06 21:44:18 +00:00
if self.screensaver_type == " random_image " then
local screensaver_dir = G_reader_settings : readSetting ( self.prefix .. " screensaver_dir " )
or G_reader_settings : readSetting ( " screensaver_dir " )
self.image_file = self : _getRandomImage ( screensaver_dir )
if self.image_file == nil then
self.screensaver_type = " disable "
self.show_message = true
2017-12-17 17:27:24 +00:00
end
end
2021-03-06 21:44:18 +00:00
if self.screensaver_type == " image_file " then
self.image_file = G_reader_settings : readSetting ( self.prefix .. " screensaver_image " )
or G_reader_settings : readSetting ( " screensaver_image " )
if self.image_file == nil or lfs.attributes ( self.image_file , " mode " ) ~= " file " then
self.screensaver_type = " disable "
self.show_message = true
2018-07-23 08:37:24 +00:00
end
end
2021-03-06 21:44:18 +00:00
if self.screensaver_type == " readingprogress " then
-- This is implemented by the Statistics plugin
if Screensaver.getReaderProgress == nil then
self.screensaver_type = " disable "
self.show_message = true
2017-12-17 17:27:24 +00:00
end
end
2020-06-06 17:23:16 +00:00
2021-04-04 01:28:45 +00:00
-- Use the right background setting depending on the effective mode, now that fallbacks have kicked in.
if self : modeIsImage ( ) then
self.screensaver_background = screensaver_img_background
else
self.screensaver_background = screensaver_msg_background
2021-03-06 21:44:18 +00:00
end
end
function Screensaver : show ( )
if self.screensaver_widget then
UIManager : close ( self.screensaver_widget )
self.screensaver_widget = nil
end
-- In as-is mode with no message and no overlay, we've got nothing to show :)
if self.screensaver_type == " disable " and not self.show_message and not self.overlay_message then
return
end
-- Build the main widget for the effective mode, all the sanity checks were handled in setup
local widget = nil
if self.screensaver_type == " cover " then
widget = ImageWidget : new {
image = self.image ,
image_disposable = true ,
height = Screen : getHeight ( ) ,
width = Screen : getWidth ( ) ,
2021-04-04 01:28:45 +00:00
scale_factor = G_reader_settings : isFalse ( " screensaver_stretch_images " ) and 0 or nil ,
2021-03-06 21:44:18 +00:00
}
elseif self.screensaver_type == " bookstatus " then
2021-03-07 22:08:23 +00:00
local ReaderUI = require ( " apps/reader/readerui " )
local ui = ReaderUI : _getRunningInstance ( )
local doc = ui.document
local doc_settings = ui.doc_settings
2021-03-06 21:44:18 +00:00
widget = BookStatusWidget : new {
thumbnail = doc : getCoverPageImage ( ) ,
props = doc : getProps ( ) ,
document = doc ,
settings = doc_settings ,
2021-03-07 22:08:23 +00:00
view = ui.view ,
2021-03-06 21:44:18 +00:00
readonly = true ,
}
elseif self.screensaver_type == " random_image " or self.screensaver_type == " image_file " then
widget = ImageWidget : new {
file = self.image_file ,
file_do_cache = false ,
alpha = true ,
height = Screen : getHeight ( ) ,
width = Screen : getWidth ( ) ,
2021-04-04 01:28:45 +00:00
scale_factor = G_reader_settings : isFalse ( " screensaver_stretch_images " ) and 0 or nil ,
2021-03-06 21:44:18 +00:00
}
elseif self.screensaver_type == " readingprogress " then
widget = Screensaver.getReaderProgress ( )
end
-- Assume that we'll be covering the full-screen by default (either because of a widget, or a background fill).
local covers_fullscreen = true
-- Speaking of, set that background fill up...
local background
if self.screensaver_background == " black " then
background = Blitbuffer.COLOR_BLACK
elseif self.screensaver_background == " white " then
background = Blitbuffer.COLOR_WHITE
elseif self.screensaver_background == " none " then
background = nil
end
if self.show_message then
-- Handle user settings & fallbacks, with that prefix mess on top...
local screensaver_message
if G_reader_settings : has ( self.prefix .. " screensaver_message " ) then
screensaver_message = G_reader_settings : readSetting ( self.prefix .. " screensaver_message " )
else
if G_reader_settings : has ( " screensaver_message " ) then
-- We prefer the global user setting to the event's fallback message.
screensaver_message = G_reader_settings : readSetting ( " screensaver_message " )
else
screensaver_message = self.fallback_message or self.default_screensaver_message
end
2017-12-17 17:27:24 +00:00
end
2021-03-06 21:44:18 +00:00
-- NOTE: Only attempt to expand if there are special characters in the message.
if screensaver_message : find ( " %% " ) then
screensaver_message = expandSpecial ( screensaver_message , self.fallback_message or self.default_screensaver_message )
2018-07-23 08:37:24 +00:00
end
2019-07-18 12:11:21 +00:00
2021-03-06 21:44:18 +00:00
local message_pos
if G_reader_settings : has ( self.prefix .. " screensaver_message_position " ) then
message_pos = G_reader_settings : readSetting ( self.prefix .. " screensaver_message_position " )
2019-07-18 12:11:21 +00:00
else
2021-04-04 01:28:45 +00:00
message_pos = G_reader_settings : readSetting ( " screensaver_message_position " )
2017-12-17 17:27:24 +00:00
end
2019-07-18 12:11:21 +00:00
2021-03-06 21:44:18 +00:00
-- The only case where we *won't* cover the full-screen is when we only display a message and no background.
if widget == nil and self.screensaver_background == " none " then
covers_fullscreen = false
end
2020-06-06 17:23:16 +00:00
local message_widget
2021-03-06 21:44:18 +00:00
if message_pos == " middle " then
2020-06-06 17:23:16 +00:00
message_widget = InfoMessage : new {
2020-02-22 08:07:28 +00:00
text = screensaver_message ,
readonly = true ,
}
else
local face = Font : getFace ( " infofont " )
local container
if message_pos == " bottom " then
container = BottomContainer
else
container = TopContainer
end
local screen_w , screen_h = Screen : getWidth ( ) , Screen : getHeight ( )
2020-06-06 17:23:16 +00:00
message_widget = container : new {
2020-02-22 08:07:28 +00:00
dimen = Geom : new {
w = screen_w ,
h = screen_h ,
} ,
TextBoxWidget : new {
text = screensaver_message ,
face = face ,
width = screen_w ,
alignment = " center " ,
}
}
end
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
-- No overlay needed as we just displayed *a* message (not necessarily the event's, though).
self.overlay_message = nil
2020-06-06 17:23:16 +00:00
2021-03-06 21:44:18 +00:00
-- Check if message_widget should be overlaid on another widget
2020-06-06 17:23:16 +00:00
if message_widget then
2021-03-06 21:44:18 +00:00
if widget then -- We have a Screensaver widget
-- Show message_widget on top of previously created widget
2020-06-06 17:23:16 +00:00
local screen_w , screen_h = Screen : getWidth ( ) , Screen : getHeight ( )
widget = OverlapGroup : new {
dimen = {
h = screen_w ,
w = screen_h ,
} ,
widget ,
message_widget ,
}
else
2021-03-06 21:44:18 +00:00
-- No prevously created widget, so just show message widget
2020-06-06 17:23:16 +00:00
widget = message_widget
end
end
2018-01-21 18:44:12 +00:00
end
2021-03-06 21:44:18 +00:00
if self.overlay_message then
widget = addOverlayMessage ( widget , self.overlay_message )
2017-12-17 17:27:24 +00:00
end
if widget then
2021-03-06 21:44:18 +00:00
self.screensaver_widget = ScreenSaverWidget : new {
2017-12-17 17:27:24 +00:00
widget = widget ,
background = background ,
2018-03-17 22:02:32 +00:00
covers_fullscreen = covers_fullscreen ,
2017-12-17 17:27:24 +00:00
}
2021-03-06 21:44:18 +00:00
self.screensaver_widget . modal = true
self.screensaver_widget . dithered = true
UIManager : show ( self.screensaver_widget , " full " )
2014-08-26 10:10:26 +00:00
end
end
2021-03-06 21:44:18 +00:00
function Screensaver : close ( )
if self.screensaver_widget == nil then
return
2019-07-18 12:11:21 +00:00
end
2017-12-17 17:27:24 +00:00
local screensaver_delay = G_reader_settings : readSetting ( " screensaver_delay " )
local screensaver_delay_number = tonumber ( screensaver_delay )
if screensaver_delay_number then
UIManager : scheduleIn ( screensaver_delay_number , function ( )
logger.dbg ( " close screensaver " )
2021-03-06 21:44:18 +00:00
if self.screensaver_widget then
UIManager : close ( self.screensaver_widget , " full " )
self.screensaver_widget = nil
2017-12-17 17:27:24 +00:00
end
end )
2021-04-04 01:28:45 +00:00
elseif screensaver_delay == " disable " then
2017-12-17 17:27:24 +00:00
logger.dbg ( " close screensaver " )
2021-03-06 21:44:18 +00:00
if self.screensaver_widget then
UIManager : close ( self.screensaver_widget , " full " )
self.screensaver_widget = nil
2017-12-17 17:27:24 +00:00
end
else
logger.dbg ( " tap to exit from screensaver " )
2014-08-26 10:10:26 +00:00
end
end
return Screensaver