Scoop API

scoop:scoop.ui.component:0.1.0

src/typography/kbd.scoop

Selected production source · src/typography/kbd.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.*
8import scoop.ui.component.row as makeRow
9import scoop.ui.component.label as makeLabel
11public enum KbdContent {
12 Text(String), Custom(View), Chord(ShortcutChord), Label(ShortcutLabel)
15public class KbdProps public constructor(
16 public val content: KbdContent,
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 grouped: Boolean = false
24public class Kbd public constructor(
25 private val props: Props<KbdProps>,
26 scope: ComponentScope
27): Component(scope) {
28 private val themes = ThemeAccess(scope)
29 public override fun onMount() {
30 themes.mount(props.value.theme)
31 }
32 public override fun render(): View {
33 val input = props.value
34 val theme = themes.read(input.theme)
35 val density = theme.tokens.typography.body.fontSize / 14.0
36 val base =
37 theme.tokens.typography.body.copy {
38 this.fontSize = 12.0 * density
39 this.lineHeight = Some(16.0 / 12.0)
40 this.weight = 500
41 this.color = theme.tokens.palette.muted.foreground
42 }
43 val style = input.textStyle?.resolve(base) ?: base
44 val visual =
45 styleOverride {
46 this.background(Some(Paint(theme.tokens.palette.muted.background)))
47 this.corners(Corners.all(theme.tokens.corners.small))
48 }.then(input.style)
49 val label =
50 when (input.content) {
51 case Chord(val value) -> when (Some(scope.nativeWindow)) {
52 case Some(val native) -> Some(native.shortcutLabel(value))
53 case None -> return empty()
54 }
55 case Label(val value) -> Some(value)
56 else -> None
57 }
58 if (input.grouped)
59 when (label) {
60 case Some(val value) -> return direction(
61 makeRow {
62 this.spacing = 4.0 * density
63 this.alignment = CrossAlignment.Center
64 for (index in 0L ..< value.parts.size) {
65 if (index > 0L && value.separator.byteLength > 0L)
66 this.add(kbdCaption(value.separator, style, input.fonts))
67 this.add(
68 kbdCap(
69 kbdCaption(value.parts[index], style, input.fonts),
70 density,
71 visual,
72 ViewKey.Number(index)
73 )
74 )
75 }
76 },
77 LayoutDirection.Ltr
78 )
79 case None -> {}
80 }
81 val child =
82 when (input.content) {
83 case Text(val value) -> kbdCaption(value, style, input.fonts)
84 case Custom(val view) -> view
85 else -> direction(
86 kbdCaption(shortcutCaption(label!!), style, input.fonts),
87 LayoutDirection.Ltr
88 )
89 }
90 return kbdCap(child, density, visual)
91 }
94internal fun shortcutCaption(label: ShortcutLabel): String {
95 val text = StringBuilder()
96 for (index in 0L ..< label.parts.size) {
97 if (index > 0L)
98 text.append(label.separator)
99 text.append(label.parts[index])
100 }
101 return text.toString()
104private fun kbdCaption(value: String, style: TextStyle, fonts: FontCollection?): View =
105 makeLabel(
106 styledText {
107 this.style = style;
108 this.text(value)
109 }
110 ) { this.fonts = fonts }
112private fun kbdCap(
113 content: View,
114 density: Double,
115 style: StyleOverride,
116 key: ViewKey = ViewKey.Position
117): View =
118 frame(element(density, { current, mount -> KbdSize(current, scope = mount) }, [content])) {
119 this.padding = Some(Insets())
120 this.style = style
121 this.key = key
122 }
124private class KbdSize(private val density: Props<Double>, scope: ComponentScope): Element<Unit>(
125 scope
126) {
127 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
128 Unit
129 > {
130 val scale = density.value
131 val natural = context.measureChild(0L, Constraints())
132 val size =
133 constraints.constrain(
134 Size(lowerBound(natural.size.width + 8.0 * scale, 20.0 * scale), 20.0 * scale)
135 )
136 val child =
137 context.measureChild(
138 0L,
139 Constraints(
140 maxWidth =
141 Some(
142 upperBound(
143 natural.size.width,
144 lowerBound(size.width - 8.0 * scale, 0.0)
145 )
146 ),
147 maxHeight = Some(size.height)
148 )
149 )
150 val top = (size.height - child.size.height) / 2.0
151 context.placeChild(0L, Point((size.width - child.size.width) / 2.0, top))
152 return LayoutResult(offsetMetrics(child, size, top), Unit)
153 }
154 public override fun scene(context: SceneContext, measured: Unit) {
155 containerScene(context, true)
156 }
159public class KbdBuilder internal constructor() {
160 public var fonts: FontCollection? = None
161 public var theme: ThemeSource? = None
162 public var textStyle: SpanStyle? = None
163 public var style: StyleOverride = styleOverride()
164 public var key: ViewKey = ViewKey.Position
165 public fun textStyle(configure: SpanStyleBuilder.() -> Unit): Unit {
166 textStyle = Some((textStyle ?: spanStyle()).copy(configure))
167 }
168 public fun style(configure: StyleOverrideBuilder.() -> Unit): Unit {
169 style = style.copy(configure)
170 }
171 internal fun snapshot(content: KbdContent, grouped: Boolean): KbdProps =
172 KbdProps(content, fonts, theme, textStyle, style, grouped)
175public fun kbd(text: String, configure: KbdBuilder.() -> Unit = {}): View =
176 makeKbd(KbdContent.Text(text), false, configure)
178public fun kbd(view: View, configure: KbdBuilder.() -> Unit = {}): View =
179 makeKbd(KbdContent.Custom(view), false, configure)
181public fun kbd(chord: ShortcutChord, configure: KbdBuilder.() -> Unit = {}): View =
182 makeKbd(KbdContent.Chord(chord), false, configure)
184public fun kbd(label: ShortcutLabel, configure: KbdBuilder.() -> Unit = {}): View =
185 makeKbd(KbdContent.Label(label), false, configure)
187public fun kbdGroup(chord: ShortcutChord, configure: KbdBuilder.() -> Unit = {}): View =
188 makeKbd(KbdContent.Chord(chord), true, configure)
190public fun kbdGroup(label: ShortcutLabel, configure: KbdBuilder.() -> Unit = {}): View =
191 makeKbd(KbdContent.Label(label), true, configure)
193internal fun makeKbd(
194 content: KbdContent,
195 grouped: Boolean,
196 configure: KbdBuilder.() -> Unit
197): View {
198 val builder = KbdBuilder()
199 builder.configure()
200 return component(
201 builder.snapshot(content, grouped),
202 { current, mount -> Kbd(current, scope = mount) },
203 builder.key
204 )
207public class KbdGroupBuilder internal constructor(): ChildrenBuilder() {
208 public var spacing: Double = 4.0
209 internal fun build(): View {
210 nonnegative(spacing, "invalid keyboard group spacing")
211 val children = childrenSnapshot()
212 val gap = spacing
213 val identity = key
214 return makeRow {
215 this.addAll(children)
216 this.spacing = gap
217 this.key = identity
218 this.alignment = CrossAlignment.Center
219 }
220 }
223public fun kbdGroup(configure: KbdGroupBuilder.() -> Unit): View = makeKbdGroup(configure)
225internal fun makeKbdGroup(configure: KbdGroupBuilder.() -> Unit): View {
226 val builder = KbdGroupBuilder()
227 builder.configure()
228 return builder.build()