TableTower/ui/sheet_tabs_bar.gd
RobinFiredrake 2661744b06 Sheets phase B: main menu, Sheets list, sheet editor
- Main menu (Spreads, Sheets, Settings, Quit) on the theme background,
  with recent spreads and sheets; logo or ☰ returns to it.
- Sheets: data model, store, templates, snapshot undo; New sheet dialog
  (blank or a template); editor with folder tabs, snap-grid canvas on the
  spread background, field palette, inspector, Play/Build, status strip.
- Field kinds: Number, Text box (BBCode, live {@name}), Heading.
- PaletteList shared by the card and field palettes; shared top bar brand.
- Keys go to the screen that shows; theme reloads keep the screen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 15:53:56 -06:00

113 lines
4.2 KiB
GDScript

class_name SheetTabsBar
extends PanelContainer
## The sheet's own tabs (Main, Spells, Notes…) as folder tabs above the canvas:
## the open one is a card-coloured tab, the others are quiet. In Build,
## "+ Tab" adds one and right-clicking a tab renames, moves or deletes it.
var ed: SheetEditor
var _row: HBoxContainer
func setup(p_editor: SheetEditor) -> void:
ed = p_editor
add_theme_stylebox_override("panel", Style.box(Style.BG_DEEP, 0, [16, 8, 16, 0]))
_row = HBoxContainer.new()
_row.add_theme_constant_override("separation", 4)
add_child(_row)
func rebuild() -> void:
for c in _row.get_children():
_row.remove_child(c)
c.queue_free()
for tab in ed.doc.tabs:
_row.add_child(_tab_button(str(tab["id"]), str(tab["name"]), str(tab["id"]) == ed.tab_id))
if ed.building():
var add := Button.new()
add.text = "+ Tab"
add.name = "AddTab"
add.focus_mode = Control.FOCUS_NONE
add.tooltip_text = "A new tab: its own canvas (right-click a tab to rename, move or delete it)"
var normal := Style.bordered(_top_box(Color(0, 0, 0, 0), [14, 8, 14, 8]), 1, Color(1, 1, 1, 0.25))
normal.border_width_bottom = 0
var hover := Style.bordered(_top_box(Color(1, 1, 1, 0.06), [14, 8, 14, 8]), 1, Color(1, 1, 1, 0.35))
hover.border_width_bottom = 0
Style.skin_button(add, normal, hover, hover, Style.ON_BG_MUTED, Style.ON_BG)
add.pressed.connect(add_tab)
_row.add_child(add)
func _tab_button(id: String, text: String, open: bool) -> Button:
var b := Button.new()
b.name = id
b.text = text
b.focus_mode = Control.FOCUS_NONE
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 14)
var bg := Style.CARD if open else Color(0, 0, 0, 0)
var hover := Style.CARD if open else Color(1, 1, 1, 0.06)
Style.skin_button(b, _top_box(bg, [18, 8, 18, 8]), _top_box(hover, [18, 8, 18, 8]), _top_box(hover, [18, 8, 18, 8]),
Style.INK if open else Style.ON_BG_MUTED, Style.INK if open else Style.ON_BG)
b.pressed.connect(ed.show_tab.bind(id))
b.gui_input.connect(func(ev: InputEvent) -> void: _tab_input(ev, id))
return b
## A box rounded only at the top, like a folder tab.
static func _top_box(color: Color, margins: Array) -> StyleBoxFlat:
var sb := Style.box(color, 8, margins)
sb.corner_radius_bottom_left = 0
sb.corner_radius_bottom_right = 0
return sb
func _tab_input(ev: InputEvent, id: String) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_RIGHT and ed.building():
Actions.popup_at(ed.m, menu_for(id), mb.global_position)
func menu_for(id: String) -> Array:
var i := ed.doc.tabs.find(ed.doc.get_tab(id))
var last := ed.doc.tabs.size() - 1
return [
{"text": "Rename tab…", "do": rename_tab.bind(id)},
{"text": "Move left", "do": move_tab.bind(id, -1), "disabled": i <= 0},
{"text": "Move right", "do": move_tab.bind(id, 1), "disabled": i >= last},
{"text": "Delete tab", "do": delete_tab.bind(id), "disabled": last == 0, "tip": "A sheet keeps at least one tab"},
]
func add_tab() -> void:
var name := "Tab %d" % (ed.doc.tabs.size() + 1)
var made: Array = []
ed.edit("Add tab", func() -> void: made.append(ed.doc.add_tab(name)), "", false)
ed.show_tab(str(made[0]["id"]))
func rename_tab(id: String) -> void:
var tab := ed.doc.get_tab(id)
ed.m._add_modal(ModalCard.ask_text("Rename tab", "Name", str(tab["name"]), "Rename", func(text: String) -> void:
ed.edit("Rename tab", func() -> void: ed.doc.get_tab(id)["name"] = text.strip_edges())))
func move_tab(id: String, step: int) -> void:
ed.edit("Move tab", func() -> void:
var tab := ed.doc.get_tab(id)
var i := ed.doc.tabs.find(tab)
ed.doc.tabs.remove_at(i)
ed.doc.tabs.insert(clampi(i + step, 0, ed.doc.tabs.size()), tab))
func delete_tab(id: String) -> void:
if ed.doc.tabs.size() <= 1:
return
var tab := ed.doc.get_tab(id)
var count := (tab["boxes"] as Array).size()
var go := func() -> void:
ed.edit("Delete tab", func() -> void: ed.doc.tabs.erase(ed.doc.get_tab(id)))
ed.m.toast("Deleted tab “%s” (Undo brings it back)" % tab["name"])
if count == 0:
go.call()
else:
ed.m._add_modal(ModalCard.confirm("Delete tab?", "Delete “%s” and its %d %s? Undo brings it back." % [tab["name"], count, "box" if count == 1 else "boxes"], "Delete", go, true))