Skip to content

Commit

Permalink
feat: supports remembering window positions
Browse files Browse the repository at this point in the history
  • Loading branch information
hstyi committed Feb 24, 2025
1 parent 09a1d9f commit f3c5009
Showing 1 changed file with 51 additions and 6 deletions.
57 changes: 51 additions & 6 deletions src/main/kotlin/app/termora/TermoraFrameManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package app.termora

import com.formdev.flatlaf.util.SystemInfo
import org.slf4j.LoggerFactory
import java.awt.Frame
import java.awt.event.WindowAdapter
import java.awt.event.WindowEvent
import javax.swing.JOptionPane
import javax.swing.UIManager
import javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE
import kotlin.math.max
import kotlin.system.exitProcess

class TermoraFrameManager {
Expand All @@ -20,16 +23,32 @@ class TermoraFrameManager {
}

private val frames = mutableListOf<TermoraFrame>()
private val properties get() = Database.getDatabase().properties

fun createWindow(): TermoraFrame {
val frame = TermoraFrame()
registerCloseCallback(frame)
val frame = TermoraFrame().apply { registerCloseCallback(this) }
frame.title = if (SystemInfo.isLinux) null else Application.getName()
frame.defaultCloseOperation = DO_NOTHING_ON_CLOSE
frame.setSize(1280, 800)
frame.setLocationRelativeTo(null)
frames.add(frame)
return frame

val rectangle = getFrameRectangle() ?: FrameRectangle(-1, -1, 1280, 800, 0)
if (rectangle.isMaximized) {
frame.setSize(1280, 800)
frame.setLocationRelativeTo(null)
frame.extendedState = rectangle.s
} else {
// 控制最小
frame.setSize(
max(rectangle.w, UIManager.getInt("Dialog.width") - 150),
max(rectangle.h, UIManager.getInt("Dialog.height") - 100)
)
if (rectangle.x == -1 && rectangle.y == -1) {
frame.setLocationRelativeTo(null)
} else {
frame.setLocation(max(rectangle.x, 0), max(rectangle.y, 0))
}
}

return frame.apply { frames.add(this) }
}

fun getWindows(): Array<TermoraFrame> {
Expand All @@ -41,6 +60,9 @@ class TermoraFrameManager {
window.addWindowListener(object : WindowAdapter() {
override fun windowClosed(e: WindowEvent) {

// 存储位置信息
saveFrameRectangle(window)

// 删除
frames.remove(window)

Expand Down Expand Up @@ -87,4 +109,27 @@ class TermoraFrameManager {

exitProcess(0)
}

private fun saveFrameRectangle(frame: TermoraFrame) {
properties.putString("TermoraFrame.x", frame.x.toString())
properties.putString("TermoraFrame.y", frame.y.toString())
properties.putString("TermoraFrame.width", frame.width.toString())
properties.putString("TermoraFrame.height", frame.height.toString())
properties.putString("TermoraFrame.extendedState", frame.extendedState.toString())
}

private fun getFrameRectangle(): FrameRectangle? {
val x = properties.getString("TermoraFrame.x")?.toIntOrNull() ?: return null
val y = properties.getString("TermoraFrame.y")?.toIntOrNull() ?: return null
val w = properties.getString("TermoraFrame.width")?.toIntOrNull() ?: return null
val h = properties.getString("TermoraFrame.height")?.toIntOrNull() ?: return null
val s = properties.getString("TermoraFrame.extendedState")?.toIntOrNull() ?: return null
return FrameRectangle(x, y, w, h, s)
}

private data class FrameRectangle(
val x: Int, val y: Int, val w: Int, val h: Int, val s: Int
) {
val isMaximized get() = (s and Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH
}
}

0 comments on commit f3c5009

Please sign in to comment.