Skip to content

Commit 3a08d5d

Browse files
committed
chore: improve window geo restore
- show window on screen where the cursor is located - default window size if saved geometry is not valid
1 parent 5c2764f commit 3a08d5d

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/mainwindow.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ MainWindow::MainWindow(QWidget *parent)
2323
setWindowIcon(QIcon(":/icons/app/icon-128.png"));
2424
setMinimumWidth(525);
2525
setMinimumHeight(448);
26-
restoreGeometry(settings.value("geometry").toByteArray());
26+
restoreMainWindow();
2727
initThemes();
2828
createActions();
2929
createTrayIcon();
@@ -35,6 +35,21 @@ MainWindow::MainWindow(QWidget *parent)
3535
initAutoLock();
3636
}
3737

38+
void MainWindow::restoreMainWindow() {
39+
if (settings.value("geometry").isValid()) {
40+
restoreGeometry(settings.value("geometry").toByteArray());
41+
QPoint pos = QCursor::pos();
42+
for (QScreen *screen : QGuiApplication::screens()) {
43+
QRect screenRect = screen->geometry();
44+
if (screenRect.contains(pos)) {
45+
this->move(screenRect.center() - this->rect().center());
46+
}
47+
}
48+
}else{
49+
this->resize(636, 760);
50+
}
51+
}
52+
3853
void MainWindow::initAutoLock() {
3954
autoLockEventFilter = new AutoLockEventFilter(
4055
settings.value("autoLockDuration", defaultAppAutoLockDuration).toInt() *
@@ -252,9 +267,10 @@ void MainWindow::tryLogOut() {
252267
}
253268

254269
void MainWindow::init_settingWidget() {
270+
int screenNumber = qApp->desktop()->screenNumber(this);
255271
if (settingsWidget == nullptr) {
256272
settingsWidget = new SettingsWidget(
257-
this, webEngine->page()->profile()->cachePath(),
273+
this, screenNumber, webEngine->page()->profile()->cachePath(),
258274
webEngine->page()->profile()->persistentStoragePath());
259275
settingsWidget->setWindowTitle(QApplication::applicationName() +
260276
" | Settings");
@@ -414,12 +430,18 @@ void MainWindow::showSettings() {
414430
if (webEngine == nullptr) {
415431
QMessageBox::critical(
416432
this, QApplication::applicationName() + "| Error",
417-
"Unable to initialize settings module.\nIs webengine initialized?");
433+
"Unable to initialize settings module.\nWebengine is not initialized.");
418434
return;
419435
}
420436
if (!settingsWidget->isVisible()) {
421437
this->updateSettingsUserAgentWidget();
422438
settingsWidget->refresh();
439+
int screenNumber = qApp->desktop()->screenNumber(this);
440+
QRect screenRect = QGuiApplication::screens().at(screenNumber)->geometry();
441+
if (!screenRect.contains(settingsWidget->pos())) {
442+
settingsWidget->move(screenRect.center() -
443+
settingsWidget->rect().center());
444+
}
423445
settingsWidget->show();
424446
}
425447
}

src/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private slots:
128128
void appAutoLockChanged();
129129
void injectNewChatJavaScript();
130130
void triggerNewChat(QString phone, QString text);
131+
void restoreMainWindow();
131132
};
132133

133134
#endif // MAINWINDOW_H

0 commit comments

Comments
 (0)