Skip to content

Commit

Permalink
src/mainwindow.cpp: don't draw on screen until ready
Browse files Browse the repository at this point in the history
The OS signal handler can't be installed until after the Qt event loop
starts since it eventually calls `QApplication::exit(0);`, and that will
cleanup anything we draw on the screen. Thus we don't want to connect
any slots that touch the what's in focus or draw on the screen until the
OS signal handler is ready. `mainwindow->init()` is called in
`src/main.cpp` before `app.exec()`, thus we don't want to connect any
slots that draw on screen in `mainwindow->init()`. Instead,
`mainwindow->init()` will connect the `setupWindow()` slot, to the the
`signalHandlerInstalled` Qt signal emitted by `this->handler`. At that
point the OS signal handler has been installed and it is safe to draw on
the screen.
  • Loading branch information
HED-jzignego committed Mar 2, 2022
1 parent dfae33f commit bdf6813
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
50 changes: 32 additions & 18 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void MainWindow::init(AnyOption *opts)
setMinimumHeight(minimalHeight);
}

hiddenCurdor = new QCursor(Qt::BlankCursor);
hiddenCursor = new QCursor(Qt::BlankCursor);

qDebug() << "Application icon: " << qwkSettings->getQString("application/icon");
setWindowIcon(QIcon(
Expand Down Expand Up @@ -302,6 +302,29 @@ void MainWindow::init(AnyOption *opts)
qwkSettings->getBool("security/local_content_can_access_remote_urls")
);

QNetworkConfigurationManager manager;
QNetworkConfiguration cfg = manager.defaultConfiguration();

n_session = new QNetworkSession(cfg);
connect(n_session, SIGNAL(stateChanged(QNetworkSession::State)), this, SLOT(networkStateChanged(QNetworkSession::State)));
n_session->open();

connect(this->handler, SIGNAL(signalHandlerInstalled()), this, SLOT(setupWindow()));
}

/* The OS signal handler can't be installed until after the Qt event loop
* starts since it eventually calls `QApplication::exit(0);`, and that will
* cleanup anything we draw on the screen. Thus we don't want to connect any
* slots that touch the what's in focus or draw on the screen until the OS
* signal handler is ready. `this->init()` is called in `src/main.cpp` before
* `app.exec()`, thus we don't do any of this in `this->init()`. Instead,
* `this->init()` will connect this slot, `this->setupWindow()`, to the the
* `signalHandlerInstalled` Qt signal emitted by `this->handler`. At that point
* the OS signal handler has been installed and it is safe to draw on the
* screen.
*/
void MainWindow::setupWindow()
{
connect(view->page()->mainFrame(), SIGNAL(titleChanged(QString)), SLOT(adjustTitle(QString)));
connect(view->page()->mainFrame(), SIGNAL(loadStarted()), SLOT(startLoading()));
connect(view->page()->mainFrame(), SIGNAL(urlChanged(const QUrl &)), SLOT(urlChanged(const QUrl &)));
Expand All @@ -311,34 +334,21 @@ void MainWindow::init(AnyOption *opts)
connect(view, SIGNAL(qwkNetworkError(QNetworkReply::NetworkError,QString)), SLOT(handleQwkNetworkError(QNetworkReply::NetworkError,QString)));
connect(view, SIGNAL(qwkNetworkReplyUrl(QUrl)), SLOT(handleQwkNetworkReplyUrl(QUrl)));

QNetworkConfigurationManager manager;
QNetworkConfiguration cfg = manager.defaultConfiguration();

n_session = new QNetworkSession(cfg);
connect(n_session, SIGNAL(stateChanged(QNetworkSession::State)), this, SLOT(networkStateChanged(QNetworkSession::State)));
n_session->open();

QDesktopWidget *desktop = QApplication::desktop();
connect(desktop, SIGNAL(resized(int)), SLOT(desktopResized(int)));

// Window show, start events loop
show();

view->setFocusPolicy(Qt::StrongFocus);

if (qwkSettings->getBool("view/hide_mouse_cursor")) {
QApplication::setOverrideCursor(Qt::BlankCursor);
view->setCursor(*hiddenCurdor);
QApplication::processEvents(); //process events to force cursor update before press
view->setCursor(*hiddenCursor);
}

int delay_resize = 1;
int delay_resize = 0;
if (qwkSettings->getBool("view/startup_resize_delayed")) {
delay_resize = qwkSettings->getUInt("view/startup_resize_delay");
}
delayedResize->singleShot(delay_resize, this, SLOT(delayedWindowResize()));

int delay_load = 1;
int delay_load = 0;
if (qwkSettings->getBool("browser/startup_load_delayed")) {
delay_load = qwkSettings->getUInt("browser/startup_load_delay");
}
Expand All @@ -361,9 +371,11 @@ void MainWindow::delayedWindowResize()
showMaximized();
} else if (qwkSettings->getBool("view/fixed-size")) {
centerFixedSizeWindow();
} else {
show();
}
QApplication::processEvents(); //process events to force update

QApplication::processEvents(); //process events to force update
}

void MainWindow::resizeEvent(QResizeEvent* event)
Expand Down Expand Up @@ -629,6 +641,8 @@ void MainWindow::desktopResized(int p)
showMaximized();
} else if (qwkSettings->getBool("view/fixed-size")) {
centerFixedSizeWindow();
} else {
show();
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ protected slots:

void desktopResized(int p);

void setupWindow();

void delayedWindowResize();
void delayedPageLoad();
void delayedPageReload();
Expand All @@ -106,7 +108,6 @@ protected slots:
void unixSignalUsr2();

protected:

void centerFixedSizeWindow();
void attachJavascripts();
void attachStyles();
Expand All @@ -127,7 +128,7 @@ protected slots:
QNetworkDiskCache *diskCache;
QWebInspector *inspector;

QCursor *hiddenCurdor;
QCursor *hiddenCursor;
QKeyEvent *eventExit;

AnyOption *cmdopts;
Expand Down
2 changes: 2 additions & 0 deletions src/unixsignals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ void UnixSignals::start()
#else
qWarning("No signal USR2 defined");
#endif

emit signalHandlerInstalled();
}

void UnixSignals::stop()
Expand Down
1 change: 1 addition & 0 deletions src/unixsignals.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class UnixSignals : public QObject
static SocketPair sockPair;

Q_SIGNALS:
void signalHandlerInstalled();
void sigBREAK();
void sigTERM();
void sigHUP();
Expand Down

0 comments on commit bdf6813

Please sign in to comment.