scoop:scoop.ui.component:0.1.0
src/input_group/component.scoop
Selected production source · src/input_group/component.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.surface.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.ui.style.Theme8
import scoop.window.*10
internal class InputGroupInput(11
val props: InputGroupProps,12
val theme: Theme,13
val focus: FocusHandle,14
val enabled: Boolean,15
val invalid: Boolean16
)18
public class InputGroup public constructor(19
private val props: Props<InputGroupProps>,20
scope: ComponentScope21
): Component(scope) {22
private val themes = ThemeAccess(scope)23
private val ownFocus = FocusHandle()24
public override fun onMount() {25
themes.mount(props.value.theme)26
}27
public override fun render(): View {28
val value = props.value29
val theme = themes.read(value.theme)30
val enabled = value.enabled && inputGroupControlEnabled(value.control)31
val invalid = value.invalid || inputGroupControlInvalid(value.control)32
val focus = inputGroupFocus(value.control, ownFocus)33
val children = ArrayList<View>()34
children.add(inputGroupControlView(value, focus, enabled, invalid))35
val density = theme.tokens.typography.body.fontSize / 14.036
for (addon in value.addons) {37
val block = inputGroupBlock(addon.align)38
val padding =39
addon.padding ?:40
Insets(41
(42
if (block)43
10.044
else45
8.046
) *47
density,48
(49
if (block)50
8.051
else52
0.053
) *54
density,55
(56
if (block)57
10.058
else59
8.060
) *61
density,62
(63
if (block)64
8.065
else66
0.067
) *68
density69
)70
children.add(71
frame(addon.content) {72
this.padding = Some(padding)73
this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Center)74
this.style = addon.style75
this.key = addon.key76
}77
)78
}79
return element(80
InputGroupInput(value, theme, focus, enabled, invalid),81
{ current, mount -> InputGroupInk(current, scope = mount) },82
children.toArray()83
)84
}85
}87
internal fun inputGroupBlock(align: InputGroupAlign): Boolean =88
align == InputGroupAlign.BlockStart || align == InputGroupAlign.BlockEnd90
internal class InputGroupInk(91
internal val props: Props<InputGroupInput>,92
scope: ComponentScope93
): Element<Unit>(scope) {94
private var area: InputArea? = None95
private var painters: ThemePainterCache? = None97
public override fun onMount() {98
area =99
Some(100
scope.createArea {101
_,102
_ -> if (scope.isMounted)103
scope.requestFrame()104
}105
)106
scope.routeChildren(area!!)107
scope.press(area!!, PressOptions()) {108
if (props.value.enabled)109
props.value.focus.requestFocus(FocusOrigin.Pointer)110
}111
area!!.scope.defaultAction =112
{113
context,114
event -> when (event) {115
case Pointer(116
InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)117
) -> {118
if (119
props.value.enabled &&120
props.value.focus.requestFocus(FocusOrigin.Pointer)121
)122
context.consume()123
}124
else -> {}125
}126
}127
scope.resource {128
area!!.scope.observeActivity {129
if (scope.isMounted)130
scope.requestFrame()131
}132
}133
scope.resource {134
area!!.scope.addEventFinished {135
_ -> if (scope.isMounted)136
scope.requestFrame()137
}138
}139
painters = Some(scope.resource { ThemePainterCache() })140
}141
public override fun update(): UiInvalidation {142
area!!.scope.enabled = props.value.enabled143
painters!!.get(props.value.theme)144
return UiInvalidation.Layout145
}146
internal fun visual(): SurfaceVisual =147
SurfaceVisual(148
SurfaceKind.Plain,149
InteractionVisual(150
props.value.enabled,151
area!!.scope.isHovered,152
focusVisible = props.value.focus.isFocusVisible,153
invalid = props.value.invalid154
)155
)156
internal fun options(): SurfaceOptions {157
val surface = props.value.props.surface158
return surface.copy { this.padding = Some(surface.padding ?: Insets()) }159
}160
public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<161
Unit162
> = LayoutResult(measureInputGroup(this, context, constraints), Unit)163
public override fun scene(context: SceneContext, measured: Unit) {164
val visual = visual()165
val shape =166
props.value.theme.surfaceGeometry(167
Rect(0.0, 0.0, context.size.width, context.size.height),168
visual,169
options(),170
context.layoutDirection171
)172
val painter = painters!!.get(props.value.theme)173
if (shape.opacity < 1.0)174
context.saveLayer(shape.opacity)175
context.draw {176
paint -> painter.drawSurface(paint.canvas, shape, visual, SurfaceLayer.Background)177
}178
if (context.size.width > 0.0 && context.size.height > 0.0) {179
context.place(area!!, shape.bounds)180
context.withState {181
content ->182
content.clipRect(shape.bounds)183
content.children()184
}185
}186
context.draw {187
paint -> painter.drawSurface(paint.canvas, shape, visual, SurfaceLayer.Foreground)188
}189
if (shape.opacity < 1.0)190
context.restore()191
}192
}