-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for getting the device's safe area insets.
- Loading branch information
1 parent
7e441f4
commit c35c6b1
Showing
22 changed files
with
395 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import app.cash.redwood.buildsupport.KmpTargets | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'org.jetbrains.kotlin.multiplatform' | ||
apply plugin: 'app.cash.redwood.build.compose' | ||
apply plugin: 'com.vanniktech.maven.publish' | ||
apply plugin: 'org.jetbrains.dokka' // Must be applied here for publish plugin. | ||
|
||
kotlin { | ||
android { | ||
publishLibraryVariants('release') | ||
} | ||
|
||
KmpTargets.addAllTargets(project, true /* skipJs */) | ||
|
||
sourceSets { | ||
commonMain { | ||
dependencies { | ||
api projects.redwoodTreehouse | ||
api projects.redwoodTreehouseHost | ||
implementation libs.jetbrains.compose.foundation | ||
implementation projects.redwoodWidgetCompose | ||
} | ||
} | ||
androidMain { | ||
dependencies { | ||
implementation libs.androidx.core | ||
} | ||
} | ||
} | ||
} | ||
|
||
android { | ||
namespace 'app.cash.treehouse.composeui' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
POM_ARTIFACT_ID=redwood-treehouse-composeui-insets | ||
POM_NAME=Redwood Treehouse Compose UI Insets |
41 changes: 41 additions & 0 deletions
41
...se-composeui-insets/src/androidMain/kotlin/app/cash/redwood/treehouse/composeui/insets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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. | ||
*/ | ||
@file:JvmName("insetsAndroid") | ||
|
||
package app.cash.redwood.treehouse.composeui | ||
|
||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.asPaddingValues | ||
import androidx.compose.foundation.layout.systemBars | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalLayoutDirection | ||
import androidx.compose.ui.unit.LayoutDirection | ||
import app.cash.redwood.layout.api.Margin | ||
|
||
@Composable | ||
public actual fun safeAreaInsets(): Margin { | ||
val layoutDirection = LocalLayoutDirection.current | ||
return WindowInsets.systemBars.asPaddingValues().toMargin(layoutDirection) | ||
} | ||
|
||
@Composable | ||
private fun PaddingValues.toMargin(layoutDirection: LayoutDirection) = Margin( | ||
left = calculateLeftPadding(layoutDirection).value.toDouble(), | ||
right = calculateRightPadding(layoutDirection).value.toDouble(), | ||
top = calculateTopPadding().value.toDouble(), | ||
bottom = calculateBottomPadding().value.toDouble(), | ||
) |
30 changes: 30 additions & 0 deletions
30
...use-composeui-insets/src/commonMain/kotlin/app/cash/redwood/treehouse/composeui/insets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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 app.cash.redwood.treehouse.composeui | ||
|
||
import androidx.compose.runtime.Composable | ||
import app.cash.redwood.layout.api.Margin | ||
|
||
/** Return the device's insets in screen density-independent pixels. */ | ||
@Composable | ||
public expect fun safeAreaInsets(): Margin | ||
|
||
internal operator fun Margin.div(density: Double) = Margin( | ||
left = left / density, | ||
right = right / density, | ||
top = top / density, | ||
bottom = bottom / density, | ||
) |
26 changes: 26 additions & 0 deletions
26
...ehouse-composeui-insets/src/iosMain/kotlin/app/cash/redwood/treehouse/composeui/insets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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 app.cash.redwood.treehouse.composeui | ||
|
||
import app.cash.redwood.layout.api.Margin | ||
import kotlinx.cinterop.useContents | ||
import platform.UIKit.UIApplication | ||
import platform.UIKit.safeAreaInsets | ||
|
||
public actual fun safeAreaInsets(): Margin { | ||
val keyWindow = UIApplication.sharedApplication.keyWindow ?: return Margin.Zero | ||
return keyWindow.safeAreaInsets.useContents { Margin(left, right, top, bottom) } | ||
} |
40 changes: 40 additions & 0 deletions
40
...ehouse-composeui-insets/src/jvmMain/kotlin/app/cash/redwood/treehouse/composeui/insets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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. | ||
*/ | ||
@file:JvmName("insetsJvm") | ||
|
||
package app.cash.redwood.treehouse.composeui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.platform.LocalDensity | ||
import app.cash.redwood.layout.api.Margin | ||
import java.awt.GraphicsEnvironment | ||
import java.awt.Insets | ||
import java.awt.Toolkit | ||
|
||
@Composable | ||
public actual fun safeAreaInsets(): Margin { | ||
val configuration = GraphicsEnvironment.getLocalGraphicsEnvironment() | ||
.defaultScreenDevice.defaultConfiguration | ||
val rawSpacing = Toolkit.getDefaultToolkit().getScreenInsets(configuration).toMargin() | ||
return rawSpacing / LocalDensity.current.density.toDouble() | ||
} | ||
|
||
private fun Insets.toMargin() = Margin( | ||
left = left.toDouble(), | ||
right = right.toDouble(), | ||
top = top.toDouble(), | ||
bottom = bottom.toDouble(), | ||
) |
25 changes: 25 additions & 0 deletions
25
...ouse-composeui-insets/src/macosMain/kotlin/app/cash/redwood/treehouse/composeui/insets.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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 app.cash.redwood.treehouse.composeui | ||
|
||
import app.cash.redwood.layout.api.Margin | ||
import kotlinx.cinterop.useContents | ||
import platform.AppKit.NSScreen | ||
|
||
public actual fun safeAreaInsets(): Margin { | ||
val mainScreen = NSScreen.mainScreen ?: return Margin.Zero | ||
return mainScreen.safeAreaInsets.useContents { Margin(left, right, top, bottom) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ kotlin { | |
androidMain { | ||
dependencies { | ||
api libs.okHttp | ||
implementation libs.androidx.core | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.