From 528a544ba4d4898aef3a6d205ec27c59caaf7e4e Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Sun, 11 Feb 2024 02:05:20 -0800 Subject: [PATCH] mainwindow: set a sensible default size When `~/.config/nvim-qt/window-geometry.conf` does not exist then restoreGeometry() will initialize a window that is tiny and must be resized to see anything. Use a sensible default size on the initial load and when we have disabled restoring window geometry. Avoid restoring state when it is invalid. --- src/gui/mainwindow.cpp | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 925feb336..c1880c0c7 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 @@ -14,6 +16,19 @@ static QString DefaultWindowTitle() noexcept return "Neovim"; } +static void SetDefaultWindowSize(QWidget *widget) +{ + QPoint widget_position = widget->mapToGlobal(QPoint{ widget->width() / 2, 0 }); + QScreen *screen = qApp->screenAt(widget_position); + QRect geometry{ screen->availableGeometry() }; + if (screen->orientation() == Qt::LandscapeOrientation) { + widget->resize(geometry.width() / 2, geometry.height()); + } + else { + widget->resize(geometry.width(), geometry.height() / 2); + } +} + MainWindow::MainWindow(NeovimConnector* c, QWidget* parent) noexcept : QMainWindow{ parent } , m_tabline{ *c, this } @@ -331,10 +346,20 @@ void MainWindow::restoreWindowGeometry() QSettings settings("nvim-qt", "window-geometry"); if (!settings.value("restore_window_geometry", true).toBool()) { + SetDefaultWindowSize(this); 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 { + SetDefaultWindowSize(this); + } + QVariant state{ settings.value("window_state") }; + if (state.isValid()) { + restoreState(state.toByteArray()); + } } void MainWindow::setGuiAdaptiveColorEnabled(bool isEnabled)