I can persist tabmanager state

This commit is contained in:
2025-11-11 23:03:52 +01:00
parent 7f56b89e66
commit fb57a6a81d
9 changed files with 164 additions and 50 deletions

View File

@@ -1,11 +1,11 @@
import uuid
from dataclasses import dataclass
from typing import Any
from fasthtml.common import Div, Button, Span
from myfasthtml.controls.BaseCommands import BaseCommands
from myfasthtml.controls.helpers import Ids
from myfasthtml.controls.helpers import Ids, mk
from myfasthtml.core.commands import Command
from myfasthtml.core.dbmanager import DbObject
from myfasthtml.core.instances import MultipleInstance, BaseInstance
from myfasthtml.icons.fluent_p3 import dismiss_circle16_regular
@@ -35,18 +35,25 @@ class TabsManagerState(DbObject):
class Commands(BaseCommands):
pass
def show_tab(self, tab_id):
return Command(f"{self._prefix}SowTab",
"Activate or show a specific tab",
self._owner.show_tab, tab_id).htmx(target=f"#{self._id}", swap="outerHTML")
class TabsManager(MultipleInstance):
def __init__(self, session):
super().__init__(session, Ids.TabsManager)
def __init__(self, session, _id=None):
super().__init__(session, Ids.TabsManager, _id=_id)
self._state = TabsManagerState(self)
self._commands = Commands(self)
self.commands = Commands(self)
def get_state(self):
return self._state
def on_new_tab(self, label: str, component: Any):
self.add_tab(label, component)
return self
def add_tab(self, label: str, component: Any, activate: bool = True) -> str:
"""
Add a new tab or update an existing one with the same component type, ID and label.
@@ -108,6 +115,13 @@ class TabsManager(MultipleInstance):
return tab_id
def show_tab(self, tab_id):
if tab_id not in self._state.tabs:
return None
self._state.active_tab = tab_id
return self
def _mk_tab_button(self, tab_id: str, tab_data: dict):
"""
Create a single tab button with its label and close button.
@@ -122,7 +136,7 @@ class TabsManager(MultipleInstance):
is_active = tab_id == self._state.active_tab
return Button(
Span(tab_data.get("label", "Untitled"), cls="mf-tab-label"),
mk.mk(Span(tab_data.get("label", "Untitled"), cls="mf-tab-label"), command=self.commands.show_tab(tab_id)),
Span(dismiss_circle16_regular, cls="mf-tab-close-btn"),
cls=f"mf-tab-button {'mf-tab-active' if is_active else ''}",
data_tab_id=tab_id,