forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ImageComposeScene will be introduced in the next commit. It performs in another thread, and have Unconfined coroutine dispatcher. This means all callbacks dispatched to that dispatcher will be performed in the caller thread. For example, when GlobalSnapshotManager will sendApplyNotifications, all callbacks will be performed in Swing thread, and will race with `render`, which performs in the test thread. To avoid that, we don't dispatch callback to coroutine dispatcher, instead we use our own dispatching inside ComposeScene via invalidate/render. Fixes JetBrains/compose-multiplatform#1392 Change-Id: I732b37dd2fa5bb03e7e8f6a001f518199360c9a1 Test: ./gradlew jvmTest desktopTest -Pandroidx.compose.multiplatformEnabled=true
- Loading branch information
Showing
4 changed files
with
86 additions
and
18 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
66 changes: 66 additions & 0 deletions
66
compose/ui/ui/src/skikoMain/kotlin/androidx/compose/ui/CommandList.desktop.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,66 @@ | ||
/* | ||
* 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 | ||
* | ||
* 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 androidx.compose.ui | ||
|
||
import androidx.compose.ui.platform.synchronized | ||
|
||
/** | ||
* Allows postponing execution of some code (command), adding it to the list via [add], | ||
* and performing all added commands in some time in the future via [perform] | ||
*/ | ||
internal class CommandList( | ||
private var onNewCommand: () -> Unit | ||
) { | ||
private val list = mutableListOf<() -> Unit>() | ||
private val listCopy = mutableListOf<() -> Unit>() | ||
|
||
/** | ||
* true if there are any commands added. | ||
* | ||
* Can be called concurrently from multiple threads. | ||
*/ | ||
val hasCommands: Boolean get() = synchronized(list) { | ||
list.isNotEmpty() | ||
} | ||
|
||
/** | ||
* Add command to the list, and notify observer via [onNewCommand]. | ||
* | ||
* Can be called concurrently from multiple threads. | ||
*/ | ||
fun add(command: () -> Unit) { | ||
synchronized(list) { | ||
list.add(command) | ||
} | ||
onNewCommand() | ||
} | ||
|
||
/** | ||
* Clear added commands and perform them. | ||
* | ||
* Doesn't support multiple [perform]'s from different threads. But does support concurrent [perform] | ||
* and concurrent [add]. | ||
*/ | ||
fun perform() { | ||
synchronized(list) { | ||
listCopy.addAll(list) | ||
list.clear() | ||
} | ||
listCopy.forEach { it.invoke() } | ||
listCopy.clear() | ||
} | ||
} |
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