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.
- deckkit/__init__.py +12 -0
- deckkit/_bundled_fonts.py +54 -0
- deckkit/_diagnostics.py +48 -0
- deckkit/_font_assets/SOURCES.txt +9 -0
- deckkit/_font_assets/SourceCodePro-Bold.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-It.ttf +0 -0
- deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceCodePro-Regular.ttf +0 -0
- deckkit/_font_assets/SourceSans3-Bold.ttf +0 -0
- deckkit/_font_assets/SourceSans3-BoldIt.ttf +0 -0
- deckkit/_font_assets/SourceSans3-It.ttf +0 -0
- deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit/_font_assets/SourceSans3-Regular.ttf +0 -0
- deckkit/_variable_evaluation.py +82 -0
- deckkit/animation.py +211 -0
- deckkit/annotations.py +161 -0
- deckkit/backends/__init__.py +1 -0
- deckkit/backends/_font_embedding.py +41 -0
- deckkit/backends/_pptx_fonts.py +230 -0
- deckkit/backends/_pptx_shadows.py +241 -0
- deckkit/backends/_pptx_tables.py +251 -0
- deckkit/backends/pptx.py +1225 -0
- deckkit/backends/pptx_animation.py +175 -0
- deckkit/backends/svg.py +527 -0
- deckkit/canvas/__init__.py +40 -0
- deckkit/canvas/_geometry.py +49 -0
- deckkit/canvas/clipping.py +336 -0
- deckkit/canvas/context.py +34 -0
- deckkit/canvas/elements.py +306 -0
- deckkit/canvas/path.py +250 -0
- deckkit/canvas/scene.py +74 -0
- deckkit/canvas/style.py +31 -0
- deckkit/charts/__init__.py +59 -0
- deckkit/charts/axes.py +249 -0
- deckkit/charts/chart.py +244 -0
- deckkit/charts/content.py +132 -0
- deckkit/charts/context.py +45 -0
- deckkit/charts/interval.py +166 -0
- deckkit/charts/legends.py +82 -0
- deckkit/charts/marks.py +410 -0
- deckkit/charts/scales.py +205 -0
- deckkit/citations.py +200 -0
- deckkit/compiler.py +410 -0
- deckkit/context.py +72 -0
- deckkit/deck.py +168 -0
- deckkit/drawables/__init__.py +42 -0
- deckkit/drawables/bounds.py +17 -0
- deckkit/drawables/connector.py +22 -0
- deckkit/drawables/drawable.py +38 -0
- deckkit/drawables/ellipse.py +21 -0
- deckkit/drawables/group.py +10 -0
- deckkit/drawables/image.py +14 -0
- deckkit/drawables/line.py +18 -0
- deckkit/drawables/path.py +78 -0
- deckkit/drawables/rectangle.py +24 -0
- deckkit/drawables/rich_text.py +176 -0
- deckkit/drawables/table.py +42 -0
- deckkit/drawables/text.py +29 -0
- deckkit/effects.py +84 -0
- deckkit/expanded_views.py +561 -0
- deckkit/fonts.py +329 -0
- deckkit/geometry.py +48 -0
- deckkit/identity.py +72 -0
- deckkit/layout.py +398 -0
- deckkit/layouts/__init__.py +20 -0
- deckkit/layouts/_content.py +27 -0
- deckkit/layouts/annotations.py +217 -0
- deckkit/layouts/containers.py +202 -0
- deckkit/layouts/grid.py +299 -0
- deckkit/layouts/modifiers.py +200 -0
- deckkit/layouts/primitives.py +213 -0
- deckkit/layouts/table.py +139 -0
- deckkit/measurement.py +420 -0
- deckkit/paint.py +73 -0
- deckkit/py.typed +0 -0
- deckkit/reconcile.py +119 -0
- deckkit/semantic.py +47 -0
- deckkit/theme.py +364 -0
- deckkit/themes/__init__.py +91 -0
- deckkit/themes/clarity.py +98 -0
- deckkit/tokens.py +226 -0
- deckkit/units.py +73 -0
- deckkit/variables.py +212 -0
- deckkit/view_expansion.py +185 -0
- deckkit/views/__init__.py +100 -0
- deckkit/views/_code_source.py +42 -0
- deckkit/views/_drawing.py +52 -0
- deckkit/views/canvas.py +27 -0
- deckkit/views/code.py +481 -0
- deckkit/views/code_evolution.py +284 -0
- deckkit/views/composite_view.py +19 -0
- deckkit/views/divider.py +38 -0
- deckkit/views/ellipse.py +38 -0
- deckkit/views/grid.py +156 -0
- deckkit/views/hstack.py +43 -0
- deckkit/views/image.py +43 -0
- deckkit/views/layout_view.py +30 -0
- deckkit/views/line.py +31 -0
- deckkit/views/modifier_views.py +281 -0
- deckkit/views/path.py +36 -0
- deckkit/views/primitive_view.py +16 -0
- deckkit/views/pygments_highlighter.py +67 -0
- deckkit/views/rectangle.py +61 -0
- deckkit/views/section_number.py +21 -0
- deckkit/views/semantic/__init__.py +51 -0
- deckkit/views/semantic/before_after.py +109 -0
- deckkit/views/semantic/bullet_graph.py +216 -0
- deckkit/views/semantic/card.py +129 -0
- deckkit/views/semantic/code_block.py +41 -0
- deckkit/views/semantic/diff.py +642 -0
- deckkit/views/semantic/estimate_interval.py +137 -0
- deckkit/views/semantic/figure.py +55 -0
- deckkit/views/semantic/item_list.py +171 -0
- deckkit/views/semantic/metric.py +74 -0
- deckkit/views/semantic/pill.py +69 -0
- deckkit/views/semantic/pipeline.py +94 -0
- deckkit/views/semantic/pipeline_stage.py +59 -0
- deckkit/views/semantic/semantic_view.py +76 -0
- deckkit/views/semantic/sparkline.py +164 -0
- deckkit/views/semantic/table.py +439 -0
- deckkit/views/semantic/terminal.py +361 -0
- deckkit/views/semantic/timeline.py +158 -0
- deckkit/views/slide.py +37 -0
- deckkit/views/slide_number.py +20 -0
- deckkit/views/slides/__init__.py +16 -0
- deckkit/views/slides/section_slide.py +89 -0
- deckkit/views/slides/slide_margins.py +32 -0
- deckkit/views/slides/slide_with_margins.py +22 -0
- deckkit/views/slides/title_and_content.py +49 -0
- deckkit/views/slides/title_and_content_slide.py +55 -0
- deckkit/views/spacer.py +45 -0
- deckkit/views/stack.py +36 -0
- deckkit/views/text.py +142 -0
- deckkit/views/view.py +229 -0
- deckkit/views/vstack.py +31 -0
- deckkit/views/zstack.py +27 -0
- deckkit-0.1.0.dist-info/METADATA +123 -0
- deckkit-0.1.0.dist-info/RECORD +144 -0
- deckkit-0.1.0.dist-info/WHEEL +5 -0
- deckkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceCodePro-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/licenses/src/deckkit/_font_assets/SourceSans3-LICENSE.txt +93 -0
- deckkit-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Theme-overridable margins around slide content."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ...context import Context
|
|
8
|
+
from ...theme import SpacingUnit
|
|
9
|
+
from ...tokens import DesignToken
|
|
10
|
+
from ...units import Length
|
|
11
|
+
from ..semantic.semantic_view import SemanticView
|
|
12
|
+
from ..view import View
|
|
13
|
+
|
|
14
|
+
SlideMargin = DesignToken[Length](
|
|
15
|
+
"component.slide-margin",
|
|
16
|
+
lambda values: values[SpacingUnit] * 10,
|
|
17
|
+
value_type=Length,
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class SlideMargins(SemanticView):
|
|
23
|
+
"""Content placed within slide margins."""
|
|
24
|
+
|
|
25
|
+
content: View
|
|
26
|
+
|
|
27
|
+
def __post_init__(self) -> None:
|
|
28
|
+
if not isinstance(self.content, View):
|
|
29
|
+
raise TypeError("SlideMargins.content must be a View")
|
|
30
|
+
|
|
31
|
+
def body(self, context: Context) -> View:
|
|
32
|
+
return self.content.padding(context.environment[SlideMargin])
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Base class for slides whose content is placed within margins."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
|
|
7
|
+
from ...context import Context
|
|
8
|
+
from ..slide import Slide
|
|
9
|
+
from ..view import View
|
|
10
|
+
from .slide_margins import SlideMargins
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class SlideWithMargins(Slide, ABC):
|
|
14
|
+
"""A slide whose content is placed within margins."""
|
|
15
|
+
|
|
16
|
+
@abstractmethod
|
|
17
|
+
def body_without_margins(self, context: Context) -> View:
|
|
18
|
+
"""Return the content to place within the slide margins."""
|
|
19
|
+
raise NotImplementedError
|
|
20
|
+
|
|
21
|
+
def body(self, context: Context) -> View:
|
|
22
|
+
return SlideMargins(self.body_without_margins(context))
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Theme-overridable title-and-content composition."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ...context import Context
|
|
8
|
+
from ...theme import SpacingUnit, TitleTextStyle, Typography
|
|
9
|
+
from ...tokens import DesignToken, TokenResolver
|
|
10
|
+
from ...units import Length
|
|
11
|
+
from ..semantic.semantic_view import StyledSemanticView
|
|
12
|
+
from ..view import View
|
|
13
|
+
from ..vstack import VStack
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class TitleAndContent(StyledSemanticView["TitleAndContent.Style"]):
|
|
18
|
+
"""A title followed by content."""
|
|
19
|
+
|
|
20
|
+
@dataclass(frozen=True)
|
|
21
|
+
class Style:
|
|
22
|
+
title_font: Typography
|
|
23
|
+
spacing: Length
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def _default_style(values: TokenResolver) -> Style:
|
|
27
|
+
return TitleAndContent.Style(values[TitleTextStyle], values[SpacingUnit] * 8)
|
|
28
|
+
|
|
29
|
+
style_token = DesignToken[Style](
|
|
30
|
+
"component.title-and-content",
|
|
31
|
+
lambda values: TitleAndContent._default_style(values),
|
|
32
|
+
value_type=Style,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
title: View
|
|
36
|
+
content: View
|
|
37
|
+
|
|
38
|
+
def __post_init__(self) -> None:
|
|
39
|
+
for name in ("title", "content"):
|
|
40
|
+
if not isinstance(getattr(self, name), View):
|
|
41
|
+
raise TypeError(f"TitleAndContent.{name} must be a View")
|
|
42
|
+
|
|
43
|
+
def body(self, context: Context) -> View:
|
|
44
|
+
style = context.environment[self.style_token]
|
|
45
|
+
return VStack(
|
|
46
|
+
self.title.font(style.title_font),
|
|
47
|
+
self.content,
|
|
48
|
+
spacing=style.spacing,
|
|
49
|
+
)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""A semantic slide with a title and content."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
|
|
8
|
+
from ...context import Context
|
|
9
|
+
from ..text import Text
|
|
10
|
+
from ..view import View
|
|
11
|
+
from .slide_with_margins import SlideWithMargins
|
|
12
|
+
from .title_and_content import TitleAndContent
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TitleAndContentSlide(SlideWithMargins, ABC):
|
|
16
|
+
"""A slide with a title and content."""
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def of(title: str | View, content: View) -> TitleAndContentSlide:
|
|
20
|
+
"""Create a static slide from a title and a content view."""
|
|
21
|
+
title_view = Text(title) if isinstance(title, str) else title
|
|
22
|
+
return _StaticTitleAndContentSlide(TitleAndContent(title_view, content))
|
|
23
|
+
|
|
24
|
+
@property
|
|
25
|
+
@abstractmethod
|
|
26
|
+
def title(self) -> View:
|
|
27
|
+
"""The slide's title."""
|
|
28
|
+
raise NotImplementedError
|
|
29
|
+
|
|
30
|
+
@abstractmethod
|
|
31
|
+
def content(self, context: Context) -> View:
|
|
32
|
+
"""Return the slide's content for the current presentation state."""
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
|
|
35
|
+
def body_without_margins(self, context: Context) -> View:
|
|
36
|
+
return TitleAndContent(self.title, self.content(context))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class _StaticTitleAndContentSlide(TitleAndContentSlide):
|
|
41
|
+
_composition: TitleAndContent
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def title(self) -> View:
|
|
45
|
+
return self._composition.title
|
|
46
|
+
|
|
47
|
+
def content(self, context: Context) -> View:
|
|
48
|
+
return self._composition.content
|
|
49
|
+
|
|
50
|
+
def body_without_margins(self, context: Context) -> View:
|
|
51
|
+
return self._composition
|
|
52
|
+
|
|
53
|
+
def _diagnostic_label(self) -> str | None:
|
|
54
|
+
title = self._composition.title
|
|
55
|
+
return " ".join(title.text.split()) if isinstance(title, Text) else None
|
deckkit/views/spacer.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Adaptive or fixed empty space in a layout."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ..layout import Layout
|
|
8
|
+
from ..units import Length, pt, require_length
|
|
9
|
+
from .layout_view import LayoutView
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, init=False)
|
|
13
|
+
class Spacer(LayoutView):
|
|
14
|
+
"""Fill available space or reserve a fixed length on the stack's axis."""
|
|
15
|
+
|
|
16
|
+
length: Length | None
|
|
17
|
+
min_length: Length
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self, length: Length | None = None, *, min_length: Length = pt(0)
|
|
21
|
+
) -> None:
|
|
22
|
+
require_length(min_length)
|
|
23
|
+
if length is not None:
|
|
24
|
+
require_length(length)
|
|
25
|
+
if min_length < pt(0) or (length is not None and length < pt(0)):
|
|
26
|
+
raise ValueError("Spacer lengths must be nonnegative")
|
|
27
|
+
if length is not None and min_length > length:
|
|
28
|
+
raise ValueError("Spacer length cannot be smaller than its minimum")
|
|
29
|
+
object.__setattr__(self, "length", length)
|
|
30
|
+
object.__setattr__(self, "min_length", min_length)
|
|
31
|
+
super().__init__()
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
def _layout_flex(self) -> float:
|
|
35
|
+
return super()._layout_flex or (1.0 if self.length is None else 0.0)
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def _layout_flex_minimum(self) -> tuple[Length, Length]:
|
|
39
|
+
minimum = self.length if self.length is not None else self.min_length
|
|
40
|
+
return minimum, minimum
|
|
41
|
+
|
|
42
|
+
def resolve_layout(self, lookup: object) -> Layout:
|
|
43
|
+
from ..layouts.primitives import SpacerLayout
|
|
44
|
+
|
|
45
|
+
return SpacerLayout(self.length, self.min_length)
|
deckkit/views/stack.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import Callable
|
|
6
|
+
|
|
7
|
+
from ..identity import ViewIdentity
|
|
8
|
+
from ..layout import Layout
|
|
9
|
+
from ..units import Length, pt, require_length
|
|
10
|
+
from .layout_view import LayoutView
|
|
11
|
+
from .view import View
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True, init=False)
|
|
15
|
+
class Stack(LayoutView, ABC):
|
|
16
|
+
children: tuple[View | None, ...]
|
|
17
|
+
spacing: Length
|
|
18
|
+
|
|
19
|
+
@abstractmethod
|
|
20
|
+
def _make_layout(self) -> Layout:
|
|
21
|
+
raise NotImplementedError(type(self).__name__)
|
|
22
|
+
|
|
23
|
+
def __init__(self, *children: View | None, spacing: Length = pt(16)) -> None:
|
|
24
|
+
require_length(spacing)
|
|
25
|
+
if spacing < pt(0):
|
|
26
|
+
raise ValueError("Spacing must be nonnegative")
|
|
27
|
+
# Preserve absent slots for structural identity.
|
|
28
|
+
object.__setattr__(self, "spacing", spacing)
|
|
29
|
+
super().__init__(*children)
|
|
30
|
+
|
|
31
|
+
def resolve_layout(self, lookup: Callable[[str], ViewIdentity]) -> Layout:
|
|
32
|
+
return self._make_layout()
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def _expanded_spacing(self) -> Length:
|
|
36
|
+
return self.spacing
|
deckkit/views/text.py
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..context import Context
|
|
6
|
+
from ..expanded_views import ExpandedMetadata, ExpandedText
|
|
7
|
+
from ..layouts import TextLayout
|
|
8
|
+
from ..measurement import StyledTextLine, StyledTextSpan, TextFlow
|
|
9
|
+
from ..paint import LinearGradient
|
|
10
|
+
from ..theme import Color, ColorStyle, ForegroundColor, TextStyle, Typography
|
|
11
|
+
from ..tokens import DesignToken
|
|
12
|
+
from .primitive_view import PrimitiveView
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class TextSpan:
|
|
17
|
+
text: str
|
|
18
|
+
font_style: DesignToken[Typography] | None = None
|
|
19
|
+
foreground_style: ColorStyle | None = None
|
|
20
|
+
background_style: ColorStyle | None = None
|
|
21
|
+
|
|
22
|
+
def __post_init__(self) -> None:
|
|
23
|
+
if not isinstance(self.text, str):
|
|
24
|
+
raise TypeError("TextSpan.text must be a string")
|
|
25
|
+
if "\n" in self.text:
|
|
26
|
+
raise ValueError(
|
|
27
|
+
"TextSpan cannot contain newlines; use separate TextLine values"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass(frozen=True, init=False)
|
|
32
|
+
class TextLine:
|
|
33
|
+
spans: tuple[TextSpan, ...]
|
|
34
|
+
background_style: ColorStyle | None
|
|
35
|
+
|
|
36
|
+
def __init__(
|
|
37
|
+
self,
|
|
38
|
+
*spans: TextSpan | str,
|
|
39
|
+
background_style: ColorStyle | None = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
normalized = []
|
|
42
|
+
for span in spans:
|
|
43
|
+
if isinstance(span, TextSpan):
|
|
44
|
+
normalized.append(span)
|
|
45
|
+
elif isinstance(span, str):
|
|
46
|
+
normalized.append(TextSpan(span))
|
|
47
|
+
else:
|
|
48
|
+
raise TypeError("TextLine values must be strings or TextSpan values")
|
|
49
|
+
object.__setattr__(
|
|
50
|
+
self,
|
|
51
|
+
"spans",
|
|
52
|
+
tuple(normalized) or (TextSpan(""),),
|
|
53
|
+
)
|
|
54
|
+
object.__setattr__(self, "background_style", background_style)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
@dataclass(frozen=True, init=False)
|
|
58
|
+
class Text(PrimitiveView):
|
|
59
|
+
lines: tuple[TextLine, ...]
|
|
60
|
+
flow: TextFlow = "wrapped"
|
|
61
|
+
fill: LinearGradient | None = None
|
|
62
|
+
|
|
63
|
+
def __init__(
|
|
64
|
+
self,
|
|
65
|
+
content: str | TextLine,
|
|
66
|
+
*lines: TextLine,
|
|
67
|
+
flow: TextFlow = "wrapped",
|
|
68
|
+
fill: LinearGradient | None = None,
|
|
69
|
+
) -> None:
|
|
70
|
+
if flow not in ("wrapped", "verbatim"):
|
|
71
|
+
raise ValueError("Text flow must be 'wrapped' or 'verbatim'")
|
|
72
|
+
if isinstance(content, str):
|
|
73
|
+
if lines:
|
|
74
|
+
raise TypeError("Additional TextLine values require a TextLine first")
|
|
75
|
+
structured = tuple(TextLine(line) for line in content.split("\n"))
|
|
76
|
+
elif isinstance(content, TextLine):
|
|
77
|
+
structured = (content, *lines)
|
|
78
|
+
else:
|
|
79
|
+
raise TypeError("Text content must be a string or TextLine")
|
|
80
|
+
object.__setattr__(self, "lines", structured)
|
|
81
|
+
object.__setattr__(self, "flow", flow)
|
|
82
|
+
if fill is not None and not isinstance(fill, LinearGradient):
|
|
83
|
+
raise TypeError("Text fill must be a LinearGradient")
|
|
84
|
+
object.__setattr__(self, "fill", fill)
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def text(self) -> str:
|
|
88
|
+
return "\n".join(
|
|
89
|
+
"".join(span.text for span in line.spans) for line in self.lines
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
def _resolved_lines(self, context: Context) -> tuple[StyledTextLine, ...]:
|
|
93
|
+
environment = context.environment
|
|
94
|
+
|
|
95
|
+
def color(style: ColorStyle) -> str:
|
|
96
|
+
return (style if isinstance(style, Color) else environment[style]).hex
|
|
97
|
+
|
|
98
|
+
base_typography = environment[TextStyle]
|
|
99
|
+
base_foreground = environment[ForegroundColor]
|
|
100
|
+
result = []
|
|
101
|
+
for line in self.lines:
|
|
102
|
+
spans = []
|
|
103
|
+
for span in line.spans:
|
|
104
|
+
typography = (
|
|
105
|
+
environment[span.font_style]
|
|
106
|
+
if span.font_style is not None
|
|
107
|
+
else base_typography
|
|
108
|
+
)
|
|
109
|
+
foreground = span.foreground_style or base_foreground
|
|
110
|
+
spans.append(
|
|
111
|
+
StyledTextSpan(
|
|
112
|
+
span.text,
|
|
113
|
+
typography.font,
|
|
114
|
+
typography.size,
|
|
115
|
+
typography.line_spacing,
|
|
116
|
+
color(foreground),
|
|
117
|
+
color(span.background_style)
|
|
118
|
+
if span.background_style is not None
|
|
119
|
+
else None,
|
|
120
|
+
line_height=typography.line_height,
|
|
121
|
+
fallback_fonts=typography.fallback_fonts,
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
result.append(
|
|
125
|
+
StyledTextLine(
|
|
126
|
+
tuple(spans),
|
|
127
|
+
color(line.background_style)
|
|
128
|
+
if line.background_style is not None
|
|
129
|
+
else None,
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
return tuple(result)
|
|
133
|
+
|
|
134
|
+
def expand(self, context: Context, metadata: ExpandedMetadata) -> ExpandedText:
|
|
135
|
+
resolved_lines = self._resolved_lines(context)
|
|
136
|
+
return ExpandedText(
|
|
137
|
+
metadata,
|
|
138
|
+
TextLayout(resolved_lines, self.flow),
|
|
139
|
+
self.flow,
|
|
140
|
+
resolved_lines,
|
|
141
|
+
self.fill.resolve(context.environment) if self.fill is not None else None,
|
|
142
|
+
)
|
deckkit/views/view.py
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""Backend-independent author values. Modifier order is significant."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable, Mapping
|
|
6
|
+
from copy import copy
|
|
7
|
+
from types import MappingProxyType
|
|
8
|
+
from typing import TYPE_CHECKING, Any, Literal, TypeVar, Union
|
|
9
|
+
|
|
10
|
+
from typing_extensions import Self
|
|
11
|
+
|
|
12
|
+
from ..animation import Animation, Transition
|
|
13
|
+
from ..effects import Shadow, ShadowStyle
|
|
14
|
+
from ..theme import (
|
|
15
|
+
ColorStyle,
|
|
16
|
+
ForegroundColor,
|
|
17
|
+
TextStyle,
|
|
18
|
+
Typography,
|
|
19
|
+
)
|
|
20
|
+
from ..tokens import DesignToken, TokenAssignment
|
|
21
|
+
from ..units import Length, pt
|
|
22
|
+
|
|
23
|
+
if TYPE_CHECKING:
|
|
24
|
+
from ..geometry import RelativePoint
|
|
25
|
+
from ..layout import LayoutPlan, VerticalAlignment, VerticalAlignmentName
|
|
26
|
+
from ..variables import Occurrence
|
|
27
|
+
from .modifier_views import EdgeInsets, EdgeSet, FrameMaximum
|
|
28
|
+
|
|
29
|
+
T = TypeVar("T")
|
|
30
|
+
|
|
31
|
+
# Tagged tuples preserve the relationship between modifier names and payloads.
|
|
32
|
+
Modifier = Union[
|
|
33
|
+
tuple[Literal["key"], str],
|
|
34
|
+
tuple[Literal["animation"], Animation],
|
|
35
|
+
tuple[Literal["transition"], Transition],
|
|
36
|
+
tuple[Literal["tokens"], Mapping[DesignToken[Any], TokenAssignment[Any]]],
|
|
37
|
+
tuple[Literal["flex"], float],
|
|
38
|
+
tuple[Literal["boundary"], bool],
|
|
39
|
+
tuple[Literal["hidden"], bool],
|
|
40
|
+
tuple[Literal["opacity"], float],
|
|
41
|
+
tuple[Literal["shadow"], ShadowStyle],
|
|
42
|
+
tuple[Literal["grouped"], bool],
|
|
43
|
+
tuple[Literal["variable_occurrence"], "Occurrence[Any]"],
|
|
44
|
+
tuple[
|
|
45
|
+
Literal["alignment_guide"],
|
|
46
|
+
tuple["VerticalAlignment", Callable[["LayoutPlan"], Length]],
|
|
47
|
+
],
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class View:
|
|
52
|
+
_modifiers: tuple[Modifier, ...] = ()
|
|
53
|
+
|
|
54
|
+
def _with(self, modifier: Modifier) -> Self:
|
|
55
|
+
result = copy(self)
|
|
56
|
+
object.__setattr__(result, "_modifiers", self._modifiers + (modifier,))
|
|
57
|
+
return result
|
|
58
|
+
|
|
59
|
+
def key(self, value: str | int) -> Self:
|
|
60
|
+
return self._with(("key", str(value)))
|
|
61
|
+
|
|
62
|
+
def with_token(self, token: DesignToken[T], assignment: TokenAssignment[T]) -> Self:
|
|
63
|
+
"""Override one design token for this view subtree."""
|
|
64
|
+
return self.with_tokens({token: assignment})
|
|
65
|
+
|
|
66
|
+
def with_tokens(
|
|
67
|
+
self, overrides: Mapping[DesignToken[Any], TokenAssignment[Any]]
|
|
68
|
+
) -> Self:
|
|
69
|
+
"""Create a scoped group of design-token overrides."""
|
|
70
|
+
if not all(isinstance(token, DesignToken) for token in overrides):
|
|
71
|
+
raise TypeError("Token override keys must be DesignToken instances")
|
|
72
|
+
return self._with(("tokens", MappingProxyType(dict(overrides))))
|
|
73
|
+
|
|
74
|
+
def font(self, style: DesignToken[Typography] | Typography) -> Self:
|
|
75
|
+
override = (
|
|
76
|
+
TextStyle.alias(style)
|
|
77
|
+
if isinstance(style, DesignToken)
|
|
78
|
+
else TextStyle.set(style)
|
|
79
|
+
)
|
|
80
|
+
return self.with_token(TextStyle, override)
|
|
81
|
+
|
|
82
|
+
def foreground_style(self, style: ColorStyle) -> Self:
|
|
83
|
+
override = (
|
|
84
|
+
ForegroundColor.alias(style)
|
|
85
|
+
if isinstance(style, DesignToken)
|
|
86
|
+
else ForegroundColor.set(style)
|
|
87
|
+
)
|
|
88
|
+
return self.with_token(ForegroundColor, override)
|
|
89
|
+
|
|
90
|
+
def alignment_guide(
|
|
91
|
+
self,
|
|
92
|
+
alignment: VerticalAlignmentName | VerticalAlignment,
|
|
93
|
+
compute: Callable[[LayoutPlan], Length],
|
|
94
|
+
) -> Self:
|
|
95
|
+
"""Set a vertical guide relative to the top of this view's measured box."""
|
|
96
|
+
from ..layout import resolve_vertical_alignment
|
|
97
|
+
|
|
98
|
+
return self._with(
|
|
99
|
+
("alignment_guide", (resolve_vertical_alignment(alignment), compute))
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
@property
|
|
103
|
+
def _layout_flex(self) -> float:
|
|
104
|
+
return next((m[1] for m in reversed(self._modifiers) if m[0] == "flex"), 0.0)
|
|
105
|
+
|
|
106
|
+
@property
|
|
107
|
+
def _layout_flex_minimum(self) -> tuple[Length, Length] | None:
|
|
108
|
+
return None
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def _layout_stretches_cross_axis(self) -> bool:
|
|
112
|
+
"""Request a stack's resolved cross size independently of the main size."""
|
|
113
|
+
return False
|
|
114
|
+
|
|
115
|
+
def padding(
|
|
116
|
+
self, amount: Length | EdgeInsets = pt(16), *, edges: EdgeSet | None = None
|
|
117
|
+
) -> View:
|
|
118
|
+
from .modifier_views import Padding
|
|
119
|
+
|
|
120
|
+
return Padding(self, amount, edges)
|
|
121
|
+
|
|
122
|
+
def frame(
|
|
123
|
+
self,
|
|
124
|
+
*,
|
|
125
|
+
width: Length | None = None,
|
|
126
|
+
height: Length | None = None,
|
|
127
|
+
min_width: Length | None = None,
|
|
128
|
+
ideal_width: Length | None = None,
|
|
129
|
+
max_width: FrameMaximum | None = None,
|
|
130
|
+
min_height: Length | None = None,
|
|
131
|
+
ideal_height: Length | None = None,
|
|
132
|
+
max_height: FrameMaximum | None = None,
|
|
133
|
+
alignment: RelativePoint | None = None,
|
|
134
|
+
) -> View:
|
|
135
|
+
from ..geometry import RelativePoint
|
|
136
|
+
from .modifier_views import Frame, FrameSpec
|
|
137
|
+
|
|
138
|
+
return Frame(
|
|
139
|
+
self,
|
|
140
|
+
FrameSpec(
|
|
141
|
+
width,
|
|
142
|
+
height,
|
|
143
|
+
min_width,
|
|
144
|
+
ideal_width,
|
|
145
|
+
max_width,
|
|
146
|
+
min_height,
|
|
147
|
+
ideal_height,
|
|
148
|
+
max_height,
|
|
149
|
+
RelativePoint.center if alignment is None else alignment,
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
def flex(self, weight: float = 1) -> Self:
|
|
154
|
+
if weight <= 0:
|
|
155
|
+
raise ValueError("Flex weight must be positive")
|
|
156
|
+
return self._with(("flex", float(weight)))
|
|
157
|
+
|
|
158
|
+
def hidden(self, *, when: bool = True) -> Self:
|
|
159
|
+
"""Keep this view in layout while hiding its rendered content."""
|
|
160
|
+
if not isinstance(when, bool):
|
|
161
|
+
raise TypeError("hidden() requires a boolean condition")
|
|
162
|
+
return self._with(("hidden", True)) if when else self
|
|
163
|
+
|
|
164
|
+
def opacity(self, amount: float) -> Self:
|
|
165
|
+
"""Multiply the opacity of the fills, strokes, text, and images in this view."""
|
|
166
|
+
from ..canvas._geometry import validate_opacity
|
|
167
|
+
|
|
168
|
+
validate_opacity(amount)
|
|
169
|
+
return self._with(("opacity", float(amount)))
|
|
170
|
+
|
|
171
|
+
def shadow(self, style: ShadowStyle) -> Self:
|
|
172
|
+
"""Cast a shadow from this view's painted content."""
|
|
173
|
+
if not isinstance(style, (Shadow, DesignToken)):
|
|
174
|
+
raise TypeError("shadow() requires a Shadow or a shadow token")
|
|
175
|
+
return self._with(("shadow", style))
|
|
176
|
+
|
|
177
|
+
def grouped(self) -> Self:
|
|
178
|
+
"""Preserve this view's painted children as an editable export group."""
|
|
179
|
+
return self._with(("grouped", True))
|
|
180
|
+
|
|
181
|
+
def background(
|
|
182
|
+
self, decoration: View, *, alignment: RelativePoint | None = None
|
|
183
|
+
) -> View:
|
|
184
|
+
"""Draw a view behind this view, using this view's measured size."""
|
|
185
|
+
from .modifier_views import Layered
|
|
186
|
+
|
|
187
|
+
return Layered(self, decoration, alignment=alignment, in_front=False)
|
|
188
|
+
|
|
189
|
+
def overlay(
|
|
190
|
+
self, decoration: View, *, alignment: RelativePoint | None = None
|
|
191
|
+
) -> View:
|
|
192
|
+
"""Draw a view in front of this view, using this view's measured size."""
|
|
193
|
+
from .modifier_views import Layered
|
|
194
|
+
|
|
195
|
+
return Layered(self, decoration, alignment=alignment, in_front=True)
|
|
196
|
+
|
|
197
|
+
def offset(self, *, x: Length = pt(0), y: Length = pt(0)) -> View:
|
|
198
|
+
"""Shift the rendered content while keeping its original layout size."""
|
|
199
|
+
from .modifier_views import Placed
|
|
200
|
+
|
|
201
|
+
return Placed(self, x, y, anchor=None)
|
|
202
|
+
|
|
203
|
+
def position(
|
|
204
|
+
self,
|
|
205
|
+
*,
|
|
206
|
+
x: Length = pt(0),
|
|
207
|
+
y: Length = pt(0),
|
|
208
|
+
anchor: RelativePoint | None = None,
|
|
209
|
+
) -> View:
|
|
210
|
+
"""Place this view's anchor at a point in the allocated region."""
|
|
211
|
+
from ..geometry import RelativePoint
|
|
212
|
+
from .modifier_views import Placed
|
|
213
|
+
|
|
214
|
+
return Placed(
|
|
215
|
+
self, x, y, anchor=RelativePoint.center if anchor is None else anchor
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
def animation(self, animation: Animation) -> Self:
|
|
219
|
+
if not isinstance(animation, Animation):
|
|
220
|
+
raise TypeError("animation() requires an Animation value")
|
|
221
|
+
return self._with(("animation", animation))
|
|
222
|
+
|
|
223
|
+
def transition(self, transition: Transition) -> Self:
|
|
224
|
+
if not isinstance(transition, Transition):
|
|
225
|
+
raise TypeError("transition() requires a Transition value")
|
|
226
|
+
return self._with(("transition", transition))
|
|
227
|
+
|
|
228
|
+
def animation_boundary(self) -> Self:
|
|
229
|
+
return self._with(("boundary", True))
|
deckkit/views/vstack.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
from ..layout import Layout
|
|
7
|
+
from ..units import Length, pt
|
|
8
|
+
from .stack import Stack
|
|
9
|
+
from .view import View
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, init=False)
|
|
13
|
+
class VStack(Stack):
|
|
14
|
+
alignment: Literal["leading", "center", "trailing"]
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
*children: View | None,
|
|
19
|
+
spacing: Length = pt(16),
|
|
20
|
+
alignment: Literal["leading", "center", "trailing"] = "leading",
|
|
21
|
+
) -> None:
|
|
22
|
+
if alignment not in ("leading", "center", "trailing"):
|
|
23
|
+
raise ValueError("VStack alignment must be leading, center, or trailing")
|
|
24
|
+
object.__setattr__(self, "alignment", alignment)
|
|
25
|
+
super().__init__(*children, spacing=spacing)
|
|
26
|
+
|
|
27
|
+
def _make_layout(self) -> Layout:
|
|
28
|
+
from ..layouts import StackLayout
|
|
29
|
+
|
|
30
|
+
alignment = {"leading": 0.0, "center": 0.5, "trailing": 1.0}[self.alignment]
|
|
31
|
+
return StackLayout("vertical", self.spacing, alignment)
|
deckkit/views/zstack.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..geometry import RelativePoint
|
|
6
|
+
from ..layout import Layout
|
|
7
|
+
from ..units import pt
|
|
8
|
+
from .stack import Stack
|
|
9
|
+
from .view import View
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, init=False)
|
|
13
|
+
class ZStack(Stack):
|
|
14
|
+
alignment: RelativePoint
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self, *children: View | None, alignment: RelativePoint = RelativePoint.center
|
|
18
|
+
) -> None:
|
|
19
|
+
if not isinstance(alignment, RelativePoint):
|
|
20
|
+
raise TypeError("ZStack alignment must be a RelativePoint")
|
|
21
|
+
object.__setattr__(self, "alignment", alignment)
|
|
22
|
+
super().__init__(*children, spacing=pt(0))
|
|
23
|
+
|
|
24
|
+
def _make_layout(self) -> Layout:
|
|
25
|
+
from ..layouts import OverlayLayout
|
|
26
|
+
|
|
27
|
+
return OverlayLayout(self.alignment)
|