Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions examples/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

#include <QWKWidgets/widgetwindowagent.h>

#ifdef Q_OS_WIN
# include <QtCore/qt_windows.h>
#endif

#include <widgetframe/windowbar.h>
#include <widgetframe/windowbutton.h>

Expand Down Expand Up @@ -128,6 +132,14 @@ bool MainWindow::event(QEvent *event) {
break;
}

#ifdef Q_OS_WINDOWS
case QEvent::WinIdChange:
if (!applyingStayOnTop) {
setStayOnTop(stayOnTop);
}
break;
#endif

default:
break;
}
Expand Down Expand Up @@ -432,11 +444,14 @@ void MainWindow::installWindowAgent() {
#ifndef Q_OS_MAC
connect(windowBar, &QWK::WindowBar::pinRequested, this, [this, pinButton](bool pin) {
if (isHidden() || isMinimized() || isMaximized() || isFullScreen()) {
pinButton->setChecked(stayOnTop);
return;
}
setWindowFlag(Qt::WindowStaysOnTopHint, pin);
show();
pinButton->setChecked(pin);
if (setStayOnTop(pin)) {
pinButton->setChecked(pin);
} else {
pinButton->setChecked(stayOnTop);
}
});
connect(windowBar, &QWK::WindowBar::minimizeRequested, this, &QWidget::showMinimized);
connect(windowBar, &QWK::WindowBar::maximizeRequested, this, [this, maxButton](bool max) {
Expand All @@ -455,6 +470,46 @@ void MainWindow::installWindowAgent() {
#endif
}

bool MainWindow::setStayOnTop(bool enabled) {
#ifdef Q_OS_WINDOWS
if (applyingStayOnTop) {
return false;
}

const auto hwnd = reinterpret_cast<HWND>(internalWinId());
if (!hwnd) {
return false;
}

applyingStayOnTop = true;
const bool success = ::SetWindowPos(hwnd, enabled ? HWND_TOPMOST : HWND_NOTOPMOST,
0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
SWP_NOOWNERZORDER) != FALSE;
applyingStayOnTop = false;
if (success) {
stayOnTop = enabled;
}
return success;
#else
if (windowFlags().testFlag(Qt::WindowStaysOnTopHint) == enabled) {
stayOnTop = enabled;
return true;
}

const bool visible = isVisible();
const QPoint frameTopLeft = frameGeometry().topLeft();
setWindowFlag(Qt::WindowStaysOnTopHint, enabled);
if (visible) {
show();
const QPoint clientOffset = geometry().topLeft() - frameGeometry().topLeft();
move(frameTopLeft + clientOffset);
}
stayOnTop = enabled;
return true;
#endif
}

void MainWindow::loadStyleSheet(Theme theme) {
if (!styleSheet().isEmpty() && theme == currentTheme)
return;
Expand Down
3 changes: 3 additions & 0 deletions examples/mainwindow/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ class MainWindow : public QMainWindow {
private:
void installWindowAgent();
void loadStyleSheet(Theme theme);
bool setStayOnTop(bool stayOnTop);

Theme currentTheme{};
bool stayOnTop{false};
bool applyingStayOnTop{false};

QWK::WidgetWindowAgent *windowAgent;
};
Expand Down