-
Notifications
You must be signed in to change notification settings - Fork 586
/
TwoPane.kt
396 lines (371 loc) · 14.6 KB
/
TwoPane.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
/*
* Copyright 2022 The Android Open Source Project
*
* 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
*
* https://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 com.google.accompanist.adaptive
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.toComposeRect
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.LayoutCoordinates
import androidx.compose.ui.layout.boundsInWindow
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.constrain
import androidx.compose.ui.unit.constrainHeight
import androidx.compose.ui.unit.constrainWidth
import androidx.compose.ui.unit.dp
import androidx.window.layout.FoldingFeature
import kotlin.math.roundToInt
/**
* A layout that places two different pieces of content defined by the [first] and [second]
* slots where the arrangement, sizes and separation behaviour is controlled by [TwoPaneStrategy].
*
* The default implementations of [TwoPaneStrategy] are fold and hinges aware, meaning that the two
* pane will adopt its layout to properly separate [first] and [second] panes so they don't
* interfere with hardware hinges (vertical or horizontal), or respect folds when needed
* (for example, when foldable is half-folded (90-degree fold AKA tabletop) the split will become
* on the bend).
*
* The [TwoPane] layout will always place both [first] and [second], based on the provided
* [strategy] and window environment. If you instead only want to place one or the other,
* that should be controlled at a higher level and not calling [TwoPane] if placing both is not
* desired.
*
* @param first the first content of the layout, a left-most in LTR, a right-most in RTL and
* top-most in a vertical split based on the [SplitResult] of [TwoPaneStrategy.calculateSplitResult]
* @param second the second content of the layout, a right-most in the LTR, a left-most in the RTL
* and the bottom-most in a vertical split based on the [SplitResult] of
* [TwoPaneStrategy.calculateSplitResult]
* @param strategy strategy of the two pane that controls the arrangement of the layout
* @param modifier an optional modifier for the layout
*/
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TwoPane(
first: @Composable () -> Unit,
second: @Composable () -> Unit,
strategy: TwoPaneStrategy,
modifier: Modifier = Modifier
) {
val density = LocalDensity.current
Layout(
modifier = modifier.wrapContentSize(),
content = {
Box(Modifier.layoutId("first")) {
first()
}
Box(Modifier.layoutId("second")) {
second()
}
}
) { measurable, constraints ->
val firstMeasurable = measurable.find { it.layoutId == "first" }!!
val secondMeasurable = measurable.find { it.layoutId == "second" }!!
layout(constraints.maxWidth, constraints.maxHeight) {
val splitResult = strategy.calculateSplitResult(
density = density,
layoutDirection = layoutDirection,
layoutCoordinates = checkNotNull(coordinates) {
"TwoPane does not support the use of alignment lines!"
}
)
val isHorizontalSplit = splitResult.isHorizontalSplit
val splitArea = splitResult.splitArea
val splitLeft = constraints.constrainWidth(splitArea.left.roundToInt())
val splitRight = constraints.constrainWidth(splitArea.right.roundToInt())
val splitTop = constraints.constrainHeight(splitArea.top.roundToInt())
val splitBottom = constraints.constrainHeight(splitArea.bottom.roundToInt())
val firstConstraints =
if (isHorizontalSplit) {
val width = when (layoutDirection) {
LayoutDirection.Ltr -> splitLeft
LayoutDirection.Rtl -> constraints.maxWidth - splitRight
}
constraints.copy(minWidth = width, maxWidth = width)
} else {
constraints.copy(minHeight = splitTop, maxHeight = splitTop)
}
val secondConstraints =
if (isHorizontalSplit) {
val width = when (layoutDirection) {
LayoutDirection.Ltr -> constraints.maxWidth - splitRight
LayoutDirection.Rtl -> splitLeft
}
constraints.copy(minWidth = width, maxWidth = width)
} else {
val height = constraints.maxHeight - splitBottom
constraints.copy(minHeight = height, maxHeight = height)
}
val firstPlaceable = firstMeasurable.measure(constraints.constrain(firstConstraints))
val secondPlaceable = secondMeasurable.measure(constraints.constrain(secondConstraints))
firstPlaceable.placeRelative(0, 0)
val detailOffsetX =
if (isHorizontalSplit) {
constraints.maxWidth - secondPlaceable.width
} else {
0
}
val detailOffsetY =
if (isHorizontalSplit) {
0
} else {
constraints.maxHeight - secondPlaceable.height
}
secondPlaceable.placeRelative(detailOffsetX, detailOffsetY)
}
}
}
/**
* A strategy for configuring the [TwoPane] component, that is responsible for the meta-data
* corresponding to the arrangement of the two panes of the layout.
*/
@Stable
fun interface TwoPaneStrategy {
/**
* Calculates the split result in local bounds of the [TwoPane].
*
* @param density the [Density] for measuring and laying out
* @param layoutDirection the [LayoutDirection] for measuring and laying out
* @param layoutCoordinates the [LayoutCoordinates] of the [TwoPane]
*/
fun calculateSplitResult(
density: Density,
layoutDirection: LayoutDirection,
layoutCoordinates: LayoutCoordinates
): SplitResult
}
/**
* Returns the specification for where to place a split in [TwoPane] as a result of
* [TwoPaneStrategy.calculateSplitResult]
*/
class SplitResult(
/**
* Whether the split is vertical or horizontal
*/
val isHorizontalSplit: Boolean,
/**
* The area that is nether a `start` pane or an `end` pane, but a separation between those two.
* In case width or height is 0 - it means that the area itself is a 0 width/height, but the
* place within the layout is still defined.
*
* The [splitArea] should be defined in local bounds to the [TwoPane].
*/
val splitArea: Rect,
)
/**
* Returns a [TwoPaneStrategy] that will place the slots vertically or horizontally if there is a
* horizontal or vertical fold respectively.
*
* If there is no fold, then the [fallbackStrategy] will be used instead.
*/
public fun TwoPaneStrategy(
fallbackStrategy: TwoPaneStrategy,
windowGeometry: WindowGeometry,
): TwoPaneStrategy = HorizontalTwoPaneStrategy(
fallbackStrategy = VerticalTwoPaneStrategy(
fallbackStrategy = fallbackStrategy,
windowGeometry = windowGeometry,
),
windowGeometry = windowGeometry
)
/**
* Returns a [TwoPaneStrategy] that will place the slots horizontally if there is a vertical fold.
*
* If there is no horizontal fold, then the [fallbackStrategy] will be used instead.
*/
public fun HorizontalTwoPaneStrategy(
fallbackStrategy: TwoPaneStrategy,
windowGeometry: WindowGeometry,
): TwoPaneStrategy = TwoPaneStrategy { density, layoutDirection, layoutCoordinates ->
val verticalFold = windowGeometry.displayFeatures.find {
it is FoldingFeature && it.orientation == FoldingFeature.Orientation.VERTICAL
} as FoldingFeature?
if (verticalFold != null &&
(
verticalFold.isSeparating ||
verticalFold.occlusionType == FoldingFeature.OcclusionType.FULL
) &&
verticalFold.bounds.toComposeRect().overlaps(layoutCoordinates.boundsInWindow())
) {
val foldBounds = verticalFold.bounds.toComposeRect()
SplitResult(
isHorizontalSplit = true,
splitArea = Rect(
layoutCoordinates.windowToLocal(foldBounds.topLeft),
layoutCoordinates.windowToLocal(foldBounds.bottomRight)
)
)
} else {
fallbackStrategy.calculateSplitResult(density, layoutDirection, layoutCoordinates)
}
}
/**
* Returns a [TwoPaneStrategy] that will place the slots vertically if there is a horizontal fold.
*
* If there is no vertical fold, then the [fallbackStrategy] will be used instead.
*/
public fun VerticalTwoPaneStrategy(
fallbackStrategy: TwoPaneStrategy,
windowGeometry: WindowGeometry,
): TwoPaneStrategy = TwoPaneStrategy { density, layoutDirection, layoutCoordinates ->
val horizontalFold = windowGeometry.displayFeatures.find {
it is FoldingFeature && it.orientation == FoldingFeature.Orientation.HORIZONTAL
} as FoldingFeature?
if (horizontalFold != null &&
(
horizontalFold.isSeparating ||
horizontalFold.occlusionType == FoldingFeature.OcclusionType.FULL
) &&
horizontalFold.bounds.toComposeRect().overlaps(layoutCoordinates.boundsInWindow())
) {
val foldBounds = horizontalFold.bounds.toComposeRect()
SplitResult(
isHorizontalSplit = false,
splitArea = Rect(
layoutCoordinates.windowToLocal(foldBounds.topLeft),
layoutCoordinates.windowToLocal(foldBounds.bottomRight)
)
)
} else {
fallbackStrategy.calculateSplitResult(density, layoutDirection, layoutCoordinates)
}
}
/**
* Returns a [TwoPaneStrategy] that will place the slots horizontally.
*
* The split will be placed at the given [splitFraction] from start, with the given [splitWidth].
*
* This strategy is _not_ fold aware.
*/
public fun FractionHorizontalTwoPaneStrategy(
splitFraction: Float,
splitWidth: Dp = 0.dp,
): TwoPaneStrategy = TwoPaneStrategy { density, layoutDirection, layoutCoordinates ->
val splitX = layoutCoordinates.size.width * when (layoutDirection) {
LayoutDirection.Ltr -> splitFraction
LayoutDirection.Rtl -> 1 - splitFraction
}
val splitWidthPixel = with(density) { splitWidth.toPx() }
SplitResult(
isHorizontalSplit = true,
splitArea = Rect(
left = splitX - splitWidthPixel / 2f,
top = 0f,
right = splitX + splitWidthPixel / 2f,
bottom = layoutCoordinates.size.height.toFloat(),
)
)
}
/**
* Returns a [TwoPaneStrategy] that will place the slots horizontally.
*
* The split will be placed at [splitOffset] either from the start or end based on
* [offsetFromStart], with the given [splitWidth].
*
* This strategy is _not_ fold aware.
*/
public fun FixedOffsetHorizontalTwoPaneStrategy(
splitOffset: Dp,
offsetFromStart: Boolean,
splitWidth: Dp = 0.dp,
): TwoPaneStrategy = TwoPaneStrategy { density, layoutDirection, layoutCoordinates ->
val splitOffsetPixel = with(density) { splitOffset.toPx() }
val splitX = when (layoutDirection) {
LayoutDirection.Ltr ->
if (offsetFromStart) {
splitOffsetPixel
} else {
layoutCoordinates.size.width - splitOffsetPixel
}
LayoutDirection.Rtl ->
if (offsetFromStart) {
layoutCoordinates.size.width - splitOffsetPixel
} else {
splitOffsetPixel
}
}
val splitWidthPixel = with(density) { splitWidth.toPx() }
SplitResult(
isHorizontalSplit = true,
splitArea = Rect(
left = splitX - splitWidthPixel / 2f,
top = 0f,
right = splitX + splitWidthPixel / 2f,
bottom = layoutCoordinates.size.height.toFloat(),
)
)
}
/**
* Returns a [TwoPaneStrategy] that will place the slots horizontally.
*
* The split will be placed at the given [splitFraction] from start, with the given [splitHeight].
*
* This strategy is _not_ fold aware.
*/
public fun FractionVerticalTwoPaneStrategy(
splitFraction: Float,
splitHeight: Dp = 0.dp,
): TwoPaneStrategy = TwoPaneStrategy { density, _, layoutCoordinates ->
val splitY = layoutCoordinates.size.height * splitFraction
val splitHeightPixel = with(density) { splitHeight.toPx() }
SplitResult(
isHorizontalSplit = false,
splitArea = Rect(
left = 0f,
top = splitY - splitHeightPixel / 2f,
right = layoutCoordinates.size.width.toFloat(),
bottom = splitY + splitHeightPixel / 2f,
)
)
}
/**
* Returns a [TwoPaneStrategy] that will place the slots horizontally.
*
* The split will be placed at [splitOffset] either from the top or bottom based on
* [offsetFromTop], with the given [splitHeight].
*
* This strategy is _not_ fold aware.
*/
public fun FixedOffsetVerticalTwoPaneStrategy(
splitOffset: Dp,
offsetFromTop: Boolean,
splitHeight: Dp = 0.dp,
): TwoPaneStrategy = TwoPaneStrategy { density, _, layoutCoordinates ->
val splitOffsetPixel = with(density) { splitOffset.toPx() }
val splitY =
if (offsetFromTop) {
splitOffsetPixel
} else {
layoutCoordinates.size.height - splitOffsetPixel
}
val splitHeightPixel = with(density) { splitHeight.toPx() }
SplitResult(
isHorizontalSplit = false,
splitArea = Rect(
left = 0f,
top = splitY - splitHeightPixel / 2f,
right = layoutCoordinates.size.width.toFloat(),
bottom = splitY + splitHeightPixel / 2f,
)
)
}