scoop:scoop.ui.component:0.1.0
src/command/component.scoop
Selected production source · src/command/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.FocusOrigin5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
public class Command public constructor(10
private val props: Props<CommandProps>,11
scope: ComponentScope12
): Component(scope) {13
private val themes = ThemeAccess(scope)14
private val localActive = state<ViewKey?>(None)15
private val ownFocus = FocusHandle()16
private val ownScroll = ScrollState()17
private val ownList = VirtualListHandle()18
private var cached: CommandProps? = None19
private var cachedQuery = ""20
private var result: CommandSource? = None21
private var rowSource: CommandRows? = None22
private val active =23
binding(24
{ (props.value.active ?: localActive.binding()).value },25
{ key -> (props.value.active ?: localActive.binding()).value = key }26
)27
public override fun onMount() {28
themes.mount(props.value.input.theme)29
}30
private fun results(): CommandSource {31
val input = props.value32
val query = input.query.committedText33
when (cached) {34
case Some(val previous) -> if (previous === input && query == cachedQuery)35
return result!!36
case None -> {}37
}38
val next = commandResults(input.source, query, input.filter)39
cached = Some(input);40
cachedQuery = query;41
result = Some(next)42
return next43
}44
private fun highlight(key: ViewKey) {45
val input = props.value46
if (!input.input.enabled || input.query.isComposing)47
return48
val source = results()49
val index = source.indexOfKey(key) ?: return50
if (source.enabledAt(index))51
active.value = Some(key)52
}53
private fun select(key: ViewKey, requestFocus: Boolean = true): SemanticActionResult {54
val input = props.value55
if (!input.input.enabled || input.query.isComposing)56
return SemanticActionResult.Rejected57
val source = results()58
val index = source.indexOfKey(key) ?: return SemanticActionResult.Unavailable59
val item = commandItem(source.entryAt(index)) ?: return SemanticActionResult.Unsupported60
if (!item.enabled)61
return SemanticActionResult.Rejected62
val focus = input.input.focus.handle ?: ownFocus63
if (requestFocus && !focus.hasFocus)64
focus.requestFocus(FocusOrigin.Pointer)65
item.onSelect()66
props.value.onSelect(key)67
return SemanticActionResult.Completed68
}69
private fun rows(source: CommandSource, input: CommandProps, theme: Theme): CommandRows {70
when (rowSource) {71
case Some(val previous) -> if (72
previous.source === source && previous.input === input && previous.theme === theme73
)74
return previous75
case None -> {}76
}77
val next =78
CommandRows(79
source,80
input,81
theme,82
active,83
{ key -> highlight(key) },84
{ key, focus -> select(key, focus) }85
)86
rowSource = Some(next)87
return next88
}89
public override fun render(): View {90
active.value91
val input = props.value92
val theme = themes.read(input.input.theme)93
val density = theme.tokens.typography.body.fontSize / 14.094
val source = results()95
val focus = input.input.focus.handle ?: ownFocus96
val list = input.listHandle ?: ownList97
val scrollState = input.scroll ?: ownScroll98
val children = ArrayList<View>()99
children.add(100
commandInput(101
input,102
focus,103
theme,104
list.semanticId,105
when (val key = active.value) {106
case Some(val value) -> if (source.indexOfKey(value) != None)107
key108
else109
None110
case None -> None111
}112
)113
)114
var height = 0.0115
if (source.itemCount == 0L) {116
children.add(117
panelFrame(118
panelCaption(119
input.empty,120
theme.tokens.typography.body.copy {121
fontSize = 14.0 * density;122
color = theme.tokens.palette.muted.foreground123
},124
input.input.fonts,125
true126
),127
Insets(8.0 * density, 24.0 * density, 8.0 * density, 24.0 * density)128
)129
)130
height = input.maxHeight * density131
} else {132
val rows = rows(source, input, theme)133
children.add(134
virtualList(rows, ItemExtent.Variable(32.0 * density)) {135
itemSemantics = Some(ItemSemantics(rows, { index -> rows.describe(index) }))136
state = Some(scrollState)137
handle = Some(list)138
this.theme = input.input.theme139
this.enabled = input.input.enabled140
this.focus = FocusOptions(tabStop = false)141
scrollbars = ScrollbarVisibility.Hidden142
lineStep = 32.0 * density143
chain = ScrollChain.Contain144
overscan = 32.0 * density145
}146
)147
height = upperBound(input.maxHeight * density, source.count.toDouble() * 32.0 * density)148
}149
val body =150
element(151
CommandLayoutProps(input.width * density, height, source.itemCount == 0L),152
{ current, mount -> CommandLayout(current, scope = mount) },153
children.toArray()154
)155
return element(156
CandidateNavigationProps(157
{ results() },158
input.query,159
active,160
focus,161
input.input.enabled,162
input.loop,163
input.autoHighlight,164
{ key -> (props.value.listHandle ?: ownList).scrollToItem(key) },165
{ key -> select(key) }166
),167
{ current, mount -> CandidateNavigation(current, scope = mount) },168
[169
panelFrame(170
body,171
Insets.all(4.0 * density),172
styleOverride {173
background(Some(Paint(theme.tokens.palette.popover.background)))174
corners(Corners.all(12.0 * density))175
}.then(input.panelStyle),176
Some(input.width * density)177
)178
]179
)180
}181
}183
private class CommandLayoutProps(val width: Double, val listHeight: Double, val empty: Boolean)185
private class CommandLayout(186
private val props: Props<CommandLayoutProps>,187
scope: ComponentScope188
): Element<Unit>(scope) {189
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<190
Unit191
> {192
val width = constraints.constrain(Size(props.value.width, 0.0)).width193
val head =194
context.measureChild(195
0L,196
Constraints(width, Some(width), maxHeight = constraints.maxHeight)197
)198
val available =199
when (constraints.maxHeight) {200
case Some(val height) -> lowerBound(0.0, height - head.size.height)201
case None -> props.value.listHeight202
}203
val height = upperBound(props.value.listHeight, available)204
val list =205
context.measureChild(206
1L,207
if (props.value.empty)208
Constraints(width, Some(width), maxHeight = Some(height))209
else210
Constraints.tight(Size(width, height))211
)212
context.placeChild(0L, Point(0.0, 0.0))213
context.placeChild(1L, Point(0.0, head.size.height))214
return LayoutResult(215
LayoutMetrics(constraints.constrain(Size(width, head.size.height + list.size.height))),216
Unit217
)218
}219
public override fun scene(context: SceneContext, measured: Unit) {220
context.children()221
}222
}