"""Declarations landing over safe natural coordinates."""
from __future__ import annotations
from typing import ClassVar, cast, override
from textual.app import App, ComposeResult
from textual.widgets import DataTable, Static
from ..components.widgets import ContentDataTable, ContentScroll
from .controller import (
DeclarationsWorkspaceController,
DeclarationsWorkspaceScreen,
declarations_copy,
natural_address,
work_state_label,
)
[docs]
class DeclarationsOverviewScreen(DeclarationsWorkspaceScreen):
"""List local declaration facts without implying filing or AEAT state."""
IS_WORKSPACE_OVERVIEW: ClassVar[bool] = True
def __init__(
self,
controller: DeclarationsWorkspaceController,
*,
id: str = "declarations-overview-screen",
) -> None:
"""Retain injected state and semantic selection."""
super().__init__(controller, id=id)
self.selected_work_unit_id: str | None = None
[docs]
@override
def compose(self) -> ComposeResult:
yield Static(declarations_copy("tui.declarations.overview.title"), classes="cadrumo-banner", markup=False)
with ContentScroll(id="declarations-page", classes="cadrumo-scroll declarations-page"):
yield Static(
declarations_copy("tui.declarations.overview.areas"),
classes="cadrumo-heading cadrumo-heading-lead",
markup=False,
)
yield ContentDataTable[str](id="declarations-navigation", cursor_type="row", zebra_stripes=True)
yield Static(
declarations_copy("tui.declarations.overview.declarations"),
classes="cadrumo-heading",
markup=False,
)
yield ContentDataTable[str](id="declarations-list", cursor_type="row", zebra_stripes=True)
yield Static(id="declarations-empty", classes="declarations-empty", markup=False)
yield Static(id="declarations-refusal", classes="declarations-refusal", markup=False)
[docs]
def on_mount(self) -> None:
"""Populate safe natural-coordinate declaration rows."""
self.populate_navigation()
table = cast("DataTable[str]", self.query_one("#declarations-list", DataTable))
table.add_column(declarations_copy("tui.declarations.column.declaration"), key="declaration")
table.add_column(declarations_copy("tui.declarations.column.local_state"), key="state")
table.add_column(declarations_copy("tui.declarations.column.calculation"), key="calculation")
table.add_column(declarations_copy("tui.declarations.column.result"), key="result")
for row in self.controller.projection.declarations:
table.add_row(
natural_address(row.modelo, row.filing_year, row.period),
work_state_label(row.state),
declarations_copy(
"tui.declarations.value.available" if row.has_current_calculation else "tui.declarations.value.none"
),
# An unknown result is WORDED, never left blank. A blank cell
# in a money column reads as zero, and the projection reaches
# `None` from several distinct unknowns -- an unmodelled
# settlement chain, no calculation yet, a cell not computed --
# none of which is a figure.
row.settled_result or declarations_copy("tui.declarations.value.result_unknown"),
key=row.work_unit_id,
)
if not table.row_count:
self.show_empty()
restored = self.controller.restored_id("declarations.work")
row_index = next((i for i, item in enumerate(table.ordered_rows) if item.key.value == restored), None)
if row_index is None:
self.query_one("#declarations-navigation", DataTable).focus()
else:
table.move_cursor(row=row_index)
table.focus()
[docs]
def on_data_table_row_selected(self, event: DataTable.RowSelected) -> None:
"""Route a navigation row or invoke the injected declaration handoff."""
if self.handle_navigation(event):
return
table = cast("DataTable[str]", event.data_table)
if table.id != "declarations-list" or event.row_key.value is None:
return
row = next(item for item in self.controller.projection.declarations if item.work_unit_id == event.row_key.value)
self.selected_work_unit_id = row.work_unit_id
factory = self.controller.modelo_workspace_factory
if factory is None:
self.refuse_handoff()
else:
child = factory(row)
cast("App[None]", self.app).push_screen(child, self._restore_declaration_focus)
def _restore_declaration_focus(self, _: None) -> None:
"""Restore the semantic declaration table after its child dismisses."""
table = cast("DataTable[str]", self.query_one("#declarations-list", DataTable))
row_index = next(
(
index
for index, table_row in enumerate(table.ordered_rows)
if table_row.key.value == self.selected_work_unit_id
),
None,
)
if row_index is not None:
table.move_cursor(row=row_index)
table.focus()
[docs]
class DeclarationsModeloWorkspaceLauncherScreen(DeclarationsOverviewScreen):
"""Select a declaration and open its injected existing Modelo workspace."""
# It shares the overview's mechanics but is an area, so Back returns there.
IS_WORKSPACE_OVERVIEW: ClassVar[bool] = False
def __init__(self, controller: DeclarationsWorkspaceController) -> None:
"""Use the landing selection mechanics under a distinct route identity."""
super().__init__(controller, id="declarations-modelo-workspace-launcher-screen")
__all__ = ["DeclarationsModeloWorkspaceLauncherScreen", "DeclarationsOverviewScreen"]