Browse Source

Refresh desktop preview (#2921)

Add refresh action to desktop preview window
Add global action for refreshing desktop preview (keymap can be assigned)
Partially addresses #2921
pull/4977/head
serge shustoff 6 months ago
parent
commit
9330503e2c
  1. 13
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewActions.kt
  2. 6
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewStateService.kt
  3. 1
      idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewToolWindow.kt
  4. 3
      idea-plugin/src/main/resources/META-INF/plugin.xml

13
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewActions.kt

@ -6,6 +6,7 @@
package org.jetbrains.compose.desktop.ide.preview package org.jetbrains.compose.desktop.ide.preview
import com.intellij.execution.executors.DefaultRunExecutor import com.intellij.execution.executors.DefaultRunExecutor
import com.intellij.icons.AllIcons
import com.intellij.openapi.actionSystem.AnAction import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.CommonDataKeys import com.intellij.openapi.actionSystem.CommonDataKeys
@ -31,6 +32,14 @@ class RunPreviewAction(
} }
} }
class RefreshShownPreviewAction : AnAction({ "Refresh last preview" }, AllIcons.Actions.Refresh) {
override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val previewLocation = project.service<PreviewStateService>().lastPreviewLocation ?: return
buildPreviewViaGradle(project, previewLocation)
}
}
internal const val PREVIEW_EDITOR_TOOLBAR_GROUP_ID = "Compose.Desktop.Preview.Editor.Toolbar" internal const val PREVIEW_EDITOR_TOOLBAR_GROUP_ID = "Compose.Desktop.Preview.Editor.Toolbar"
class RefreshOrRunPreviewAction : AnAction(PreviewIcons.COMPOSE) { class RefreshOrRunPreviewAction : AnAction(PreviewIcons.COMPOSE) {
@ -79,10 +88,10 @@ private fun buildPreviewViaGradle(project: Project, previewLocation: PreviewLoca
GradleConstants.SYSTEM_ID, GradleConstants.SYSTEM_ID,
object : TaskCallback { object : TaskCallback {
override fun onSuccess() { override fun onSuccess() {
previewService.buildFinished(success = true) previewService.buildFinished(previewLocation, success = true)
} }
override fun onFailure() { override fun onFailure() {
previewService.buildFinished(success = false) previewService.buildFinished(previewLocation, success = false)
} }
}, },
ProgressExecutionMode.IN_BACKGROUND_ASYNC, ProgressExecutionMode.IN_BACKGROUND_ASYNC,

6
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewStateService.kt

@ -32,6 +32,9 @@ class PreviewStateService(private val myProject: Project) : Disposable {
private val configurePreviewTaskNameCache = private val configurePreviewTaskNameCache =
ConfigurePreviewTaskNameCache(ConfigurePreviewTaskNameProviderImpl()) ConfigurePreviewTaskNameCache(ConfigurePreviewTaskNameProviderImpl())
var lastPreviewLocation: PreviewLocation? = null
private set
init { init {
val projectRefreshListener = ConfigurePreviewTaskNameCacheInvalidator(configurePreviewTaskNameCache) val projectRefreshListener = ConfigurePreviewTaskNameCacheInvalidator(configurePreviewTaskNameCache)
ExternalSystemProgressNotificationManager.getInstance() ExternalSystemProgressNotificationManager.getInstance()
@ -63,7 +66,8 @@ class PreviewStateService(private val myProject: Project) : Disposable {
previewListener.onNewBuildRequest() previewListener.onNewBuildRequest()
} }
internal fun buildFinished(success: Boolean) { internal fun buildFinished(previewLocation: PreviewLocation, success: Boolean) {
lastPreviewLocation = previewLocation
previewListener.onFinishedBuild(success) previewListener.onFinishedBuild(success)
} }
} }

1
idea-plugin/src/main/kotlin/org/jetbrains/compose/desktop/ide/preview/PreviewToolWindow.kt

@ -25,6 +25,7 @@ class PreviewToolWindow : ToolWindowFactory, DumbAware {
} }
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) { override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
toolWindow.setTitleActions(listOf(RefreshShownPreviewAction()))
toolWindow.contentManager.let { content -> toolWindow.contentManager.let { content ->
val panel = PreviewPanel(project) val panel = PreviewPanel(project)
val loadingPanel = JBLoadingPanel(BorderLayout(), project) val loadingPanel = JBLoadingPanel(BorderLayout(), project)

3
idea-plugin/src/main/resources/META-INF/plugin.xml

@ -48,6 +48,9 @@
</extensions> </extensions>
<actions> <actions>
<group id="Compose.Desktop.Preview">
<action class="org.jetbrains.compose.desktop.ide.preview.RefreshShownPreviewAction"/>
</group>
<group id="Compose.Desktop.Preview.Editor.Toolbar"> <group id="Compose.Desktop.Preview.Editor.Toolbar">
<action class="org.jetbrains.compose.desktop.ide.preview.RefreshOrRunPreviewAction"/> <action class="org.jetbrains.compose.desktop.ide.preview.RefreshOrRunPreviewAction"/>
</group> </group>

Loading…
Cancel
Save