bookmarkbrowser: default book source (#15143)

This commit is contained in:
hius07 authored and GitHub committed 2026-03-21 17:32:38 +02:00
1 parent 80cb27dbf5
commit c8fecc8a8a
1 file changed
+195 -112
+195 -112
View File
@@ -29,6 +29,191 @@ local BookmarkBrowser = WidgetContainer:extend{
checkmark = "\u{2713}",
}
function BookmarkBrowser:showSourceDialog(ui, force_show_dialog)
self.ui = ui
local home_dir = G_reader_settings:readSetting("home_dir")
local default_source = G_reader_settings:readSetting("bookmarks_browser_source")
local source_dialog
local function fetch_and_show_bookmarks(self_source_key, fetch_func_or_folder, subfolders)
UIManager:close(source_dialog)
UIManager:show(InfoMessage:new{
text = _("Fetching bookmarks…"),
timeout = 0.1,
})
UIManager:nextTick(function()
local books = {}
if type(fetch_func_or_folder) == "function" then
fetch_func_or_folder(books)
else
util.findFiles(fetch_func_or_folder, function(file)
books[file] = DocumentRegistry:hasProvider(file) or nil
end, subfolders)
end
if self.bm_list then
self.bm_list:onClose()
end
self.source_key = self_source_key
self:show(books)
end)
end
local sources = {
history = {
text = _("History"),
enabled = true,
callback = function()
fetch_and_show_bookmarks("history", function(books)
for _, v in ipairs(require("readhistory").hist) do
books[v.file] = v.select_enabled or nil
end
end)
end,
},
collections = {
text = _("Collections"),
enabled = true,
callback = function(saved_collections)
local caller_callback = function(selected_collections)
if default_source and default_source.collections then
default_source.collections = selected_collections
end
fetch_and_show_bookmarks("collections", function(books)
for coll_name in pairs(selected_collections) do
for file in pairs(require("readcollection").coll[coll_name]) do
books[file] = true
end
end
end)
end
if saved_collections and saved_collections ~= true then
caller_callback(saved_collections)
else
self.ui.collections:onShowCollList({}, caller_callback, true)
end
end,
},
selected_files = {
text = _("Selected files"),
enabled = ui.selected_files ~= nil,
callback = function()
fetch_and_show_bookmarks("selected_files", function(books)
for file in pairs(ui.selected_files) do
books[file] = true
end
end)
end,
},
home_folder = {
text = _("Home folder"),
enabled = home_dir ~= nil,
callback = function()
fetch_and_show_bookmarks("home_folder", home_dir, false)
end,
},
home_folder_subfolders = {
text = _("Home folder with subfolders"),
enabled = home_dir ~= nil,
callback = function()
fetch_and_show_bookmarks("home_folder_subfolders", home_dir, true)
end,
},
folder = {
text = _("Folder"),
enabled = true,
callback = function(saved_folder)
if saved_folder and saved_folder ~= true then
fetch_and_show_bookmarks("folder", saved_folder, false)
else
UIManager:show(PathChooser:new{
select_file = false,
path = home_dir,
onConfirm = function(path)
if default_source and default_source.folder then
default_source.folder = path
end
fetch_and_show_bookmarks("folder", path, false)
end,
})
end
end,
},
folder_subfolders = {
text = _("Folder with subfolders"),
enabled = true,
callback = function(saved_folder)
if saved_folder and saved_folder ~= true then
fetch_and_show_bookmarks("folder_subfolders", saved_folder, true)
else
UIManager:show(PathChooser:new{
select_file = false,
path = home_dir,
onConfirm = function(path)
if default_source and default_source.folder_subfolders then
default_source.folder_subfolders = path
end
fetch_and_show_bookmarks("folder_subfolders", path, true)
end,
})
end
end,
},
}
if not force_show_dialog and default_source then
local default_source_key, default_source_value = next(default_source)
if sources[default_source_key].enabled then
-- show bookmarks immediately, without source dialog
sources[default_source_key].callback(default_source_value)
return
end
end
-- show source dialog
local function gen_source_button(source_key)
local text = sources[source_key].text
if source_key == self.source_key then
text = text .. " " .. self.checkmark
end
local is_default_source_key = default_source and default_source[source_key]
if is_default_source_key then
text = text .. " ★"
end
return {{
text = text,
enabled = sources[source_key].enabled,
callback = function() -- fetch and show bookmark list
sources[source_key].callback()
end,
hold_callback = function() -- toggle default source
if is_default_source_key then
G_reader_settings:delSetting("bookmarks_browser_source")
else
-- for selected_collections/folder: the callback will choose and save it instead of "true"
G_reader_settings:saveSetting("bookmarks_browser_source", { [source_key] = true })
end
UIManager:close(source_dialog)
self:showSourceDialog(ui, true)
end,
}}
end
source_dialog = ButtonDialog:new{
title = _("Book source"),
title_align = "center",
width_factor = 0.8,
buttons = {
gen_source_button("history"),
gen_source_button("collections"),
gen_source_button("selected_files"),
gen_source_button("home_folder"),
gen_source_button("home_folder_subfolders"),
gen_source_button("folder"),
gen_source_button("folder_subfolders"),
},
}
UIManager:show(source_dialog)
end
function BookmarkBrowser:show(files, ui)
self.ui = ui or self.ui
@@ -40,7 +225,6 @@ function BookmarkBrowser:show(files, ui)
self.multilines_show_more_text = G_reader_settings:isTrue("bookmarks_items_multilines_show_more_text")
self.line_color = G_reader_settings:isTrue("bookmarks_items_show_separator")
and Blitbuffer.COLOR_DARK_GRAY or Blitbuffer.COLOR_WHITE
self.items_text = G_reader_settings:readSetting("bookmarks_items_text_type") or "note"
self.sorting_mode = G_reader_settings:readSetting("bookmarks_items_sorting") or "page"
self.is_reverse_sorting = G_reader_settings:isTrue("bookmarks_items_reverse_sorting")
self.highlight_color_default = G_reader_settings:readSetting("highlight_color")
@@ -76,6 +260,7 @@ function BookmarkBrowser:show(files, ui)
self:showSearchDialog(search_string)
end,
close_callback = function()
self.source_key = nil
self.books = nil
self.filter_table = nil
self.bm_list = nil
@@ -554,6 +739,15 @@ function BookmarkBrowser:showBookmarkListMenu()
{
self:genShowBookListButton(close_dialog_callback),
},
{
{
text = _("Book source"),
callback = function()
UIManager:close(bm_list_menu)
self:showSourceDialog(self.ui, true)
end,
},
},
}
bm_list_menu = ButtonDialog:new{
title = _("Filters"),
@@ -617,10 +811,6 @@ function BookmarkBrowser:showBookList(multi_choice)
title = T(_("Books: %1"), #item_table),
subtitle = subtitle,
item_table = item_table,
items_per_page = self.items_per_page,
items_font_size = self.items_font_size,
multilines_show_more_text = self.multilines_show_more_text,
line_color = self.line_color,
title_bar_left_icon = title_bar_left_icon,
onLeftButtonTap = onLeftButtonTap,
onMenuSelect = onMenuSelect,
@@ -810,111 +1000,4 @@ function BookmarkBrowser:showSearchDialog(search_string)
search_dialog:onShowKeyboard()
end
function BookmarkBrowser:showSourceDialog(ui)
self.ui = ui
local home_dir = G_reader_settings:readSetting("home_dir")
local source_dialog
local function fetch_and_show_bookmarks(fetch_func_or_folder, subfolders)
UIManager:close(source_dialog)
UIManager:show(InfoMessage:new{
text = _("Fetching bookmarks…"),
timeout = 0.1,
})
UIManager:nextTick(function()
local books = {}
if type(fetch_func_or_folder) == "function" then
fetch_func_or_folder(books)
else
util.findFiles(fetch_func_or_folder, function(file)
books[file] = DocumentRegistry:hasProvider(file) or nil
end, subfolders)
end
self:show(books)
end)
end
local buttons = {
{{
text = _("History"),
callback = function()
fetch_and_show_bookmarks(function(books)
for _, v in ipairs(require("readhistory").hist) do
books[v.file] = v.select_enabled or nil
end
end)
end,
}},
{{
text = _("Collections"),
callback = function()
local caller_callback = function(selected_collections)
fetch_and_show_bookmarks(function(books)
for coll_name in pairs(selected_collections) do
for file in pairs(require("readcollection").coll[coll_name]) do
books[file] = true
end
end
end)
end
self.ui.collections:onShowCollList({}, caller_callback, true)
end,
}},
{{
text = _("Selected files"),
enabled = ui.selected_files ~= nil,
callback = function()
fetch_and_show_bookmarks(function(books)
for file in pairs(ui.selected_files) do
books[file] = true
end
end)
end,
}},
{{
text = _("Home folder"),
enabled = home_dir ~= nil,
callback = function()
fetch_and_show_bookmarks(home_dir, false)
end,
}},
{{
text = _("Home folder with subfolders"),
enabled = home_dir ~= nil,
callback = function()
fetch_and_show_bookmarks(home_dir, true)
end,
}},
{{
text = _("Folder"),
callback = function()
UIManager:show(PathChooser:new{
select_file = false,
path = home_dir,
onConfirm = function(path)
fetch_and_show_bookmarks(path, false)
end,
})
end,
}},
{{
text = _("Folder with subfolders"),
callback = function()
UIManager:show(PathChooser:new{
select_file = false,
path = home_dir,
onConfirm = function(path)
fetch_and_show_bookmarks(path, true)
end,
})
end,
}},
}
source_dialog = ButtonDialog:new{
title = _("Book source"),
title_align = "center",
width_factor = 0.8,
buttons = buttons,
}
UIManager:show(source_dialog)
end
return BookmarkBrowser