2026-11-08 12:23:21 -06:00
|
|
|
class_name CollectionSwitcher
|
|
|
|
|
extends MenuButton
|
2026-11-14 19:19:47 -06:00
|
|
|
## 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).
|
|
|
|
|
|
2026-11-08 12:23:21 -06:00
|
|
|
const MAX_LABEL := 22
|
2026-11-14 19:19:47 -06:00
|
|
|
const MARK := "● "
|
|
|
|
|
const NO_MARK := " "
|
2026-11-08 12:23:21 -06:00
|
|
|
|
|
|
|
|
var main: Main
|
2026-11-14 19:19:47 -06:00
|
|
|
## What each menu item does; an item's id is its index here.
|
|
|
|
|
var _calls: Array[Callable] = []
|
2026-11-08 12:23:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func setup(p_main: Main) -> void:
|
|
|
|
|
main = p_main
|
2026-11-09 08:21:26 -06:00
|
|
|
Style.menu_button(text, false, self)
|
Theme system: runtime colour tokens, theme sets and a Theme dialog
Style's colours become static vars named by role (BG, PANEL, CARD, THREAD,
ACCENT, TRIM, GLOW…) instead of materials (felt, copper, brass), so a theme
can replace them. Stray hard-coded colours moved into tokens.
ThemeStore applies built-in sets (Tarot, the new default: jewel-purple
cloth with a shine; Felt, the old look) and the user's own sets from
user://themes/. Fills for the background and panels can be gradients; the
background pattern (dots, stars, none) and logo are per set. View ▾ →
Theme… edits a set; changes to a built-in save as a new set. Using a theme
reloads the UI and keeps the open spread.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 18:10:16 -06:00
|
|
|
add_theme_color_override("font_disabled_color", Style.ON_BG)
|
2026-11-08 12:23:21 -06:00
|
|
|
add_theme_font_override("font", Style.body_bold())
|
2026-11-11 08:41:30 -06:00
|
|
|
tooltip_text = "Spreads"
|
2026-11-08 12:23:21 -06:00
|
|
|
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) + "…")
|
2026-11-14 19:19:47 -06:00
|
|
|
var where := " (in %s)" % main.data.folder.replace("/", " ▸ ") if main.data.folder != "" else ""
|
2026-11-14 14:35:35 -06:00
|
|
|
tooltip_text = "%s%s · %d cards\nSwitch spreads, or make, rename, copy, sort into folders or delete one" % [n, where, main.data.tables.size()]
|
2026-11-08 12:23:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _fill() -> void:
|
|
|
|
|
var pm := get_popup()
|
|
|
|
|
pm.clear()
|
2026-11-14 19:19:47 -06:00
|
|
|
_calls.clear()
|
2026-11-14 14:35:35 -06:00
|
|
|
for child in pm.get_children():
|
|
|
|
|
if child is PopupMenu:
|
|
|
|
|
pm.remove_child(child)
|
|
|
|
|
child.queue_free()
|
2026-11-14 19:19:47 -06:00
|
|
|
var can := main.collections_enabled()
|
|
|
|
|
var folders: Array[String] = []
|
2026-11-08 12:23:21 -06:00
|
|
|
if can:
|
2026-11-14 19:19:47 -06:00
|
|
|
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, ""):
|
2026-11-14 14:35:35 -06:00
|
|
|
_add_spread(pm, entry)
|
2026-11-14 19:19:47 -06:00
|
|
|
for f in SpreadFolders.children(folders, ""):
|
|
|
|
|
_add_folder(pm, f, folders, entries)
|
2026-11-08 12:23:21 -06:00
|
|
|
else:
|
2026-11-14 19:19:47 -06:00
|
|
|
_item(pm, MARK + main.data.name, Callable(), true)
|
2026-11-08 12:23:21 -06:00
|
|
|
pm.add_separator()
|
2026-11-14 19:19:47 -06:00
|
|
|
_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)
|
2026-11-08 12:23:21 -06:00
|
|
|
if not can:
|
|
|
|
|
pm.add_separator()
|
2026-11-14 19:19:47 -06:00
|
|
|
_item(pm, "Saving is off in this session", Callable(), true)
|
2026-11-08 12:23:21 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_id(id: int) -> void:
|
2026-11-14 19:19:47 -06:00
|
|
|
if id >= 0 and id < _calls.size() and _calls[id].is_valid():
|
|
|
|
|
_calls[id].call()
|
2026-11-08 12:23:21 -06:00
|
|
|
|
|
|
|
|
|
2026-11-14 19:19:47 -06:00
|
|
|
## 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)
|
2026-11-14 14:35:35 -06:00
|
|
|
|
|
|
|
|
|
2026-11-14 19:19:47 -06:00
|
|
|
func _sub() -> PopupMenu:
|
|
|
|
|
var sub := PopupMenu.new()
|
|
|
|
|
ModalCard.style_popup(sub)
|
|
|
|
|
sub.id_pressed.connect(_on_id)
|
|
|
|
|
return sub
|
2026-11-14 14:35:35 -06:00
|
|
|
|
|
|
|
|
|
2026-11-14 19:19:47 -06:00
|
|
|
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
|
|
|
|
|
|
2026-11-08 12:23:21 -06:00
|
|
|
|
2026-11-14 19:19:47 -06:00
|
|
|
# ---------- Storage rules (no UI) ----------
|
2026-11-14 14:35:35 -06:00
|
|
|
|
2026-11-08 12:23:21 -06:00
|
|
|
## 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")
|