@ -8,13 +8,17 @@ package org.jetbrains.compose.desktop.ide.preview
import com.intellij.openapi.Disposable
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.diff.impl.DiffUtil
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretEvent
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.event.CaretListener
import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider
import com.intellij.openapi.editor.toolbar.floating.AbstractFloatingToolbarProvider
import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent
import com.intellij.openapi.editor.toolbar.floating.FloatingToolbarComponent
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.Project
import com.intellij.testFramework.LightVirtualFileBase
import com.intellij.util.concurrency.AppExecutorUtil
import com.intellij.util.concurrency.AppExecutorUtil
import org.jetbrains.kotlin.idea.KotlinFileType
class PreviewFloatingToolbarProvider : AbstractFloatingToolbarProvider ( PREVIEW _EDITOR _TOOLBAR _GROUP _ID ) {
class PreviewFloatingToolbarProvider : AbstractFloatingToolbarProvider ( PREVIEW _EDITOR _TOOLBAR _GROUP _ID ) {
override val autoHideable = false
override val autoHideable = false
@ -22,7 +26,9 @@ class PreviewFloatingToolbarProvider : AbstractFloatingToolbarProvider(PREVIEW_E
// todo: disable if not in Compose JVM module
// todo: disable if not in Compose JVM module
override fun register ( dataContext : DataContext , component : FloatingToolbarComponent , parentDisposable : Disposable ) {
override fun register ( dataContext : DataContext , component : FloatingToolbarComponent , parentDisposable : Disposable ) {
val editor = dataContext . getData ( CommonDataKeys . EDITOR ) ?: return
val editor = dataContext . getData ( CommonDataKeys . EDITOR ) ?: return
registerComponent ( component , editor , parentDisposable )
if ( isInsideMainKtEditor ( editor ) ) {
registerComponent ( component , editor , parentDisposable )
}
}
}
private fun registerComponent (
private fun registerComponent (
@ -50,11 +56,24 @@ internal class PreviewEditorToolbarVisibilityUpdater(
}
}
private fun updateVisibility ( ) {
private fun updateVisibility ( ) {
val parentPreviewFun = parentPreviewAtCaretOrNull ( editor )
if ( ! editor . isDisposed ) {
if ( parentPreviewFun != null ) {
val parentPreviewFun = parentPreviewAtCaretOrNull ( editor )
toolbar . scheduleShow ( )
if ( parentPreviewFun != null ) {
} else {
toolbar . scheduleShow ( )
toolbar . scheduleHide ( )
} else {
toolbar . scheduleHide ( )
}
}
}
}
}
}
}
private fun isInsideMainKtEditor ( editor : Editor ) : Boolean =
! DiffUtil . isDiffEditor ( editor ) && editor . isKtFileEditor ( )
private fun Editor . isKtFileEditor ( ) : Boolean {
val documentManager = FileDocumentManager . getInstance ( )
val virtualFile = documentManager . getFile ( document ) ?: return false
return virtualFile !is LightVirtualFileBase
&& virtualFile . isValid
&& virtualFile . fileType == KotlinFileType . INSTANCE
}