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,202 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Sequence
4
+ from dataclasses import dataclass
5
+
6
+ from ..geometry import RelativePoint
7
+ from ..layout import (
8
+ LayoutChild,
9
+ LayoutContext,
10
+ LayoutPlan,
11
+ Placement,
12
+ ProposedSize,
13
+ StackAxis,
14
+ VerticalAlignment,
15
+ )
16
+ from ..units import Length, pt, require_length
17
+ from ._content import content
18
+
19
+
20
+ def _flex_shares(
21
+ children: Sequence[LayoutChild],
22
+ indices: list[int],
23
+ remaining: Length,
24
+ axis: StackAxis,
25
+ ) -> dict[int, Length]:
26
+ dimension = 0 if axis == "horizontal" else 1
27
+ floors = {
28
+ i: (getattr(children[i], "flex_minimum", None) or (pt(0), pt(0)))[dimension]
29
+ for i in indices
30
+ }
31
+ # Reserve the most demanding floors first, then share the remaining space
32
+ # by weight once every unassigned child can receive at least its minimum.
33
+ ordered = sorted(
34
+ indices, key=lambda i: floors[i].points / children[i].flex, reverse=True
35
+ )
36
+ weights = []
37
+ weight = 0.0
38
+ for i in reversed(ordered):
39
+ weight += children[i].flex
40
+ weights.append(weight)
41
+ shares: dict[int, Length] = {}
42
+ for index, (child_index, weight) in enumerate(zip(ordered, reversed(weights))):
43
+ child_weight = children[child_index].flex
44
+ floor = floors[child_index]
45
+ if floor <= remaining * (child_weight / weight):
46
+ shares.update(
47
+ (i, remaining * (children[i].flex / weight)) for i in ordered[index:]
48
+ )
49
+ break
50
+ shares[child_index] = floor
51
+ remaining = max(pt(0), remaining - floor)
52
+ return shares
53
+
54
+
55
+ @dataclass(frozen=True)
56
+ class StackLayout:
57
+ axis: StackAxis = "horizontal"
58
+ spacing: Length = pt(16)
59
+ cross_alignment: float = 0.0
60
+ vertical_alignment: VerticalAlignment | None = None
61
+
62
+ def __post_init__(self) -> None:
63
+ if self.axis not in ("horizontal", "vertical"):
64
+ raise ValueError("Stack axis must be horizontal or vertical")
65
+ require_length(self.spacing)
66
+ if self.spacing < pt(0):
67
+ raise ValueError("Spacing must be nonnegative")
68
+ if not 0.0 <= self.cross_alignment <= 1.0:
69
+ raise ValueError("Cross-axis alignment must be between zero and one")
70
+ if self.vertical_alignment is not None and self.axis != "horizontal":
71
+ raise ValueError("Vertical guides require a horizontal stack")
72
+
73
+ def measure(
74
+ self,
75
+ proposal: ProposedSize,
76
+ children: Sequence[LayoutChild],
77
+ context: LayoutContext,
78
+ ) -> LayoutPlan:
79
+ horizontal = self.axis == "horizontal"
80
+ available = proposal.width if horizontal else proposal.height
81
+ cross_proposal = proposal.height if horizontal else proposal.width
82
+
83
+ def main(plan: LayoutPlan) -> Length:
84
+ return plan.width if horizontal else plan.height
85
+
86
+ def cross(plan: LayoutPlan) -> Length:
87
+ return plan.height if horizontal else plan.width
88
+
89
+ def request(value: Length | None, cross_size: Length | None) -> ProposedSize:
90
+ return (
91
+ ProposedSize(value, cross_size, self.axis)
92
+ if horizontal
93
+ else ProposedSize(cross_size, value, self.axis)
94
+ )
95
+
96
+ stretching = tuple(
97
+ getattr(child, "stretches_cross_axis", False) for child in children
98
+ )
99
+ plans: dict[int, LayoutPlan] = {}
100
+ used = self.spacing * max(0, len(children) - 1)
101
+ flexible = []
102
+ for i, child in enumerate(children):
103
+ if child.flex:
104
+ flexible.append(i)
105
+ else:
106
+ plans[i] = child.measure(
107
+ request(None, None if stretching[i] else cross_proposal)
108
+ )
109
+ used += main(plans[i])
110
+ shares = (
111
+ _flex_shares(children, flexible, max(pt(0), available - used), self.axis)
112
+ if available is not None
113
+ else {}
114
+ )
115
+ for i in flexible:
116
+ plans[i] = children[i].measure(
117
+ request(shares.get(i), None if stretching[i] else cross_proposal)
118
+ )
119
+ other = max((cross(plan) for plan in plans.values()), default=pt(0))
120
+ guide_offsets: dict[int, Length] = {}
121
+ if self.vertical_alignment is not None:
122
+ guides = {
123
+ i: plan.vertical_guide(self.vertical_alignment)
124
+ for i, plan in plans.items()
125
+ if not stretching[i]
126
+ }
127
+ row_guide = max(guides.values(), default=pt(0))
128
+ guide_offsets = {i: row_guide - value for i, value in guides.items()}
129
+ other = max(
130
+ other,
131
+ max(
132
+ (plans[i].height + offset for i, offset in guide_offsets.items()),
133
+ default=pt(0),
134
+ ),
135
+ )
136
+ # Stretching children establish their minimum before receiving the
137
+ # resolved cross size. Preserve their original main-axis proposals.
138
+ for i, stretches in enumerate(stretching):
139
+ if stretches:
140
+ plans[i] = children[i].measure(request(shares.get(i), other))
141
+ cursor = pt(0)
142
+ placements = []
143
+ for i, child in enumerate(children):
144
+ plan = plans[i]
145
+ cross_offset = guide_offsets.get(
146
+ i, (other - cross(plan)) * self.cross_alignment
147
+ )
148
+ placements.append(
149
+ Placement(
150
+ child,
151
+ plan,
152
+ cursor if horizontal else cross_offset,
153
+ cross_offset if horizontal else cursor,
154
+ propagates_guides=not stretching[i],
155
+ )
156
+ )
157
+ cursor += main(plan) + self.spacing
158
+ extent = max(pt(0), cursor - self.spacing)
159
+ width, height = (extent, other) if horizontal else (other, extent)
160
+ return content(width, height, tuple(placements))
161
+
162
+
163
+ @dataclass(frozen=True)
164
+ class OverlayLayout:
165
+ alignment: RelativePoint = RelativePoint.center
166
+
167
+ def measure(
168
+ self,
169
+ proposal: ProposedSize,
170
+ children: Sequence[LayoutChild],
171
+ context: LayoutContext,
172
+ ) -> LayoutPlan:
173
+ unconstrained_axis = ProposedSize(proposal.width, proposal.height)
174
+ initial = [child.measure(unconstrained_axis) for child in children]
175
+ width = max((plan.width for plan in initial), default=pt(0))
176
+ height = max((plan.height for plan in initial), default=pt(0))
177
+ plans = [child.measure(ProposedSize(width, height)) for child in children]
178
+ width = max([width] + [plan.width for plan in plans])
179
+ height = max([height] + [plan.height for plan in plans])
180
+ placements = tuple(
181
+ Placement(
182
+ child,
183
+ plan,
184
+ (width - plan.width) * self.alignment.x,
185
+ (height - plan.height) * self.alignment.y,
186
+ )
187
+ for child, plan in zip(children, plans)
188
+ )
189
+ return content(width, height, placements)
190
+
191
+
192
+ @dataclass(frozen=True)
193
+ class PassthroughLayout:
194
+ def measure(
195
+ self,
196
+ proposal: ProposedSize,
197
+ children: Sequence[LayoutChild],
198
+ context: LayoutContext,
199
+ ) -> LayoutPlan:
200
+ (child,) = children
201
+ plan = child.measure(proposal)
202
+ return content(plan.width, plan.height, (Placement(child, plan),))
@@ -0,0 +1,299 @@
1
+ """Shared column sizing and row placement for grids and tables."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Sequence
6
+ from dataclasses import dataclass
7
+ from math import isfinite
8
+
9
+ from ..geometry import RelativePoint
10
+ from ..layout import (
11
+ LayoutChild,
12
+ LayoutContext,
13
+ LayoutOverflow,
14
+ LayoutPlan,
15
+ Placement,
16
+ ProposedSize,
17
+ )
18
+ from ..units import Length, pt, require_length
19
+
20
+ _EPSILON = pt(1e-6)
21
+
22
+
23
+ @dataclass(frozen=True)
24
+ class ColumnWidth:
25
+ """Bounds, preferred width and share of extra width for a column."""
26
+
27
+ minimum: Length = pt(0)
28
+ ideal: Length | None = None
29
+ maximum: Length | None = None
30
+ weight: float = 0
31
+
32
+ def __post_init__(self) -> None:
33
+ for length in (self.minimum, self.ideal, self.maximum):
34
+ if length is not None:
35
+ require_length(length)
36
+ if length < pt(0):
37
+ raise ValueError("Column widths must be nonnegative")
38
+ if self.maximum is not None and self.maximum < self.minimum:
39
+ raise ValueError("Column maximum must be at least its minimum")
40
+ if self.ideal is not None and (
41
+ self.ideal < self.minimum
42
+ or (self.maximum is not None and self.ideal > self.maximum)
43
+ ):
44
+ raise ValueError("Column ideal must be within its width limits")
45
+ if not isfinite(self.weight) or self.weight < 0:
46
+ raise ValueError("Column weight must be finite and nonnegative")
47
+
48
+
49
+ @dataclass(frozen=True)
50
+ class _SpanWidth:
51
+ start: int
52
+ end: int
53
+ minimum: Length
54
+ ideal: Length
55
+
56
+
57
+ def _fit_spans(
58
+ widths: list[Length],
59
+ maximums: Sequence[Length | None],
60
+ spans: Sequence[tuple[int, int, Length]],
61
+ ) -> list[Length]:
62
+ if not spans:
63
+ return widths
64
+ # A span constrains the difference between two column boundaries.
65
+ # Propagate lower bounds forward and upper bounds backward so overlapping
66
+ # spans can share width. The least feasible boundaries minimize total width.
67
+ edges = [(i, i + 1, width) for i, width in enumerate(widths)]
68
+ edges.extend(spans)
69
+ edges.extend(
70
+ (i + 1, i, -limit) for i, limit in enumerate(maximums) if limit is not None
71
+ )
72
+ boundaries = [pt(0)] * (len(widths) + 1)
73
+ for _ in boundaries:
74
+ changed = False
75
+ for start, end, width in edges:
76
+ bound = boundaries[start] + width
77
+ if bound > boundaries[end] + _EPSILON:
78
+ boundaries[end] = bound
79
+ changed = True
80
+ if not changed:
81
+ return [end - start for start, end in zip(boundaries, boundaries[1:])]
82
+ raise AssertionError("Infeasible column widths")
83
+
84
+
85
+ def _resolve_column_widths(
86
+ columns: Sequence[Length | ColumnWidth],
87
+ natural: Sequence[Length],
88
+ required: Sequence[Length],
89
+ proposal: ProposedSize,
90
+ *,
91
+ spacing: Length = pt(0),
92
+ spans: Sequence[_SpanWidth] = (),
93
+ minimum_only: bool = False,
94
+ ) -> list[Length]:
95
+ minimums = []
96
+ ideals = []
97
+ maximums: list[Length | None] = []
98
+ for column, preferred, minimum in zip(columns, natural, required):
99
+ if isinstance(column, Length):
100
+ ideal = column
101
+ maximum: Length | None = column
102
+ else:
103
+ minimum = max(column.minimum, minimum)
104
+ ideal = max(minimum, column.ideal or preferred)
105
+ maximum = column.maximum
106
+ if maximum is not None:
107
+ ideal = min(ideal, maximum)
108
+ if ideal + _EPSILON < minimum:
109
+ raise LayoutOverflow(None, (ideal, pt(0)), (minimum, None))
110
+ minimums.append(ideal if isinstance(column, Length) else minimum)
111
+ ideals.append(ideal)
112
+ maximums.append(maximum)
113
+
114
+ required_spans = []
115
+ preferred_spans = []
116
+ for span in spans:
117
+ limits = maximums[span.start : span.end]
118
+ ideal = max(span.minimum, span.ideal)
119
+ if all(limit is not None for limit in limits):
120
+ maximum = sum((limit for limit in limits if limit is not None), pt(0))
121
+ if span.minimum > maximum + _EPSILON:
122
+ raise LayoutOverflow(
123
+ None, (maximum, proposal.height or pt(0)), (span.minimum, None)
124
+ )
125
+ ideal = min(ideal, maximum)
126
+ required_spans.append((span.start, span.end, span.minimum))
127
+ preferred_spans.append((span.start, span.end, ideal))
128
+ minimums = _fit_spans(minimums, maximums, required_spans)
129
+ if minimum_only:
130
+ return minimums
131
+ # Use a feasible preferred arrangement as the other end of interpolation.
132
+ # When spans move width among columns, both endpoints still honor the
133
+ # required constraints.
134
+ widths = _fit_spans(ideals, maximums, preferred_spans)
135
+ if proposal.width is None:
136
+ return widths
137
+ gaps = spacing * max(0, len(columns) - 1)
138
+ floor = sum(minimums, gaps)
139
+ if floor > proposal.width + _EPSILON:
140
+ raise LayoutOverflow(
141
+ None, (proposal.width, proposal.height or pt(0)), (floor, None)
142
+ )
143
+ preferred = sum(widths, gaps)
144
+ if preferred > proposal.width:
145
+ slack = preferred - floor
146
+ fraction = max(0.0, (proposal.width - floor) / slack) if slack else 0.0
147
+ return [lo + (hi - lo) * fraction for lo, hi in zip(minimums, widths)]
148
+
149
+ # Redistribute shares when a growing column reaches its maximum.
150
+ extra = proposal.width - preferred
151
+ growing = {
152
+ index: column
153
+ for index, column in enumerate(columns)
154
+ if isinstance(column, ColumnWidth) and column.weight > 0
155
+ }
156
+ while growing and extra > _EPSILON:
157
+ weight = sum(column.weight for column in growing.values())
158
+ for index, column in tuple(growing.items()):
159
+ share = extra * (column.weight / weight)
160
+ if column.maximum is not None and widths[index] + share >= column.maximum:
161
+ extra -= column.maximum - widths[index]
162
+ widths[index] = column.maximum
163
+ del growing[index]
164
+ break
165
+ else:
166
+ for index, column in growing.items():
167
+ widths[index] += extra * (column.weight / weight)
168
+ break
169
+ return widths
170
+
171
+
172
+ @dataclass(frozen=True)
173
+ class _CellArea:
174
+ x: Length
175
+ width: Length
176
+ alignment: RelativePoint
177
+ leading: Length = pt(0)
178
+ top: Length = pt(0)
179
+ horizontal_padding: Length = pt(0)
180
+ vertical_padding: Length = pt(0)
181
+
182
+
183
+ def _measure_grid_row(
184
+ children: Sequence[LayoutChild], areas: Sequence[_CellArea], width: Length
185
+ ) -> LayoutPlan:
186
+ plans = []
187
+ height = pt(0)
188
+ for child, area in zip(children, areas):
189
+ inner_width = max(pt(0), area.width - area.horizontal_padding)
190
+ plan = child.measure(ProposedSize(width=inner_width))
191
+ outer_height = plan.height + area.vertical_padding
192
+ if plan.width > inner_width + _EPSILON:
193
+ raise LayoutOverflow(
194
+ child.identity,
195
+ (area.width, outer_height),
196
+ (plan.width + area.horizontal_padding, None),
197
+ )
198
+ plans.append(plan)
199
+ height = max(height, outer_height)
200
+ placements = tuple(
201
+ Placement(
202
+ child,
203
+ plan,
204
+ area.x
205
+ + area.leading
206
+ + (area.width - area.horizontal_padding - plan.width) * area.alignment.x,
207
+ area.top
208
+ + (height - area.vertical_padding - plan.height) * area.alignment.y,
209
+ )
210
+ for child, area, plan in zip(children, areas, plans)
211
+ )
212
+ return LayoutPlan(width, height, placements)
213
+
214
+
215
+ @dataclass(frozen=True)
216
+ class _GridSlot:
217
+ start: int
218
+ end: int
219
+ alignment: RelativePoint
220
+ contributes_width: bool
221
+
222
+
223
+ @dataclass(frozen=True)
224
+ class GridLayout:
225
+ columns: tuple[Length | ColumnWidth, ...]
226
+ rows: tuple[tuple[_GridSlot, ...], ...]
227
+ horizontal_spacing: Length
228
+ vertical_spacing: Length
229
+
230
+ def measure(
231
+ self,
232
+ proposal: ProposedSize,
233
+ children: Sequence[LayoutChild],
234
+ context: LayoutContext,
235
+ ) -> LayoutPlan:
236
+ slots = (slot for row in self.rows for slot in row)
237
+ natural = [pt(0)] * len(self.columns)
238
+ minimums = natural.copy()
239
+ spans = []
240
+ for child, slot in zip(children, slots):
241
+ if not slot.contributes_width:
242
+ continue
243
+ ideal = child.measure(ProposedSize()).width
244
+ try:
245
+ minimum = child.measure(ProposedSize(width=pt(0))).width
246
+ except LayoutOverflow:
247
+ # Some views require a usable proposal. Their ideal size is
248
+ # a conservative floor when they cannot answer a zero probe.
249
+ minimum = ideal
250
+ index = slot.start
251
+ count = slot.end - index
252
+ if count == 1:
253
+ minimums[index] = max(minimums[index], minimum)
254
+ natural[index] = max(natural[index], ideal)
255
+ else:
256
+ gaps = (count - 1) * self.horizontal_spacing
257
+ spans.append(
258
+ _SpanWidth(
259
+ index,
260
+ slot.end,
261
+ max(pt(0), minimum - gaps),
262
+ max(pt(0), ideal - gaps),
263
+ )
264
+ )
265
+ widths = _resolve_column_widths(
266
+ self.columns,
267
+ natural,
268
+ minimums,
269
+ proposal,
270
+ spacing=self.horizontal_spacing,
271
+ spans=spans,
272
+ minimum_only=proposal.width == pt(0),
273
+ )
274
+ offsets = [pt(0)]
275
+ for width in widths:
276
+ offsets.append(offsets[-1] + width + self.horizontal_spacing)
277
+ extent = offsets[-1] - self.horizontal_spacing if widths else pt(0)
278
+ placements: list[Placement] = []
279
+ index = 0
280
+ y = pt(0)
281
+ for row in self.rows:
282
+ areas = [
283
+ _CellArea(
284
+ offsets[slot.start],
285
+ offsets[slot.end] - offsets[slot.start] - self.horizontal_spacing,
286
+ slot.alignment,
287
+ )
288
+ for slot in row
289
+ ]
290
+ end = index + len(row)
291
+ plan = _measure_grid_row(children[index:end], areas, extent)
292
+ placements.extend(
293
+ Placement(p.child, p.plan, p.x, p.y + y) for p in plan.placements
294
+ )
295
+ index = end
296
+ y += plan.height + self.vertical_spacing
297
+ return LayoutPlan(
298
+ extent, max(pt(0), y - self.vertical_spacing), tuple(placements)
299
+ )
@@ -0,0 +1,200 @@
1
+ from __future__ import annotations
2
+
3
+ from collections.abc import Sequence
4
+ from dataclasses import dataclass
5
+
6
+ from ..geometry import RelativePoint
7
+ from ..layout import LayoutChild, LayoutContext, LayoutPlan, Placement, ProposedSize
8
+ from ..units import Length, pt
9
+ from ..views.modifier_views import (
10
+ EdgeInsets,
11
+ EdgeSet,
12
+ FrameMaximum,
13
+ FrameSpec,
14
+ _padding_insets,
15
+ unbounded,
16
+ )
17
+ from ._content import content
18
+
19
+
20
+ @dataclass(frozen=True)
21
+ class LayeredLayout:
22
+ alignment: RelativePoint
23
+ in_front: bool
24
+
25
+ def measure(
26
+ self,
27
+ proposal: ProposedSize,
28
+ children: Sequence[LayoutChild],
29
+ context: LayoutContext,
30
+ ) -> LayoutPlan:
31
+ primary, decoration = children
32
+ main = primary.measure(proposal)
33
+ layer = decoration.measure(ProposedSize(main.width, main.height))
34
+ foreground = Placement(primary, main)
35
+ secondary = Placement(
36
+ decoration,
37
+ layer,
38
+ (main.width - layer.width) * self.alignment.x,
39
+ (main.height - layer.height) * self.alignment.y,
40
+ propagates_guides=False,
41
+ )
42
+ placements = (
43
+ (foreground, secondary) if self.in_front else (secondary, foreground)
44
+ )
45
+ return content(main.width, main.height, placements)
46
+
47
+
48
+ @dataclass(frozen=True)
49
+ class PlacementLayout:
50
+ x: Length
51
+ y: Length
52
+ anchor: RelativePoint | None
53
+
54
+ def measure(
55
+ self,
56
+ proposal: ProposedSize,
57
+ children: Sequence[LayoutChild],
58
+ context: LayoutContext,
59
+ ) -> LayoutPlan:
60
+ (child,) = children
61
+ plan = child.measure(proposal)
62
+ x, y = self.x, self.y
63
+ width, height = plan.width, plan.height
64
+ if self.anchor is not None:
65
+ x -= plan.width * self.anchor.x
66
+ y -= plan.height * self.anchor.y
67
+ width = proposal.width if proposal.width is not None else width
68
+ height = proposal.height if proposal.height is not None else height
69
+ return LayoutPlan(
70
+ width,
71
+ height,
72
+ (Placement(child, plan, x, y, propagates_guides=self.anchor is not None),),
73
+ {"content_offset": (x, y), "content_size": (plan.width, plan.height)},
74
+ vertical_guides=plan.vertical_guides if self.anchor is None else {},
75
+ )
76
+
77
+
78
+ def _subtract(value: Length | None, amount: Length) -> Length | None:
79
+ return None if value is None else max(pt(0), value - amount)
80
+
81
+
82
+ @dataclass(frozen=True, init=False)
83
+ class PaddingLayout:
84
+ insets: EdgeInsets
85
+
86
+ def __init__(
87
+ self, amount: Length | EdgeInsets, edges: EdgeSet | None = None
88
+ ) -> None:
89
+ object.__setattr__(self, "insets", _padding_insets(amount, edges))
90
+
91
+ def measure(
92
+ self,
93
+ proposal: ProposedSize,
94
+ children: Sequence[LayoutChild],
95
+ context: LayoutContext,
96
+ ) -> LayoutPlan:
97
+ (child,) = children
98
+ insets = self.insets
99
+ plan = child.measure(
100
+ ProposedSize(
101
+ _subtract(proposal.width, insets.horizontal),
102
+ _subtract(proposal.height, insets.vertical),
103
+ proposal.stack_axis,
104
+ )
105
+ )
106
+ return LayoutPlan(
107
+ plan.width + insets.horizontal,
108
+ plan.height + insets.vertical,
109
+ (Placement(child, plan, insets.leading, insets.top),),
110
+ {
111
+ "content_offset": (insets.leading, insets.top),
112
+ "content_size": (plan.width, plan.height),
113
+ },
114
+ )
115
+
116
+
117
+ def _clamp(
118
+ value: Length, minimum: Length | None, maximum: FrameMaximum | None
119
+ ) -> Length:
120
+ if minimum is not None:
121
+ value = max(value, minimum)
122
+ if isinstance(maximum, Length):
123
+ value = min(value, maximum)
124
+ return value
125
+
126
+
127
+ def _child_dimension(
128
+ proposed: Length | None,
129
+ exact: Length | None,
130
+ minimum: Length | None,
131
+ ideal: Length | None,
132
+ maximum: FrameMaximum | None,
133
+ ) -> Length | None:
134
+ if exact is not None:
135
+ return exact
136
+ value = ideal if proposed is None and ideal is not None else proposed
137
+ return None if value is None else _clamp(value, minimum, maximum)
138
+
139
+
140
+ def _frame_dimension(
141
+ proposed: Length | None,
142
+ child: Length,
143
+ exact: Length | None,
144
+ minimum: Length | None,
145
+ ideal: Length | None,
146
+ maximum: FrameMaximum | None,
147
+ ) -> Length:
148
+ if exact is not None:
149
+ return exact
150
+ if maximum is unbounded and proposed is not None:
151
+ return max(proposed, minimum or pt(0))
152
+ if proposed is None and ideal is not None:
153
+ return _clamp(ideal, minimum, maximum)
154
+ return _clamp(child, minimum, maximum)
155
+
156
+
157
+ @dataclass(frozen=True)
158
+ class FrameLayout:
159
+ spec: FrameSpec
160
+
161
+ def measure(
162
+ self,
163
+ proposal: ProposedSize,
164
+ children: Sequence[LayoutChild],
165
+ context: LayoutContext,
166
+ ) -> LayoutPlan:
167
+ (child,) = children
168
+ s = self.spec
169
+ child_proposal = ProposedSize(
170
+ _child_dimension(
171
+ proposal.width, s.width, s.min_width, s.ideal_width, s.max_width
172
+ ),
173
+ _child_dimension(
174
+ proposal.height, s.height, s.min_height, s.ideal_height, s.max_height
175
+ ),
176
+ proposal.stack_axis,
177
+ )
178
+ plan = child.measure(child_proposal)
179
+ width = _frame_dimension(
180
+ proposal.width, plan.width, s.width, s.min_width, s.ideal_width, s.max_width
181
+ )
182
+ height = _frame_dimension(
183
+ proposal.height,
184
+ plan.height,
185
+ s.height,
186
+ s.min_height,
187
+ s.ideal_height,
188
+ s.max_height,
189
+ )
190
+ x = (width - plan.width) * s.alignment.x
191
+ y = (height - plan.height) * s.alignment.y
192
+ return LayoutPlan(
193
+ width,
194
+ height,
195
+ (Placement(child, plan, x, y),),
196
+ {
197
+ "content_offset": (x, y),
198
+ "content_size": (plan.width, plan.height),
199
+ },
200
+ )