Fix Windows tray show foregrounding

This commit is contained in:
Курнат Андрей
2026-06-10 08:26:50 +03:00
parent 439cf82893
commit dfe31ac68b
2 changed files with 11 additions and 3 deletions
+7 -1
View File
@@ -53,6 +53,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QMenuBar> #include <QMenuBar>
#include <QScreen> #include <QScreen>
#include <QWindow>
using namespace Qt::Literals::StringLiterals; using namespace Qt::Literals::StringLiterals;
using namespace OCC; using namespace OCC;
@@ -283,6 +284,9 @@ void Application::showSettings()
ensureWindowIntersectsAvailableScreen(window); ensureWindowIntersectsAvailableScreen(window);
window->raise(); window->raise();
window->activateWindow(); window->activateWindow();
if (auto *windowHandle = window->windowHandle()) {
windowHandle->requestActivate();
}
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
// Windows disallows raising a Window when you're not the active application. // Windows disallows raising a Window when you're not the active application.
@@ -294,7 +298,9 @@ void Application::showSettings()
foregroundThreadId != 0 && foregroundThreadId != currentThreadId && AttachThreadInput(currentThreadId, foregroundThreadId, true); foregroundThreadId != 0 && foregroundThreadId != currentThreadId && AttachThreadInput(currentThreadId, foregroundThreadId, true);
const auto hwnd = reinterpret_cast<HWND>(window->winId()); const auto hwnd = reinterpret_cast<HWND>(window->winId());
ShowWindow(hwnd, SW_RESTORE); ShowWindow(hwnd, IsIconic(hwnd) ? SW_RESTORE : SW_SHOW);
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
SetForegroundWindow(hwnd); SetForegroundWindow(hwnd);
SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW); SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
+4 -2
View File
@@ -24,17 +24,19 @@
#include <QApplication> #include <QApplication>
#include <QDesktopServices> #include <QDesktopServices>
#include <QMenu> #include <QMenu>
#include <QMetaObject> #include <QTimer>
using namespace Qt::Literals::StringLiterals; using namespace Qt::Literals::StringLiterals;
namespace OCC { namespace OCC {
namespace { namespace {
constexpr auto showSettingsAfterTrayEventDelay = 100;
void showSettingsAfterTrayEvent() void showSettingsAfterTrayEvent()
{ {
auto *app = ocApp(); auto *app = ocApp();
QMetaObject::invokeMethod(app, [app] { app->showSettings(); }, Qt::QueuedConnection); QTimer::singleShot(showSettingsAfterTrayEventDelay, app, [app] { app->showSettings(); });
} }
} }