Skip to content

Commit

Permalink
mainwindow: set a sensible default size
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davvid committed Mar 2, 2024
1 parent 307ab43 commit 528a544
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "mainwindow.h"

#include <QApplication>
#include <QCloseEvent>
#include <QEventLoop>
#include <QLayout>
#include <QScreen>
#include <QSettings>
#include <QStyleFactory>
#include <QToolBar>
Expand All @@ -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 }
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 528a544

Please sign in to comment.