Scoop API

scoop:scoop.ui.component:0.1.0

src/typography/builders.scoop

Selected production source · src/typography/builders.scoop

1package scoop.ui.component
3import scoop.graphics.text.*
4import scoop.ui.*
5import scoop.ui.style.*
6import scoop.ui.text.*
8public open class TypographyBuilder internal constructor() {
9 public var fonts: FontCollection? = None
10 public var theme: ThemeSource? = None
11 public var textStyle: SpanStyle? = None
12 public var style: StyleOverride = styleOverride()
13 public var settings: ParagraphStyle? = None
14 public var selectable: Boolean = true
15 public var selection: SelectableTextOptions? = None
16 public var focus: FocusOptions = FocusOptions()
17 public var actions: Array<TextRangeAction> = []
18 public var decorations: Array<TextRangeDecoration> = []
19 public var onSelectionChanged: (TextPosition, TextPosition) -> Unit = { _, _ -> Unit }
20 public var inlines: Array<InlineView> = []
21 public var key: ViewKey = ViewKey.Position
22 public var semantics: ComponentSemantics = ComponentSemantics()
23 public fun textStyle(configure: SpanStyleBuilder.() -> Unit): Unit {
24 textStyle = Some((textStyle ?: spanStyle()).copy(configure))
25 }
26 public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {
27 style = style.copy(configure)
28 }
29 public fun settings(configure: ParagraphStyleBuilder.() -> Unit): Unit {
30 settings = Some((settings ?: paragraphStyle()).copy(configure))
31 }
32 public fun focus(configure: FocusOptionsBuilder.() -> Unit): Unit {
33 focus = focus.copy(configure)
34 }
35 internal fun snapshot(
36 content: TypographyContent,
37 role: TypographyRole,
38 objects: Array<InlineView> = inlines
39 ): TypographyProps =
40 TypographyProps(
41 content,
42 role,
43 fonts,
44 theme,
45 textStyle,
46 style,
47 settings,
48 selectable,
49 selection,
50 focus,
51 actions,
52 decorations,
53 onSelectionChanged,
54 objects,
55 semantics
56 )
59private class TypographyCode(val key: InlineKey, val text: String)
61public class TypographyTextBuilder internal constructor(): TypographyBuilder() {
62 private val fragments = ArrayList<TextSpan>()
63 private val objects = ArrayList<InlineView>()
64 private val codes = ArrayList<TypographyCode>()
65 public fun add(fragment: TextSpan): Unit {
66 fragments.add(fragment)
67 }
68 public fun text(value: String): Unit {
69 add(textSpan { this.text(value) })
70 }
71 public fun span(configure: TextSpanBuilder.() -> Unit): Unit {
72 add(textSpan(configure))
73 }
74 public fun code(value: String, key: InlineKey? = None): Unit {
75 val identity = key ?: InlineKey.Number(fragments.size)
76 add(textSpan { this.inlineObject(identity, value) })
77 codes.add(TypographyCode(identity, value))
78 }
79 public fun inline(
80 key: InlineKey,
81 view: View,
82 alternateText: String,
83 configure: InlineViewBuilder.() -> Unit = {}
84 ): Unit {
85 add(textSpan { this.inlineObject(key, alternateText) })
86 objects.add(inlineView(key, view, configure))
87 }
88 internal fun snapshotText(role: TypographyRole): TypographyProps {
89 val all = ArrayList<InlineView>()
90 for (item in inlines)
91 all.add(item)
92 for (item in objects)
93 all.add(item)
94 val fonts = this.fonts
95 val theme = this.theme
96 for (item in codes)
97 all.add(
98 inlineView(
99 item.key,
100 typography(item.text, TypographyRole.InlineCode) {
101 this.fonts = fonts
102 this.theme = theme
103 this.selectable = false
104 }
105 )
106 )
107 return snapshot(TypographyContent.Fragments(fragments.toArray()), role, all.toArray())
108 }