Scoop API

scoop:scoop.ui.component:0.1.0

src/input_group/component.scoop

Selected production source · src/input_group/component.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.ui.style.Theme
8import scoop.window.*
10internal class InputGroupInput(
11 val props: InputGroupProps,
12 val theme: Theme,
13 val focus: FocusHandle,
14 val enabled: Boolean,
15 val invalid: Boolean
18public class InputGroup public constructor(
19 private val props: Props<InputGroupProps>,
20 scope: ComponentScope
21): 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.value
29 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.0
36 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.0
44 else
45 8.0
46 ) *
47 density,
48 (
49 if (block)
50 8.0
51 else
52 0.0
53 ) *
54 density,
55 (
56 if (block)
57 10.0
58 else
59 8.0
60 ) *
61 density,
62 (
63 if (block)
64 8.0
65 else
66 0.0
67 ) *
68 density
69 )
70 children.add(
71 frame(addon.content) {
72 this.padding = Some(padding)
73 this.alignment = Alignment(AxisAlignment.Start, AxisAlignment.Center)
74 this.style = addon.style
75 this.key = addon.key
76 }
77 )
78 }
79 return element(
80 InputGroupInput(value, theme, focus, enabled, invalid),
81 { current, mount -> InputGroupInk(current, scope = mount) },
82 children.toArray()
83 )
84 }
87internal fun inputGroupBlock(align: InputGroupAlign): Boolean =
88 align == InputGroupAlign.BlockStart || align == InputGroupAlign.BlockEnd
90internal class InputGroupInk(
91 internal val props: Props<InputGroupInput>,
92 scope: ComponentScope
93): Element<Unit>(scope) {
94 private var area: InputArea? = None
95 private var painters: ThemePainterCache? = None
97 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.enabled
143 painters!!.get(props.value.theme)
144 return UiInvalidation.Layout
145 }
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.invalid
154 )
155 )
156 internal fun options(): SurfaceOptions {
157 val surface = props.value.props.surface
158 return surface.copy { this.padding = Some(surface.padding ?: Insets()) }
159 }
160 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
161 Unit
162 > = 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.layoutDirection
171 )
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 }