Files
lucioandClaude Opus 5.5 60532a4043 Rebuild the UI on vendored Textual; make the checkout self-contained
The internal servers have no access to outside package repositories and
run Python 3.8/3.9, so the UI moves from hand-drawn curses to Textual
6.2.1 (the last release supporting 3.8.1), vendored with its pure-Python
dependencies in vendor/. The launcher puts vendor/ first on sys.path, so a
plain checkout runs with no pip and no network.

- dsflow/: the single script split into a package (pure moves of the
  backend sections); action rules moved to ops.py, free of Textual
- dsflow/ui/: tabs, detail pane, live sanity-run panel, log viewer,
  modal dialogs, filter, themes, mouse support
- tests/test_ui.py: headless UI tests through Textual's pilot
- tools/vendor.py (refresh vendor/), tools/screenshots.py (README images)
- README, CLAUDE.md, TODO and the feature doc updated for the new layout,
  the Python 3.8.1 floor and the vendoring rules

Verified: 11 tests green on Python 3.12 and 3.8.20; the shipped file set
runs under `python -I -S` on both.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-09-24 03:24:21 -07:00

205 lines
5.8 KiB
Python
Vendored

from __future__ import annotations
from typing import TYPE_CHECKING, ClassVar
from rich.console import RenderableType
if TYPE_CHECKING:
from textual.app import RenderResult
from typing_extensions import Self
from textual.binding import Binding, BindingType
from textual.events import Click
from textual.geometry import Size
from textual.message import Message
from textual.reactive import reactive
from textual.scrollbar import ScrollBarRender
from textual.widget import Widget
class Switch(Widget, can_focus=True):
"""A switch widget that represents a boolean value.
Can be toggled by clicking on it or through its [bindings][textual.widgets.Switch.BINDINGS].
The switch widget also contains [component classes][textual.widgets.Switch.COMPONENT_CLASSES]
that enable more customization.
"""
BINDINGS: ClassVar[list[BindingType]] = [
Binding("enter,space", "toggle_switch", "Toggle", show=False),
]
"""
| Key(s) | Description |
| :- | :- |
| enter,space | Toggle the switch state. |
"""
COMPONENT_CLASSES: ClassVar[set[str]] = {
"switch--slider",
}
"""
| Class | Description |
| :- | :- |
| `switch--slider` | Targets the slider of the switch. |
"""
DEFAULT_CSS = """
Switch {
border: tall $border-blurred;
background: $surface;
height: auto;
width: auto;
padding: 0 2;
&.-on .switch--slider {
color: $success;
}
& .switch--slider {
color: $panel;
background: $panel-darken-2;
}
&:hover {
& > .switch--slider {
color: $panel-lighten-1
}
&.-on > .switch--slider {
color: $success-lighten-1;
}
}
&:focus {
border: tall $border;
background-tint: $foreground 5%;
}
&:light {
&.-on .switch--slider {
color: $success;
}
& .switch--slider {
color: $primary 15%;
background: $panel-darken-2;
}
&:hover {
& > .switch--slider {
color: $primary 25%;
}
&.-on > .switch--slider {
color: $success-lighten-1;
}
}
}
}
"""
value: reactive[bool] = reactive(False, init=False)
"""The value of the switch; `True` for on and `False` for off."""
_slider_position = reactive(0.0)
"""The position of the slider."""
class Changed(Message):
"""Posted when the status of the switch changes.
Can be handled using `on_switch_changed` in a subclass of `Switch`
or in a parent widget in the DOM.
Attributes:
value: The value that the switch was changed to.
switch: The `Switch` widget that was changed.
"""
def __init__(self, switch: Switch, value: bool) -> None:
super().__init__()
self.value: bool = value
self.switch: Switch = switch
@property
def control(self) -> Switch:
"""Alias for self.switch."""
return self.switch
def __init__(
self,
value: bool = False,
*,
animate: bool = True,
name: str | None = None,
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
):
"""Initialise the switch.
Args:
value: The initial value of the switch.
animate: True if the switch should animate when toggled.
name: The name of the switch.
id: The ID of the switch in the DOM.
classes: The CSS classes of the switch.
disabled: Whether the switch is disabled or not.
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
if value:
self._slider_position = 1.0
self.set_reactive(Switch.value, value)
self._should_animate = animate
if tooltip is not None:
self.tooltip = tooltip
def watch_value(self, value: bool) -> None:
target_slider_position = 1.0 if value else 0.0
if self._should_animate:
self.animate(
"_slider_position",
target_slider_position,
duration=0.3,
level="basic",
)
else:
self._slider_position = target_slider_position
self.post_message(self.Changed(self, self.value))
def watch__slider_position(self, slider_position: float) -> None:
self.set_class(slider_position == 1, "-on")
def render(self) -> RenderResult:
style = self.get_component_rich_style("switch--slider")
return ScrollBarRender(
virtual_size=100,
window_size=50,
position=self._slider_position * 50,
style=style,
vertical=False,
)
def get_content_width(self, container: Size, viewport: Size) -> int:
return 4
def get_content_height(self, container: Size, viewport: Size, width: int) -> int:
return 1
async def _on_click(self, event: Click) -> None:
"""Toggle the state of the switch."""
event.stop()
self.toggle()
def action_toggle_switch(self) -> None:
"""Toggle the state of the switch."""
self.toggle()
def toggle(self) -> Self:
"""Toggle the switch value.
As a result of the value changing, a `Switch.Changed` message will
be posted.
Returns:
The `Switch` instance.
"""
self.value = not self.value
return self