TableTower/core/recent_spreads.gd
RobinFiredrake 07d96a68f8 Spread folders: pick from a list, nesting, keep empty folders, Recent
- 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>
2026-11-14 19:19:47 -06:00

44 lines
1.2 KiB
GDScript

class_name RecentSpreads
extends RefCounted
## The spreads opened most recently, newest first (a StateStore setting).
## The spread switcher lists them at the top.
const KEY := "recent_spreads"
const CAP := 5
static func ids(state: StateStore) -> Array:
var v: Variant = state.get_setting(KEY, []) if state else []
return (v as Array).duplicate() if v is Array else []
## Puts `id` first.
static func note(state: StateStore, id: String) -> void:
if state == null or id == "":
return
var list := ids(state)
list.erase(id)
list.push_front(id)
if list.size() > CAP:
list.resize(CAP)
state.set_setting(KEY, list)
static func forget(state: StateStore, id: String) -> void:
var list := ids(state)
if list.has(id):
list.erase(id)
state.set_setting(KEY, list)
## The library entries of the recent spreads, newest first, without
## `current_id` and without spreads that no longer exist.
static func pick(state: StateStore, entries: Array, current_id: String) -> Array[Dictionary]:
var by_id := {}
for entry: Dictionary in entries:
by_id[str(entry["id"])] = entry
var out: Array[Dictionary] = []
for id in ids(state):
if str(id) != current_id and by_id.has(str(id)):
out.append(by_id[str(id)])
return out