Skip to content

Commit

Permalink
feat: Add dialog size memory feature
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Nov 12, 2023
1 parent c7850a8 commit 68e93ed
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
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
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
Expand All @@ -36,6 +45,8 @@ abstract class ContextDialog : JDialog() {
}
})

restoreSize()

this.initAfterShown {
try {
init()
Expand All @@ -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)
}
}
}
}

Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -209,12 +195,4 @@ class SuvApiExportDialog : ContextDialog() {
dispose()
}
}

override fun onDispose() {
PropertiesComponent.getInstance().setValue(
LAST_SIZE,
"${this.size.width},${this.size.height}"
)
super.onDispose()
}
}

0 comments on commit 68e93ed

Please sign in to comment.