@ -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,8 +26,10 @@ 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
if ( isInsideMainKtEditor ( editor ) ) {
registerComponent ( component , editor , parentDisposable )
registerComponent ( component , editor , parentDisposable )
}
}
}
private fun registerComponent (
private fun registerComponent (
component : FloatingToolbarComponent ,
component : FloatingToolbarComponent ,
@ -50,6 +56,7 @@ internal class PreviewEditorToolbarVisibilityUpdater(
}
}
private fun updateVisibility ( ) {
private fun updateVisibility ( ) {
if ( ! editor . isDisposed ) {
val parentPreviewFun = parentPreviewAtCaretOrNull ( editor )
val parentPreviewFun = parentPreviewAtCaretOrNull ( editor )
if ( parentPreviewFun != null ) {
if ( parentPreviewFun != null ) {
toolbar . scheduleShow ( )
toolbar . scheduleShow ( )
@ -57,4 +64,16 @@ internal class PreviewEditorToolbarVisibilityUpdater(
toolbar . scheduleHide ( )
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
}
}