Skip to content

Commit

Permalink
Qt: Optionally remember window pos/size
Browse files Browse the repository at this point in the history
  • Loading branch information
wheremyfoodat committed Oct 31, 2024
1 parent 7ae8412 commit 818271c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
23 changes: 20 additions & 3 deletions include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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();
Expand Down
24 changes: 22 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
Expand Down
29 changes: 24 additions & 5 deletions src/panda_qt/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (windowSettings.showAppVersion) {
setWindowTitle("Alber v" PANDA3DS_VERSION);
}

if (windowSettings.rememberPosition) {
setGeometry(windowSettings.x, windowSettings.y, windowSettings.width, config.windowSettings.height);
}

if (emu->getConfig().printAppVersion) {
printf("Welcome to Panda3DS v%s!\n", PANDA3DS_VERSION);
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 818271c

Please sign in to comment.