2023-06-08 05:27:52 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
2020-02-03 19:08:18 +00:00
|
|
|
local Device = require("device")
|
2023-06-08 05:27:52 +00:00
|
|
|
local DocumentRegistry = require("document/documentregistry")
|
2019-11-05 23:17:28 +00:00
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local ReadCollection = require("readcollection")
|
|
|
|
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")
|
2019-11-05 23:17:28 +00:00
|
|
|
local Screen = require("device").screen
|
2022-12-25 07:46:44 +00:00
|
|
|
local filemanagerutil = require("apps/filemanager/filemanagerutil")
|
2019-11-05 23:17:28 +00:00
|
|
|
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.
2022-10-06 00:14:48 +00:00
|
|
|
local FileManagerCollection = WidgetContainer:extend{
|
2023-12-10 06:05:34 +00:00
|
|
|
title = _("Favorites"),
|
2019-11-05 23:17:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function FileManagerCollection:init()
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerCollection:addToMainMenu(menu_items)
|
|
|
|
menu_items.collections = {
|
2023-12-10 06:05:34 +00:00
|
|
|
text = self.title,
|
2019-11-05 23:17:28 +00:00
|
|
|
callback = function()
|
2023-12-10 06:05:34 +00:00
|
|
|
self:onShowColl()
|
2019-11-05 23:17:28 +00:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerCollection:updateItemTable()
|
2023-12-10 06:05:34 +00:00
|
|
|
local item_table = {}
|
|
|
|
for _, item in pairs(ReadCollection.coll[self.coll_menu.collection_name]) do
|
|
|
|
table.insert(item_table, item)
|
2019-11-05 23:17:28 +00:00
|
|
|
end
|
2023-12-10 06:05:34 +00:00
|
|
|
table.sort(item_table, function(v1, v2) return v1.order < v2.order end)
|
|
|
|
self.coll_menu:switchItemTable(self.title, item_table, -1)
|
2019-11-05 23:17:28 +00:00
|
|
|
end
|
|
|
|
|
2023-03-31 16:35:27 +00:00
|
|
|
function FileManagerCollection:onMenuChoice(item)
|
2023-12-10 06:05:34 +00:00
|
|
|
local file = item.file
|
|
|
|
if self.ui.document then
|
|
|
|
if self.ui.document.file ~= file then
|
|
|
|
self.ui:switchDocument(file)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local ReaderUI = require("apps/reader/readerui")
|
|
|
|
ReaderUI:showReader(file)
|
|
|
|
end
|
2023-03-31 16:35:27 +00:00
|
|
|
end
|
|
|
|
|
2019-11-05 23:17:28 +00:00
|
|
|
function FileManagerCollection:onMenuHold(item)
|
2023-12-10 06:05:34 +00:00
|
|
|
local file = item.file
|
2019-11-05 23:17:28 +00:00
|
|
|
self.collfile_dialog = nil
|
2023-03-31 16:35:27 +00:00
|
|
|
local function close_dialog_callback()
|
|
|
|
UIManager:close(self.collfile_dialog)
|
|
|
|
end
|
2023-06-08 05:27:52 +00:00
|
|
|
local function close_dialog_menu_callback()
|
|
|
|
UIManager:close(self.collfile_dialog)
|
|
|
|
self._manager.coll_menu.close_callback()
|
|
|
|
end
|
2023-12-10 06:05:34 +00:00
|
|
|
local function close_dialog_update_callback()
|
2023-02-04 21:05:06 +00:00
|
|
|
UIManager:close(self.collfile_dialog)
|
2023-02-17 21:06:55 +00:00
|
|
|
self._manager:updateItemTable()
|
2023-12-10 06:05:34 +00:00
|
|
|
self._manager.files_updated = true
|
2023-02-17 21:06:55 +00:00
|
|
|
end
|
2023-12-10 06:05:34 +00:00
|
|
|
local is_currently_opened = file == (self.ui.document and self.ui.document.file)
|
2023-02-17 21:06:55 +00:00
|
|
|
|
|
|
|
local buttons = {}
|
2023-12-10 06:05:34 +00:00
|
|
|
local doc_settings_or_file = is_currently_opened and self.ui.doc_settings or file
|
|
|
|
table.insert(buttons, filemanagerutil.genStatusButtonsRow(doc_settings_or_file, close_dialog_update_callback))
|
|
|
|
table.insert(buttons, {}) -- separator
|
2023-02-17 21:06:55 +00:00
|
|
|
table.insert(buttons, {
|
2023-12-10 06:05:34 +00:00
|
|
|
filemanagerutil.genResetSettingsButton(file, close_dialog_update_callback, is_currently_opened),
|
2019-11-05 23:17:28 +00:00
|
|
|
{
|
2023-02-17 21:06:55 +00:00
|
|
|
text = _("Remove from favorites"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(self.collfile_dialog)
|
2023-12-10 06:05:34 +00:00
|
|
|
ReadCollection:removeItem(file, self.collection_name)
|
2023-02-17 21:06:55 +00:00
|
|
|
self._manager:updateItemTable()
|
|
|
|
end,
|
2022-12-25 08:23:43 +00:00
|
|
|
},
|
2023-02-17 21:06:55 +00:00
|
|
|
})
|
|
|
|
table.insert(buttons, {
|
2023-12-10 06:05:34 +00:00
|
|
|
filemanagerutil.genShowFolderButton(file, close_dialog_menu_callback),
|
|
|
|
filemanagerutil.genBookInformationButton(file, close_dialog_callback),
|
2023-02-17 21:06:55 +00:00
|
|
|
})
|
2023-03-07 20:24:42 +00:00
|
|
|
table.insert(buttons, {
|
2023-12-10 06:05:34 +00:00
|
|
|
filemanagerutil.genBookCoverButton(file, close_dialog_callback),
|
|
|
|
filemanagerutil.genBookDescriptionButton(file, close_dialog_callback),
|
2023-03-07 20:24:42 +00:00
|
|
|
})
|
2023-02-17 21:06:55 +00:00
|
|
|
|
2023-12-10 06:05:34 +00:00
|
|
|
if Device:canExecuteScript(file) then
|
2020-02-03 19:08:18 +00:00
|
|
|
table.insert(buttons, {
|
2023-12-10 06:05:34 +00:00
|
|
|
filemanagerutil.genExecuteScriptButton(file, close_dialog_menu_callback)
|
2020-02-03 19:08:18 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2023-06-08 05:27:52 +00:00
|
|
|
self.collfile_dialog = ButtonDialog:new{
|
2023-12-10 06:05:34 +00:00
|
|
|
title = item.text,
|
2019-11-05 23:17:28 +00:00
|
|
|
title_align = "center",
|
|
|
|
buttons = buttons,
|
|
|
|
}
|
|
|
|
UIManager:show(self.collfile_dialog)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2021-04-09 21:12:15 +00:00
|
|
|
function FileManagerCollection:MenuSetRotationModeHandler(rotation)
|
|
|
|
if rotation ~= nil and rotation ~= Screen:getRotationMode() then
|
|
|
|
UIManager:close(self._manager.coll_menu)
|
|
|
|
if self._manager.ui.view and self._manager.ui.view.onSetRotationMode then
|
|
|
|
self._manager.ui.view:onSetRotationMode(rotation)
|
|
|
|
elseif self._manager.ui.onSetRotationMode then
|
|
|
|
self._manager.ui:onSetRotationMode(rotation)
|
|
|
|
else
|
|
|
|
Screen:setRotationMode(rotation)
|
|
|
|
end
|
|
|
|
self._manager:onShowColl()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-12-10 06:05:34 +00:00
|
|
|
function FileManagerCollection:onShowColl(collection_name)
|
|
|
|
collection_name = collection_name or ReadCollection.default_collection_name
|
2019-11-05 23:17:28 +00:00
|
|
|
self.coll_menu = Menu:new{
|
|
|
|
ui = self.ui,
|
|
|
|
covers_fullscreen = true, -- hint for UIManager:_repaint()
|
|
|
|
is_borderless = true,
|
|
|
|
is_popout = false,
|
2023-12-19 07:22:53 +00:00
|
|
|
-- item and book cover thumbnail dimensions in Mosaic and Detailed list display modes
|
|
|
|
-- must be equal in File manager, History and Collection windows to avoid image scaling
|
|
|
|
title_bar_fm_style = true,
|
2023-06-08 05:27:52 +00:00
|
|
|
title_bar_left_icon = "appbar.menu",
|
|
|
|
onLeftButtonTap = function() self:showCollDialog() end,
|
2023-03-31 16:35:27 +00:00
|
|
|
onMenuChoice = self.onMenuChoice,
|
2019-11-05 23:17:28 +00:00
|
|
|
onMenuHold = self.onMenuHold,
|
2021-04-09 21:12:15 +00:00
|
|
|
onSetRotationMode = self.MenuSetRotationModeHandler,
|
2019-11-05 23:17:28 +00:00
|
|
|
_manager = self,
|
2023-12-10 06:05:34 +00:00
|
|
|
collection_name = collection_name,
|
2019-11-05 23:17:28 +00:00
|
|
|
}
|
|
|
|
self.coll_menu.close_callback = function()
|
2023-12-10 06:05:34 +00:00
|
|
|
if self.files_updated then
|
|
|
|
if self.ui.file_chooser then
|
|
|
|
self.ui.file_chooser:refreshPath()
|
|
|
|
end
|
|
|
|
self.files_updated = nil
|
|
|
|
end
|
ReaderUI: Saner FM/RD lifecycle
* Ensure that going from one to the other tears down the former and
its plugins before instantiating the latter and its plugins.
UIManager: Unify Event sending & broadcasting
* Make the two behave the same way (walk the widget stack from top to
bottom), and properly handle the window stack shrinking shrinking
*and* growing.
Previously, broadcasting happened bottom-to-top and didn't really
handle the list shrinking/growing, while sending only handled the list
shrinking by a single element, and hopefully that element being the one
the event was just sent to.
These two items combined allowed us to optimize suboptimal
refresh behavior with Menu and other Menu classes when
opening/closing a document.
e.g., the "opening document" Notification is now properly regional,
and the "open last doc" option no longer flashes like a crazy person
anymore.
Plugins: Allow optimizing Menu refresh with custom menus, too.
Requires moving Menu's close_callback *after* onMenuSelect, which, eh,
probably makes sense, and is probably harmless in the grand scheme of
things.
2021-05-01 16:53:04 +00:00
|
|
|
UIManager:close(self.coll_menu)
|
2023-12-10 06:05:34 +00:00
|
|
|
self.coll_menu = nil
|
2019-11-05 23:17:28 +00:00
|
|
|
end
|
2023-12-10 06:05:34 +00:00
|
|
|
self:updateItemTable()
|
2019-11-05 23:17:28 +00:00
|
|
|
UIManager:show(self.coll_menu)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2023-06-08 05:27:52 +00:00
|
|
|
function FileManagerCollection:showCollDialog()
|
|
|
|
local coll_dialog
|
|
|
|
local buttons = {
|
|
|
|
{{
|
2023-12-10 06:05:34 +00:00
|
|
|
text = _("Sort favorites"),
|
2023-06-08 05:27:52 +00:00
|
|
|
callback = function()
|
|
|
|
UIManager:close(coll_dialog)
|
2023-12-10 06:05:34 +00:00
|
|
|
self:sortCollection()
|
2023-06-08 05:27:52 +00:00
|
|
|
end,
|
|
|
|
}},
|
|
|
|
{{
|
|
|
|
text = _("Add a book to favorites"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(coll_dialog)
|
|
|
|
local PathChooser = require("ui/widget/pathchooser")
|
|
|
|
local path_chooser = PathChooser:new{
|
|
|
|
path = G_reader_settings:readSetting("home_dir"),
|
|
|
|
select_directory = false,
|
|
|
|
file_filter = function(file)
|
2023-11-05 05:24:18 +00:00
|
|
|
return DocumentRegistry:hasProvider(file)
|
2023-06-08 05:27:52 +00:00
|
|
|
end,
|
|
|
|
onConfirm = function(file)
|
2023-12-10 06:05:34 +00:00
|
|
|
if not ReadCollection:hasFile(file) then
|
|
|
|
ReadCollection:addItem(file, self.coll_menu.collection_name)
|
2023-06-08 05:27:52 +00:00
|
|
|
self:updateItemTable()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(path_chooser)
|
|
|
|
end,
|
|
|
|
}},
|
2023-12-10 06:05:34 +00:00
|
|
|
}
|
|
|
|
if self.ui.document then
|
|
|
|
local has_file = ReadCollection:hasFile(self.ui.document.file)
|
|
|
|
table.insert(buttons, {{
|
|
|
|
text_func = function()
|
|
|
|
return has_file and _("Remove current book from favorites") or _("Add current book to favorites")
|
|
|
|
end,
|
2023-06-08 05:27:52 +00:00
|
|
|
callback = function()
|
|
|
|
UIManager:close(coll_dialog)
|
2023-12-10 06:05:34 +00:00
|
|
|
if has_file then
|
|
|
|
ReadCollection:removeItem(self.ui.document.file)
|
|
|
|
else
|
|
|
|
ReadCollection:addItem(self.ui.document.file, self.coll_menu.collection_name)
|
|
|
|
end
|
|
|
|
self:updateItemTable()
|
2023-06-08 05:27:52 +00:00
|
|
|
end,
|
2023-12-10 06:05:34 +00:00
|
|
|
}})
|
|
|
|
end
|
2023-06-08 05:27:52 +00:00
|
|
|
coll_dialog = ButtonDialog:new{
|
|
|
|
buttons = buttons,
|
|
|
|
}
|
|
|
|
UIManager:show(coll_dialog)
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerCollection:sortCollection()
|
2023-12-10 06:05:34 +00:00
|
|
|
local item_table = ReadCollection:getOrderedCollection(self.coll_menu.collection_name)
|
2023-06-08 05:27:52 +00:00
|
|
|
local SortWidget = require("ui/widget/sortwidget")
|
|
|
|
local sort_widget
|
|
|
|
sort_widget = SortWidget:new{
|
|
|
|
title = _("Sort favorites"),
|
|
|
|
item_table = item_table,
|
|
|
|
callback = function()
|
2023-12-10 06:05:34 +00:00
|
|
|
ReadCollection:updateCollectionOrder(self.coll_menu.collection_name, sort_widget.item_table)
|
2023-06-08 05:27:52 +00:00
|
|
|
self:updateItemTable()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
UIManager:show(sort_widget)
|
|
|
|
end
|
|
|
|
|
2023-09-06 06:41:10 +00:00
|
|
|
function FileManagerCollection:onBookMetadataChanged()
|
|
|
|
if self.coll_menu then
|
|
|
|
self.coll_menu:updateItems()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-05 23:17:28 +00:00
|
|
|
return FileManagerCollection
|