45 lines
1.2 KiB
GDScript3
45 lines
1.2 KiB
GDScript3
|
|
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
|