2026-11-08 12:23:21 -06:00
|
|
|
class_name CollectionSwitcher
|
|
|
|
|
extends MenuButton
|
|
|
|
|
## Top-bar dropdown that names the current collection and offers switching,
|
|
|
|
|
## New, Rename, Duplicate and Delete. The storage rules are static functions
|
|
|
|
|
## (create_collection and friends) so tests can run them on a temporary
|
|
|
|
|
## LibraryStore; Main does the UI side (rebuilding cards, dialogs).
|
|
|
|
|
|
|
|
|
|
const ID_NEW := 1000
|
|
|
|
|
const ID_RENAME := 1001
|
|
|
|
|
const ID_DUPLICATE := 1002
|
|
|
|
|
const ID_DELETE := 1003
|
|
|
|
|
const MAX_LABEL := 22
|
|
|
|
|
|
|
|
|
|
var main: Main
|
|
|
|
|
var _ids: Array[String] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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())
|
|
|
|
|
tooltip_text = "Collections"
|
|
|
|
|
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) + "…")
|
|
|
|
|
tooltip_text = "Collection: %s" % n
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _fill() -> void:
|
|
|
|
|
var pm := get_popup()
|
|
|
|
|
pm.clear()
|
|
|
|
|
_ids.clear()
|
|
|
|
|
var can := main.collections_enabled()
|
|
|
|
|
if can:
|
|
|
|
|
for entry in main.library.list():
|
|
|
|
|
var id := str(entry["id"])
|
|
|
|
|
var current := id == main.data.id
|
|
|
|
|
pm.add_item(("● " if current else " ") + str(entry["name"]), _ids.size())
|
|
|
|
|
_ids.append(id)
|
|
|
|
|
else:
|
|
|
|
|
pm.add_item("● " + main.data.name, 0)
|
|
|
|
|
pm.set_item_disabled(0, true)
|
|
|
|
|
_ids.append(main.data.id)
|
|
|
|
|
pm.add_separator()
|
|
|
|
|
pm.add_item("New collection…", ID_NEW)
|
|
|
|
|
pm.add_item("Rename…", ID_RENAME)
|
|
|
|
|
pm.add_item("Duplicate", ID_DUPLICATE)
|
|
|
|
|
pm.add_item("Delete…", ID_DELETE)
|
|
|
|
|
if not can:
|
|
|
|
|
for id in [ID_NEW, ID_RENAME, ID_DUPLICATE, ID_DELETE]:
|
|
|
|
|
pm.set_item_disabled(pm.get_item_index(id), true)
|
|
|
|
|
pm.add_separator()
|
|
|
|
|
pm.add_item("Saving is off in this session", -1)
|
|
|
|
|
pm.set_item_disabled(pm.item_count - 1, true)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _on_id(id: int) -> void:
|
|
|
|
|
match id:
|
|
|
|
|
ID_NEW: main.new_collection()
|
|
|
|
|
ID_RENAME: main.rename_collection()
|
|
|
|
|
ID_DUPLICATE: main.duplicate_collection()
|
|
|
|
|
ID_DELETE: main.delete_collection()
|
|
|
|
|
_:
|
|
|
|
|
if id >= 0 and id < _ids.size() and _ids[id] != main.data.id:
|
|
|
|
|
main.switch_collection(_ids[id])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------- 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")
|