feat: navigation

This commit is contained in:
2026-04-01 16:04:06 +02:00
parent a0a5f18128
commit a95594f446
7 changed files with 312 additions and 6 deletions

View File

@@ -19,7 +19,7 @@ from uframe.ir import (
Heading, Text, Label, Divider, Link, ListNode, ListItem,
Gauge, Sparkline, Status, Table,
Form, Field, Password, Radio, Checkbox, FormButton,
BigTitle, ImageNode,
BigTitle, ImageNode, HNav, VNav,
HeadingLevel, DividerStyle, ListStyle, Align, BorderWeight,
)
from uframe.themes import ThemeDef, THEME_DEFAULT
@@ -223,6 +223,12 @@ def paint(node: IRNode, grid: CharGrid, theme: ThemeDef | None = None) -> None:
grid.put(x, y, char, style=CellStyle(fg=color))
grid.put_text(x + 2, y, node.label)
elif isinstance(node, HNav):
_paint_hnav(node, grid, x, y, w, th)
elif isinstance(node, VNav):
_paint_vnav(node, grid, x, y, w, th)
elif isinstance(node, ImageNode):
style = CellStyle(fg=node.style.fg or th.palette.accent)
try:
@@ -346,6 +352,153 @@ def paint(node: IRNode, grid: CharGrid, theme: ThemeDef | None = None) -> None:
paint(child, grid, th)
def _paint_hnav(node: HNav, grid: CharGrid, x: int, y: int, w: int, th: ThemeDef) -> None:
"""Paint a horizontal navigation bar."""
active_style = CellStyle(bold=True, fg=th.palette.accent)
link_style = CellStyle(fg=th.palette.info)
sep_style = CellStyle(fg=th.palette.muted)
border_style = CellStyle()
items = [it for it in node.items if it.kind in ("item", "separator")]
marker = ""
if node.nav_style in ("bar", "tabs", "pills"):
# Bordered bar
bc = th.border_dict("light")
grid.draw_border(x, y, w, 3, border_chars=bc,
title_caps=(th.title_caps.left, th.title_caps.right))
col = x + 2
for it in items:
if it.kind == "separator":
grid.put(col, y + 1, "", style=sep_style)
col += 2
continue
if it.active:
grid.put_text(col, y + 1, marker, style=active_style)
col += len(marker)
grid.put_text(col, y + 1, it.label, style=active_style)
else:
grid.put_text(col, y + 1, it.label, style=link_style, link=it.dest)
col += len(it.label) + 2
if col < x + w - 2:
grid.put(col, y + 1, "", style=sep_style)
col += 2
elif node.nav_style == "breadcrumb":
col = x + 2
sep = ""
for i, it in enumerate(items):
if it.kind == "separator":
continue
if i > 0:
grid.put_text(col, y, sep, style=sep_style)
col += len(sep)
if it.active:
grid.put_text(col, y, it.label, style=active_style)
else:
grid.put_text(col, y, it.label, style=link_style, link=it.dest)
col += len(it.label)
elif node.nav_style == "underline":
col = x + 2
active_start = 0
active_len = 0
for i, it in enumerate(items):
if it.kind == "separator":
continue
if it.active:
active_start = col
active_len = len(it.label)
grid.put_text(col, y, it.label, style=active_style)
else:
grid.put_text(col, y, it.label, style=link_style, link=it.dest)
col += len(it.label) + 5
# Underline beneath active
if active_len > 0:
for c in range(active_start, active_start + active_len):
grid.put(c, y + 1, "", style=CellStyle(fg=th.palette.accent))
def _paint_vnav(node: VNav, grid: CharGrid, x: int, y: int, w: int, th: ThemeDef) -> None:
"""Paint a vertical navigation panel."""
active_style = CellStyle(bold=True, fg=th.palette.accent)
link_style = CellStyle(fg=th.palette.info)
heading_style = CellStyle(bold=True, fg=th.palette.muted)
sep_style = CellStyle(fg=th.palette.muted)
marker = ""
items = node.items
row_y = y
if node.nav_style == "boxed":
# Draw a box and render items inside
bc = th.border_dict("light")
h = node.rect.h
grid.draw_border(x, y, w, h, border_chars=bc,
title_caps=(th.title_caps.left, th.title_caps.right))
row_y = y + 1
for it in items:
if it.kind == "separator":
# Draw internal separator
for c in range(x + 1, x + w - 1):
grid.put(c, row_y, bc.get("h", ""), style=sep_style,
is_border=True)
grid.put(x, row_y, "", style=sep_style, is_border=True)
grid.put(x + w - 1, row_y, "", style=sep_style, is_border=True)
row_y += 1
elif it.kind == "heading":
grid.put_text(x + 2, row_y, it.label.upper(), style=heading_style)
row_y += 1
elif it.kind == "item":
if it.active:
grid.put_text(x + 2, row_y, marker, style=active_style)
grid.put_text(x + 2 + len(marker), row_y, it.label, style=active_style)
else:
grid.put_text(x + 4, row_y, it.label, style=link_style, link=it.dest)
row_y += 1
elif node.nav_style == "tree":
for idx, it in enumerate(items):
if it.kind == "separator":
for c in range(x, x + w):
grid.put(c, row_y, "", style=sep_style)
row_y += 1
elif it.kind == "heading":
grid.put_text(x, row_y, it.label, style=heading_style)
row_y += 1
elif it.kind == "item":
# Determine connector
remaining = [i for i in items[idx+1:] if i.kind == "item"]
connector = "└── " if not remaining else "├── "
grid.put_text(x, row_y, connector, style=sep_style)
if it.active:
grid.put_text(x + len(connector), row_y, it.label, style=active_style)
grid.put_text(x + len(connector) + len(it.label) + 2, row_y, "",
style=CellStyle(fg=th.palette.accent))
else:
grid.put_text(x + len(connector), row_y, it.label,
style=link_style, link=it.dest)
row_y += 1
else:
# list, sidebar, minimal
for it in items:
if it.kind == "separator":
for c in range(x, min(x + w, x + 16)):
grid.put(c, row_y, "", style=sep_style)
row_y += 1
elif it.kind == "heading":
grid.put_text(x, row_y, it.label.upper(), style=heading_style)
row_y += 1
elif it.kind == "item":
if it.active:
grid.put_text(x, row_y, marker, style=active_style)
grid.put_text(x + len(marker), row_y, it.label, style=active_style)
else:
grid.put_text(x + 2, row_y, it.label, style=link_style, link=it.dest)
row_y += 1
def _paint_table(node: Table, grid: CharGrid, x: int, y: int, w: int) -> None:
"""Paint a box-drawn table with header and data rows."""
if not node.columns: