scoop:scoop.ui.component:0.1.0
src/typography/typography.scoop
Selected production source · src/typography/typography.scoop
1
package scoop.ui.component3
import scoop.graphics.Point4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.text.*8
import scoop.window.accessibility.*10
public enum TypographyContent {11
Plain(String), Styled(StyledText), Fragments(Array<TextSpan>)12
}14
public 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()31
)33
public class Typography public constructor(34
private val props: Props<TypographyProps>,35
scope: ComponentScope36
): 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.value43
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 -> None53
}54
val textSemantics =55
if (heading != None)56
input.semantics.copy { reference = None }57
else58
input.semantics59
val body =60
if (input.selectable)61
selectableStyledText(content) {62
this.fonts = input.fonts63
this.settings = input.settings64
this.inlines = input.inlines65
this.options = input.selection ?: themeSelectionOptions(theme)66
this.focus = input.focus67
this.actions = input.actions68
this.decorations = input.decorations69
this.onSelectionChanged = input.onSelectionChanged70
this.semantics = textSemantics71
}72
else73
label(content) {74
this.fonts = input.fonts75
this.settings = input.settings76
this.inlines = input.inlines77
this.semantics = textSemantics78
}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.reference99
)100
case None -> {}101
}102
return if (input.role == TypographyRole.InlineCode)103
element({ mount -> InlineCodeWidth(scope = mount) }, [framed])104
else105
framed106
}107
}109
/** A paragraph's inline limit is an upper bound, not the code token's preferred width. */110
private class InlineCodeWidth(scope: ComponentScope): Element<Unit>(scope) {111
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<112
Unit113
> {114
val natural = context.measureChild(0L, Constraints(maxHeight = constraints.maxHeight))115
val width = constraints.constrain(natural.size).width116
val child =117
context.measureChild(118
0L,119
Constraints(120
minWidth = constraints.minWidth,121
maxWidth = Some(width),122
minHeight = constraints.minHeight,123
maxHeight = constraints.maxHeight124
)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
}132
}134
internal 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.style142
else -> base143
}144
return styledText {145
this.style = local?.resolve(resolved) ?: resolved146
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
}154
}156
public fun typography(157
text: String,158
role: TypographyRole = TypographyRole.P,159
configure: TypographyBuilder.() -> Unit = {}160
): View = makeTypography(TypographyContent.Plain(text), role, configure)162
public fun typography(163
content: StyledText,164
role: TypographyRole = TypographyRole.P,165
configure: TypographyBuilder.() -> Unit = {}166
): View = makeTypography(TypographyContent.Styled(content), role, configure)168
public fun typography(169
role: TypographyRole = TypographyRole.P,170
configure: TypographyTextBuilder.() -> Unit171
): View = makeTypographyText(role, configure)173
internal fun makeTypography(174
content: TypographyContent,175
role: TypographyRole,176
configure: TypographyBuilder.() -> Unit177
): 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.key184
)185
}187
internal fun makeTypographyText(188
role: TypographyRole,189
configure: TypographyTextBuilder.() -> Unit190
): View {191
val builder = TypographyTextBuilder()192
builder.configure()193
return component(194
builder.snapshotText(role),195
{ current, mount -> Typography(current, scope = mount) },196
builder.key197
)198
}