From 6e95e6d09b6cbf259ca4c85ff5ef85c0dc148d7f Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Wed, 6 Mar 2024 21:18:32 -0800 Subject: [PATCH] Set a default size on first launch When `~/.config/nvim-qt/window-geometry.conf` does not exist then restoreGeometry() will initialize a window that is tiny and must be resized in order to be usable in some environments Resize the window to half the screen when launched for the first time. Do not restore window state when it is invalid or missing. --- src/gui/mainwindow.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 5e62e6171..5fbb2113b 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1,8 +1,10 @@ #include "mainwindow.h" +#include #include #include #include +#include #include #include #include @@ -333,8 +335,28 @@ void MainWindow::restoreWindowGeometry() if (!settings.value("restore_window_geometry", true).toBool()) { return; } - restoreGeometry(settings.value("window_geometry").toByteArray()); - restoreState(settings.value("window_state").toByteArray()); + QVariant geometry{ settings.value("window_geometry") }; + if (geometry.isValid()) { + restoreGeometry(geometry.toByteArray()); + } + else { + const QPoint local_position{ width() / 2, 0 }; + const QPoint global_position = mapToGlobal(local_position); + const QScreen* screen{ qApp->screenAt(global_position) }; + if (screen) { + const QRect geometry{ screen->availableGeometry() }; + if (screen->orientation() == Qt::LandscapeOrientation) { + resize(geometry.width() / 2, geometry.height()); + } + else { + resize(geometry.width(), geometry.height() / 2); + } + } + } + QVariant state{ settings.value("window_state") }; + if (state.isValid()) { + restoreState(state.toByteArray()); + } } void MainWindow::setGuiAdaptiveColorEnabled(bool isEnabled)