deckkit 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. deckkit/__init__.py +12 -0
  2. deckkit/_bundled_fonts.py +54 -0
  3. deckkit/_diagnostics.py +48 -0
  4. deckkit/_font_assets/SOURCES.txt +9 -0
  5. deckkit/_font_assets/SourceCodePro-Bold.ttf +0 -0
  6. deckkit/_font_assets/SourceCodePro-BoldIt.ttf +0 -0
  7. deckkit/_font_assets/SourceCodePro-It.ttf +0 -0
  8. deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
  9. deckkit/_font_assets/SourceCodePro-Regular.ttf +0 -0
  10. deckkit/_font_assets/SourceSans3-Bold.ttf +0 -0
  11. deckkit/_font_assets/SourceSans3-BoldIt.ttf +0 -0
  12. deckkit/_font_assets/SourceSans3-It.ttf +0 -0
  13. deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
  14. deckkit/_font_assets/SourceSans3-Regular.ttf +0 -0
  15. deckkit/_variable_evaluation.py +82 -0
  16. deckkit/animation.py +211 -0
  17. deckkit/annotations.py +161 -0
  18. deckkit/backends/__init__.py +1 -0
  19. deckkit/backends/_font_embedding.py +41 -0
  20. deckkit/backends/_pptx_fonts.py +230 -0
  21. deckkit/backends/_pptx_shadows.py +241 -0
  22. deckkit/backends/_pptx_tables.py +251 -0
  23. deckkit/backends/pptx.py +1225 -0
  24. deckkit/backends/pptx_animation.py +175 -0
  25. deckkit/backends/svg.py +527 -0
  26. deckkit/canvas/__init__.py +40 -0
  27. deckkit/canvas/_geometry.py +49 -0
  28. deckkit/canvas/clipping.py +336 -0
  29. deckkit/canvas/context.py +34 -0
  30. deckkit/canvas/elements.py +306 -0
  31. deckkit/canvas/path.py +250 -0
  32. deckkit/canvas/scene.py +74 -0
  33. deckkit/canvas/style.py +31 -0
  34. deckkit/charts/__init__.py +59 -0
  35. deckkit/charts/axes.py +249 -0
  36. deckkit/charts/chart.py +244 -0
  37. deckkit/charts/content.py +132 -0
  38. deckkit/charts/context.py +45 -0
  39. deckkit/charts/interval.py +166 -0
  40. deckkit/charts/legends.py +82 -0
  41. deckkit/charts/marks.py +410 -0
  42. deckkit/charts/scales.py +205 -0
  43. deckkit/citations.py +200 -0
  44. deckkit/compiler.py +410 -0
  45. deckkit/context.py +72 -0
  46. deckkit/deck.py +168 -0
  47. deckkit/drawables/__init__.py +42 -0
  48. deckkit/drawables/bounds.py +17 -0
  49. deckkit/drawables/connector.py +22 -0
  50. deckkit/drawables/drawable.py +38 -0
  51. deckkit/drawables/ellipse.py +21 -0
  52. deckkit/drawables/group.py +10 -0
  53. deckkit/drawables/image.py +14 -0
  54. deckkit/drawables/line.py +18 -0
  55. deckkit/drawables/path.py +78 -0
  56. deckkit/drawables/rectangle.py +24 -0
  57. deckkit/drawables/rich_text.py +176 -0
  58. deckkit/drawables/table.py +42 -0
  59. deckkit/drawables/text.py +29 -0
  60. deckkit/effects.py +84 -0
  61. deckkit/expanded_views.py +561 -0
  62. deckkit/fonts.py +329 -0
  63. deckkit/geometry.py +48 -0
  64. deckkit/identity.py +72 -0
  65. deckkit/layout.py +398 -0
  66. deckkit/layouts/__init__.py +20 -0
  67. deckkit/layouts/_content.py +27 -0
  68. deckkit/layouts/annotations.py +217 -0
  69. deckkit/layouts/containers.py +202 -0
  70. deckkit/layouts/grid.py +299 -0
  71. deckkit/layouts/modifiers.py +200 -0
  72. deckkit/layouts/primitives.py +213 -0
  73. deckkit/layouts/table.py +139 -0
  74. deckkit/measurement.py +420 -0
  75. deckkit/paint.py +73 -0
  76. deckkit/py.typed +0 -0
  77. deckkit/reconcile.py +119 -0
  78. deckkit/semantic.py +47 -0
  79. deckkit/theme.py +364 -0
  80. deckkit/themes/__init__.py +91 -0
  81. deckkit/themes/clarity.py +98 -0
  82. deckkit/tokens.py +226 -0
  83. deckkit/units.py +73 -0
  84. deckkit/variables.py +212 -0
  85. deckkit/view_expansion.py +185 -0
  86. deckkit/views/__init__.py +100 -0
  87. deckkit/views/_code_source.py +42 -0
  88. deckkit/views/_drawing.py +52 -0
  89. deckkit/views/canvas.py +27 -0
  90. deckkit/views/code.py +481 -0
  91. deckkit/views/code_evolution.py +284 -0
  92. deckkit/views/composite_view.py +19 -0
  93. deckkit/views/divider.py +38 -0
  94. deckkit/views/ellipse.py +38 -0
  95. deckkit/views/grid.py +156 -0
  96. deckkit/views/hstack.py +43 -0
  97. deckkit/views/image.py +43 -0
  98. deckkit/views/layout_view.py +30 -0
  99. deckkit/views/line.py +31 -0
  100. deckkit/views/modifier_views.py +281 -0
  101. deckkit/views/path.py +36 -0
  102. deckkit/views/primitive_view.py +16 -0
  103. deckkit/views/pygments_highlighter.py +67 -0
  104. deckkit/views/rectangle.py +61 -0
  105. deckkit/views/section_number.py +21 -0
  106. deckkit/views/semantic/__init__.py +51 -0
  107. deckkit/views/semantic/before_after.py +109 -0
  108. deckkit/views/semantic/bullet_graph.py +216 -0
  109. deckkit/views/semantic/card.py +129 -0
  110. deckkit/views/semantic/code_block.py +41 -0
  111. deckkit/views/semantic/diff.py +642 -0
  112. deckkit/views/semantic/estimate_interval.py +137 -0
  113. deckkit/views/semantic/figure.py +55 -0
  114. deckkit/views/semantic/item_list.py +171 -0
  115. deckkit/views/semantic/metric.py +74 -0
  116. deckkit/views/semantic/pill.py +69 -0
  117. deckkit/views/semantic/pipeline.py +94 -0
  118. deckkit/views/semantic/pipeline_stage.py +59 -0
  119. deckkit/views/semantic/semantic_view.py +76 -0
  120. deckkit/views/semantic/sparkline.py +164 -0
  121. deckkit/views/semantic/table.py +439 -0
  122. deckkit/views/semantic/terminal.py +361 -0
  123. deckkit/views/semantic/timeline.py +158 -0
  124. deckkit/views/slide.py +37 -0
  125. deckkit/views/slide_number.py +20 -0
  126. deckkit/views/slides/__init__.py +16 -0
  127. deckkit/views/slides/section_slide.py +89 -0
  128. deckkit/views/slides/slide_margins.py +32 -0
  129. deckkit/views/slides/slide_with_margins.py +22 -0
  130. deckkit/views/slides/title_and_content.py +49 -0
  131. deckkit/views/slides/title_and_content_slide.py +55 -0
  132. deckkit/views/spacer.py +45 -0
  133. deckkit/views/stack.py +36 -0
  134. deckkit/views/text.py +142 -0
  135. deckkit/views/view.py +229 -0
  136. deckkit/views/vstack.py +31 -0
  137. deckkit/views/zstack.py +27 -0
  138. deckkit-0.1.0.dist-info/METADATA +123 -0
  139. deckkit-0.1.0.dist-info/RECORD +144 -0
  140. deckkit-0.1.0.dist-info/WHEEL +5 -0
  141. deckkit-0.1.0.dist-info/licenses/LICENSE +21 -0
  142. deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
  143. deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
  144. deckkit-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,137 @@
1
+ """A compact point estimate and interval."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from math import isfinite
7
+ from typing import overload
8
+
9
+ from typing_extensions import Self
10
+
11
+ from ...canvas import Stroke
12
+ from ...charts.chart import Chart, ChartInsets
13
+ from ...charts.content import value
14
+ from ...charts.interval import IntervalMark
15
+ from ...charts.scales import LinearScaleSpec
16
+ from ...context import Context
17
+ from ...theme import AccentColor, ColorStyle, PrimaryColor, SpacingUnit
18
+ from ...tokens import DesignToken, TokenResolver
19
+ from ...units import Length, pt, require_length
20
+ from ..modifier_views import unbounded
21
+ from ..view import View
22
+ from .semantic_view import StyledSemanticView
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class EstimateInterval(StyledSemanticView["EstimateInterval.Style"]):
27
+ """Show an estimate with a horizontal interval, optionally on a shared scale."""
28
+
29
+ @dataclass(frozen=True)
30
+ class Style:
31
+ ideal_width: Length
32
+ height: Length
33
+ padding: Length
34
+ stroke: Stroke
35
+ point_fill: ColorStyle
36
+ point_radius: Length
37
+ cap_length: Length
38
+
39
+ def __post_init__(self) -> None:
40
+ for length in (
41
+ self.ideal_width,
42
+ self.height,
43
+ self.padding,
44
+ self.point_radius,
45
+ self.cap_length,
46
+ ):
47
+ require_length(length)
48
+ if length < pt(0):
49
+ raise ValueError(
50
+ "EstimateInterval style lengths must be nonnegative"
51
+ )
52
+ if self.height == pt(0) or self.ideal_width == pt(0):
53
+ raise ValueError(
54
+ "EstimateInterval ideal width and height must be positive"
55
+ )
56
+ if not isinstance(self.stroke, Stroke):
57
+ raise TypeError("EstimateInterval.stroke must be a Stroke")
58
+
59
+ @staticmethod
60
+ def _default_style(values: TokenResolver) -> Style:
61
+ unit = values[SpacingUnit]
62
+ return EstimateInterval.Style(
63
+ ideal_width=unit * 24,
64
+ height=unit * 6,
65
+ padding=unit / 2,
66
+ stroke=Stroke(AccentColor, unit / 3),
67
+ point_fill=PrimaryColor,
68
+ point_radius=unit * 0.7,
69
+ cap_length=unit * 1.5,
70
+ )
71
+
72
+ style_token = DesignToken[Style](
73
+ "component.estimate-interval",
74
+ lambda values: EstimateInterval._default_style(values),
75
+ value_type=Style,
76
+ )
77
+
78
+ estimate: float
79
+ lower: float
80
+ upper: float
81
+ domain: tuple[float, float] | None = None
82
+
83
+ def __post_init__(self) -> None:
84
+ if not all(
85
+ isfinite(number) for number in (self.estimate, self.lower, self.upper)
86
+ ):
87
+ raise ValueError("EstimateInterval values must be finite")
88
+ if self.lower > self.upper:
89
+ raise ValueError(
90
+ "EstimateInterval lower bound must not exceed its upper bound"
91
+ )
92
+ if self.domain is not None:
93
+ LinearScaleSpec(domain=self.domain, nice=False).infer_domain(())
94
+
95
+ # The base method accepts fields from any style dataclass.
96
+ @overload # type: ignore[override]
97
+ def style(self, style: EstimateInterval.Style, /) -> Self: ...
98
+
99
+ @overload
100
+ def style(
101
+ self,
102
+ *,
103
+ ideal_width: Length = ...,
104
+ height: Length = ...,
105
+ padding: Length = ...,
106
+ stroke: Stroke = ...,
107
+ point_fill: ColorStyle = ...,
108
+ point_radius: Length = ...,
109
+ cap_length: Length = ...,
110
+ ) -> Self: ...
111
+
112
+ def style(self, *styles: EstimateInterval.Style, **changes: object) -> Self:
113
+ """Replace the style or override individual interval fields."""
114
+ return super().style(*styles, **changes)
115
+
116
+ def body(self, context: Context) -> View:
117
+ style = context.environment[self.style_token]
118
+ mark = IntervalMark(
119
+ (self,),
120
+ category=value("Row", lambda row: "interval"),
121
+ estimate=value("Estimate", lambda row: row.estimate),
122
+ lower=value("Lower", lambda row: row.lower),
123
+ upper=value("Upper", lambda row: row.upper),
124
+ stroke=style.stroke,
125
+ point_fill=style.point_fill,
126
+ point_radius=style.point_radius,
127
+ cap_length=style.cap_length,
128
+ key=lambda row: "interval",
129
+ )
130
+ return Chart(
131
+ mark,
132
+ x_scale=LinearScaleSpec(domain=self.domain, nice=False),
133
+ plot_insets=ChartInsets(*((style.padding,) * 4)),
134
+ x_axis=None,
135
+ y_axis=None,
136
+ legend=None,
137
+ ).frame(ideal_width=style.ideal_width, max_width=unbounded, height=style.height)
@@ -0,0 +1,55 @@
1
+ """A theme-overridable figure with an optional caption and number."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from typing import TYPE_CHECKING
7
+
8
+ from ...context import Context
9
+ from ...theme import CaptionTextStyle, SpacingUnit
10
+ from ..hstack import HStack
11
+ from ..view import View
12
+ from ..vstack import VStack
13
+ from .semantic_view import SemanticView
14
+
15
+ if TYPE_CHECKING:
16
+ from ...variables import Occurrence
17
+
18
+
19
+ @dataclass(frozen=True)
20
+ class Figure(SemanticView):
21
+ """A figure whose number can be displayed here and referenced elsewhere."""
22
+
23
+ content: View
24
+ caption: View | None = None
25
+ number: Occurrence[int | None] | None = None
26
+
27
+ def __post_init__(self) -> None:
28
+ if not isinstance(self.content, View):
29
+ raise TypeError("Figure.content must be a View")
30
+ if self.caption is not None and not isinstance(self.caption, View):
31
+ raise TypeError("Figure.caption must be a View")
32
+ if self.number is not None:
33
+ from ...variables import Occurrence
34
+
35
+ if not isinstance(self.number, Occurrence):
36
+ raise TypeError("Figure.number must be an Occurrence")
37
+ object.__setattr__(
38
+ self, "_modifiers", (("variable_occurrence", self.number),)
39
+ )
40
+
41
+ def body(self, context: Context) -> View:
42
+ spacing = context.environment[SpacingUnit]
43
+ label = (
44
+ self.number.label(format=lambda number: f"Figure {number}")
45
+ if self.number is not None
46
+ else None
47
+ )
48
+ caption = (
49
+ HStack(
50
+ label, self.caption, spacing=spacing, alignment="first_baseline"
51
+ ).font(CaptionTextStyle)
52
+ if label is not None or self.caption is not None
53
+ else None
54
+ )
55
+ return VStack(self.content, caption, spacing=spacing)
@@ -0,0 +1,171 @@
1
+ """Ordered and unordered lists with replaceable marker and row views."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Callable
6
+ from dataclasses import dataclass
7
+ from typing import overload
8
+
9
+ from typing_extensions import Self
10
+
11
+ from ...context import Context
12
+ from ...geometry import RelativePoint
13
+ from ...identity import ViewIdentity
14
+ from ...layout import Layout, _SharedWidthGroup, _SharedWidthLayout
15
+ from ...theme import ForegroundColor, SpacingUnit
16
+ from ...tokens import DesignToken, TokenResolver
17
+ from ...units import Length, pt, require_length
18
+ from ..ellipse import Ellipse
19
+ from ..hstack import HStack
20
+ from ..layout_view import LayoutView
21
+ from ..modifier_views import unbounded
22
+ from ..text import Text
23
+ from ..view import View
24
+ from ..vstack import VStack
25
+ from .semantic_view import SemanticView, StyledSemanticView
26
+
27
+
28
+ def _default_marker(ordinal: int | None) -> View:
29
+ if ordinal is None:
30
+ return Ellipse(fill=ForegroundColor).frame(width=pt(5), height=pt(5))
31
+ return Text(f"{ordinal}.", flow="verbatim")
32
+
33
+
34
+ @dataclass(frozen=True)
35
+ class _ItemPosition:
36
+ ordinal: int | None
37
+ column: _SharedWidthGroup
38
+
39
+
40
+ _POSITION: DesignToken[_ItemPosition | None] = DesignToken(
41
+ "internal.list-item-position", lambda values: None
42
+ )
43
+
44
+
45
+ @dataclass(frozen=True, init=False)
46
+ class _MarkerColumn(LayoutView):
47
+ column: _SharedWidthGroup
48
+
49
+ def __init__(self, marker: View, column: _SharedWidthGroup) -> None:
50
+ object.__setattr__(self, "column", column)
51
+ super().__init__(marker)
52
+
53
+ def resolve_layout(self, lookup: Callable[[str], ViewIdentity]) -> Layout:
54
+ return _SharedWidthLayout(self.column)
55
+
56
+
57
+ @dataclass(frozen=True)
58
+ class ListItem(SemanticView):
59
+ """An explicit list item, useful as a keyed boundary for arbitrary content."""
60
+
61
+ content: str | View
62
+
63
+ def __post_init__(self) -> None:
64
+ if not isinstance(self.content, (str, View)):
65
+ raise TypeError("ListItem.content must be a string or a View")
66
+
67
+ def body(self, context: Context) -> View:
68
+ content = Text(self.content) if isinstance(self.content, str) else self.content
69
+ position = context.environment[_POSITION]
70
+ if position is None:
71
+ return content
72
+
73
+ style = context.environment[ItemList.style_token]
74
+ marker = style.marker(position.ordinal)
75
+ if not isinstance(marker, View):
76
+ raise TypeError("ItemList.Style.marker must return a View")
77
+ row = HStack(
78
+ _MarkerColumn(marker, position.column),
79
+ content.flex(),
80
+ alignment="cap_center",
81
+ spacing=style.marker_gap,
82
+ ).frame(max_width=unbounded, alignment=RelativePoint.left)
83
+ if style.row is not None:
84
+ row = style.row(row)
85
+ if not isinstance(row, View):
86
+ raise TypeError("ItemList.Style.row must return a View")
87
+ return row.with_token(_POSITION, _POSITION.set(None))
88
+
89
+
90
+ @dataclass(frozen=True, init=False)
91
+ class ItemList(StyledSemanticView["ItemList.Style"]):
92
+ """Lay out items in a shared marker column, with optional one-based ordinals."""
93
+
94
+ @dataclass(frozen=True)
95
+ class Style:
96
+ """Control spacing, the ordinal-aware marker, and the row wrapper."""
97
+
98
+ spacing: Length
99
+ marker_gap: Length
100
+ marker: Callable[[int | None], View] = _default_marker
101
+ row: Callable[[View], View] | None = None
102
+
103
+ def __post_init__(self) -> None:
104
+ for length in (self.spacing, self.marker_gap):
105
+ require_length(length)
106
+ if length < pt(0):
107
+ raise ValueError("ItemList style lengths must be nonnegative")
108
+ if not callable(self.marker):
109
+ raise TypeError("ItemList.Style.marker must be callable")
110
+ if self.row is not None and not callable(self.row):
111
+ raise TypeError("ItemList.Style.row must be callable or None")
112
+
113
+ @staticmethod
114
+ def _default_style(values: TokenResolver) -> Style:
115
+ unit = values[SpacingUnit]
116
+ return ItemList.Style(spacing=unit * 3, marker_gap=unit * 2.5)
117
+
118
+ style_token = DesignToken[Style](
119
+ "component.item-list",
120
+ lambda values: ItemList._default_style(values),
121
+ value_type=Style,
122
+ )
123
+
124
+ items: tuple[ListItem, ...]
125
+ ordered: bool
126
+
127
+ # The base method accepts fields from any style dataclass.
128
+ @overload # type: ignore[override]
129
+ def style(self, style: ItemList.Style, /) -> Self: ...
130
+
131
+ @overload
132
+ def style(
133
+ self,
134
+ *,
135
+ spacing: Length = ...,
136
+ marker_gap: Length = ...,
137
+ marker: Callable[[int | None], View] = ...,
138
+ row: Callable[[View], View] | None = ...,
139
+ ) -> Self: ...
140
+
141
+ def style(self, *styles: ItemList.Style, **changes: object) -> Self:
142
+ """Replace the whole style or override specific list fields."""
143
+ return super().style(*styles, **changes)
144
+
145
+ def __init__(self, *items: str | View, ordered: bool = False) -> None:
146
+ if not isinstance(ordered, bool):
147
+ raise TypeError("ItemList.ordered must be a bool")
148
+ object.__setattr__(
149
+ self,
150
+ "items",
151
+ tuple(
152
+ item if isinstance(item, ListItem) else ListItem(item) for item in items
153
+ ),
154
+ )
155
+ object.__setattr__(self, "ordered", ordered)
156
+
157
+ def body(self, context: Context) -> View:
158
+ style = context.environment[self.style_token]
159
+ column = _SharedWidthGroup()
160
+ return VStack(
161
+ *(
162
+ item.with_token(
163
+ _POSITION,
164
+ _POSITION.set(
165
+ _ItemPosition(ordinal if self.ordered else None, column)
166
+ ),
167
+ )
168
+ for ordinal, item in enumerate(self.items, start=1)
169
+ ),
170
+ spacing=style.spacing,
171
+ )
@@ -0,0 +1,74 @@
1
+ """A semantic view for displaying a highlighted value and its label."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ from ...context import Context
8
+ from ...theme import (
9
+ AccentColor,
10
+ CaptionTextStyle,
11
+ Color,
12
+ MetricTextStyle,
13
+ SpacingUnit,
14
+ Typography,
15
+ )
16
+ from ...tokens import DesignToken, TokenResolver
17
+ from ...units import Length
18
+ from ..view import View
19
+ from ..vstack import VStack
20
+ from .semantic_view import StyledSemanticView
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class Metric(StyledSemanticView["Metric.Style"]):
25
+ """Emphasize a value together with a short descriptive label.
26
+
27
+ Both slots accept arbitrary :class:`~deckkit.views.View` values. The default
28
+ recipe stacks the value over the label, applies the metric font to the value,
29
+ and colors it with the theme's accent style.
30
+
31
+ Args:
32
+ value: The primary value or visualization to emphasize.
33
+ label: Supporting content that explains the value.
34
+ """
35
+
36
+ @dataclass(frozen=True)
37
+ class Style:
38
+ value_font: Typography
39
+ value_color: Color
40
+ label_font: Typography
41
+ spacing: Length
42
+
43
+ @staticmethod
44
+ def _default_style(values: TokenResolver) -> Style:
45
+ return Metric.Style(
46
+ values[MetricTextStyle],
47
+ values[AccentColor],
48
+ values[CaptionTextStyle],
49
+ values[SpacingUnit] * 2,
50
+ )
51
+
52
+ style_token = DesignToken[Style](
53
+ "component.metric",
54
+ lambda values: Metric._default_style(values),
55
+ value_type=Style,
56
+ )
57
+
58
+ value: View
59
+ label: View
60
+
61
+ def __post_init__(self) -> None:
62
+ if not isinstance(self.value, View) or not isinstance(self.label, View):
63
+ raise TypeError(
64
+ "Metric value and label must be Views; wrap text in Text(...)"
65
+ )
66
+
67
+ def body(self, context: Context) -> View:
68
+ """Stack and style the value and label using the default recipe."""
69
+ style = context.environment[self.style_token]
70
+ return VStack(
71
+ self.value.font(style.value_font).foreground_style(style.value_color),
72
+ self.label.font(style.label_font),
73
+ spacing=style.spacing,
74
+ )
@@ -0,0 +1,69 @@
1
+ """A semantic view for compact labeled content."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ from ...canvas import Stroke
8
+ from ...context import Context
9
+ from ...theme import (
10
+ Color,
11
+ PrimaryColor,
12
+ RadiusScale,
13
+ SpacingUnit,
14
+ SurfaceColor,
15
+ )
16
+ from ...tokens import DesignToken, TokenResolver
17
+ from ...units import Length
18
+ from ..rectangle import Rectangle
19
+ from ..view import View
20
+ from ..zstack import ZStack
21
+ from .semantic_view import StyledSemanticView
22
+
23
+
24
+ @dataclass(frozen=True)
25
+ class Pill(StyledSemanticView["Pill.Style"]):
26
+ """Display arbitrary content over a capsule-shaped background.
27
+
28
+ Args:
29
+ content: View displayed inside the pill.
30
+ """
31
+
32
+ @dataclass(frozen=True)
33
+ class Style:
34
+ background: Color
35
+ foreground: Color
36
+ padding: Length
37
+ corner_radius: Length
38
+ stroke: Stroke | None = None
39
+
40
+ @staticmethod
41
+ def _default_style(values: TokenResolver) -> Style:
42
+ return Pill.Style(
43
+ values[SurfaceColor],
44
+ values[PrimaryColor],
45
+ values[SpacingUnit] * 2,
46
+ values[SpacingUnit] * (4.5 * values[RadiusScale]),
47
+ )
48
+
49
+ style_token = DesignToken[Style](
50
+ "component.pill", lambda values: Pill._default_style(values), value_type=Style
51
+ )
52
+
53
+ content: View
54
+
55
+ def __post_init__(self) -> None:
56
+ if not isinstance(self.content, View):
57
+ raise TypeError("Pill.content must be a View; wrap text in Text(...)")
58
+
59
+ def body(self, context: Context) -> View:
60
+ """Place padded content over a themed capsule."""
61
+ style = context.environment[self.style_token]
62
+ return ZStack(
63
+ Rectangle(
64
+ corner_radius=style.corner_radius,
65
+ fill=style.background,
66
+ stroke=style.stroke,
67
+ ),
68
+ self.content.foreground_style(style.foreground).padding(style.padding),
69
+ )
@@ -0,0 +1,94 @@
1
+ """A semantic view for a connected sequence of pipeline stages."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+ from typing import cast
7
+
8
+ from ...annotations import Anchor, Annotated, Annotation, Connector
9
+ from ...context import Context
10
+ from ...geometry import RelativePoint
11
+ from ...theme import Color, PrimaryColor, SpacingUnit
12
+ from ...tokens import DesignToken, TokenResolver
13
+ from ...units import Length
14
+ from ..hstack import HStack
15
+ from ..view import View
16
+ from .pipeline_stage import PipelineStage
17
+ from .semantic_view import StyledSemanticView
18
+
19
+
20
+ @dataclass(frozen=True, init=False)
21
+ class Pipeline(StyledSemanticView["Pipeline.Style"]):
22
+ """Lay out pipeline stages and connect each adjacent pair.
23
+
24
+ Stages are assigned stable generated keys unless they already have explicit
25
+ keys. These keys anchor the connectors produced by the default recipe. Extra
26
+ annotations are layered onto the same content and may refer to explicit or
27
+ generated stage keys.
28
+
29
+ Args:
30
+ *stages: One or more stages in display order.
31
+ annotations: Additional annotations to render with the pipeline.
32
+
33
+ Raises:
34
+ ValueError: If no stages are supplied.
35
+ TypeError: If stages or annotations have invalid types.
36
+ """
37
+
38
+ @dataclass(frozen=True)
39
+ class Style:
40
+ spacing: Length
41
+ connector: Color
42
+
43
+ @staticmethod
44
+ def _default_style(values: TokenResolver) -> Style:
45
+ return Pipeline.Style(values[SpacingUnit] * 8, values[PrimaryColor])
46
+
47
+ style_token = DesignToken[Style](
48
+ "component.pipeline",
49
+ lambda values: Pipeline._default_style(values),
50
+ value_type=Style,
51
+ )
52
+
53
+ stages: tuple[PipelineStage, ...]
54
+ annotations: tuple[Annotation, ...]
55
+
56
+ def __init__(
57
+ self,
58
+ *stages: PipelineStage,
59
+ annotations: tuple[Annotation, ...] = (),
60
+ ) -> None:
61
+ if not stages:
62
+ raise ValueError("Pipeline requires at least one PipelineStage")
63
+ if not all(isinstance(stage, PipelineStage) for stage in stages):
64
+ raise TypeError("Pipeline children must be PipelineStage instances")
65
+ if not all(isinstance(annotation, Annotation) for annotation in annotations):
66
+ raise TypeError("Pipeline annotations must inherit Annotation")
67
+ object.__setattr__(self, "stages", tuple(stages))
68
+ object.__setattr__(self, "annotations", tuple(annotations))
69
+
70
+ def body(self, context: Context) -> View:
71
+ """Arrange stages horizontally and connect adjacent stage anchors."""
72
+ style = context.environment[self.style_token]
73
+ keyed_stages: list[PipelineStage] = []
74
+ keys: list[str] = []
75
+ for index, stage in enumerate(self.stages):
76
+ modifiers = dict(stage._modifiers)
77
+ key = (
78
+ cast(str, modifiers["key"])
79
+ if "key" in modifiers
80
+ else f"pipeline-stage-{index}"
81
+ )
82
+ keys.append(key)
83
+ keyed_stages.append(stage if "key" in modifiers else stage.key(key))
84
+
85
+ content: View = HStack(*keyed_stages, spacing=style.spacing)
86
+ connectors = tuple(
87
+ Connector(
88
+ Anchor(start, RelativePoint.right),
89
+ Anchor(end, RelativePoint.left),
90
+ style=style.connector,
91
+ )
92
+ for start, end in zip(keys, keys[1:])
93
+ )
94
+ return Annotated(content, *connectors, *self.annotations)
@@ -0,0 +1,59 @@
1
+ """A semantic view representing one stage in a pipeline."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ from ...canvas import Stroke
8
+ from ...context import Context
9
+ from ...theme import Color, PrimaryColor, SpacingUnit, SurfaceColor
10
+ from ...tokens import DesignToken, TokenResolver
11
+ from ...units import Length
12
+ from ..rectangle import Rectangle
13
+ from ..view import View
14
+ from ..zstack import ZStack
15
+ from .semantic_view import StyledSemanticView
16
+
17
+
18
+ @dataclass(frozen=True)
19
+ class PipelineStage(StyledSemanticView["PipelineStage.Style"]):
20
+ """Display a labeled stage suitable for use in a :class:`Pipeline`.
21
+
22
+ Args:
23
+ label: Arbitrary view content shown inside the stage.
24
+ """
25
+
26
+ @dataclass(frozen=True)
27
+ class Style:
28
+ background: Color
29
+ foreground: Color
30
+ padding: Length
31
+ stroke: Stroke | None = None
32
+
33
+ @staticmethod
34
+ def _default_style(values: TokenResolver) -> Style:
35
+ return PipelineStage.Style(
36
+ values[SurfaceColor], values[PrimaryColor], values[SpacingUnit] * 5
37
+ )
38
+
39
+ style_token = DesignToken[Style](
40
+ "component.pipeline-stage",
41
+ lambda values: PipelineStage._default_style(values),
42
+ value_type=Style,
43
+ )
44
+
45
+ label: View
46
+
47
+ def __post_init__(self) -> None:
48
+ if not isinstance(self.label, View):
49
+ raise TypeError(
50
+ "PipelineStage.label must be a View; wrap text in Text(...)"
51
+ )
52
+
53
+ def body(self, context: Context) -> View:
54
+ """Place the padded label over a themed surface rectangle."""
55
+ style = context.environment[self.style_token]
56
+ return ZStack(
57
+ Rectangle(fill=style.background, stroke=style.stroke),
58
+ self.label.foreground_style(style.foreground).padding(style.padding),
59
+ )