bootstack.Workbench#

class bootstack.Workbench(*, title='', size=None, theme=None, icon=None, light_theme='bootstrap-light', dark_theme='bootstrap-dark', follow_system_appearance=False, available_themes=(), locale=None, localize_mode='auto', window_style='mica', macos_quit_behavior='native', remember_window_state=False, state_path=None, on_close=None, position=None, min_size=None, max_size=None, resizable=None, scaling=None, hdpi=True, rail_surface='chrome', sidebar_surface='raised', statusbar_surface='chrome', undecorated=False, show_sidebar=True, sidebar_mode='expanded', sidebar_width=None, rail_width=None, rail_labels=False, collapsible=True, nav_accent='primary', remember_nav_state=False, show_statusbar=False, **kwargs)#

Bases: _ShellBase

Two-tier application window: a workspace rail plus per-workspace sidebars.

The advanced, VS Code-style scaffold — a vertical icon rail of workspaces down the far left, each revealing its own navigation sidebar and content. Add workspaces with add_workspace(); each is itself a sidebar host, authored with the same page_nav() / list_nav() / tree_nav() / custom_nav() front doors as a single-tier AppShell.

For a single sidebar with no rail, use AppShell instead.

Parameters:
  • title (str) – Window title and (in undecorated mode) chrome label.

  • size (tuple[int, int] | None) – Initial window size as (width, height).

  • theme (str | None) – Theme name to apply on startup (e.g. 'bootstrap-dark').

  • icon (str | Image | AppIcon | None) – Title-bar and taskbar icon — a path, an Image, or an AppIcon.

  • light_theme (str) – Theme used for the light end of system-appearance tracking.

  • dark_theme (str) – Theme used for the dark end of system-appearance tracking.

  • follow_system_appearance (bool) – If True, track the OS appearance (macOS).

  • available_themes (Sequence[str]) – Theme names to expose to theme pickers.

  • locale (str | None) – Locale identifier (e.g. 'en_US', 'de_DE').

  • localize_mode (LocalizeMode) – Localization behavior.

  • window_style (WindowStyle | str | None) – Windows-only window effect, or None to disable.

  • macos_quit_behavior (Literal['native', 'classic']) – macOS close / Cmd+Q behavior. No-op on Win/Linux.

  • remember_window_state (bool) – If True, window geometry is saved and restored.

  • state_path (str | None) – Optional override for the persisted window-state file.

  • on_close (Callable[[], bool | None] | None) – Handler invoked on the window’s close button. Return False to veto the close; None or True to allow it.

  • position (tuple[int, int] | None) – Initial window position as (x, y).

  • min_size (tuple[int, int] | None) – Minimum window size as (width, height).

  • max_size (tuple[int, int] | None) – Maximum window size as (width, height).

  • resizable (tuple[bool, bool] | None) – Whether the window can be resized as (x, y).

  • scaling (float | None) – Explicit UI scaling factor. When None, scaling is automatic.

  • hdpi (bool) – Enable high-DPI awareness for the application. Default True.

  • rail_surface (SurfaceToken | str) – Surface token for the workspace rail.

  • sidebar_surface (SurfaceToken | str) – Surface token for the per-workspace navigation sidebar.

  • statusbar_surface (SurfaceToken | str) – Surface token for the bottom status band.

  • undecorated (bool) – Remove OS window decorations and draw a custom border.

  • show_sidebar (bool) – Render the sidebar region. Default True.

  • sidebar_mode (SidebarMode) – Initial sidebar mode — 'expanded'/'hidden'.

  • sidebar_width (int | None) – Expanded sidebar width in pixels.

  • rail_width (int | None) – Workspace-rail width in pixels.

  • rail_labels (bool) – Show a caption under each rail icon (widens the rail).

  • collapsible (bool) – Allow collapsing the sidebar; binds Ctrl/Cmd-B. Default True.

  • nav_accent (AccentToken | str | None) – Accent for the active nav item and the rail indicator. Defaults to 'primary'; another token retints, or None for a neutral wash.

  • remember_nav_state (bool) – Persist the sidebar mode and per-workspace active page across sessions. Default False.

  • show_statusbar (bool) – Force the bottom status band on. Default False.

property current: str | None#

Key of the active workspace’s active page, or None.

property current_workspace: str | None#

Key of the active workspace, or None.

property dark_theme: str#

Theme used for the dark end of system-appearance / toggle_theme.

property follow_system_appearance: bool#

Whether the app tracks the OS light/dark appearance (macOS).

property light_theme: str#

Theme used for the light end of system-appearance / toggle_theme.

property locale: str | None#

The active locale (e.g. 'en_US'). Setting it switches locale live.

property locale_date_format: str | None#

Short date pattern for the active locale (e.g. 'M/d/yy'). Read-only.

property locale_decimal: str | None#

Decimal separator for the active locale (e.g. '.'). Read-only.

property locale_language: str | None#

Base language code derived from the locale (e.g. 'en'). Read-only.

property locale_thousands: str | None#

Thousands separator for the active locale (e.g. ','). Read-only.

property locale_time_format: str | None#

mm a. Read-only.

Type:

Short time pattern for the active locale, e.g. h

property nav: Any#

The active provider’s sidebar navigation panel (or None).

property pages: Any#

The active static provider’s content page deck (or None).

property rail: Rail#

The workspace switcher. Methods no-op when the rail is not rendered.

property remember_window_state: bool#

Whether window geometry is saved on close and restored on launch.

property schedule: Schedule#

Scheduler tied to this widget’s lifetime.

All jobs are automatically cancelled when the widget is destroyed. First access creates the Schedule instance; subsequent accesses return the same instance.

Usage:

self.schedule.delay(500, callback)
self.schedule.every(1000, tick)
job = self.schedule.idle(refresh)
job.cancel()
property sidebar_mode: Literal['expanded', 'compact', 'hidden']#

The sidebar mode ('hidden'/'compact'/'expanded').

property statusbar: StatusBar#

The bottom status band (passive status). Lazily created on first access.

The band renders once a segment is added (or show_statusbar=True).

property theme: str#

The active theme name. Setting it switches the theme live.

property title: str#

The window title bar text (also the app’s display name).

property window_style: WindowStyle | str | None#

Windows-only window effect, or None to disable.

classmethod from_store(store, **overrides)#

Construct from a persisted Store (or plain dict).

Reads configuration from store, tolerantly ignoring keys that are not valid configuration (so version skew does not raise). Explicit keyword overrides win over stored values. See App.from_store.

add_close_handler(handler)#

Register a veto-able close handler — an alias for on_close().

Parameters:

handler (Callable[[], bool | None]) – Called with no arguments on a close request. Return False to cancel the close.

add_toolbar(*, divider=False, use_macos_menus=True, **toolbar_kwargs)#

Append a Toolbar to the window’s top chrome stack and return it.

Toolbars stack full-width, top-to-bottom, in the order added — fill each with buttons, labels, widgets, and menus (toolbar.add_menu(...)). The returned Toolbar is a context manager, so the natural form reads:

with window.add_toolbar() as tb:
    tb.add_menu("File")
    tb.add_spacer()
    tb.add_theme_toggle()
Parameters:
  • divider (bool) – Draw a hairline beneath this toolbar (default False).

  • use_macos_menus (bool) – On macOS, bridge this toolbar’s menus to the native global menu bar (in-window dropdowns hide). Default True; set False to keep its menus in-window even on macOS. No effect on Windows/Linux.

  • **toolbar_kwargs (Any) – Forwarded to Toolbar — e.g. surface, density, button_variant, show_window_controls, draggable. Window chrome defaults surface='chrome' and density='compact' (a tight strip); pass density='default' for a roomier bar.

Returns:

The Toolbar for this layer.

Return type:

Any

add_workspace(key, *, text='', icon=None, pin_to_footer=False)#

Add a workspace — a rail icon revealing its own sidebar + content.

The rail appears once there is more than one workspace. The returned Workspace is a sidebar host: declare its sidebar with page_nav() / list_nav() / tree_nav() / custom_nav().

Parameters:
  • key (str) – Unique workspace identifier.

  • text (str) – Tooltip / rail label for the workspace icon.

  • icon (str | dict | None) – Rail icon name or icon configuration dict.

  • pin_to_footer (bool) – If True, pin the rail icon to the bottom of the rail (the conventional spot for Settings / Account) instead of the top cluster.

close()#

Close the window and exit its event loop.

For the application window this quits the loop started by run() — the natural action for a “Quit” command. It does not run the on_close veto handlers (those guard the user clicking the window’s close button), so a programmatic close() always proceeds.

destroy()#

Destroy the widget and release the resources it holds.

Removes the widget from its parent, destroys its children, and cancels any pending or repeating jobs on its schedule. After this the widget must not be used again. Destroying a container destroys everything inside it.

emit(event, *, data=None)#

Fire a named event on this widget, as if it produced the event itself.

This is how a composite widget surfaces high-level activity to its listeners, and the generic counterpart to the on_*() shorthands for firing events that have no dedicated method.

Parameters:
  • event (str) – The event name, unprefixed — the same name you pass to on() or an on_<event>() shorthand (e.g. 'change', 'select').

  • data (Any) – The payload delivered to handlers. For a data-carrying event, pass the matching payload dataclass from bootstack.events — the same object an on_<event>() handler receives. Leave as None for native events (click, hover, focus, …), which carry no payload.

Example

widget.emit("change", data=bs.events.ChangeEvent(value=new_value))
hide()#

Hide the window without destroying it.

The window is unmapped (and usually removed from the taskbar). Bring it back with show().

hide_sidebar()#

Hide the sidebar entirely.

maximize()#

Maximize the window where the platform supports it.

minimize()#

Minimize the window to the taskbar or dock.

navigate(workspace, page, *, data=None)#

Switch to workspace and select page within it.

Parameters:
  • workspace (str) – The workspace key to activate.

  • page (str) – The page key to activate within that workspace.

  • data (dict | None) – Optional data dict passed to the page’s change event.

on(event, handler=None)#

Bind handler to event, or return a composable Stream.

With a handler — binds immediately and returns a Subscription:

sub = widget.on("change", handler)
sub.cancel()

Without a handler — returns a Stream for operator chaining. The Tk binding is created lazily when .listen() is called:

sub = widget.on("change").debounce(300).listen(handler)
sub.cancel()
Parameters:
  • event (str) – Event name (e.g. "change", "click").

  • handler (Callable[[Any], Any] | None) – Optional callback. If omitted, a Stream is returned.

Returns:

Subscription when a handler is provided; Stream otherwise.

Return type:

Stream | Subscription

on_close(handler)#

Register a handler invoked when the user clicks the window’s close button.

Handlers run in registration order; return False from one to veto the close (the window stays open), or None/True to allow it. This guards the close-button gesture only — it is not run by the programmatic close().

Parameters:

handler (Callable[[], bool | None]) – Called with no arguments on a close request. Return False to cancel the close.

on_destroy(handler=None)#

Register a callback fired when the widget is destroyed.

Fires once, as the widget is torn down — the place to release resources the widget owns that aren’t cleaned up automatically (file handles, observers, external subscriptions). The handler receives a curated Event.

Parameters:

handler (Callable[[Event], Any] | None) – Called as the widget is destroyed. Omit to get a composable Stream.

Returns:

A cancellable Subscription when a handler is given, otherwise a Stream.

Return type:

Stream | Subscription

on_locale_change(handler=None)#

React to locale changes; the handler receives the new locale code.

Returns:

Subscription (with handler) or Stream (without handler).

Return type:

Stream | Subscription

on_page_change(handler=None)#

Register a callback fired when the active page changes.

Parameters:

handler (Callable[[PageChangeEvent], Any] | None) – Called with a PageChangeEvent (the new and previous page keys, plus any navigate() data). Omit to get a composable Stream.

on_sidebar_mode_change(handler=None)#

Register a callback fired when the sidebar mode changes.

Parameters:

handler (Callable[[DisplayModeEvent], Any] | None) – Called with a DisplayModeEvent (compactexpanded). Omit to get a composable Stream.

on_sidebar_toggle(handler=None)#

Register a callback fired when the sidebar is shown or hidden.

Parameters:

handler (Callable[[PaneToggleEvent], Any] | None) – Called with a PaneToggleEvent. Omit to get a composable Stream.

on_theme_change(handler=None)#

React to theme changes; the handler receives the new theme name.

Fired after the theme is fully rebuilt, so handlers can safely read new colors. Useful for persisting the choice, e.g. app.on_theme_change(lambda t: store.update(theme=t)).

Returns:

Subscription (with handler) or Stream (without handler).

Return type:

Stream | Subscription

on_workspace_change() Stream#
on_workspace_change(handler: Callable[[WorkspaceChangeEvent], Any]) Subscription

Register a callback fired when the rail switches workspace.

Parameters:

handler (Callable[[WorkspaceChangeEvent], Any] | None) – Called with a WorkspaceChangeEvent. Omit to get a composable Stream.

remove_close_handler(handler)#

Remove a close handler previously registered with on_close().

Parameters:

handler (Callable[[], bool | None]) – The same callable passed to on_close() or add_close_handler(). No-op if it was not registered.

run()#

Show the window and start the event loop.

set_fullscreen(value=True)#

Enter or leave fullscreen mode where supported.

Parameters:

value (bool) – True to enter fullscreen, False to exit. Default True.

set_topmost(value=True)#

Pin the window above all others, or release it.

Parameters:

value (bool) – True to keep the window above others, False to release. Default True.

show()#

Show the window — typically to reveal it again after hide().

For the application window, run() already shows it and starts the event loop; reach for show() to bring a hidden window back.

show_sidebar()#

Show the sidebar, restoring its last non-hidden mode.

toggle_sidebar()#

Toggle the sidebar between hidden and shown (the hamburger action).

Supporting classes#

Opaque handles returned by Workbench’s methods — you obtain them from the shell rather than constructing them directly. A workspace is a sidebar host, so it returns the same PageNav / Page handles documented on AppShell.

class bootstack.widgets.appshell.Workspace(internal_ws, internal_shell)#

Bases: _SidebarHost

One workspace in a Workbench (returned by add_workspace()).

A sidebar host (see _SidebarHost) plus its own navigate(): declare its sidebar with page_nav() / list_nav() / tree_nav() / custom_nav(), exactly as you would a single-tier AppShell.

property current: str | None#

Key of this workspace’s active page, or None.

property key: str#

The workspace identifier.

navigate(page, *, data=None)#

Navigate to a page in this workspace.

class bootstack.widgets.appshell.Rail(shell)#

Bases: object

The workspace rail (returned by workbench.rail).

The rail is mostly framework-driven; its public surface switches workspaces and observes changes. Methods are no-ops when the rail is not rendered (a single-workspace workbench).

property current: str | None#

Key of the active workspace, or None.

on_change() Stream#
on_change(handler: Callable[[WorkspaceChangeEvent], Any]) Subscription

Register a callback fired when the active workspace changes.

Parameters:

handler (Callable[[WorkspaceChangeEvent], Any] | None) – Called with a WorkspaceChangeEvent. Omit to get a composable Stream.

select(key)#

Switch to the workspace key and show its sidebar.