-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
815 additions
and
22 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
redwood-compose/src/commonMain/kotlin/app/cash/redwood/compose/BackHandler.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,68 @@ | ||
/* | ||
* 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.compose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.compositionLocalOf | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import app.cash.redwood.ui.OnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher | ||
|
||
public val LocalOnBackPressedDispatcher: ProvidableCompositionLocal<OnBackPressedDispatcher> = | ||
compositionLocalOf { | ||
throw AssertionError("OnBackPressedDispatcher was not provided!") | ||
} | ||
|
||
public val OnBackPressedDispatcher.Companion.current: OnBackPressedDispatcher | ||
@Composable | ||
@ReadOnlyComposable | ||
get() = LocalOnBackPressedDispatcher.current | ||
|
||
// Multiplatform variant of | ||
// https://github.com/androidx/androidx/blob/94ae1a9fb3ce778295e8cc724ae29f1231436bcb/activity/activity-compose/src/main/java/androidx/activity/compose/BackHandler.kt#L82 | ||
@Composable | ||
public fun BackHandler(enabled: Boolean = true, onBack: () -> Unit) { | ||
// Safely update the current `onBack` lambda when a new one is provided. | ||
val currentOnBack by rememberUpdatedState(onBack) | ||
// Remember in Composition a back callback that calls the `onBack` lambda. | ||
// Explicit return type necessary per https://youtrack.jetbrains.com/issue/KT-42073 | ||
val backCallback: OnBackPressedCallback = remember { | ||
object : OnBackPressedCallback(enabled) { | ||
override fun handleOnBackPressed() { | ||
currentOnBack() | ||
} | ||
} | ||
} | ||
// On every successful composition, update the callback with the `enabled` value. | ||
SideEffect { | ||
backCallback.isEnabled = enabled | ||
} | ||
val backDispatcher = OnBackPressedDispatcher.current | ||
DisposableEffect(backDispatcher) { | ||
// Add callback to the backDispatcher. | ||
val cancellable = backDispatcher.addCallback(backCallback) | ||
// When the effect leaves the Composition, remove the callback. | ||
onDispose { | ||
cancellable.cancel() | ||
} | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...ood-composeui/src/androidMain/kotlin/app/cash/redwood/composeui/RedwoodContent.android.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,49 @@ | ||
/* | ||
* 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.composeui | ||
|
||
import androidx.activity.OnBackPressedCallback as AndroidOnBackPressedCallback | ||
import androidx.activity.compose.LocalOnBackPressedDispatcherOwner | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.OnBackPressedCallback as RedwoodOnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher as RedwoodOnBackPressedDispatcher | ||
|
||
@Composable | ||
internal actual fun platformOnBackPressedDispatcher(): RedwoodOnBackPressedDispatcher { | ||
val delegate = LocalOnBackPressedDispatcherOwner.current!!.onBackPressedDispatcher | ||
return remember(delegate) { | ||
object : RedwoodOnBackPressedDispatcher { | ||
override fun addCallback(onBackPressedCallback: RedwoodOnBackPressedCallback): Cancellable { | ||
val androidOnBackPressedCallback = onBackPressedCallback.toAndroid() | ||
delegate.addCallback(androidOnBackPressedCallback) | ||
return object : Cancellable { | ||
override fun cancel() { | ||
androidOnBackPressedCallback.remove() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun RedwoodOnBackPressedCallback.toAndroid(): AndroidOnBackPressedCallback = | ||
object : AndroidOnBackPressedCallback(this@toAndroid.isEnabled) { | ||
override fun handleOnBackPressed() { | ||
this@toAndroid.handleOnBackPressed() | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
redwood-composeui/src/iosMain/kotlin/app/cash/redwood/composeui/RedwoodContent.ios.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,34 @@ | ||
/* | ||
* 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.composeui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.OnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher | ||
|
||
@Composable | ||
internal actual fun platformOnBackPressedDispatcher(): OnBackPressedDispatcher { | ||
return remember { | ||
object : OnBackPressedDispatcher { | ||
override fun addCallback(onBackPressedCallback: OnBackPressedCallback): Cancellable = | ||
object : Cancellable { | ||
override fun cancel() = Unit | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
redwood-composeui/src/jvmMain/kotlin/app/cash/redwood/composeui/RedwoodContent.jvm.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,34 @@ | ||
/* | ||
* 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.composeui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.OnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher | ||
|
||
@Composable | ||
internal actual fun platformOnBackPressedDispatcher(): OnBackPressedDispatcher { | ||
return remember { | ||
object : OnBackPressedDispatcher { | ||
override fun addCallback(onBackPressedCallback: OnBackPressedCallback): Cancellable = | ||
object : Cancellable { | ||
override fun cancel() = Unit | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
redwood-composeui/src/macosMain/kotlin/app/cash/redwood/composeui/RedwoodContent.macos.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,34 @@ | ||
/* | ||
* 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.composeui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.OnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher | ||
|
||
@Composable | ||
internal actual fun platformOnBackPressedDispatcher(): OnBackPressedDispatcher { | ||
return remember { | ||
object : OnBackPressedDispatcher { | ||
override fun addCallback(onBackPressedCallback: OnBackPressedCallback): Cancellable = | ||
object : Cancellable { | ||
override fun cancel() = Unit | ||
} | ||
} | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
redwood-runtime/src/commonMain/kotlin/app/cash/redwood/ui/Cancellable.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,20 @@ | ||
/* | ||
* 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.ui | ||
|
||
public interface Cancellable { | ||
public fun cancel() | ||
} |
Oops, something went wrong.