examples/mainwindow/dark-style.qss
@@ -104,7 +104,7 @@ QMenu { padding: 4px; background: #303030; border: 1.25px solid transparent; border: 1px solid transparent; } QMenu::indicator { examples/mainwindow/light-style.qss
@@ -103,8 +103,8 @@ QMenu { padding: 4px; background: #303030; border: 1.25px solid transparent; background: white; border: 1px solid #E0E0E0; } QMenu::indicator { @@ -119,23 +119,21 @@ QMenu::item { background: transparent; color: #CCCCCC; color: #333333; padding: 6px 24px; } QMenu::item:selected { color: white; background-color: #0060C0; background-color: rgba(0, 0, 0, 10%); } QMenu::item:disabled { color: #666666; background-color: transparent; color: #CCCCCC; } QMenu::separator { height: 2px; background-color: #5B5B5B; background-color: #CCCCCC; margin: 6px 0; } examples/mainwindow/mainwindow.cpp
@@ -131,19 +131,10 @@ darkAction->setChecked(currentTheme == Dark); // }); // Agent action auto framelessOnAction = new QAction(tr("Enable Frameless"), menuBar); framelessOnAction->setCheckable(true); framelessOnAction->setChecked(true); connect(framelessOnAction, &QAction::triggered, agent, &QWK::WindowAgentBase::setEnabled); connect(agent, &QWK::WindowAgentBase::enabledChanged, framelessOnAction, &QAction::setChecked); // Real menu auto settings = new QMenu(tr("Settings(&S)"), menuBar); settings->addAction(darkAction); settings->addSeparator(); settings->addAction(framelessOnAction); menuBar->addMenu(file); menuBar->addMenu(edit); src/core/contexts/abstractwindowcontext.cpp
@@ -18,7 +18,11 @@ } m_host = host; m_delegate.reset(delegate); setEnabled(true); m_windowHandle = m_delegate->window(m_host); if (m_windowHandle) { winIdChanged(); } } void AbstractWindowContext::setWindowAttribute(const QString &key, const QVariant &var) { @@ -152,6 +156,9 @@ void AbstractWindowContext::virtual_hook(int id, void *data) { switch (id) { case CentralizeHook: { if (!m_windowHandle) return; QRect screenGeometry = m_windowHandle->screen()->geometry(); int x = (screenGeometry.width() - m_windowHandle->width()) / 2; int y = (screenGeometry.height() - m_windowHandle->height()) / 2; @@ -190,39 +197,11 @@ } void AbstractWindowContext::notifyWinIdChange() { if (!m_internalEnabled) return; auto oldWindow = m_windowHandle; m_windowHandle = m_delegate->window(m_host); if (oldWindow == m_windowHandle) return; auto isDestroyed = oldWindow && m_windowHandleCache.isNull(); m_windowHandle = m_delegate->window(m_host); m_windowHandleCache = m_windowHandle; winIdChanged(oldWindow, isDestroyed); } void AbstractWindowContext::setEnabled(bool enabled) { if (enabled == m_internalEnabled) return; m_internalEnabled = enabled; if (enabled) { m_windowHandle = m_delegate->window(m_host); m_windowHandleCache = m_windowHandle; if (m_windowHandle) { winIdChanged(nullptr, false); } return; } if (!m_windowHandle) return; auto oldWindow = m_windowHandle; m_windowHandle = nullptr; m_windowHandleCache.clear(); winIdChanged(oldWindow, false); winIdChanged(); } } src/core/contexts/abstractwindowcontext_p.h
@@ -73,11 +73,8 @@ void showSystemMenu(const QPoint &pos); void notifyWinIdChange(); inline bool isEnabled() const; void setEnabled(bool enabled); protected: virtual void winIdChanged(QWindow *oldWindow, bool isDestroyed) = 0; virtual void winIdChanged() = 0; protected: QObject *m_host{}; @@ -93,10 +90,6 @@ std::array<QObject *, WindowAgentBase::NumSystemButton> m_systemButtons{}; QVariantHash m_windowAttributes; private: bool m_internalEnabled = false; QPointer<QWindow> m_windowHandleCache; }; inline QObject *AbstractWindowContext::host() const { @@ -133,10 +126,6 @@ return m_systemButtonArea; } #endif inline bool AbstractWindowContext::isEnabled() const { return m_internalEnabled; } } src/core/contexts/cocoawindowcontext.mm
@@ -403,7 +403,7 @@ AbstractWindowContext::virtual_hook(id, data); } void CocoaWindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) { void CocoaWindowContext::winIdChanged() { // If the original window id is valid, remove all resources related if (windowId) { releaseWindowProxy(windowId); src/core/contexts/cocoawindowcontext_p.h
@@ -24,7 +24,7 @@ void virtual_hook(int id, void *data) override; protected: void winIdChanged(QWindow *oldWindow, bool isDestroyed) override; void winIdChanged() override; protected: WId windowId = 0; src/core/contexts/qtwindowcontext.cpp
@@ -247,17 +247,11 @@ AbstractWindowContext::virtual_hook(id, data); } void QtWindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) { Q_UNUSED(isDestroyed) // If the original window id is valid, remove all resources related if (oldWindow) { qtWindowEventFilter.reset(); } void QtWindowContext::winIdChanged() { if (!m_windowHandle) { m_delegate->setWindowFlags(m_host, m_delegate->getWindowFlags(m_host) & ~Qt::FramelessWindowHint); qtWindowEventFilter.reset(); return; } src/core/contexts/qtwindowcontext_p.h
@@ -24,7 +24,7 @@ void virtual_hook(int id, void *data) override; protected: void winIdChanged(QWindow *oldWindow, bool isDestroyed) override; void winIdChanged() override; protected: std::unique_ptr<QObject> qtWindowEventFilter; src/core/contexts/win32windowcontext.cpp
@@ -379,16 +379,7 @@ getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi); } static void updateInternalWindowFrameMargins(HWND hwnd, QWindow *window) { const auto margins = [hwnd]() -> QMargins { const auto titleBarHeight = int(getTitleBarHeight(hwnd)); if (isWin10OrGreater()) { return {0, -titleBarHeight, 0, 0}; } else { const auto frameSize = int(getResizeBorderThickness(hwnd)); return {-frameSize, -titleBarHeight, -frameSize, -frameSize}; } }(); static void setInternalWindowFrameMargins(QWindow *window, const QMargins &margins) { const QVariant marginsVar = QVariant::fromValue(margins); window->setProperty("_q_windowsCustomMargins", marginsVar); #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) @@ -829,7 +820,20 @@ return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam); } static inline void addManagedWindow(HWND hWnd, Win32WindowContext *ctx) { static inline void addManagedWindow(QWindow *window, HWND hWnd, Win32WindowContext *ctx) { const auto margins = [hWnd]() -> QMargins { const auto titleBarHeight = int(getTitleBarHeight(hWnd)); if (isWin10OrGreater()) { return {0, -titleBarHeight, 0, 0}; } else { const auto frameSize = int(getResizeBorderThickness(hWnd)); return {-frameSize, -titleBarHeight, -frameSize, -frameSize}; } }(); // Inform Qt we want and have set custom margins setInternalWindowFrameMargins(window, margins); // Store original window proc if (!g_qtWindowProc) { g_qtWindowProc = reinterpret_cast<WNDPROC>(::GetWindowLongPtrW(hWnd, GWLP_WNDPROC)); @@ -845,7 +849,6 @@ g_wndProcHash->insert(hWnd, ctx); } template <bool Destroyed = true> static inline void removeManagedWindow(HWND hWnd) { // Remove window handle mapping if (!g_wndProcHash->remove(hWnd)) @@ -854,11 +857,6 @@ // Remove event filter if the all windows has been destroyed if (g_wndProcHash->empty()) { WindowsNativeEventFilter::uninstall(); } // Restore window proc if constexpr (!Destroyed) { ::SetWindowLongPtrW(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_qtWindowProc)); } } @@ -878,18 +876,24 @@ void Win32WindowContext::virtual_hook(int id, void *data) { switch (id) { case CentralizeHook: { if (!windowId) return; const auto hwnd = reinterpret_cast<HWND>(windowId); moveWindowToDesktopCenter(hwnd); return; } case RaiseWindowHook: { if (!windowId) return; const auto hwnd = reinterpret_cast<HWND>(windowId); bringWindowToFront(hwnd); return; } case ShowSystemMenuHook: { if (!windowId) return; const auto &pos = *static_cast<const QPoint *>(data); auto hWnd = reinterpret_cast<HWND>(windowId); #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) @@ -931,6 +935,9 @@ } case DrawWindows10BorderHook: { if (!windowId) return; auto args = static_cast<void **>(data); auto &painter = *static_cast<QPainter *>(args[0]); const auto &rect = *static_cast<const QRect *>(args[1]); @@ -984,16 +991,10 @@ return getWindowFrameBorderThickness(reinterpret_cast<HWND>(windowId)); } void Win32WindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) { Q_UNUSED(isDestroyed) void Win32WindowContext::winIdChanged() { // If the original window id is valid, remove all resources related if (windowId) { if (isDestroyed) { removeManagedWindow(reinterpret_cast<HWND>(windowId)); } else { removeManagedWindow<false>(reinterpret_cast<HWND>(windowId)); } removeManagedWindow(reinterpret_cast<HWND>(windowId)); windowId = 0; } @@ -1015,11 +1016,8 @@ } #endif // Inform Qt we want and have set custom margins updateInternalWindowFrameMargins(hWnd, m_windowHandle); // TODO: Restore? // Add managed window addManagedWindow(hWnd, this); addManagedWindow(m_windowHandle, hWnd, this); // Cache win id windowId = winId; src/core/contexts/win32windowcontext_p.h
@@ -39,7 +39,7 @@ int borderThickness() const; protected: void winIdChanged(QWindow *oldWindow, bool isDestroyed) override; void winIdChanged() override; public: bool windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result); src/core/windowagentbase.cpp
@@ -60,20 +60,6 @@ d->context->setWindowAttribute(key, var); } bool WindowAgentBase::isEnabled() const { Q_D(const WindowAgentBase); return d->context->isEnabled(); } void WindowAgentBase::setEnabled(bool enabled) { Q_D(WindowAgentBase); if (enabled == d->context->isEnabled()) { return; } d->context->setEnabled(enabled); Q_EMIT enabledChanged(enabled); } void WindowAgentBase::showSystemMenu(const QPoint &pos) { Q_D(WindowAgentBase); d->context->showSystemMenu(pos); src/core/windowagentbase.h
@@ -31,16 +31,10 @@ QVariant windowAttribute(const QString &key) const; void setWindowAttribute(const QString &key, const QVariant &var); bool isEnabled() const; void setEnabled(bool enabled); public Q_SLOTS: void showSystemMenu(const QPoint &pos); void centralize(); void raise(); Q_SIGNALS: void enabledChanged(bool enabled); protected: explicit WindowAgentBase(WindowAgentBasePrivate &d, QObject *parent = nullptr); src/widgets/widgetwindowagent.h
@@ -26,6 +26,7 @@ QWidget *systemButton(SystemButton button) const; void setSystemButton(SystemButton button, QWidget *w); // Not implement, don't use now #ifdef Q_OS_MAC QWidget *systemButtonArea() const; void setSystemButtonArea(QWidget *widget); src/widgets/widgetwindowagent_win.cpp
@@ -67,7 +67,8 @@ Q_UNUSED(obj) switch (event->type()) { case QEvent::Paint: { if (widget->windowState() & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)) if (widget->windowState() & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen)) break; auto paintEvent = static_cast<QPaintEvent *>(event);