Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add dialog size memory feature #1069

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}