scoop:scoop.ui.component:0.1.0
src/typography/kbd.scoop
Selected production source · src/typography/kbd.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.*8
import scoop.ui.component.row as makeRow9
import scoop.ui.component.label as makeLabel11
public enum KbdContent {12
Text(String), Custom(View), Chord(ShortcutChord), Label(ShortcutLabel)13
}15
public 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 = false22
)24
public class Kbd public constructor(25
private val props: Props<KbdProps>,26
scope: ComponentScope27
): 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.value34
val theme = themes.read(input.theme)35
val density = theme.tokens.typography.body.fontSize / 14.036
val base =37
theme.tokens.typography.body.copy {38
this.fontSize = 12.0 * density39
this.lineHeight = Some(16.0 / 12.0)40
this.weight = 50041
this.color = theme.tokens.palette.muted.foreground42
}43
val style = input.textStyle?.resolve(base) ?: base44
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 -> None57
}58
if (input.grouped)59
when (label) {60
case Some(val value) -> return direction(61
makeRow {62
this.spacing = 4.0 * density63
this.alignment = CrossAlignment.Center64
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.Ltr78
)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) -> view85
else -> direction(86
kbdCaption(shortcutCaption(label!!), style, input.fonts),87
LayoutDirection.Ltr88
)89
}90
return kbdCap(child, density, visual)91
}92
}94
internal 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()102
}104
private 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 }112
private fun kbdCap(113
content: View,114
density: Double,115
style: StyleOverride,116
key: ViewKey = ViewKey.Position117
): View =118
frame(element(density, { current, mount -> KbdSize(current, scope = mount) }, [content])) {119
this.padding = Some(Insets())120
this.style = style121
this.key = key122
}124
private class KbdSize(private val density: Props<Double>, scope: ComponentScope): Element<Unit>(125
scope126
) {127
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<128
Unit129
> {130
val scale = density.value131
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.0151
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
}157
}159
public class KbdBuilder internal constructor() {160
public var fonts: FontCollection? = None161
public var theme: ThemeSource? = None162
public var textStyle: SpanStyle? = None163
public var style: StyleOverride = styleOverride()164
public var key: ViewKey = ViewKey.Position165
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)173
}175
public fun kbd(text: String, configure: KbdBuilder.() -> Unit = {}): View =176
makeKbd(KbdContent.Text(text), false, configure)178
public fun kbd(view: View, configure: KbdBuilder.() -> Unit = {}): View =179
makeKbd(KbdContent.Custom(view), false, configure)181
public fun kbd(chord: ShortcutChord, configure: KbdBuilder.() -> Unit = {}): View =182
makeKbd(KbdContent.Chord(chord), false, configure)184
public fun kbd(label: ShortcutLabel, configure: KbdBuilder.() -> Unit = {}): View =185
makeKbd(KbdContent.Label(label), false, configure)187
public fun kbdGroup(chord: ShortcutChord, configure: KbdBuilder.() -> Unit = {}): View =188
makeKbd(KbdContent.Chord(chord), true, configure)190
public fun kbdGroup(label: ShortcutLabel, configure: KbdBuilder.() -> Unit = {}): View =191
makeKbd(KbdContent.Label(label), true, configure)193
internal fun makeKbd(194
content: KbdContent,195
grouped: Boolean,196
configure: KbdBuilder.() -> Unit197
): 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.key204
)205
}207
public class KbdGroupBuilder internal constructor(): ChildrenBuilder() {208
public var spacing: Double = 4.0209
internal fun build(): View {210
nonnegative(spacing, "invalid keyboard group spacing")211
val children = childrenSnapshot()212
val gap = spacing213
val identity = key214
return makeRow {215
this.addAll(children)216
this.spacing = gap217
this.key = identity218
this.alignment = CrossAlignment.Center219
}220
}221
}223
public fun kbdGroup(configure: KbdGroupBuilder.() -> Unit): View = makeKbdGroup(configure)225
internal fun makeKbdGroup(configure: KbdGroupBuilder.() -> Unit): View {226
val builder = KbdGroupBuilder()227
builder.configure()228
return builder.build()229
}