Scoop API

scoop:scoop.ui.component:0.1.0

src/resizable/handle.scoop

Selected production source · src/resizable/handle.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.surface.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.*
8import scoop.window.accessibility.*
10internal class ResizableHandle(
11 private val props: Props<ResizablePanelInput>,
12 scope: ComponentScope
13): Element<Unit>(scope) {
14 private var area: InputArea? = None
15 private var focus: FocusTarget? = None
16 private var drag: Drag? = None
17 private var painters: ThemePainterCache? = None
18 private var grip: StylePainter? = None
19 private var ownsCursor = false
20 private val semantics = createComponentSemantics(scope, SemanticRole.Separator)
21 private fun enabled(): Boolean =
22 props.value.hasHandle &&
23 props.value.root.props.enabled &&
24 resizableHandle(props.value.root.props, props.value.index).enabled
25 private fun options(): DragOptions =
26 DragOptions(
27 if (props.value.root.props.orientation == Orientation.Horizontal)
28 DragAxis.Horizontal
29 else
30 DragAxis.Vertical,
31 threshold = props.value.root.props.dragThreshold,
32 enabled = enabled()
33 )
34 public override fun onMount() {
35 area = Some(scope.createArea(focusable = true) { context, event -> notify(context, event) })
36 focus =
37 Some(
38 scope.focusTarget(
39 area!!,
40 resizableHandle(props.value.root.props, props.value.index).focus
41 )
42 )
43 semantics?.bindFocus(focus)
44 painters = Some(scope.resource { ThemePainterCache() })
45 grip = Some(scope.resource { StylePainter() })
46 drag =
47 Some(
48 scope.drag(
49 area!!,
50 options(),
51 { _ -> props.value.root.owner.control!!.available(props.value.index) },
52 { event -> receiveDrag(event) }
53 )
54 )
55 scope.resource {
56 area!!.scope.addDefaultAction {
57 context,
58 event -> when (event) {
59 case Keyboard(val key) -> {
60 if (area!!.isFocusActive)
61 props.value.root.owner.control!!.key(props.value.index, context, key)
62 }
63 case Pointer(
64 InputPointerEvent.Button(_, MouseButton.Left, ElementState.Pressed, _)
65 ) -> {
66 if (enabled())
67 focus!!.handle.requestFocus(FocusOrigin.Pointer)
68 }
69 else -> {}
70 }
71 }
72 }
73 }
74 private fun receiveDrag(event: DragEvent) {
75 props.value.root.owner.control!!.handle(props.value.index, drag!!, focus!!, event)
76 if (scope.isMounted) {
77 refreshCursor()
78 scope.requestFrame()
79 }
80 }
81 private fun notify(context: RoutedEventContext, event: InputEvent) {
82 if (context.phase != RoutedPhase.Notification || !scope.isMounted)
83 return
84 when (event) {
85 case FocusChanged(false) -> props.value.root.owner.control!!.lostFocus(props.value.key)
86 else -> {}
87 }
88 if (!scope.isMounted)
89 return
90 refreshCursor()
91 scope.requestFrame()
92 }
93 private fun refreshCursor() {
94 val active = enabled() && (area!!.scope.isHovered || (drag?.isDragging ?: false))
95 if (active) {
96 resizeCursor?.invoke(scope.nativeWindow, Some(props.value.root.props.orientation))
97 ownsCursor = true
98 } else
99 if (ownsCursor) {
100 resizeCursor?.invoke(scope.nativeWindow, None)
101 ownsCursor = false
102 }
103 }
104 public override fun update(): UiInvalidation {
105 val input = props.value
106 focus!!.options = resizableHandle(input.root.props, input.index).focus
107 if (!scope.isMounted)
108 return UiInvalidation.Layout
109 area!!.scope.enabled = enabled()
110 if (!scope.isMounted)
111 return UiInvalidation.Layout
112 drag!!.update(
113 options(),
114 { _ -> props.value.root.owner.control!!.available(props.value.index) },
115 { event -> receiveDrag(event) }
116 )
117 if (!scope.isMounted)
118 return UiInvalidation.Layout
119 painters!!.get(input.root.theme)
120 val options = resizableHandle(input.root.props, input.index).semantics
121 val control = input.root.owner.control!!
122 semantics?.bindReference(options.reference)
123 semantics?.update(
124 options.properties(enabled = enabled(), range = control.range(input.index)).copy {
125 orientation =
126 Some(
127 if (input.root.props.orientation == Orientation.Horizontal)
128 SemanticOrientation.Horizontal
129 else
130 SemanticOrientation.Vertical
131 )
132 if (name == None && options.relations.labelledBy.size == 0L)
133 name = Some("Resize panels")
134 },
135 SemanticActions(
136 setNumber =
137 Some {
138 value -> props.value.root.owner.control!!.setNumber(
139 props.value.index,
140 value
141 )
142 },
143 increment =
144 Some {
145 props.value.root.owner.control!!.adjust(
146 props.value.index,
147 ResizableKey.Step(1.0)
148 )
149 },
150 decrement =
151 Some {
152 props.value.root.owner.control!!.adjust(
153 props.value.index,
154 ResizableKey.Step(-1.0)
155 )
156 },
157 invoke =
158 if (
159 input.root.props.panels[input.index].collapsible ||
160 input.root.props.panels[input.index + 1L].collapsible
161 )
162 Some {
163 props.value.root.owner.control!!.adjust(
164 props.value.index,
165 ResizableKey.Toggle
166 )
167 }
168 else
169 None
170 ),
171 options.relations
172 )
173 refreshCursor()
174 return UiInvalidation.Layout
175 }
176 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
177 Unit
178 > {
179 val input = props.value
180 val theme = input.root.theme
181 val density = theme.tokens.typography.body.fontSize / 14.0
182 val size = constraints.constrain(Size(density, density))
183 return LayoutResult(LayoutMetrics(size), Unit)
184 }
185 public override fun scene(context: SceneContext, measured: Unit) {
186 when (semantics) {
187 case Some(val handle) -> context.semantics(
188 handle,
189 Rect(0.0, 0.0, context.size.width, context.size.height)
190 );
191 case None -> {}
192 }
193 val input = props.value
194 val theme = input.root.theme
195 val density = theme.tokens.typography.body.fontSize / 14.0
196 val size = context.size
197 val visual =
198 SurfaceVisual(
199 SurfaceKind.Plain,
200 InteractionVisual(
201 enabled(),
202 area!!.scope.isHovered,
203 drag!!.isDragging,
204 area!!.isFocusVisible
205 )
206 )
207 val local =
208 styleOverride {
209 background(Some(Paint(theme.tokens.palette.border)))
210 border(None);
211 corners(Corners.all(0.0))
212 }.then(resizableHandle(input.root.props, input.index).style)
213 val geometry =
214 theme.surfaceGeometry(
215 Rect(0.0, 0.0, size.width, size.height),
216 visual,
217 surfaceOptions {
218 style = local;
219 padding = Some(Insets());
220 focus = Some(OutlineStyle(theme.tokens.palette.ring, density, density))
221 },
222 context.layoutDirection
223 )
225 val horizontal = input.root.props.orientation == Orientation.Horizontal
226 val shape = geometry
227 context.draw { paint -> painters!!.get(theme).drawSurface(paint.canvas, shape, visual) }
228 if (
229 resizableHandle(input.root.props, input.index).withHandle ?: input.root.props.withHandle
230 ) {
231 val size = resizableSize(4.0 * density, 24.0 * density, horizontal)
232 grip!!.scene(
233 context,
234 Rect(
235 (context.size.width - size.width) / 2.0,
236 (context.size.height - size.height) / 2.0,
237 size.width,
238 size.height
239 ),
240 style {
241 background = Some(Paint(theme.tokens.palette.border));
242 corners = Corners.all(8.0 * density)
243 }
244 ) { _ -> }
245 }
246 val hitWidth = resizableHandle(input.root.props, input.index).hitWidth * density
247 if (context.size.width > 0.0 && context.size.height > 0.0)
248 context.place(
249 area!!,
250 if (horizontal)
251 Rect((context.size.width - hitWidth) / 2.0, 0.0, hitWidth, context.size.height)
252 else
253 Rect(0.0, (context.size.height - hitWidth) / 2.0, context.size.width, hitWidth)
254 )
255 }
256 public override fun onUnmount() {
257 props.value.root.owner.control!!.lostFocus(props.value.key)
258 if (ownsCursor && scope.isMounted)
259 resizeCursor?.invoke(scope.nativeWindow, None)
260 }