From 818271c7adacb02f1897353874e729f2bb53873c Mon Sep 17 00:00:00 2001
From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com>
Date: Thu, 31 Oct 2024 17:30:30 +0200
Subject: [PATCH 1/2] Qt: Optionally remember window pos/size

---
 include/config.hpp           | 23 ++++++++++++++++++++---
 src/config.cpp               | 24 ++++++++++++++++++++++--
 src/panda_qt/main_window.cpp | 29 ++++++++++++++++++++++++-----
 3 files changed, 66 insertions(+), 10 deletions(-)

diff --git a/include/config.hpp b/include/config.hpp
index 0cffbf93..81a03385 100644
--- a/include/config.hpp
+++ b/include/config.hpp
@@ -21,7 +21,7 @@ struct EmulatorConfig {
 	static constexpr bool ubershaderDefault = true;
 #endif
 	static constexpr bool accelerateShadersDefault = true;
-	
+
 	bool shaderJitEnabled = shaderJitDefault;
 	bool useUbershaders = ubershaderDefault;
 	bool accelerateShaders = accelerateShadersDefault;
@@ -41,10 +41,9 @@ struct EmulatorConfig {
 
 	bool audioEnabled = false;
 	bool vsyncEnabled = true;
-	
+
 	bool enableRenderdoc = false;
 	bool printAppVersion = true;
-	bool appVersionOnWindow = false;
 
 	bool chargerPlugged = true;
 	// Default to 3% battery to make users suffer
@@ -54,6 +53,24 @@ struct EmulatorConfig {
 	std::filesystem::path defaultRomPath = "";
 	std::filesystem::path filePath;
 
+	// Frontend window settings
+	struct WindowSettings {
+		static constexpr int defaultX = 200;
+		static constexpr int defaultY = 200;
+		static constexpr int defaultWidth = 800;
+		static constexpr int defaultHeight = 240 * 2;
+
+		bool rememberPosition = false;  // Remember window position & size
+		bool showAppVersion = false;
+
+		int x = defaultX;
+		int y = defaultY;
+		int width = defaultHeight;
+		int height = defaultHeight;
+	};
+
+	WindowSettings windowSettings;
+
 	EmulatorConfig(const std::filesystem::path& path);
 	void load();
 	void save();
diff --git a/src/config.cpp b/src/config.cpp
index 25fded6c..4cd18858 100644
--- a/src/config.cpp
+++ b/src/config.cpp
@@ -43,7 +43,21 @@ void EmulatorConfig::load() {
 			defaultRomPath = toml::find_or<std::string>(general, "DefaultRomPath", "");
 
 			printAppVersion = toml::find_or<toml::boolean>(general, "PrintAppVersion", true);
-			appVersionOnWindow = toml::find_or<toml::boolean>(general, "AppVersionOnWindow", false);
+		}
+	}
+
+	if (data.contains("Window")) {
+		auto windowResult = toml::expect<toml::value>(data.at("Window"));
+		if (windowResult.is_ok()) {
+			auto window = windowResult.unwrap();
+
+			windowSettings.showAppVersion = toml::find_or<toml::boolean>(window, "AppVersionOnWindow", false);
+			windowSettings.rememberPosition = toml::find_or<toml::boolean>(window, "RememberWindowPosition", false);
+
+			windowSettings.x = toml::find_or<toml::integer>(window, "WindowPosX", WindowSettings::defaultX);
+			windowSettings.y = toml::find_or<toml::integer>(window, "WindowPosY", WindowSettings::defaultY);
+			windowSettings.width = toml::find_or<toml::integer>(window, "WindowWidth", WindowSettings::defaultWidth);
+			windowSettings.height = toml::find_or<toml::integer>(window, "WindowHeight", WindowSettings::defaultHeight);
 		}
 	}
 
@@ -133,7 +147,13 @@ void EmulatorConfig::save() {
 	data["General"]["UsePortableBuild"] = usePortableBuild;
 	data["General"]["DefaultRomPath"] = defaultRomPath.string();
 	data["General"]["PrintAppVersion"] = printAppVersion;
-	data["General"]["AppVersionOnWindow"] = appVersionOnWindow;
+
+	data["Window"]["AppVersionOnWindow"] = windowSettings.showAppVersion;
+	data["Window"]["RememberWindowPosition"] = windowSettings.rememberPosition;
+	data["Window"]["WindowPosX"] = windowSettings.x;
+	data["Window"]["WindowPosY"] = windowSettings.y;
+	data["Window"]["WindowWidth"] = windowSettings.width;
+	data["Window"]["WindowHeight"] = windowSettings.height;
 	
 	data["GPU"]["EnableShaderJIT"] = shaderJitEnabled;
 	data["GPU"]["Renderer"] = std::string(Renderer::typeToString(rendererType));
diff --git a/src/panda_qt/main_window.cpp b/src/panda_qt/main_window.cpp
index 45690da7..e99e3046 100644
--- a/src/panda_qt/main_window.cpp
+++ b/src/panda_qt/main_window.cpp
@@ -99,12 +99,22 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
 		}
 	}
 
-	if (emu->getConfig().appVersionOnWindow) {
-		setWindowTitle("Alber v" PANDA3DS_VERSION);
-	}
+	// Handle UI configs before setting up the emulator thread
+	{
+		auto& config = emu->getConfig();
+		auto& windowSettings = config.windowSettings;
 
-	if (emu->getConfig().printAppVersion) {
-		printf("Welcome to Panda3DS v%s!\n", PANDA3DS_VERSION);
+		if (windowSettings.showAppVersion) {
+			setWindowTitle("Alber v" PANDA3DS_VERSION);
+		}
+
+		if (windowSettings.rememberPosition) {
+			setGeometry(windowSettings.x, windowSettings.y, windowSettings.width, config.windowSettings.height);
+		}
+
+		if (config.printAppVersion) {
+			printf("Welcome to Panda3DS v%s!\n", PANDA3DS_VERSION);
+		}
 	}
 
 	// The emulator graphics context for the thread should be initialized in the emulator thread due to how GL contexts work
@@ -221,6 +231,15 @@ void MainWindow::closeEvent(QCloseEvent *event) {
 	if (emuThread.joinable()) {
 		emuThread.join();
 	}
+
+	// Cache window position/size in config file to restore next time
+	const QRect& windowGeometry = geometry();
+	auto& windowConfig = emu->getConfig().windowSettings;
+
+	windowConfig.x = windowGeometry.x();
+	windowConfig.y = windowGeometry.y();
+	windowConfig.width = windowGeometry.width();
+	windowConfig.height = windowGeometry.height();
 }
 
 // Cleanup when the main window closes

From 08aedfde6361cac8c011fc98de55b993847d4897 Mon Sep 17 00:00:00 2001
From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com>
Date: Thu, 31 Oct 2024 17:56:30 +0200
Subject: [PATCH 2/2] Fix SDL build

---
 src/panda_sdl/frontend_sdl.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/panda_sdl/frontend_sdl.cpp b/src/panda_sdl/frontend_sdl.cpp
index de496d56..405e08da 100644
--- a/src/panda_sdl/frontend_sdl.cpp
+++ b/src/panda_sdl/frontend_sdl.cpp
@@ -35,7 +35,7 @@ FrontendSDL::FrontendSDL() : keyboardMappings(InputMappings::defaultKeyboardMapp
 	needOpenGL = needOpenGL || (config.rendererType == RendererType::OpenGL);
 #endif
 
-	const char* windowTitle = config.appVersionOnWindow ? ("Alber v" PANDA3DS_VERSION) : "Alber";
+	const char* windowTitle = config.windowSettings.showAppVersion ? ("Alber v" PANDA3DS_VERSION) : "Alber";
 	if (config.printAppVersion) {
 		printf("Welcome to Panda3DS v%s!\n", PANDA3DS_VERSION);
 	}
@@ -393,4 +393,4 @@ void FrontendSDL::setupControllerSensors(SDL_GameController* controller) {
 	if (haveAccelerometer) {
 		SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
 	}
-}
\ No newline at end of file
+}