-
Notifications
You must be signed in to change notification settings - Fork 585
/
Padding.kt
387 lines (366 loc) · 13.8 KB
/
Padding.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
/*
* Copyright 2020 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.
*/
@file:Suppress("NOTHING_TO_INLINE", "unused")
package com.google.accompanist.insets
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
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.dp
/**
* Selectively apply additional space which matches the width/height of any system bars present
* on the respective edges of the screen.
*
* @param enabled Whether to apply padding using the system bars dimensions on the respective edges.
* Defaults to `true`.
*/
inline fun Modifier.systemBarsPadding(enabled: Boolean = true): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.systemBars,
applyStart = enabled,
applyTop = enabled,
applyEnd = enabled,
applyBottom = enabled
)
)
}
/**
* Selectively apply additional space which matches the width/height of any system bars present
* on the respective edges of the screen.
*
* @param start Whether to apply padding to the start edge, which matches the system bars width
* (if present) on the start edge of the screen. Defaults to `true`.
* @param top Whether to apply padding to the top edge, which matches the system bars height
* (if present) at the top edge of the screen. Defaults to `true`.
* @param end Whether to apply padding to the end edge, which matches the system bars width
* (if present) on the end edge of the screen. Defaults to `true`.
* @param bottom Whether to apply padding to the bottom edge, which matches the system bars
* height (if present) at the bottom edge of the screen. Defaults to `true`.
*/
inline fun Modifier.systemBarsPadding(
start: Boolean = true,
top: Boolean = true,
end: Boolean = true,
bottom: Boolean = true,
): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.systemBars,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom
)
)
}
/**
* Apply additional space which matches the height of the status bars height along the top edge
* of the content.
*/
inline fun Modifier.statusBarsPadding(): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.statusBars,
applyTop = true
)
)
}
/**
* Apply additional space which matches the height of the navigation bars height
* along the [bottom] edge of the content, and additional space which matches the width of
* the navigation bars on the respective [start] and [end] edges.
*
* @param bottom Whether to apply padding to the bottom edge, which matches the navigation bars
* height (if present) at the bottom edge of the screen. Defaults to `true`.
* @param start Whether to apply padding to the start edge, which matches the navigation bars width
* (if present) on the start edge of the screen. Defaults to `true`.
* @param end Whether to apply padding to the end edge, which matches the navigation bars width
* (if present) on the end edge of the screen. Defaults to `true`.
*/
inline fun Modifier.navigationBarsPadding(
bottom: Boolean = true,
start: Boolean = true,
end: Boolean = true,
): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.navigationBars,
applyStart = start,
applyEnd = end,
applyBottom = bottom
)
)
}
/**
* Apply additional space which matches the height of the [WindowInsets.ime] (on-screen keyboard)
* height along the bottom edge of the content.
*
* This method has no special handling for the [WindowInsets.navigationBars], which usually
* intersect the [WindowInsets.ime]. Most apps will usually want to use the
* [Modifier.navigationBarsWithImePadding] modifier.
*/
inline fun Modifier.imePadding(): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.ime,
applyStart = true,
applyEnd = true,
applyBottom = true
)
)
}
/**
* Selectively apply additional space which matches the width/height of any display cutout present
* on the respective edges of the screen.
*
* @param start Whether to apply padding to the start edge, which matches the display cutout width
* (if present) on the start edge of the screen. Defaults to `true`.
* @param top Whether to apply padding to the top edge, which matches the display cutout height
* (if present) at the top edge of the screen. Defaults to `true`.
* @param end Whether to apply padding to the end edge, which matches the display cutout width
* (if present) on the end edge of the screen. Defaults to `true`.
* @param bottom Whether to apply padding to the bottom edge, which matches the display cutout
* height (if present) at the bottom edge of the screen. Defaults to `true`.
*/
inline fun Modifier.cutoutPadding(
start: Boolean = true,
top: Boolean = true,
end: Boolean = true,
bottom: Boolean = true,
): Modifier = composed {
padding(
rememberInsetsPaddingValues(
insets = LocalWindowInsets.current.displayCutout,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom
)
)
}
/**
* Apply additional space which matches the height of the [WindowInsets.ime] (on-screen keyboard)
* height and [WindowInsets.navigationBars]. This is what apps should use to handle any insets
* at the bottom of the screen.
*/
inline fun Modifier.navigationBarsWithImePadding(): Modifier = composed {
val ime = LocalWindowInsets.current.ime
val navBars = LocalWindowInsets.current.navigationBars
val insets = remember(ime, navBars) { derivedWindowInsetsTypeOf(ime, navBars) }
padding(
rememberInsetsPaddingValues(
insets = insets,
applyStart = true,
applyEnd = true,
applyBottom = true
)
)
}
/**
* Returns the current insets converted into a [PaddingValues].
*
* @param start Whether to apply the inset on the start dimension.
* @param top Whether to apply the inset on the top dimension.
* @param end Whether to apply the inset on the end dimension.
* @param bottom Whether to apply the inset on the bottom dimension.
* @param additionalHorizontal Value to add to the start and end dimensions.
* @param additionalVertical Value to add to the top and bottom dimensions.
*/
@Deprecated(
"Replaced with rememberInsetsPaddingValues()",
ReplaceWith(
"""rememberInsetsPaddingValues(
insets = this,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom,
additionalStart = additionalHorizontal,
additionalTop = additionalVertical,
additionalEnd = additionalHorizontal,
additionalBottom = additionalVertical
)""",
"com.google.accompanist.insets.rememberInsetsPaddingValues"
)
)
@Composable
inline fun WindowInsets.Type.toPaddingValues(
start: Boolean = true,
top: Boolean = true,
end: Boolean = true,
bottom: Boolean = true,
additionalHorizontal: Dp = 0.dp,
additionalVertical: Dp = 0.dp,
): PaddingValues = rememberInsetsPaddingValues(
insets = this,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom,
additionalStart = additionalHorizontal,
additionalTop = additionalVertical,
additionalEnd = additionalHorizontal,
additionalBottom = additionalVertical
)
/**
* Returns the current insets converted into a [PaddingValues].
*
* @param start Whether to apply the inset on the start dimension.
* @param top Whether to apply the inset on the top dimension.
* @param end Whether to apply the inset on the end dimension.
* @param bottom Whether to apply the inset on the bottom dimension.
* @param additionalStart Value to add to the start dimension.
* @param additionalTop Value to add to the top dimension.
* @param additionalEnd Value to add to the end dimension.
* @param additionalBottom Value to add to the bottom dimension.
*/
@Deprecated(
"Replaced with rememberInsetsPaddingValues()",
ReplaceWith(
"""rememberInsetsPaddingValues(
insets = this,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom,
additionalStart = additionalStart,
additionalTop = additionalTop,
additionalEnd = additionalEnd,
additionalBottom = additionalBottom
)""",
"com.google.accompanist.insets.rememberInsetsPaddingValues"
)
)
@Composable
inline fun WindowInsets.Type.toPaddingValues(
start: Boolean = true,
top: Boolean = true,
end: Boolean = true,
bottom: Boolean = true,
additionalStart: Dp = 0.dp,
additionalTop: Dp = 0.dp,
additionalEnd: Dp = 0.dp,
additionalBottom: Dp = 0.dp,
): PaddingValues = rememberInsetsPaddingValues(
insets = this,
applyStart = start,
applyTop = top,
applyEnd = end,
applyBottom = bottom,
additionalStart = additionalStart,
additionalTop = additionalTop,
additionalEnd = additionalEnd,
additionalBottom = additionalBottom
)
/**
* Returns the provided insets [insets] in the form of a [PaddingValues] instance.
*
* The returned instance is stable, meaning that any changes to the inset values will result in
* composition being notified, and these padding values being re-read.
*
* @param insets The [WindowInsets.Type] to read from.
* @param applyStart Whether to apply the inset on the start dimension. Defaults to true.
* @param applyTop Whether to apply the inset on the top dimension. Defaults to true.
* @param applyEnd Whether to apply the inset on the end dimension. Defaults to true.
* @param applyBottom Whether to apply the inset on the bottom dimension. Defaults to true.
* @param additionalStart Value to add to the start dimension.
* @param additionalTop Value to add to the top dimension.
* @param additionalEnd Value to add to the end dimension.
* @param additionalBottom Value to add to the bottom dimension.
*/
@Composable
fun rememberInsetsPaddingValues(
insets: Insets,
applyStart: Boolean = true,
applyTop: Boolean = true,
applyEnd: Boolean = true,
applyBottom: Boolean = true,
additionalStart: Dp = 0.dp,
additionalTop: Dp = 0.dp,
additionalEnd: Dp = 0.dp,
additionalBottom: Dp = 0.dp,
): PaddingValues {
val density = LocalDensity.current
return remember(density, insets) {
InsetsPaddingValues(insets = insets, density = density)
}.apply {
this.applyStart = applyStart
this.applyTop = applyTop
this.applyEnd = applyEnd
this.applyBottom = applyBottom
this.additionalStart = additionalStart
this.additionalTop = additionalTop
this.additionalEnd = additionalEnd
this.additionalBottom = additionalBottom
}
}
/**
* Our [PaddingValues] implementation which is state-backed, reading values from [insets] as
* appropriate. Can be created via [rememberInsetsPaddingValues].
*/
@Stable
internal class InsetsPaddingValues(
private val insets: Insets,
private val density: Density,
) : PaddingValues {
var applyStart: Boolean by mutableStateOf(false)
var applyTop: Boolean by mutableStateOf(false)
var applyEnd: Boolean by mutableStateOf(false)
var applyBottom: Boolean by mutableStateOf(false)
var additionalStart: Dp by mutableStateOf(0.dp)
var additionalTop: Dp by mutableStateOf(0.dp)
var additionalEnd: Dp by mutableStateOf(0.dp)
var additionalBottom: Dp by mutableStateOf(0.dp)
override fun calculateLeftPadding(layoutDirection: LayoutDirection): Dp {
return when (layoutDirection) {
LayoutDirection.Ltr -> {
additionalStart + if (applyStart) with(density) { insets.left.toDp() } else 0.dp
}
LayoutDirection.Rtl -> {
additionalEnd + if (applyEnd) with(density) { insets.left.toDp() } else 0.dp
}
}
}
override fun calculateTopPadding(): Dp = additionalTop + when {
applyTop -> with(density) { insets.top.toDp() }
else -> 0.dp
}
override fun calculateRightPadding(layoutDirection: LayoutDirection): Dp {
return when (layoutDirection) {
LayoutDirection.Ltr -> {
additionalEnd + if (applyEnd) with(density) { insets.right.toDp() } else 0.dp
}
LayoutDirection.Rtl -> {
additionalStart + if (applyStart) with(density) { insets.right.toDp() } else 0.dp
}
}
}
override fun calculateBottomPadding(): Dp = additionalBottom + when {
applyBottom -> with(density) { insets.bottom.toDp() }
else -> 0.dp
}
}