From 4393c59fb3a362e86030c3bf789bcb002f539bf1 Mon Sep 17 00:00:00 2001 From: Cl0udS3c <50030246+CloudS3c@users.noreply.github.com> Date: Mon, 23 Dec 2024 03:47:00 +0100 Subject: [PATCH] feat: adds validation if frame is within any screens bounds (#42) * chore: Added validation if frame is within any screens bounds. * refactor: improve documentation and code style --------- Co-authored-by: Kris --- .../src/main/kotlin/net/rsprox/gui/App.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/gui/proxy-tool/src/main/kotlin/net/rsprox/gui/App.kt b/gui/proxy-tool/src/main/kotlin/net/rsprox/gui/App.kt index b17e6a03..5bab5c74 100644 --- a/gui/proxy-tool/src/main/kotlin/net/rsprox/gui/App.kt +++ b/gui/proxy-tool/src/main/kotlin/net/rsprox/gui/App.kt @@ -13,6 +13,7 @@ import net.rsprox.proxy.ProxyService import net.rsprox.proxy.config.BINARY_PATH import java.awt.Desktop import java.awt.Dimension +import java.awt.GraphicsEnvironment import java.awt.event.ComponentAdapter import java.awt.event.ComponentEvent import java.awt.event.WindowAdapter @@ -56,6 +57,9 @@ public class App { frame.size = defaultSize frame.minimumSize = UIScale.scale(Dimension(800, 600)) frame.iconImages = FlatSVGUtils.createWindowIconImages("/favicon.svg") + if (!validFramePosition(frame)) { + frame.setLocationRelativeTo(null) + } val windowHandler = object : WindowAdapter() { override fun windowClosing(e: WindowEvent) { @@ -220,6 +224,17 @@ public class App { add(sessionsPanel) } + /** + * Checks if the frame intersects with any screen device. If the function returns false, the window + * is completely outside the device's visible area, meaning the user would not be able to interact + * with the window. + */ + private fun validFramePosition(frame: JFrame): Boolean { + return GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices.any { device -> + device.defaultConfiguration.bounds.intersects(frame.bounds) + } + } + public companion object { public val service: ProxyService = ProxyService(ByteBufAllocator.DEFAULT) }