From 68e93ed86dcdf31d9d49959f9495b86e9d1fbf3a Mon Sep 17 00:00:00 2001 From: tangcent Date: Sun, 12 Nov 2023 09:51:07 +0800 Subject: [PATCH] feat: Add dialog size memory feature --- .../idea/plugin/dialog/ContextDialog.kt | 43 +++++++++++++++++-- .../idea/plugin/dialog/SuvApiExportDialog.kt | 22 ---------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/ContextDialog.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/ContextDialog.kt index 078e37b31..e77b74e42 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/ContextDialog.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/ContextDialog.kt @@ -1,6 +1,8 @@ package com.itangcent.idea.plugin.dialog import com.google.inject.Inject +import com.intellij.ide.util.PropertiesComponent +import com.itangcent.common.logger.Log import com.itangcent.common.logger.traceError import com.itangcent.common.utils.safe import com.itangcent.idea.utils.initAfterShown @@ -8,12 +10,19 @@ import com.itangcent.intellij.constant.EventKey import com.itangcent.intellij.context.ActionContext import com.itangcent.intellij.extend.guice.PostConstruct import com.itangcent.intellij.logger.Logger +import java.awt.Dimension import java.awt.event.WindowAdapter import java.awt.event.WindowEvent import javax.swing.JDialog import javax.swing.WindowConstants abstract class ContextDialog : JDialog() { + companion object : Log() { + private const val LAST_SIZE = "com.itangcent.easyapi.suv.last.size" + } + + private val lastSizePropertiesName + get() = "$LAST_SIZE.${this::class.qualifiedName}" @Inject protected lateinit var actionContext: ActionContext @@ -36,6 +45,8 @@ abstract class ContextDialog : JDialog() { } }) + restoreSize() + this.initAfterShown { try { init() @@ -45,6 +56,22 @@ abstract class ContextDialog : JDialog() { } finally { init = true } + + //restore size + restoreSize() + } + } + + private fun restoreSize() { + PropertiesComponent.getInstance().getValue(lastSizePropertiesName)?.let { + val split = it.split(",") + if (split.size == 2) { + val width = split[0].toIntOrNull() + val height = split[1].toIntOrNull() + if (width != null && height != null) { + this.size = Dimension(width, height) + } + } } } @@ -87,12 +114,22 @@ abstract class ContextDialog : JDialog() { if (!disposed) { disposed = true dispose() - safe { onDispose() } + actionContext.runAsync { + safe { + PropertiesComponent.getInstance().setValue( + lastSizePropertiesName, + "${this.size.width},${this.size.height}" + ) + } + safe { onDispose() } + safe { + actionContext.unHold() + actionContext.stop() + } + } } } open fun onDispose() { - actionContext.unHold() - actionContext.stop() } } \ No newline at end of file diff --git a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/SuvApiExportDialog.kt b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/SuvApiExportDialog.kt index b5f1db496..ccda16a3b 100644 --- a/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/SuvApiExportDialog.kt +++ b/idea-plugin/src/main/kotlin/com/itangcent/idea/plugin/dialog/SuvApiExportDialog.kt @@ -19,7 +19,6 @@ class SuvApiExportDialog : ContextDialog() { companion object { private const val LAST_USED_CHANNEL = "com.itangcent.easyapi.suv.last.used.channel" - private const val LAST_SIZE = "com.itangcent.easyapi.suv.last.size" } private var contentPane: JPanel? = null @@ -134,19 +133,6 @@ class SuvApiExportDialog : ContextDialog() { override fun init() { actionContext.runAsync { - doAfterInit { - PropertiesComponent.getInstance().getValue(LAST_SIZE)?.let { - val split = it.split(",") - if (split.size == 2) { - val width = split[0].toIntOrNull() - val height = split[1].toIntOrNull() - if (width != null && height != null) { - this.size = Dimension(width, height) - } - } - } - } - for (i in 0..10) { Thread.sleep(500) if (disposed) { @@ -209,12 +195,4 @@ class SuvApiExportDialog : ContextDialog() { dispose() } } - - override fun onDispose() { - PropertiesComponent.getInstance().setValue( - LAST_SIZE, - "${this.size.width},${this.size.height}" - ) - super.onDispose() - } }