diff --git a/plugins/editor/build.gradle.kts b/plugins/editor/build.gradle.kts index 6cc79b5..6d983df 100644 --- a/plugins/editor/build.gradle.kts +++ b/plugins/editor/build.gradle.kts @@ -4,7 +4,7 @@ plugins { -project.version = "0.0.6" +project.version = "0.0.7" dependencies { diff --git a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorDialog.kt b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorDialog.kt deleted file mode 100644 index 0e0a57e..0000000 --- a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorDialog.kt +++ /dev/null @@ -1,94 +0,0 @@ -package app.termora.plugins.editor - -import app.termora.DialogWrapper -import app.termora.Disposable -import app.termora.Disposer -import app.termora.OptionPane -import java.awt.Dimension -import java.awt.Window -import java.awt.event.WindowAdapter -import java.awt.event.WindowEvent -import java.io.File -import java.nio.file.Path -import java.util.concurrent.atomic.AtomicBoolean -import javax.swing.JComponent -import javax.swing.JOptionPane -import javax.swing.UIManager -import kotlin.io.path.absolutePathString -import kotlin.io.path.name - - -class EditorDialog(file: Path, owner: Window, private val myDisposable: Disposable) : DialogWrapper(null) { - - private val filename = file.name - private val filepath = File(file.absolutePathString()) - private val editorPanel = EditorPanel(this, filepath) - private val disposed = AtomicBoolean() - - init { - size = Dimension(UIManager.getInt("Dialog.width"), UIManager.getInt("Dialog.height")) - isModal = false - controlsVisible = true - isResizable = true - title = filename - iconImages = owner.iconImages - escapeDispose = false - defaultCloseOperation = DO_NOTHING_ON_CLOSE - - initEvents() - - setLocationRelativeTo(owner) - - init() - } - - - private fun initEvents() { - - addWindowListener(object : WindowAdapter() { - override fun windowClosing(e: WindowEvent?) { - if (disposed.compareAndSet(false, true)) { - doCancelAction() - } - } - }) - - Disposer.register(myDisposable, object : Disposable { - override fun dispose() { - if (disposed.compareAndSet(false, true)) { - doCancelAction() - } - } - }) - - Disposer.register(disposable, object : Disposable { - override fun dispose() { - if (disposed.compareAndSet(false, true)) { - Disposer.dispose(myDisposable) - } - } - }) - } - - override fun doCancelAction() { - if (editorPanel.changes()) { - if (OptionPane.showConfirmDialog( - this, - "文件尚未保存,你确定要退出吗?", - optionType = JOptionPane.OK_CANCEL_OPTION, - ) != JOptionPane.OK_OPTION - ) { - return - } - } - super.doCancelAction() - } - - override fun createCenterPanel(): JComponent { - return editorPanel - } - - override fun createSouthPanel(): JComponent? { - return null - } -} \ No newline at end of file diff --git a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorFrame.kt b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorFrame.kt new file mode 100644 index 0000000..3ebddf6 --- /dev/null +++ b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorFrame.kt @@ -0,0 +1,91 @@ +package app.termora.plugins.editor + +import app.termora.Disposable +import app.termora.Disposer +import app.termora.EnableManager +import app.termora.OptionPane +import java.awt.BorderLayout +import java.awt.Dimension +import java.awt.Window +import java.awt.event.WindowAdapter +import java.awt.event.WindowEvent +import java.io.File +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicBoolean +import javax.swing.JFrame +import javax.swing.JOptionPane +import javax.swing.UIManager +import kotlin.io.path.absolutePathString +import kotlin.io.path.name +import kotlin.math.max + +class EditorFrame(private val file: Path, private val owner: Window, private val disposable: Disposable) : JFrame() { + private val enableManager get() = EnableManager.getInstance() + private val disposed = AtomicBoolean() + private val filepath = File(file.absolutePathString()) + private val frame get() = this + private val editorPanel = EditorPanel(this, filepath) + + init { + initView() + initEvent() + } + + private fun initEvent() { + + Disposer.register(disposable, object : Disposable { + override fun dispose() { + if (disposed.compareAndSet(false, true)) frame.dispose() + } + }) + + addWindowListener(object : WindowAdapter() { + override fun windowClosed(e: WindowEvent) { + if (disposed.compareAndSet(false, true)) Disposer.dispose(disposable) + enableManager.setFlag("Plugins.editor.dialog.width", width) + enableManager.setFlag("Plugins.editor.dialog.height", height) + enableManager.setFlag("Plugins.editor.dialog.extendedState", extendedState) + } + + override fun windowClosing(e: WindowEvent?) { + if (editorPanel.changes()) { + if (OptionPane.showConfirmDialog( + frame, + EditorI18n.getString("termora.plugins.editor.not-save"), + optionType = JOptionPane.OK_CANCEL_OPTION, + ) == JOptionPane.OK_OPTION + ) { + frame.dispose() + } + } else { + frame.dispose() + } + } + }) + + } + + private fun initView() { + size = Dimension(UIManager.getInt("Dialog.width"), UIManager.getInt("Dialog.height")) + val state = enableManager.getFlag("Plugins.editor.dialog.extendedState", 0) + + if ((state and MAXIMIZED_BOTH) == MAXIMIZED_BOTH) { + frame.setLocationRelativeTo(null) + frame.extendedState = state + } else { + val mySize = size + mySize.width = max(enableManager.getFlag("Plugins.editor.dialog.width", mySize.width), mySize.width) + mySize.height = max(enableManager.getFlag("Plugins.editor.dialog.height", mySize.height), mySize.height) + size = mySize + setLocationRelativeTo(owner) + } + + title = file.name + iconImages = owner.iconImages + defaultCloseOperation = DO_NOTHING_ON_CLOSE + + rootPane.contentPane.layout = BorderLayout() + rootPane.contentPane.add(editorPanel, BorderLayout.CENTER) + + } +} \ No newline at end of file diff --git a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorI18n.kt b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorI18n.kt new file mode 100644 index 0000000..5aba976 --- /dev/null +++ b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorI18n.kt @@ -0,0 +1,13 @@ +package app.termora.plugins.editor + +import app.termora.NamedI18n +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +object EditorI18n : NamedI18n("i18n/messages") { + private val log = LoggerFactory.getLogger(EditorI18n::class.java) + + override fun getLogger(): Logger { + return log + } +} \ No newline at end of file diff --git a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorPanel.kt b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorPanel.kt index 647dcc2..cda8b00 100644 --- a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorPanel.kt +++ b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/EditorPanel.kt @@ -35,7 +35,7 @@ import javax.swing.event.DocumentEvent import kotlin.math.max import kotlin.math.min -class EditorPanel(private val window: JDialog, private val file: File) : JPanel(BorderLayout()) { +class EditorPanel(private val window: JFrame, private val file: File) : JPanel(BorderLayout()) { companion object { private val log = LoggerFactory.getLogger(EditorPanel::class.java) diff --git a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/MyTransportEditFileExtension.kt b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/MyTransportEditFileExtension.kt index 4de7fef..b8369a7 100644 --- a/plugins/editor/src/main/kotlin/app/termora/plugins/editor/MyTransportEditFileExtension.kt +++ b/plugins/editor/src/main/kotlin/app/termora/plugins/editor/MyTransportEditFileExtension.kt @@ -14,7 +14,7 @@ class MyTransportEditFileExtension private constructor() : TransportEditFileExte override fun edit(owner: Window, path: Path): Disposable { val disposable = Disposer.newDisposable() - SwingUtilities.invokeLater { EditorDialog(path, owner, disposable).isVisible = true } + SwingUtilities.invokeLater { EditorFrame(path, owner, disposable).isVisible = true } return disposable } } \ No newline at end of file diff --git a/plugins/editor/src/main/resources/i18n/messages.properties b/plugins/editor/src/main/resources/i18n/messages.properties new file mode 100644 index 0000000..283e6ff --- /dev/null +++ b/plugins/editor/src/main/resources/i18n/messages.properties @@ -0,0 +1,2 @@ +termora.plugins.editor.not-save=The file has not been saved. Are you sure you want to exit? + diff --git a/plugins/editor/src/main/resources/i18n/messages_ru_RU.properties b/plugins/editor/src/main/resources/i18n/messages_ru_RU.properties new file mode 100644 index 0000000..ca83b53 --- /dev/null +++ b/plugins/editor/src/main/resources/i18n/messages_ru_RU.properties @@ -0,0 +1 @@ +termora.plugins.editor.not-save=Файл не сохранён. Вы уверены, что хотите выйти? \ No newline at end of file diff --git a/plugins/editor/src/main/resources/i18n/messages_zh_CN.properties b/plugins/editor/src/main/resources/i18n/messages_zh_CN.properties new file mode 100644 index 0000000..35765e4 --- /dev/null +++ b/plugins/editor/src/main/resources/i18n/messages_zh_CN.properties @@ -0,0 +1 @@ +termora.plugins.editor.not-save=文件尚未保存,你确定要退出吗? diff --git a/plugins/editor/src/main/resources/i18n/messages_zh_TW.properties b/plugins/editor/src/main/resources/i18n/messages_zh_TW.properties new file mode 100644 index 0000000..2f53881 --- /dev/null +++ b/plugins/editor/src/main/resources/i18n/messages_zh_TW.properties @@ -0,0 +1 @@ +termora.plugins.editor.not-save=檔案尚未儲存,你確定要退出嗎? \ No newline at end of file diff --git a/src/main/kotlin/app/termora/plugin/internal/updater/MyApplicationRunnerExtension.kt b/src/main/kotlin/app/termora/plugin/internal/updater/MyApplicationRunnerExtension.kt index 29102db..0453e2c 100644 --- a/src/main/kotlin/app/termora/plugin/internal/updater/MyApplicationRunnerExtension.kt +++ b/src/main/kotlin/app/termora/plugin/internal/updater/MyApplicationRunnerExtension.kt @@ -1,8 +1,11 @@ package app.termora.plugin.internal.updater import app.termora.* +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import kotlinx.coroutines.swing.Swing +import kotlinx.coroutines.withContext import org.semver4j.Semver import org.slf4j.LoggerFactory import java.awt.KeyboardFocusManager @@ -20,7 +23,7 @@ internal class MyApplicationRunnerExtension private constructor() : ApplicationR override fun ready() { - swingCoroutineScope.launch { + swingCoroutineScope.launch(Dispatchers.IO) { try { delay(3.seconds) scheduleUpdate() @@ -31,7 +34,7 @@ internal class MyApplicationRunnerExtension private constructor() : ApplicationR } - private fun scheduleUpdate() { + private suspend fun scheduleUpdate() { if (disabledUpdater) return val latestVersion = updaterManager.fetchLatestVersion() @@ -45,13 +48,15 @@ internal class MyApplicationRunnerExtension private constructor() : ApplicationR return } - val owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().focusedWindow - ?: TermoraFrameManager.getInstance().getWindows().firstOrNull() - if (owner == null) return - - val dialog = UpdaterDialog(owner, latestVersion) - dialog.isModal = true - dialog.isVisible = true + withContext(Dispatchers.Swing) { + val owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().focusedWindow + ?: TermoraFrameManager.getInstance().getWindows().firstOrNull() + if (owner != null) { + val dialog = UpdaterDialog(owner, latestVersion) + dialog.isModal = true + dialog.isVisible = true + } + } } } \ No newline at end of file