2020-01-04 00:18:51 +00:00
|
|
|
local BD = require("ui/bidi")
|
2018-08-22 20:34:20 +00:00
|
|
|
local ButtonDialog = require("ui/widget/buttondialog")
|
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
|
|
|
local InputDialog = require("ui/widget/inputdialog")
|
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local Screen = require("device").screen
|
|
|
|
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")
|
2020-09-25 13:54:14 +00:00
|
|
|
local lfs = require("libs/libkoreader-lfs")
|
2018-08-22 20:34:20 +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 FileManagerShortcuts = WidgetContainer:extend{
|
2023-11-16 05:48:10 +00:00
|
|
|
title = _("Folder shortcuts"),
|
2021-03-06 21:44:18 +00:00
|
|
|
folder_shortcuts = G_reader_settings:readSetting("folder_shortcuts", {}),
|
|
|
|
}
|
2018-08-22 20:34:20 +00:00
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:updateItemTable()
|
2018-08-22 20:34:20 +00:00
|
|
|
local item_table = {}
|
2021-03-06 21:44:18 +00:00
|
|
|
for _, item in ipairs(self.folder_shortcuts) do
|
2018-08-22 20:34:20 +00:00
|
|
|
table.insert(item_table, {
|
|
|
|
text = string.format("%s (%s)", item.text, item.folder),
|
|
|
|
folder = item.folder,
|
2023-11-16 05:48:10 +00:00
|
|
|
name = item.text,
|
2018-08-22 20:34:20 +00:00
|
|
|
})
|
|
|
|
end
|
2020-09-25 13:54:14 +00:00
|
|
|
table.sort(item_table, function(l, r)
|
|
|
|
return l.text < r.text
|
|
|
|
end)
|
|
|
|
|
2019-03-09 12:55:03 +00:00
|
|
|
-- try to stay on current page
|
2020-09-25 13:54:14 +00:00
|
|
|
local select_number
|
2023-11-16 05:48:10 +00:00
|
|
|
if self.shortcuts_menu.page and self.shortcuts_menu.perpage and self.shortcuts_menu.page > 0 then
|
|
|
|
select_number = (self.shortcuts_menu.page - 1) * self.shortcuts_menu.perpage + 1
|
2019-03-09 12:55:03 +00:00
|
|
|
end
|
2023-11-16 05:48:10 +00:00
|
|
|
self.shortcuts_menu:switchItemTable(nil, item_table, select_number)
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:_getIndex(folder)
|
|
|
|
for i, v in ipairs(self.folder_shortcuts) do
|
|
|
|
if v.folder == folder then
|
|
|
|
return i
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
2023-11-16 05:48:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:hasFolderShortcut(folder)
|
|
|
|
return self:_getIndex(folder) and true or false
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:onMenuChoice(item)
|
|
|
|
local folder = item.folder
|
|
|
|
if lfs.attributes(folder, "mode") ~= "directory" then return end
|
|
|
|
if self.select_callback then
|
|
|
|
self.select_callback(folder)
|
|
|
|
else
|
|
|
|
if self._manager.ui.file_chooser then
|
|
|
|
self._manager.ui.file_chooser:changeToPath(folder)
|
|
|
|
else -- called from Reader
|
|
|
|
self._manager.ui:onClose()
|
|
|
|
self._manager.ui:showFileManager(folder .. "/")
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:onMenuHold(item)
|
2023-11-16 05:48:10 +00:00
|
|
|
local dialog
|
|
|
|
local buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = _("Remove shortcut"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(dialog)
|
|
|
|
self._manager:removeShortcut(item.folder)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Rename shortcut"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(dialog)
|
|
|
|
self._manager:editShortcut(item.folder)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if self._manager.ui.file_chooser and self._manager.ui.clipboard then
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Paste to folder"),
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(dialog)
|
|
|
|
self._manager.ui:pasteHere(item.folder)
|
|
|
|
end
|
|
|
|
},
|
|
|
|
})
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
2023-11-16 05:48:10 +00:00
|
|
|
|
|
|
|
dialog = ButtonDialog:new{
|
|
|
|
title = item.name .. "\n" .. BD.dirpath(item.folder),
|
|
|
|
title_align = "center",
|
|
|
|
buttons = buttons,
|
|
|
|
}
|
|
|
|
UIManager:show(dialog)
|
|
|
|
return true
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:removeShortcut(folder)
|
|
|
|
local index = self:_getIndex(folder)
|
|
|
|
table.remove(self.folder_shortcuts, index)
|
|
|
|
if self.shortcuts_menu then
|
|
|
|
self.fm_updated = true
|
|
|
|
self:updateItemTable()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:editShortcut(folder, post_callback)
|
|
|
|
local index = self:_getIndex(folder)
|
|
|
|
local item, name
|
|
|
|
if index then -- rename
|
|
|
|
item = self.folder_shortcuts[index]
|
|
|
|
name = item.text
|
|
|
|
end
|
|
|
|
|
|
|
|
local input_dialog
|
|
|
|
input_dialog = InputDialog:new {
|
|
|
|
title = _("Enter folder shortcut name"),
|
|
|
|
input = name,
|
|
|
|
description = BD.dirpath(folder),
|
|
|
|
buttons = {{
|
2018-08-22 20:34:20 +00:00
|
|
|
{
|
2023-11-16 05:48:10 +00:00
|
|
|
text = _("Cancel"),
|
|
|
|
id = "close",
|
|
|
|
callback = function()
|
|
|
|
UIManager:close(input_dialog)
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = _("Save"),
|
|
|
|
is_enter_default = true,
|
|
|
|
callback = function()
|
|
|
|
local new_name = input_dialog:getInputText()
|
|
|
|
if new_name == "" or new_name == name then return end
|
|
|
|
UIManager:close(input_dialog)
|
|
|
|
if item then
|
|
|
|
item.text = new_name
|
|
|
|
else
|
|
|
|
table.insert(self.folder_shortcuts, {
|
|
|
|
text = new_name,
|
|
|
|
folder = folder,
|
|
|
|
})
|
|
|
|
if post_callback then
|
|
|
|
post_callback()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if self.shortcuts_menu then
|
|
|
|
self.fm_updated = true
|
|
|
|
self:updateItemTable()
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
},
|
|
|
|
}},
|
2018-08-22 20:34:20 +00:00
|
|
|
}
|
2023-11-16 05:48:10 +00:00
|
|
|
UIManager:show(input_dialog)
|
|
|
|
input_dialog:onShowKeyboard()
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:addShortcut()
|
|
|
|
local PathChooser = require("ui/widget/pathchooser")
|
|
|
|
local path_chooser = PathChooser:new{
|
|
|
|
select_directory = true,
|
|
|
|
select_file = false,
|
|
|
|
path = self.ui.file_chooser and self.ui.file_chooser.path or self.ui:getLastDirFile(),
|
|
|
|
onConfirm = function(path)
|
|
|
|
if self:hasFolderShortcut(path) then
|
|
|
|
UIManager:show(InfoMessage:new{
|
|
|
|
text = _("Shortcut already exists."),
|
|
|
|
})
|
|
|
|
else
|
|
|
|
self:editShortcut(path)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
UIManager:show(path_chooser)
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2023-11-16 05:48:10 +00:00
|
|
|
function FileManagerShortcuts:genShowFolderShortcutsButton(pre_callback)
|
|
|
|
return {
|
|
|
|
text = self.title,
|
|
|
|
callback = function()
|
|
|
|
pre_callback()
|
|
|
|
self:onShowFolderShortcutsDialog()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:genAddRemoveShortcutButton(folder, pre_callback, post_callback)
|
|
|
|
if self:hasFolderShortcut(folder) then
|
|
|
|
return {
|
|
|
|
text = _("Remove from folder shortcuts"),
|
|
|
|
callback = function()
|
|
|
|
pre_callback()
|
|
|
|
self:removeShortcut(folder)
|
|
|
|
post_callback()
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return {
|
|
|
|
text = _("Add to folder shortcuts"),
|
|
|
|
callback = function()
|
|
|
|
pre_callback()
|
|
|
|
self:editShortcut(folder, post_callback)
|
|
|
|
end,
|
|
|
|
}
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
2019-03-09 12:55:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FileManagerShortcuts:onSetDimensions(dimen)
|
|
|
|
self.dimen = dimen
|
2018-08-22 20:34:20 +00:00
|
|
|
end
|
|
|
|
|
2021-04-09 21:12:15 +00:00
|
|
|
function FileManagerShortcuts:MenuSetRotationModeHandler(rotation)
|
|
|
|
if rotation ~= nil and rotation ~= Screen:getRotationMode() then
|
2023-11-16 05:48:10 +00:00
|
|
|
UIManager:close(self._manager.shortcuts_menu)
|
2021-04-09 21:12:15 +00:00
|
|
|
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:onShowFolderShortcutsDialog()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-02-07 16:12:48 +00:00
|
|
|
function FileManagerShortcuts:onShowFolderShortcutsDialog(select_callback)
|
2023-11-16 05:48:10 +00:00
|
|
|
self.shortcuts_menu = Menu:new{
|
|
|
|
title = self.title,
|
|
|
|
covers_fullscreen = true,
|
2019-03-09 12:55:03 +00:00
|
|
|
is_borderless = true,
|
2023-11-16 05:48:10 +00:00
|
|
|
is_popout = false,
|
|
|
|
select_callback = select_callback, -- called from PathChooser titlebar left button
|
|
|
|
title_bar_left_icon = not select_callback and "plus" or nil,
|
|
|
|
onLeftButtonTap = function() self:addShortcut() end,
|
|
|
|
onMenuChoice = self.onMenuChoice,
|
2022-02-07 16:12:48 +00:00
|
|
|
onMenuHold = not select_callback and self.onMenuHold or nil,
|
2021-04-09 21:12:15 +00:00
|
|
|
onSetRotationMode = self.MenuSetRotationModeHandler,
|
2019-03-09 12:55:03 +00:00
|
|
|
_manager = self,
|
2019-03-04 21:45:55 +00:00
|
|
|
}
|
2023-11-16 05:48:10 +00:00
|
|
|
self.shortcuts_menu.close_callback = function()
|
|
|
|
UIManager:close(self.shortcuts_menu)
|
|
|
|
if self.fm_updated then
|
|
|
|
if self.ui.file_chooser then
|
|
|
|
self.ui.file_chooser:refreshPath()
|
|
|
|
self.ui:updateTitleBarPath()
|
|
|
|
end
|
|
|
|
self.fm_updated = nil
|
|
|
|
end
|
|
|
|
self.shortcuts_menu = nil
|
|
|
|
end
|
|
|
|
self:updateItemTable()
|
|
|
|
UIManager:show(self.shortcuts_menu)
|
2019-03-04 21:45:55 +00:00
|
|
|
end
|
|
|
|
|
2018-08-22 20:34:20 +00:00
|
|
|
return FileManagerShortcuts
|