@ -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 . n onBlocking { updateVisibility ( ) }
runN onBlocking { updateVisibility ( ) }
. inSmartMode ( project )
. submit ( AppExecutorUtil . getAppExecutorService ( ) )
}