/
CommandMenuPopupContent.kt
658 lines (611 loc) · 26.8 KB
/
CommandMenuPopupContent.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* Copyright 2020-2022 Aurora, Kirill Grouchnikov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pushingpixels.aurora.component.utils
import androidx.compose.foundation.ScrollbarAdapter
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalContext
import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.ComposeWindow
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.Placeable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.*
import org.pushingpixels.aurora.common.AuroraInternalApi
import org.pushingpixels.aurora.common.AuroraPopupManager
import org.pushingpixels.aurora.component.*
import org.pushingpixels.aurora.component.model.*
import org.pushingpixels.aurora.component.projection.HorizontalSeparatorProjection
import org.pushingpixels.aurora.theming.*
import org.pushingpixels.aurora.theming.colorscheme.AuroraSkinColors
import java.awt.*
import java.awt.geom.Rectangle2D
import javax.swing.border.Border
import kotlin.math.ceil
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
internal val Color.awtColor: java.awt.Color
get() = java.awt.Color(
this.red, this.green, this.blue, this.alpha
)
internal data class PopupContentLayoutInfo(
val fullSize: Size,
val buttonPanelSize: Size,
val separatorSize: Size,
val generalContentSize: Size,
val generalContentItemHeights: FloatArray,
val generalVerticalScrollbarSize: Size
)
internal fun displayPopupContent(
currentWindow: ComposeWindow,
layoutDirection: LayoutDirection,
density: Density,
textStyle: TextStyle,
resourceLoader: Font.ResourceLoader,
skinColors: AuroraSkinColors,
skinPainters: AuroraPainters,
decorationAreaType: DecorationAreaType,
compositionLocalContext: CompositionLocalContext,
anchorBoundsInWindow: Rect,
contentModel: State<CommandMenuContentModel?>,
presentationModel: CommandPopupMenuPresentationModel,
toDismissPopupsOnActivation: Boolean,
toUseBackgroundStriping: Boolean,
popupPlacementStrategy: PopupPlacementStrategy,
overlays: Map<Command, CommandButtonPresentationModel.Overlay>
) {
val popupContentWindow = ComposeWindow()
popupContentWindow.focusableWindowState = false
popupContentWindow.type = Window.Type.POPUP
popupContentWindow.isAlwaysOnTop = true
popupContentWindow.isUndecorated = true
popupContentWindow.isResizable = false
val fillColor = skinColors.getBackgroundColorScheme(decorationAreaType).backgroundFillColor
val awtFillColor = fillColor.awtColor
popupContentWindow.background = awtFillColor
val locationOnScreen = currentWindow.rootPane.locationOnScreen
val hasButtonPanel = (contentModel.value!!.panelContentModel != null)
val panelButtonLayoutManager =
if (hasButtonPanel) presentationModel.panelPresentationModel!!.commandPresentationState.createLayoutManager(
layoutDirection = layoutDirection,
density = density,
textStyle = textStyle,
resourceLoader = resourceLoader
) else null
// Compute the size of the popup content, accounting for the panel
val panelPreferredSize =
if (hasButtonPanel) getPreferredCommandPopupMenuPanelSize(
contentModel = contentModel.value!!.panelContentModel!!,
presentationModel = presentationModel.panelPresentationModel!!,
buttonLayoutManager = panelButtonLayoutManager!!,
layoutDirection = layoutDirection,
density = density
) else Size(0.0f, 0.0f)
// Command presentation for menu content, taking some values from
// the popup menu presentation model configured on the top-level presentation model
val regularButtonPresentationModel = CommandButtonPresentationModel(
presentationState = presentationModel.menuPresentationState,
iconActiveFilterStrategy = presentationModel.menuIconActiveFilterStrategy,
iconEnabledFilterStrategy = presentationModel.menuIconEnabledFilterStrategy,
iconDisabledFilterStrategy = presentationModel.menuIconDisabledFilterStrategy,
popupPlacementStrategy = presentationModel.popupPlacementStrategy,
backgroundAppearanceStrategy = BackgroundAppearanceStrategy.Flat,
horizontalAlignment = HorizontalAlignment.Leading,
contentPadding = presentationModel.menuContentPadding,
isMenu = true
)
val regularButtonLayoutManager =
presentationModel.menuPresentationState.createLayoutManager(
layoutDirection = layoutDirection,
density = density,
textStyle = textStyle,
resourceLoader = resourceLoader
)
var atLeastOneRegularButtonHasIcon = false
for (commandGroup in contentModel.value!!.groups) {
for (secondaryCommand in commandGroup.commands) {
if (secondaryCommand.icon != null) {
atLeastOneRegularButtonHasIcon = true
}
if (secondaryCommand.isActionToggle) {
atLeastOneRegularButtonHasIcon = true
}
}
}
val regularButtonPresentationModelOverlay =
CommandButtonPresentationModel.Overlay(
forceAllocateSpaceForIcon = atLeastOneRegularButtonHasIcon,
textStyle = TextStyle(fontWeight = FontWeight.Bold)
)
val regularButtonPresentationModelWithOverlay =
regularButtonPresentationModel.overlayWith(
regularButtonPresentationModelOverlay
)
// Account for possible presence of vertical scroll bar
var showingVerticalRegularContentScrollBar = false
var regularButtonColumnWidth = 0.0f
var regularButtonCount = 0
var regularButtonHeight = 0.0f
for (commandGroup in contentModel.value!!.groups) {
for (secondaryCommand in commandGroup.commands) {
val preferredSize = regularButtonLayoutManager.getPreferredSize(
command = secondaryCommand,
presentationModel = regularButtonPresentationModelWithOverlay,
preLayoutInfo = regularButtonLayoutManager.getPreLayoutInfo(
command = secondaryCommand,
presentationModel = regularButtonPresentationModelWithOverlay
)
)
regularButtonColumnWidth = max(regularButtonColumnWidth, preferredSize.width)
regularButtonHeight = max(regularButtonHeight, preferredSize.height)
regularButtonCount++
if (presentationModel.maxVisibleMenuCommands == regularButtonCount) {
// This is the maximum number of menu commands that we should show
showingVerticalRegularContentScrollBar = true
}
}
}
val separatorHeight = SeparatorSizingConstants.Thickness.value * density.density
var index = 0
val itemHeights = FloatArray(regularButtonCount + contentModel.value!!.groups.size - 1)
for ((groupIndex, commandGroup) in contentModel.value!!.groups.withIndex()) {
for (secondaryCommand in commandGroup.commands) {
itemHeights[index++] = regularButtonHeight
}
if (groupIndex < (contentModel.value!!.groups.size - 1)) {
itemHeights[index++] = separatorHeight
}
}
val visibleCommands = if (presentationModel.maxVisibleMenuCommands == 0) regularButtonCount
else min(presentationModel.maxVisibleMenuCommands, regularButtonCount)
val regularButtonColumnHeight = visibleCommands * regularButtonHeight +
(contentModel.value!!.groups.size - 1) * separatorHeight
val generalVerticalScrollbarWidth = if (showingVerticalRegularContentScrollBar) {
ScrollBarSizingConstants.DefaultScrollBarThickness.value * density.density +
2 * ScrollBarSizingConstants.DefaultScrollBarMargin.value * density.density
} else {
0.0f
}
// If we're displaying the button panel, we need to tweak either the button panel width
// or the general content width so that they match
val fullContentWidth = max(
panelPreferredSize.width,
regularButtonColumnWidth + generalVerticalScrollbarWidth
)
val fullContentHeight = panelPreferredSize.height +
(if (hasButtonPanel) SeparatorSizingConstants.Thickness.value * density.density else 0.0f) +
regularButtonColumnHeight
val finalGeneralContentWidth = fullContentWidth - generalVerticalScrollbarWidth
val offset = ceil(density.density).toInt()
val contentLayoutInfo = PopupContentLayoutInfo(
fullSize = Size(
width = fullContentWidth + 2 * offset,
height = fullContentHeight + 2 * offset
),
buttonPanelSize = Size(width = fullContentWidth, height = panelPreferredSize.height),
separatorSize = Size(
width = fullContentWidth,
height = if (hasButtonPanel) SeparatorSizingConstants.Thickness.value * density.density else 0.0f
),
generalContentSize = Size(
width = finalGeneralContentWidth,
height = regularButtonColumnHeight
),
generalContentItemHeights = itemHeights,
generalVerticalScrollbarSize = Size(
width = generalVerticalScrollbarWidth,
height = regularButtonColumnHeight
)
)
// Full size of the popup accounts for extra two pixels on each side for the popup border
val fullPopupWidth = ceil(fullContentWidth / density.density).toInt() + 4
val fullPopupHeight = ceil(fullContentHeight / density.density).toInt() + 4
// From this point, all coordinates are in Swing display units - which are density independent.
// This is why the popup width and height was converted from pixels.
val initialAnchorX = if (layoutDirection == LayoutDirection.Ltr)
(locationOnScreen.x + anchorBoundsInWindow.left).toInt() else
(locationOnScreen.x + anchorBoundsInWindow.left + anchorBoundsInWindow.width).toInt() - fullPopupWidth
val initialAnchor = IntOffset(
x = initialAnchorX,
y = (locationOnScreen.y + anchorBoundsInWindow.top).toInt()
)
val popupRect = when (popupPlacementStrategy) {
PopupPlacementStrategy.Downward -> Rectangle(
initialAnchor.x,
initialAnchor.y + anchorBoundsInWindow.height.toInt(),
fullPopupWidth,
fullPopupHeight
)
PopupPlacementStrategy.Upward -> Rectangle(
initialAnchor.x,
initialAnchor.y - fullPopupHeight,
fullPopupWidth,
fullPopupHeight
)
PopupPlacementStrategy.Startward -> if (layoutDirection == LayoutDirection.Ltr)
Rectangle(
initialAnchor.x - fullPopupWidth,
initialAnchor.y,
fullPopupWidth,
fullPopupHeight
) else
Rectangle(
initialAnchor.x + fullPopupWidth,
initialAnchor.y,
fullPopupWidth,
fullPopupHeight
)
PopupPlacementStrategy.Endward -> if (layoutDirection == LayoutDirection.Ltr)
Rectangle(
initialAnchor.x + anchorBoundsInWindow.width.toInt(),
initialAnchor.y,
fullPopupWidth,
fullPopupHeight
) else
Rectangle(
initialAnchor.x - anchorBoundsInWindow.width.toInt(),
initialAnchor.y,
fullPopupWidth,
fullPopupHeight
)
PopupPlacementStrategy.CenteredVertically -> Rectangle(
initialAnchor.x,
initialAnchor.y + anchorBoundsInWindow.height.toInt() / 2
- fullPopupHeight / 2,
fullPopupWidth,
fullPopupHeight
)
}
// Make sure the popup stays in screen bounds
val screenBounds = popupContentWindow.graphicsConfiguration.bounds
if (popupRect.x < 0) {
popupRect.translate(-popupRect.x, 0)
}
if ((popupRect.x + popupRect.width) > screenBounds.width) {
popupRect.translate(
screenBounds.width - popupRect.x - popupRect.width,
0
)
}
if (popupRect.y < 0) {
popupRect.translate(0, -popupRect.y)
}
if ((popupRect.y + popupRect.height) > screenBounds.height) {
popupRect.translate(
0,
screenBounds.height - popupRect.y - popupRect.height
)
}
popupContentWindow.bounds = popupRect
val borderScheme = skinColors.getColorScheme(
decorationAreaType = DecorationAreaType.None,
associationKind = ColorSchemeAssociationKind.Border,
componentState = ComponentState.Enabled
)
val popupBorderColor = skinPainters.borderPainter.getRepresentativeColor(borderScheme)
val awtBorderColor = popupBorderColor.awtColor
val borderThickness = 1.0f / density.density
popupContentWindow.rootPane.border = object : Border {
override fun paintBorder(
c: Component,
g: Graphics,
x: Int,
y: Int,
width: Int,
height: Int
) {
val g2d = g.create() as Graphics2D
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
g2d.setRenderingHint(
RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE
)
g2d.color = awtFillColor
g2d.fill(Rectangle(0, 0, width, height))
val thickness = 0.5f
g2d.stroke = BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND)
g2d.color = awtBorderColor
g2d.draw(
Rectangle2D.Float(
borderThickness / 2.0f, borderThickness / 2.0f,
width - borderThickness, height - borderThickness
)
)
g2d.dispose()
}
override fun getBorderInsets(c: Component?): Insets {
return Insets(1, 1, 1, 1)
}
override fun isBorderOpaque(): Boolean {
return false
}
}
popupContentWindow.compositionLocalContext = compositionLocalContext
popupContentWindow.setContent {
TopLevelPopupContent(
popupContentWindow = popupContentWindow,
menuContentModel = contentModel,
menuPresentationModel = presentationModel,
toDismissPopupsOnActivation = toDismissPopupsOnActivation,
toUseBackgroundStriping = toUseBackgroundStriping,
overlays = overlays,
contentLayoutInfo = contentLayoutInfo
)
}
popupContentWindow.invalidate()
popupContentWindow.validate()
popupContentWindow.isVisible = true
// Hide the popups that "start" from the current window
AuroraPopupManager.hidePopups(originator = currentWindow)
// And display our new popup content
AuroraPopupManager.addPopup(
originator = currentWindow,
popupWindow = popupContentWindow,
popupKind = AuroraPopupManager.PopupKind.POPUP
)
}
@Composable
private fun TopLevelPopupContent(
popupContentWindow: ComposeWindow,
menuContentModel: State<CommandMenuContentModel?>,
menuPresentationModel: CommandPopupMenuPresentationModel,
toDismissPopupsOnActivation: Boolean,
toUseBackgroundStriping: Boolean,
overlays: Map<Command, CommandButtonPresentationModel.Overlay>,
contentLayoutInfo: PopupContentLayoutInfo
) {
val stateVertical = rememberScrollState(0)
val hasPanel = (menuContentModel.value!!.panelContentModel != null)
PopupContentLayout(
modifier = Modifier.auroraBackground(),
hasPanel = hasPanel,
contentLayoutInfo = contentLayoutInfo
) {
if (hasPanel) {
AuroraCommandButtonPanel(
contentModel = menuContentModel.value!!.panelContentModel!!,
presentationModel = menuPresentationModel.panelPresentationModel!!.toCommandPanelPresentationModel(),
extraAction = {
if (toDismissPopupsOnActivation) {
AuroraPopupManager.hidePopups(null)
}
}
)
HorizontalSeparatorProjection(
presentationModel = SeparatorPresentationModel(
startGradientAmount = 0.dp,
endGradientAmount = 0.dp
)
).project()
}
Layout(modifier = Modifier.verticalScroll(stateVertical), content = {
PopupGeneralContent(
popupContentWindow = popupContentWindow,
menuContentModel = menuContentModel,
menuPresentationModel = menuPresentationModel,
toDismissPopupsOnActivation = toDismissPopupsOnActivation,
toUseBackgroundStriping = toUseBackgroundStriping,
overlays = overlays
)
}) { measurables, _ ->
val placeables = measurables.mapIndexed { index, measurable ->
// Measure each child with fixed (widest) width and matching height (button
// height or separator height)
measurable.measure(
Constraints.fixed(
width = contentLayoutInfo.generalContentSize.width.roundToInt(),
height = contentLayoutInfo.generalContentItemHeights[index].toInt()
)
)
}
// The children are laid out in a column
val contentMaxHeight = placeables.sumOf { it.height }
layout(
width = contentLayoutInfo.generalContentSize.width.roundToInt(),
height = contentMaxHeight
) {
var yPosition = 0
placeables.forEach { placeable ->
placeable.placeRelative(
x = 0,
y = yPosition
)
yPosition += placeable.height
}
}
}
if (contentLayoutInfo.generalVerticalScrollbarSize.width > 0.0f) {
AuroraVerticalScrollbar(
adapter = remember(stateVertical) {
ScrollbarAdapter(stateVertical)
}
)
}
}
}
@OptIn(AuroraInternalApi::class)
@Composable
private fun PopupGeneralContent(
popupContentWindow: ComposeWindow,
menuContentModel: State<CommandMenuContentModel?>,
menuPresentationModel: CommandPopupMenuPresentationModel,
toDismissPopupsOnActivation: Boolean,
toUseBackgroundStriping: Boolean,
overlays: Map<Command, CommandButtonPresentationModel.Overlay>
) {
// If at least one secondary command in this popup menu has icon factory
// we force all command buttons to allocate space for the icon (for overall
// alignment of content across the entire popup menu)
var atLeastOneButtonHasIcon = false
for (commandGroup in menuContentModel.value!!.groups) {
for (secondaryCommand in commandGroup.commands) {
if (secondaryCommand.icon != null) {
atLeastOneButtonHasIcon = true
}
if (secondaryCommand.isActionToggle) {
atLeastOneButtonHasIcon = true
}
}
}
// Command presentation for menu content, taking some values from
// the popup menu presentation model configured on the top-level presentation model
val menuButtonPresentationModel = CommandButtonPresentationModel(
presentationState = menuPresentationModel.menuPresentationState,
iconActiveFilterStrategy = menuPresentationModel.menuIconActiveFilterStrategy,
iconEnabledFilterStrategy = menuPresentationModel.menuIconEnabledFilterStrategy,
iconDisabledFilterStrategy = menuPresentationModel.menuIconDisabledFilterStrategy,
forceAllocateSpaceForIcon = atLeastOneButtonHasIcon,
popupPlacementStrategy = menuPresentationModel.popupPlacementStrategy,
backgroundAppearanceStrategy = BackgroundAppearanceStrategy.Flat,
horizontalAlignment = HorizontalAlignment.Leading,
contentPadding = menuPresentationModel.menuContentPadding,
isMenu = true,
sides = Sides(straightSides = Side.values().toSet())
)
var runningCommandIndex = 0
val backgroundColorScheme = AuroraSkin.colors.getBackgroundColorScheme(
decorationAreaType = AuroraSkin.decorationAreaType
)
val backgroundEvenRows = backgroundColorScheme.backgroundFillColor
val backgroundOddRows = backgroundColorScheme.accentedBackgroundFillColor
for ((commandGroupIndex, commandGroup) in menuContentModel.value!!.groups.withIndex()) {
for (secondaryCommand in commandGroup.commands) {
// Check if we have a presentation overlay for this secondary command
val hasOverlay = overlays.containsKey(secondaryCommand)
var currSecondaryPresentationModel = if (hasOverlay)
menuButtonPresentationModel.overlayWith(overlays[secondaryCommand]!!)
else menuButtonPresentationModel
if (secondaryCommand == menuContentModel.value!!.highlightedCommand) {
// If our secondary content model has a highlighted command, pass bold
// font weight to the text style of the matching command button.
currSecondaryPresentationModel =
currSecondaryPresentationModel.overlayWith(
CommandButtonPresentationModel.Overlay(
textStyle = TextStyle(
fontWeight = FontWeight.Bold
)
)
)
}
// Create a command button for each secondary command, passing the same
// overlays into it.
AuroraCommandButton(
modifier = if (toUseBackgroundStriping)
Modifier.background(
color = if ((runningCommandIndex % 2) == 0) backgroundEvenRows else backgroundOddRows
) else Modifier,
actionInteractionSource = remember { MutableInteractionSource() },
popupInteractionSource = remember { MutableInteractionSource() },
command = secondaryCommand,
parentWindow = popupContentWindow,
extraAction = {
if (toDismissPopupsOnActivation and
currSecondaryPresentationModel.toDismissPopupsOnActivation
) {
AuroraPopupManager.hidePopups(null)
}
},
popupPlacementStrategyProvider = null,
presentationModel = currSecondaryPresentationModel,
overlays = overlays
)
runningCommandIndex++
}
if (commandGroupIndex < (menuContentModel.value!!.groups.size - 1)) {
HorizontalSeparatorProjection(
presentationModel = SeparatorPresentationModel(
startGradientAmount = 0.dp,
endGradientAmount = 0.dp
)
).project()
}
}
}
@Composable
private fun PopupContentLayout(
hasPanel: Boolean,
modifier: Modifier = Modifier,
contentLayoutInfo: PopupContentLayoutInfo,
content: @Composable () -> Unit
) {
val offset = ceil(LocalDensity.current.density).toInt()
Layout(modifier = modifier, content = content) { measurables, _ ->
var panelPlaceable: Placeable? = null
var panelSeparatorPlaceable: Placeable? = null
if (hasPanel) {
// The column width is determined by the panel
panelPlaceable = measurables[0].measure(
Constraints.fixed(
width = contentLayoutInfo.buttonPanelSize.width.toInt(),
height = contentLayoutInfo.buttonPanelSize.height.toInt()
)
)
panelSeparatorPlaceable = measurables[1].measure(
Constraints.fixed(
width = contentLayoutInfo.separatorSize.width.toInt(),
height = contentLayoutInfo.separatorSize.height.toInt()
)
)
}
val generalContentPlaceable = measurables[if (hasPanel) 2 else 0].measure(
Constraints.fixed(
width = contentLayoutInfo.generalContentSize.width.toInt(),
height = contentLayoutInfo.generalContentSize.height.toInt()
)
)
var verticalScrollBarPlaceable: Placeable? = null
val scrollBarMarginPx = ScrollBarSizingConstants.DefaultScrollBarMargin.roundToPx()
val scrollBarThicknessPx = ScrollBarSizingConstants.DefaultScrollBarThickness.roundToPx()
if (contentLayoutInfo.generalVerticalScrollbarSize.width > 0.0f) {
verticalScrollBarPlaceable = measurables[if (hasPanel) 3 else 1].measure(
Constraints.fixed(
width = scrollBarThicknessPx,
height = contentLayoutInfo.generalVerticalScrollbarSize.height.toInt() - 2 * scrollBarMarginPx
)
)
}
layout(
width = contentLayoutInfo.fullSize.width.toInt(),
height = contentLayoutInfo.fullSize.height.toInt()
) {
// Offset everything by [offset,offset] for border insets
var yPosition = offset
if (panelPlaceable != null) {
panelPlaceable.placeRelative(offset, offset)
yPosition += panelPlaceable.height
panelSeparatorPlaceable!!.placeRelative(offset, yPosition)
yPosition += panelSeparatorPlaceable.height
}
generalContentPlaceable.placeRelative(x = offset, y = yPosition)
verticalScrollBarPlaceable?.placeRelative(
x = offset + generalContentPlaceable.width + scrollBarMarginPx + offset,
y = yPosition + scrollBarMarginPx
)
}
}
}