TableTower/ui/collection_switcher.gd

164 lines
5.7 KiB
GDScript3
Raw Normal View History

class_name CollectionSwitcher
extends MenuButton
## Top-bar dropdown that names the current collection and offers switching,
## New, Rename, Duplicate, Move to folder and Delete. Spreads in a folder are
## listed in that folder's submenu. 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 ID_FOLDER := 1004
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) + "…")
var where := " (in %s)" % main.data.folder 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()
_ids.clear()
var can := main.collections_enabled()
for child in pm.get_children():
if child is PopupMenu:
pm.remove_child(child)
child.queue_free()
if can:
var folders := {}
for entry in main.library.list():
var folder := str(entry.get("folder", ""))
if folder != "":
if not folders.has(folder):
folders[folder] = []
(folders[folder] as Array).append(entry)
continue
_add_spread(pm, entry)
var names: Array = folders.keys()
names.sort_custom(func(left: String, right: String) -> bool: return left.naturalnocasecmp_to(right) < 0)
for folder: String in names:
var sub := PopupMenu.new()
ModalCard.style_popup(sub)
sub.id_pressed.connect(_on_id)
var holds_current := false
for entry: Dictionary in folders[folder]:
_add_spread(sub, entry)
holds_current = holds_current or str(entry["id"]) == main.data.id
pm.add_submenu_node_item(("● " if holds_current else " ") + folder, sub)
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("Move to folder…", ID_FOLDER)
pm.add_item("Delete…", ID_DELETE)
if not can:
for id in [ID_NEW, ID_RENAME, ID_DUPLICATE, ID_FOLDER, 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()
ID_FOLDER: _ask_folder()
_:
if id >= 0 and id < _ids.size() and _ids[id] != main.data.id:
main.switch_collection(_ids[id])
func _add_spread(menu: PopupMenu, entry: Dictionary) -> void:
var id := str(entry["id"])
menu.add_item(("● " if id == main.data.id else " ") + str(entry["name"]), _ids.size())
_ids.append(id)
## Asks for the folder to list the current spread under (empty = none).
func _ask_folder() -> void:
var known := {}
for entry in main.library.list():
if str(entry.get("folder", "")) != "":
known[str(entry["folder"])] = true
var prompt := "Folder (leave empty for none)"
if not known.is_empty():
prompt += ". Folders so far: " + ", ".join(known.keys())
main._add_modal(ModalCard.ask_text("Move to folder", prompt, main.data.folder, "Move", func(folder: String) -> void:
set_folder(main.library, main.data, folder)
refresh(), true))
# ---------- Storage rules (no UI) ----------
## Lists `current` under `folder` ("" = none) and saves it.
static func set_folder(library: LibraryStore, current: TowerBoard, folder: String) -> void:
current.folder = folder.strip_edges()
library.save(current)
## 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")