Scoop API

scoop:scoop.ui.component:0.1.0

src/typography/typography.scoop

Selected production source · src/typography/typography.scoop

1package scoop.ui.component
3import scoop.graphics.Point
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.text.*
8import scoop.window.accessibility.*
10public enum TypographyContent {
11 Plain(String), Styled(StyledText), Fragments(Array<TextSpan>)
14public class TypographyProps public constructor(
15 public val content: TypographyContent,
16 public val role: TypographyRole = TypographyRole.P,
17 public val fonts: FontCollection? = None,
18 public val theme: ThemeSource? = None,
19 public val textStyle: SpanStyle? = None,
20 public val style: StyleOverride = styleOverride(),
21 public val settings: ParagraphStyle? = None,
22 public val selectable: Boolean = true,
23 /** None resolves selection colors from the current theme and root preferences. */
24 public val selection: SelectableTextOptions? = None,
25 public val focus: FocusOptions = FocusOptions(),
26 public val actions: Array<TextRangeAction> = [],
27 public val decorations: Array<TextRangeDecoration> = [],
28 public val onSelectionChanged: (TextPosition, TextPosition) -> Unit = { _, _ -> Unit },
29 public val inlines: Array<InlineView> = [],
30 public val semantics: ComponentSemantics = ComponentSemantics()
33public class Typography public constructor(
34 private val props: Props<TypographyProps>,
35 scope: ComponentScope
36): Component(scope) {
37 private val themes = ThemeAccess(scope)
38 public override fun onMount() {
39 themes.mount(props.value.theme)
40 }
41 public override fun render(): View {
42 val input = props.value
43 val theme = themes.read(input.theme)
44 val content =
45 typographyContent(input.content, typographyStyle(input.role, theme), input.textStyle)
46 val heading =
47 when (input.role) {
48 case H1 -> Some(1)
49 case H2 -> Some(2)
50 case H3 -> Some(3)
51 case H4 -> Some(4)
52 else -> None
53 }
54 val textSemantics =
55 if (heading != None)
56 input.semantics.copy { reference = None }
57 else
58 input.semantics
59 val body =
60 if (input.selectable)
61 selectableStyledText(content) {
62 this.fonts = input.fonts
63 this.settings = input.settings
64 this.inlines = input.inlines
65 this.options = input.selection ?: themeSelectionOptions(theme)
66 this.focus = input.focus
67 this.actions = input.actions
68 this.decorations = input.decorations
69 this.onSelectionChanged = input.onSelectionChanged
70 this.semantics = textSemantics
71 }
72 else
73 label(content) {
74 this.fonts = input.fonts
75 this.settings = input.settings
76 this.inlines = input.inlines
77 this.semantics = textSemantics
78 }
79 val framed =
80 frame(body) {
81 this.logicalPadding =
82 Some(
83 typographyPadding(input.role, theme.tokens.typography.body.fontSize / 14.0)
84 )
85 this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Start)
86 this.style = typographyDecoration(input.role, theme).then(input.style)
87 }
88 when (heading) {
89 case Some(val level) -> return semanticContent(
90 framed,
91 SemanticRole.Heading,
92 input.semantics.properties().copy {
93 headingLevel = Some(level)
94 if (name == None && input.semantics.relations.labelledBy.size == 0L)
95 name = Some(content.toPlainText())
96 },
97 relations = input.semantics.relations,
98 reference = input.semantics.reference
99 )
100 case None -> {}
101 }
102 return if (input.role == TypographyRole.InlineCode)
103 element({ mount -> InlineCodeWidth(scope = mount) }, [framed])
104 else
105 framed
106 }
109/** A paragraph's inline limit is an upper bound, not the code token's preferred width. */
110private class InlineCodeWidth(scope: ComponentScope): Element<Unit>(scope) {
111 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
112 Unit
113 > {
114 val natural = context.measureChild(0L, Constraints(maxHeight = constraints.maxHeight))
115 val width = constraints.constrain(natural.size).width
116 val child =
117 context.measureChild(
118 0L,
119 Constraints(
120 minWidth = constraints.minWidth,
121 maxWidth = Some(width),
122 minHeight = constraints.minHeight,
123 maxHeight = constraints.maxHeight
124 )
125 )
126 context.placeChild(0L, Point(0.0, 0.0))
127 return LayoutResult(child, Unit)
128 }
129 public override fun scene(context: SceneContext, measured: Unit) {
130 context.children()
131 }
134internal fun typographyContent(
135 input: TypographyContent,
136 base: TextStyle,
137 local: SpanStyle?
138): StyledText {
139 val resolved =
140 when (input) {
141 case Styled(val value) -> value.style
142 else -> base
143 }
144 return styledText {
145 this.style = local?.resolve(resolved) ?: resolved
146 when (input) {
147 case Plain(val value) -> this.text(value)
148 case Styled(val value) -> for (fragment in value.spans)
149 this.add(fragment)
150 case Fragments(val values) -> for (fragment in values)
151 this.add(fragment)
152 }
153 }
156public fun typography(
157 text: String,
158 role: TypographyRole = TypographyRole.P,
159 configure: TypographyBuilder.() -> Unit = {}
160): View = makeTypography(TypographyContent.Plain(text), role, configure)
162public fun typography(
163 content: StyledText,
164 role: TypographyRole = TypographyRole.P,
165 configure: TypographyBuilder.() -> Unit = {}
166): View = makeTypography(TypographyContent.Styled(content), role, configure)
168public fun typography(
169 role: TypographyRole = TypographyRole.P,
170 configure: TypographyTextBuilder.() -> Unit
171): View = makeTypographyText(role, configure)
173internal fun makeTypography(
174 content: TypographyContent,
175 role: TypographyRole,
176 configure: TypographyBuilder.() -> Unit
177): View {
178 val builder = TypographyBuilder()
179 builder.configure()
180 return component(
181 builder.snapshot(content, role),
182 { current, mount -> Typography(current, scope = mount) },
183 builder.key
184 )
187internal fun makeTypographyText(
188 role: TypographyRole,
189 configure: TypographyTextBuilder.() -> Unit
190): View {
191 val builder = TypographyTextBuilder()
192 builder.configure()
193 return component(
194 builder.snapshotText(role),
195 { current, mount -> Typography(current, scope = mount) },
196 builder.key
197 )