Nested groups: groups can hold groups

TableGroup.parent (JSON "parent") puts a group inside another. Grouping a
selection that includes groups nests them (the new group goes in the group
they all share). A collapsed group hides every card and group inside it and
stands in for them on threads; moving, deleting, search, focus (opens every
collapsed group around a card), packing, undo and templates all work
through the nesting. Ungroup hands the contents to the outer group.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
RobinFiredrake 2026-11-10 18:47:05 -06:00
parent 1199fafcf9
commit 5d427d3856
15 changed files with 292 additions and 62 deletions

View file

@ -23,6 +23,8 @@ var collapsed: bool = false
var details: String = ""
## Ids of the member tables, in the order they were added.
var members: Array[String] = []
## The group this one sits inside ("" = on the spread itself).
var parent: String = ""
func _init(p_id: String = "", p_name: String = "") -> void:
@ -69,6 +71,8 @@ func to_dict() -> Dictionary:
d["collapsed"] = true
if details != "":
d["details"] = details
if parent != "":
d["parent"] = parent
return d
@ -78,6 +82,7 @@ static func from_dict(d: Dictionary) -> TableGroup:
g.size = Vector2(maxf(80.0, float(d.get("w", 320))), maxf(TITLE_H, float(d.get("h", 200))))
g.collapsed = bool(d.get("collapsed", false))
g.details = str(d.get("details", ""))
g.parent = str(d.get("parent", ""))
var src: Variant = d.get("members", [])
if src is Array:
for m: Variant in src:

View file

@ -36,11 +36,12 @@ static func make(board: TowerBoard, table_ids: Array, kind: String, tname: Strin
want[str(tid)] = true
var groups: Array[TableGroup] = []
for gid: Variant in group_ids:
var g := board.get_group(str(gid))
if g:
groups.append(g)
for m in g.members:
want[m] = true
# A group brings its inner groups along.
for g in board.groups_in(str(gid)):
if not groups.has(g):
groups.append(g)
for m in g.members:
want[m] = true
# The top-left corner: the tables' corner, or a group frame's when it's further out.
var origin := Vector2(INF, INF)
for t in board.tables:
@ -89,6 +90,11 @@ static func make(board: TowerBoard, table_ids: Array, kind: String, tname: Strin
members.append(m)
gd["members"] = members
gd.erase("id")
# Inner groups point at their outer group by its place in this list.
gd.erase("parent")
var outer := board.get_group(g.parent)
if outer and groups.has(outer):
gd["parent_at"] = groups.find(outer)
group_list.append(gd)
var tpl := {
"format_version": FORMAT_VERSION,
@ -279,7 +285,13 @@ static func instantiate(tpl: Dictionary, values: Dictionary, board: TowerBoard,
if id_map.has(m):
members.append(id_map[m])
g.members = members
g.parent = ""
groups.append(g)
var gsrc: Array = src["groups"].filter(func(v: Variant) -> bool: return v is Dictionary)
for i in mini(gsrc.size(), groups.size()):
var at := int((gsrc[i] as Dictionary).get("parent_at", -1))
if at >= 0 and at < groups.size() and at != i:
groups[i].parent = groups[at].id
return {"tables": tables, "groups": groups, "relinked": relinked, "dropped": dropped}

View file

@ -62,10 +62,85 @@ func group_of(table_id: String) -> TableGroup:
return null
## True when the table is hidden inside a collapsed group.
## True when the table is hidden inside a collapsed group (at any depth).
func is_hidden(table_id: String) -> bool:
var g := group_of(table_id)
return g != null and g.collapsed
while g != null:
if g.collapsed:
return true
g = get_group(g.parent)
return false
# ----- Nested groups -----
func child_groups(gid: String) -> Array[TableGroup]:
var out: Array[TableGroup] = []
for g in groups:
if g.parent == gid and gid != "":
out.append(g)
return out
## Every table inside a group, its inner groups' tables included.
func tables_in(gid: String) -> Array[String]:
var out: Array[String] = []
var g := get_group(gid)
if g == null:
return out
out.append_array(g.members)
for c in child_groups(gid):
out.append_array(tables_in(c.id))
return out
## The group and every group inside it, outermost first.
func groups_in(gid: String) -> Array[TableGroup]:
var out: Array[TableGroup] = []
var g := get_group(gid)
if g == null:
return out
out.append(g)
for c in child_groups(gid):
out.append_array(groups_in(c.id))
return out
## The outermost collapsed group around `g` (not `g` itself), or null when
## nothing around it is collapsed.
func hidden_by(g: TableGroup) -> TableGroup:
var out: TableGroup = null
var p := get_group(g.parent)
while p != null:
if p.collapsed:
out = p
p = get_group(p.parent)
return out
## How many groups `g` sits inside.
func depth_of(g: TableGroup) -> int:
var n := 0
var p := get_group(g.parent)
while p != null and n < groups.size():
n += 1
p = get_group(p.parent)
return n
## Puts group `gid` inside `parent_id` ("" = the spread). False when that would
## put a group inside itself.
func set_group_parent(gid: String, parent_id: String) -> bool:
var g := get_group(gid)
if g == null:
return false
var p := get_group(parent_id)
while p != null:
if p.id == gid:
return false
p = get_group(p.parent)
g.parent = parent_id
return true
## Makes a group of the given tables (taking them out of any other group).
@ -97,36 +172,46 @@ func remove_from_group(table_id: String) -> void:
g.members.erase(table_id)
## Removes the group only; its cards stay on the board.
## Removes the group only; its cards and inner groups move up to the group
## around it (or onto the spread).
func ungroup(gid: String) -> void:
var g := get_group(gid)
if g == null:
return
var outer := get_group(g.parent)
for c in child_groups(gid):
c.parent = g.parent
if outer:
for tid in g.members:
if not outer.members.has(tid):
outer.members.append(tid)
for i in range(groups.size() - 1, -1, -1):
if groups[i].id == gid:
groups.remove_at(i)
## Removes the group and every table in it. Returns the removed table ids.
## Removes the group, its inner groups and every table in them. Returns the
## removed table ids.
func delete_group(gid: String) -> Array[String]:
var g := get_group(gid)
var gone: Array[String] = []
if g == null:
if get_group(gid) == null:
return gone
gone.assign(g.members.duplicate())
ungroup(gid)
gone.assign(tables_in(gid))
for inner in groups_in(gid):
groups.erase(inner)
for tid in gone:
remove_table(tid)
return gone
## Moves a group's frame and every member table by `delta`.
## Moves a group's frame, its inner groups and every table in them by `delta`.
func move_group(gid: String, delta: Vector2) -> void:
var g := get_group(gid)
if g == null:
return
g.position += delta
for tid in g.members:
var t := get_table(tid)
if t:
t.position += delta
for g in groups_in(gid):
g.position += delta
for tid in g.members:
var t := get_table(tid)
if t:
t.position += delta
## Drops members that aren't tables, keeps each table in its first group only,
@ -147,6 +232,19 @@ func clean_groups() -> void:
taken[m] = true
keep.append(m)
g.members = keep
# Parents must exist and never loop back.
for g in groups:
if g.parent != "" and (not gids.has(g.parent) or g.parent == g.id):
g.parent = ""
for g in groups:
var seen := {g.id: true}
var p := get_group(g.parent)
while p != null:
if seen.has(p.id):
g.parent = ""
break
seen[p.id] = true
p = get_group(p.parent)
## Upgrades a parsed file to FORMAT_VERSION, one version step at a time.

View file

@ -41,7 +41,7 @@ func _test_lists() -> void:
var build := _texts(Actions.card(main, t))
_check(build.has("Table settings…") and build.has("Colour") and build.has("Delete table"), "Build card menu: %s" % str(build))
bg = _texts(Actions.background(main, Vector2.ZERO))
_check(bg.has("New card") and bg.has("Group selected cards Ctrl+G"), "Build spread menu: %s" % str(bg))
_check(bg.has("New card") and bg.has("Group the selection Ctrl+G"), "Build spread menu: %s" % str(bg))
var key := ""
for e in t.entries:
if not e.pipes.is_empty():

View file

@ -31,6 +31,7 @@ func _init() -> void:
await _test_lit_and_choice()
await _test_move()
await _test_box_select()
await _test_nested()
await _test_drop_in_and_out()
await _test_collapse_all_and_fit()
await _test_ungroup_and_delete()
@ -367,6 +368,46 @@ func _test_box_select() -> void:
await process_frame
## A group inside a group: grouping a selected group, collapsing the outer one
## hides everything, moving moves all, ungrouping hands things to the outer group.
func _test_nested() -> void:
var inner := _group()
main.clear_selection()
main.selecting.set_group(inner.id, true)
main.editing.toggle_selected("loose")
var outer := main.grouping.group_selection()
await _frames(3)
_check(outer != null and inner.parent == outer.id and outer.members == ["loose"], "grouping a group puts it inside the new one")
_check(main.data.tables_in(outer.id).size() == inner.members.size() + 1, "the outer group holds every card inside")
if not outer.collapsed:
main.toggle_group(outer.id)
await _frames(3)
_check(main.group_node(inner.id) == null and main.card_for("a") == null and main.card_for("loose") == null, "collapsing the outer group hides the inner group and all cards")
_check(main.hidden_groups().get("a") == main.group_node(outer.id), "the outer group stands in for cards deep inside")
var tpl := TemplateKit.make(main.data, [], "group", "Nest", "", [outer.id])
_check((tpl["groups"] as Array).size() == 2 and (tpl["groups"][1] as Dictionary).get("parent_at") == 0, "a group template keeps the inner group")
var r := TemplateKit.instantiate(tpl, {}, main.data, Vector2(3000, 0))
_check((r["groups"][1] as TableGroup).parent == (r["groups"][0] as TableGroup).id, "and inserting it nests again")
main.toggle_group(outer.id)
await _frames(3)
var before := inner.position
main.move_group(outer.id, Vector2(10, 0))
_check(inner.position == before + Vector2(10, 0), "moving the outer group moves the inner one")
main.move_group(outer.id, Vector2(-10, 0))
main.ungroup(outer.id)
await _frames(3)
_check(inner.parent == "" and main.data.group_of("loose") == null, "ungrouping hands the contents back to the spread")
var d := main.data.to_dict()
var back := TowerBoard.new()
back.load_dict(d)
_check(back.groups.size() == main.data.groups.size(), "groups survive saving")
func _frames(n: int = 2) -> void:
for i in n:
await process_frame
func _shown(tid: String) -> bool:
var c := main.card_for(tid)
return c != null and c.visible

View file

@ -157,7 +157,7 @@ static func background(m: Main, at: Vector2) -> Array:
list.append({"text": "New card", "sub": kinds})
list.append({"text": "New card from template…", "do": m.templating.open_template_picker})
var picked := m.selection.size()
list.append({"text": "Group selected cards Ctrl+G", "do": m.grouping.group_selection, "disabled": picked == 0,
list.append({"text": "Group the selection Ctrl+G", "do": m.grouping.group_selection, "disabled": picked == 0 and m.selecting.groups.is_empty(),
"tip": "Select cards first: drag a box around them"})
list.append({"sep": ""})
list.append({"text": "Search… Ctrl+F", "do": m.nav.open_search})

View file

@ -85,8 +85,9 @@ func build(board: TowerBoard) -> void:
_groups.clear()
for g in board.groups:
var gname := plain(g.name)
for tid in g.members:
_groups[tid] = gname
for tid in board.tables_in(g.id):
if not _groups.has(tid) or g.parent == "":
_groups[tid] = gname
_add("", "", g.id, FIELD_GROUP, gname)
_add("", "", g.id, FIELD_DETAILS, plain(g.details))
for t in board.tables:

View file

@ -300,7 +300,7 @@ static func patch(board: TowerBoard, snap: Dictionary, from: Dictionary) -> Dict
elif old_g.get(g.id) != gstrs[i]:
var fresh := TableGroup.from_dict(JSON.parse_string(gstrs[i]))
fresh.collapsed = g.collapsed
if fresh.name != g.name or fresh.details != g.details or fresh.members != g.members:
if fresh.name != g.name or fresh.details != g.details or fresh.members != g.members or fresh.parent != g.parent:
regroup = true
_copy_fields(fresh, g)
gupdated.append(g.id)

View file

@ -179,8 +179,8 @@ func _build_frame() -> void:
func count_text() -> String:
var n := group.members.size()
return "%d %s" % [n, "card" if n == 1 else "cards"]
var text := MainSelection.describe_counts(main.data.tables_in(group.id).size(), main.data.groups_in(group.id).size() - 1)
return text if text != "" else "0 cards"
func set_drop_target(on: bool) -> void:
@ -273,13 +273,14 @@ func _build_compact() -> void:
list.add_child(_lit_row(key))
if shown.is_empty():
var names := PackedStringArray()
for tid in group.members.slice(0, 3):
var inside := main.data.tables_in(group.id)
for tid in inside.slice(0, 3):
var t := main.data.get_table(tid)
if t:
names.append(Formatter.strip_tags(t.name))
var txt := ", ".join(names)
if group.members.size() > 3:
txt += " and %d more" % (group.members.size() - 3)
if inside.size() > 3:
txt += " and %d more" % (inside.size() - 3)
var note := Style.make_label(txt if txt != "" else "No cards yet", Style.body_italic(true), 14, Style.MUTED)
note.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
var nm := MarginContainer.new()
@ -304,7 +305,7 @@ func _stack_badge() -> Control:
var front := Style.bordered(Style.box(Style.BG, 3), 1, Style.BG_DEEP)
c.draw_style_box(back, Rect2(8, 2, 18, 22))
c.draw_style_box(front, Rect2(3, 6, 18, 22))
var s := str(group.members.size())
var s := str(main.data.tables_in(group.id).size())
var f := Style.display(true)
var w := f.get_string_size(s, HORIZONTAL_ALIGNMENT_LEFT, -1, 14).x
c.draw_string(f, Vector2(12.0 - w / 2.0, 22), s, HORIZONTAL_ALIGNMENT_LEFT, -1, 14, Style.ACCENT)

View file

@ -21,9 +21,13 @@ func _process(_delta: float) -> void:
refit()
## Fits every expanded, unfrozen group's frame around its visible members.
## Fits every expanded, unfrozen group's frame around its visible members and
## inner groups, innermost first so outer frames fit the inner ones' new size.
func refit() -> void:
for g in main.data.groups:
var order := main.data.groups.duplicate()
if order.size() > 1:
order.sort_custom(func(a: TableGroup, b: TableGroup) -> bool: return main.data.depth_of(a) > main.data.depth_of(b))
for g: TableGroup in order:
if g.collapsed or frozen.has(g.id):
continue
var rects: Array = []
@ -31,6 +35,10 @@ func refit() -> void:
var c := main.card_for(tid)
if c and c.visible and c.size.x > 0.0:
rects.append(Rect2(c.position, c.size))
for inner in main.data.child_groups(g.id):
var n := main.group_node(inner.id)
if n and n.visible:
rects.append(Rect2(n.position, n.size) if inner.collapsed else inner.rect())
if g.fit_to(rects):
var n := main.group_node(g.id)
if n:

View file

@ -44,10 +44,10 @@ func _make_add_menu() -> MenuButton:
pm.add_item(str(ADD_KINDS[i][0]), i)
pm.add_separator("Groups and templates")
pm.add_item("From template…", MENU_FROM_TEMPLATE)
pm.add_item("Group selected cards Ctrl+G", MENU_GROUP)
pm.add_item("Group the selection Ctrl+G", MENU_GROUP)
pm.add_item("Save selection as template…", MENU_SAVE_SELECTION)
pm.about_to_popup.connect(func() -> void:
pm.set_item_disabled(pm.get_item_index(MENU_GROUP), m.selection.is_empty())
pm.set_item_disabled(pm.get_item_index(MENU_GROUP), m.selection.is_empty() and m.selecting.groups.is_empty())
pm.set_item_disabled(pm.get_item_index(MENU_SAVE_SELECTION), m.selection.is_empty())
)
pm.id_pressed.connect(func(id: int) -> void:

View file

@ -31,10 +31,11 @@ func hidden_groups() -> Dictionary:
if _hidden_cache == null:
var out := {}
if m.data:
# The outermost collapsed group stands in for everything inside it.
for g in m.data.groups:
var n := group_node(g.id)
if g.collapsed and n:
for tid in g.members:
if g.collapsed and n and m.data.hidden_by(g) == null:
for tid in m.data.tables_in(g.id):
out[tid] = n
_hidden_cache = out
return _hidden_cache
@ -52,7 +53,13 @@ func _rebuild_groups() -> void:
_hidden_cache = null
if m.data == null or m.board == null:
return
for g in m.data.groups:
# Outer frames first, so inner frames draw on top of them. Groups inside a
# collapsed group get no node.
var order := m.data.groups.duplicate()
order.sort_custom(func(a: TableGroup, b: TableGroup) -> bool: return m.data.depth_of(a) < m.data.depth_of(b))
for g: TableGroup in order:
if m.data.hidden_by(g) != null:
continue
var n := GroupCard.new()
if g.collapsed:
m.board.cards.add_child(n)
@ -103,7 +110,7 @@ func _apply_group_visibility() -> void:
## "tid:eid" keys of a group's lit results, in roll order.
func lit_keys_for_group(g: TableGroup) -> Array:
var members := {}
for tid in g.members:
for tid in m.data.tables_in(g.id):
members[tid] = true
var out: Array = []
for k: String in m.lit_texts:
@ -153,13 +160,14 @@ func move_group(gid: String, delta: Vector2) -> void:
if g == null:
return
m.data.move_group(gid, delta)
for tid in g.members:
for tid in m.data.tables_in(gid):
var c := m.card_for(tid)
if c:
c.position = m.data.get_table(tid).position
var n := group_node(gid)
if n:
n.sync_rect()
for inner in m.data.groups_in(gid):
var n := group_node(inner.id)
if n:
n.sync_rect()
## "Group 1", "Group 2"… whichever isn't used.
@ -179,24 +187,49 @@ func group_selection() -> TableGroup:
for t in m.data.tables:
if m.selection.has(t.id):
ids.append(t.id)
if ids.is_empty():
m.toast("Select some cards first: Ctrl- or Shift-click their headers, or drag a box around them")
# Selected groups go inside the new group; cards they carry move with them.
var inner: Array[String] = []
for gid: String in m.selecting.groups:
if m.data.get_group(gid) and not m.selecting.covers_group(gid):
inner.append(gid)
ids = ids.filter(func(tid: String) -> bool: return not m.selecting.covers(tid))
if ids.is_empty() and inner.is_empty():
m.toast("Select some cards or groups first: drag a box around them, or Ctrl-click their headers")
return null
m.undo_redo._begin_edit()
var outer := _common_parent(ids, inner)
var g := m.data.add_group(_free_group_name(), ids)
g.parent = outer
var rects: Array = []
for tid in ids:
var c := m.card_for(tid)
rects.append(Rect2(c.position, c.size))
if c:
rects.append(Rect2(c.position, c.size))
for gid in inner:
m.data.set_group_parent(gid, g.id)
var n := group_node(gid)
rects.append(Rect2(n.position, n.size) if n else m.data.get_group(gid).rect())
g.fit_to(rects)
# Groups don't nest: emptied groups stay, so they can be refilled or ungrouped.
# Emptied groups stay, so they can be refilled or ungrouped.
m.clear_selection()
_groups_changed()
m.undo_redo._end_edit()
m.toast("Grouped %d %s as “%s”" % [ids.size(), "card" if ids.size() == 1 else "cards", g.name], true)
m.toast("Grouped %s as “%s”" % [m.selecting.describe_counts(ids.size(), inner.size()), g.name], true)
return g
## The group the new group goes in: the one every picked card and group
## already shares, else the spread itself ("").
func _common_parent(ids: Array[String], inner: Array[String]) -> String:
var parents := {}
for tid in ids:
var g := m.data.group_of(tid)
parents[g.id if g else ""] = true
for gid in inner:
parents[m.data.get_group(gid).parent] = true
return str(parents.keys()[0]) if parents.size() == 1 else ""
## Removes the group; its cards stay where they are.
func ungroup(gid: String) -> void:
var g := m.data.get_group(gid)
@ -213,7 +246,7 @@ func confirm_delete_group(gid: String) -> void:
var g := m.data.get_group(gid)
if g == null:
return
var n := g.members.size()
var n := m.data.tables_in(gid).size()
m._add_modal(ModalCard.confirm("Delete group",
"Delete “%s” and the %d %s in it? Threads from other cards into them are removed too. You can undo this from the message that follows." % [Formatter.strip_tags(g.name), n, "card" if n == 1 else "cards"],
"Delete group and cards", func() -> void: delete_group(gid), true))

View file

@ -54,10 +54,16 @@ func focus_table(table_id: String, entry_id: String = "") -> void:
var notes := PackedStringArray()
var undoable := false
var settle := false
# Open every collapsed group around the card, outermost first.
var around: Array[TableGroup] = []
var g := m.data.group_of(table_id)
if g and g.collapsed:
var info := _expand_group(g)
notes.append("Opened group %s" % Formatter.strip_tags(g.name))
while g != null:
if g.collapsed:
around.push_front(g)
g = m.data.get_group(g.parent)
for outer in around:
var info := _expand_group(outer)
notes.append("Opened group %s" % Formatter.strip_tags(outer.name))
if int(info["moved"]) > 0:
notes.append("moved %d %s to make room" % [info["moved"], "block" if int(info["moved"]) == 1 else "blocks"])
undoable = true
@ -193,7 +199,8 @@ func _layout_blocks(skip: String = "") -> Array:
for g in m.data.groups:
for tid in g.members:
grouped[tid] = true
if g.id == skip:
# Inner groups move with the outermost one.
if g.id == skip or g.parent != "":
continue
var r := g.rect()
if g.collapsed:

View file

@ -44,10 +44,26 @@ func clear() -> void:
set_group(gid, false)
## True when a selected group carries this card (moving the group moves it).
## True when a selected group carries this card, at any depth (moving the
## group moves it).
func covers(tid: String) -> bool:
var g := m.data.group_of(tid)
return g != null and groups.has(g.id)
while g != null:
if groups.has(g.id):
return true
g = m.data.get_group(g.parent)
return false
## True when a selected group carries this group (so moving it would move it twice).
func covers_group(gid: String) -> bool:
var g := m.data.get_group(gid)
var p := m.data.get_group(g.parent) if g else null
while p != null:
if groups.has(p.id):
return true
p = m.data.get_group(p.parent)
return false
## True when dragging this card should move the whole selection.
@ -59,7 +75,8 @@ func moves_together(tid: String) -> bool:
## Moves every selected group by `delta`.
func move_groups(delta: Vector2) -> void:
for gid: String in groups:
m.move_group(gid, delta)
if not covers_group(gid):
m.move_group(gid, delta)
## A group's title was dragged: moves just it, or the whole selection when it's
@ -80,9 +97,13 @@ func drag_group(gid: String, delta: Vector2) -> void:
## "3 cards, 1 group" for the selection bar.
func describe(cards: int) -> String:
return describe_counts(cards, groups.size())
static func describe_counts(cards: int, n_groups: int) -> String:
var parts := PackedStringArray()
if cards > 0:
parts.append("%d %s" % [cards, "card" if cards == 1 else "cards"])
if not groups.is_empty():
parts.append("%d %s" % [groups.size(), "group" if groups.size() == 1 else "groups"])
if n_groups > 0:
parts.append("%d %s" % [n_groups, "group" if n_groups == 1 else "groups"])
return ", ".join(parts)

View file

@ -59,8 +59,8 @@ func store_template(out: Dictionary) -> void:
## The selection as a template; groups whose cards are all selected come along.
func save_selection_as_template() -> void:
if m.selection.is_empty():
m.toast("Select some cards first")
if m.selection.is_empty() and m.selecting.groups.is_empty():
m.toast("Select some cards or groups first")
return
var gids: Array = []
for g in m.data.groups:
@ -73,6 +73,9 @@ func save_selection_as_template() -> void:
break
if all:
gids.append(g.id)
for gid: String in m.selecting.groups:
if not gids.has(gid):
gids.append(gid)
save_template_from(m.selection.keys(), "selection", gids)