diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt index 91ae5afca9..0a05eb0a6f 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt +++ b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewFloatingToolbarProvider.kt @@ -8,33 +8,18 @@ package org.jetbrains.compose.desktop.ide.preview import com.intellij.openapi.Disposable import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.DataContext -import com.intellij.openapi.application.ReadAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.editor.event.CaretEvent import com.intellij.openapi.editor.event.CaretListener import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent -import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponentImpl import com.intellij.openapi.project.Project import com.intellij.util.concurrency.AppExecutorUtil class PreviewFloatingToolbarProvider : AbstractFloatingToolbarProvider(PREVIEW_EDITOR_TOOLBAR_GROUP_ID) { override val autoHideable = false - override val priority: Int = 100 // todo: disable if not in Compose JVM module - override fun register(toolbar: FloatingToolbarComponent, parentDisposable: Disposable) { - try { - // todo: use provided data context once 2020.3 is no longer supported - val toolbarClass = FloatingToolbarComponentImpl::class.java - val getDataMethod = toolbarClass.getMethod("getData", String::class.java) - val editor = getDataMethod.invoke(toolbar, CommonDataKeys.EDITOR.name) as? Editor ?: return - registerComponent(toolbar, editor, parentDisposable) - } catch (e: Exception) { - LOG.error(e) - } - } - override fun register(dataContext: DataContext, component: FloatingToolbarComponent, parentDisposable: Disposable) { val editor = dataContext.getData(CommonDataKeys.EDITOR) ?: return registerComponent(component, editor, parentDisposable) @@ -59,7 +44,7 @@ internal class PreviewEditorToolbarVisibilityUpdater( private val editor: Editor ) : CaretListener { override fun caretPositionChanged(event: CaretEvent) { - ReadAction.nonBlocking { updateVisibility() } + runNonBlocking { updateVisibility() } .inSmartMode(project) .submit(AppExecutorUtil.getAppExecutorService()) } diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt index 3e4de4242e..e7c9b99fc1 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt +++ b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/editorUtils.kt @@ -9,7 +9,6 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.fileEditor.FileDocumentManager import com.intellij.psi.PsiFile import com.intellij.psi.PsiManager -import com.intellij.testFramework.LightVirtualFile import com.intellij.util.concurrency.annotations.RequiresReadLock import org.jetbrains.kotlin.idea.KotlinFileType import org.jetbrains.kotlin.psi.KtNamedFunction diff --git a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt index a5925bdc53..7bc501682b 100644 --- a/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt +++ b/idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/ijCompatibilityUtils.kt @@ -6,10 +6,13 @@ package org.jetbrains.compose.desktop.ide.preview import com.intellij.ide.lightEdit.LightEdit +import com.intellij.openapi.application.NonBlockingReadAction +import com.intellij.openapi.application.ReadAction import com.intellij.openapi.externalSystem.model.Key import com.intellij.openapi.externalSystem.model.project.AbstractNamedData import com.intellij.openapi.project.Project import java.lang.reflect.Modifier +import java.util.concurrent.Callable // todo: filter only Compose projects internal fun isPreviewCompatible(project: Project): Boolean = @@ -33,4 +36,10 @@ internal val kotlinTargetDataKey: Key = run { ?: error("Cannot find '${kotlinTargetDataClass.canonicalName}.Companion.getKEY'") @Suppress("UNCHECKED_CAST") getKeyMethod.invoke(companionInstance) as Key -} \ No newline at end of file +} + +internal inline fun runNonBlocking(crossinline fn: () -> Unit): NonBlockingReadAction = + ReadAction.nonBlocking(Callable { + fn() + null + }) \ No newline at end of file