- Move to folder is a submenu of existing folders (+ No folder, New folder…);
typed names match existing folders without regard to case.
- Folders nest ("Traveller/Worlds"); each folder has New spread here… and
Rename folder….
- When the last spread leaves a folder, ask Keep / Remove it; kept empty
folders have Remove folder.
- Recent spreads at the top of the switcher.
- List view's "folders" are called groups in the UI (+ Group).
- TestBase gets the shared _wipe helper.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.4 KiB
GDScript
174 lines
6.4 KiB
GDScript
class_name CollectionSwitcher
|
|
extends MenuButton
|
|
## Top-bar dropdown that names the current spread and offers switching, New,
|
|
## Rename, Duplicate, Move to folder and Delete. Recent spreads come first,
|
|
## then spreads with no folder, then the folders as submenus (folders can hold
|
|
## folders). The storage rules are static functions (create_collection and
|
|
## friends; folders in core/spread_folders.gd) so tests can run them on a
|
|
## temporary LibraryStore; Main does the UI side (rebuilding cards, dialogs).
|
|
|
|
const MAX_LABEL := 22
|
|
const MARK := "● "
|
|
const NO_MARK := " "
|
|
|
|
var main: Main
|
|
## What each menu item does; an item's id is its index here.
|
|
var _calls: Array[Callable] = []
|
|
|
|
|
|
func setup(p_main: Main) -> void:
|
|
main = p_main
|
|
Style.menu_button(text, false, self)
|
|
add_theme_color_override("font_disabled_color", Style.ON_BG)
|
|
add_theme_font_override("font", Style.body_bold())
|
|
tooltip_text = "Spreads"
|
|
ModalCard.style_popup(get_popup())
|
|
get_popup().about_to_popup.connect(_fill)
|
|
get_popup().id_pressed.connect(_on_id)
|
|
refresh()
|
|
|
|
|
|
func refresh() -> void:
|
|
var n := main.data.name if main.data.name != "" else "Untitled"
|
|
text = "%s ▾" % (n if n.length() <= MAX_LABEL else n.substr(0, MAX_LABEL - 1) + "…")
|
|
var where := " (in %s)" % main.data.folder.replace("/", " ▸ ") if main.data.folder != "" else ""
|
|
tooltip_text = "%s%s · %d cards\nSwitch spreads, or make, rename, copy, sort into folders or delete one" % [n, where, main.data.tables.size()]
|
|
|
|
|
|
func _fill() -> void:
|
|
var pm := get_popup()
|
|
pm.clear()
|
|
_calls.clear()
|
|
for child in pm.get_children():
|
|
if child is PopupMenu:
|
|
pm.remove_child(child)
|
|
child.queue_free()
|
|
var can := main.collections_enabled()
|
|
var folders: Array[String] = []
|
|
if can:
|
|
var entries := main.library.list()
|
|
folders = SpreadFolders.all_folders(entries, SpreadFolders.kept(main.state))
|
|
var recent := RecentSpreads.pick(main.state, entries, main.data.id)
|
|
if not folders.is_empty() and not recent.is_empty():
|
|
pm.add_separator("Recent")
|
|
for entry in recent:
|
|
_add_spread(pm, entry)
|
|
pm.add_separator()
|
|
for entry in SpreadFolders.spreads_in(entries, ""):
|
|
_add_spread(pm, entry)
|
|
for f in SpreadFolders.children(folders, ""):
|
|
_add_folder(pm, f, folders, entries)
|
|
else:
|
|
_item(pm, MARK + main.data.name, Callable(), true)
|
|
pm.add_separator()
|
|
_item(pm, "New spread…", main.new_collection, not can)
|
|
_item(pm, "Rename…", main.rename_collection, not can)
|
|
_item(pm, "Duplicate", main.duplicate_collection, not can)
|
|
if can:
|
|
pm.add_submenu_node_item("Move to folder", _move_menu(folders))
|
|
else:
|
|
_item(pm, "Move to folder", Callable(), true)
|
|
_item(pm, "Delete…", main.delete_collection, not can)
|
|
if not can:
|
|
pm.add_separator()
|
|
_item(pm, "Saving is off in this session", Callable(), true)
|
|
|
|
|
|
func _on_id(id: int) -> void:
|
|
if id >= 0 and id < _calls.size() and _calls[id].is_valid():
|
|
_calls[id].call()
|
|
|
|
|
|
## Adds an item that runs `call` when picked.
|
|
func _item(menu: PopupMenu, label: String, call: Callable, disabled: bool = false) -> void:
|
|
menu.add_item(label, _calls.size())
|
|
menu.set_item_disabled(menu.item_count - 1, disabled)
|
|
_calls.append(call)
|
|
|
|
|
|
func _sub() -> PopupMenu:
|
|
var sub := PopupMenu.new()
|
|
ModalCard.style_popup(sub)
|
|
sub.id_pressed.connect(_on_id)
|
|
return sub
|
|
|
|
|
|
func _add_spread(menu: PopupMenu, entry: Dictionary) -> void:
|
|
var id := str(entry["id"])
|
|
_item(menu, (MARK if id == main.data.id else NO_MARK) + str(entry["name"]), main.switch_collection.bind(id))
|
|
|
|
|
|
## A folder's submenu: its inner folders, its spreads, then folder actions.
|
|
func _add_folder(menu: PopupMenu, folder: String, folders: Array[String], entries: Array) -> void:
|
|
var sub := _sub()
|
|
for f in SpreadFolders.children(folders, folder):
|
|
_add_folder(sub, f, folders, entries)
|
|
for entry in SpreadFolders.spreads_in(entries, folder):
|
|
_add_spread(sub, entry)
|
|
var empty := SpreadFolders.is_empty(folder, entries, SpreadFolders.kept(main.state))
|
|
if empty:
|
|
_item(sub, NO_MARK + "(empty)", Callable(), true)
|
|
sub.add_separator()
|
|
_item(sub, "New spread here…", main.new_collection.bind(folder))
|
|
_item(sub, "Rename folder…", SpreadFolderDialogs.ask_rename.bind(main, folder))
|
|
if empty:
|
|
_item(sub, "Remove folder", SpreadFolderDialogs.delete_empty.bind(main, folder))
|
|
var here := SpreadFolders.is_inside(main.data.folder, folder)
|
|
menu.add_submenu_node_item((MARK if here else NO_MARK) + SpreadFolders.leaf(folder), sub)
|
|
|
|
|
|
## "Move to folder": No folder, every folder (inner ones indented), New folder.
|
|
func _move_menu(folders: Array[String]) -> PopupMenu:
|
|
var sub := _sub()
|
|
var at := main.data.folder
|
|
_item(sub, (MARK if at == "" else NO_MARK) + "No folder", SpreadFolderDialogs.move_current.bind(main, ""))
|
|
if not folders.is_empty():
|
|
sub.add_separator()
|
|
for f in folders:
|
|
var label := " ".repeat(SpreadFolders.depth(f)) + SpreadFolders.leaf(f)
|
|
_item(sub, (MARK if at == f else NO_MARK) + label, SpreadFolderDialogs.move_current.bind(main, f))
|
|
sub.add_separator()
|
|
_item(sub, "New folder…", SpreadFolderDialogs.ask_new.bind(main))
|
|
return sub
|
|
|
|
|
|
# ---------- Storage rules (no UI) ----------
|
|
|
|
## Saves `current`, then makes and returns a new empty collection.
|
|
static func create_collection(library: LibraryStore, current: TowerBoard, coll_name: String) -> TowerBoard:
|
|
if current:
|
|
library.save(current)
|
|
return library.create(coll_name)
|
|
|
|
|
|
## Saves `current`, then loads another collection. Null if it can't be loaded.
|
|
static func switch_collection(library: LibraryStore, current: TowerBoard, id: String) -> TowerBoard:
|
|
if current:
|
|
library.save(current)
|
|
return library.load_collection(id)
|
|
|
|
|
|
## Renames `current` in memory and on disk. False for a blank name.
|
|
static func rename_collection(library: LibraryStore, current: TowerBoard, new_name: String) -> bool:
|
|
if new_name.strip_edges() == "":
|
|
return false
|
|
current.name = new_name.strip_edges()
|
|
return library.save(current)
|
|
|
|
|
|
## Saves `current` and returns its copy (new id, no local state), or null.
|
|
static func duplicate_collection(library: LibraryStore, current: TowerBoard, new_name: String = "") -> TowerBoard:
|
|
library.save(current)
|
|
return library.duplicate(current.id, new_name)
|
|
|
|
|
|
## Deletes `current` with its local state. Returns the collection to show next:
|
|
## the first remaining one, or a fresh empty one if it was the last.
|
|
static func delete_collection(library: LibraryStore, current: TowerBoard) -> TowerBoard:
|
|
if not library.delete(current.id):
|
|
return null
|
|
for entry in library.list():
|
|
var b := library.load_collection(str(entry["id"]))
|
|
if b:
|
|
return b
|
|
return library.create("Untitled")
|