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 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) + "…") tooltip_text = "Spread: %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 spread…", 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")