chore: baseline usable version
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
project(QWKWidgets
|
||||
VERSION ${QWINDOWKIT_VERSION}
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
set(_src
|
||||
qwkwidgetsglobal.h
|
||||
widgetitemdelegate_p.h
|
||||
widgetitemdelegate.cpp
|
||||
widgetwindowagent.h
|
||||
widgetwindowagent_p.h
|
||||
widgetwindowagent.cpp
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
list(APPEND _src widgetwindowagent_win.cpp)
|
||||
elseif(APPLE)
|
||||
list(APPEND _src widgetwindowagent_mac.cpp)
|
||||
endif()
|
||||
|
||||
qwk_add_library(${PROJECT_NAME} AUTOGEN
|
||||
SOURCES ${_src}
|
||||
FEATURES cxx_std_17
|
||||
LINKS QWKCore
|
||||
QT_LINKS Core Gui Widgets
|
||||
QT_INCLUDE_PRIVATE Core Gui Widgets
|
||||
INCLUDE_PRIVATE *
|
||||
PREFIX QWK_WIDGETS
|
||||
)
|
||||
|
||||
set(QWINDOWKIT_ENABLED_TARGETS ${QWINDOWKIT_ENABLED_TARGETS} ${PROJECT_NAME} PARENT_SCOPE)
|
||||
set(QWINDOWKIT_ENABLED_SUBDIRECTORIES ${QWINDOWKIT_ENABLED_SUBDIRECTORIES} widgets PARENT_SCOPE)
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef QWKWIDGETSGLOBAL_H
|
||||
#define QWKWIDGETSGLOBAL_H
|
||||
|
||||
#include <QtCore/QtGlobal>
|
||||
|
||||
#ifndef QWK_WIDGETS_EXPORT
|
||||
# ifdef QWK_WIDGETS_STATIC
|
||||
# define QWK_WIDGETS_EXPORT
|
||||
# else
|
||||
# ifdef QWK_WIDGETS_LIBRARY
|
||||
# define QWK_WIDGETS_EXPORT Q_DECL_EXPORT
|
||||
# else
|
||||
# define QWK_WIDGETS_EXPORT Q_DECL_IMPORT
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // QWKWIDGETSGLOBAL_H
|
||||
@@ -0,0 +1,131 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "widgetitemdelegate_p.h"
|
||||
|
||||
#include <QtGui/QMouseEvent>
|
||||
#include <QtWidgets/QApplication>
|
||||
#include <QtWidgets/QWidget>
|
||||
|
||||
#include <QWKCore/private/abstractwindowcontext_p.h>
|
||||
|
||||
extern Q_DECL_IMPORT QWidget *qt_button_down;
|
||||
|
||||
namespace QWK {
|
||||
|
||||
class WidgetWinIdChangeEventFilter : public WinIdChangeEventFilter {
|
||||
public:
|
||||
explicit WidgetWinIdChangeEventFilter(QObject *host, AbstractWindowContext *ctx)
|
||||
: WinIdChangeEventFilter(host, ctx), widget(static_cast<QWidget *>(host)) {
|
||||
widget->installEventFilter(this);
|
||||
}
|
||||
|
||||
WId winId() const override {
|
||||
return widget->effectiveWinId();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event) override {
|
||||
Q_UNUSED(obj)
|
||||
if (event->type() == QEvent::WinIdChange) {
|
||||
context->notifyWinIdChange();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QWidget *widget;
|
||||
};
|
||||
|
||||
WidgetItemDelegate::WidgetItemDelegate() = default;
|
||||
|
||||
WidgetItemDelegate::~WidgetItemDelegate() = default;
|
||||
|
||||
QWindow *WidgetItemDelegate::window(const QObject *obj) const {
|
||||
return static_cast<const QWidget *>(obj)->windowHandle();
|
||||
}
|
||||
|
||||
bool WidgetItemDelegate::isEnabled(const QObject *obj) const {
|
||||
return static_cast<const QWidget *>(obj)->isEnabled();
|
||||
}
|
||||
|
||||
bool WidgetItemDelegate::isVisible(const QObject *obj) const {
|
||||
return static_cast<const QWidget *>(obj)->isVisible();
|
||||
}
|
||||
|
||||
QRect WidgetItemDelegate::mapGeometryToScene(const QObject *obj) const {
|
||||
auto widget = static_cast<const QWidget *>(obj);
|
||||
const QPoint originPoint = widget->mapTo(widget->window(), QPoint(0, 0));
|
||||
const QSize size = widget->size();
|
||||
return {originPoint, size};
|
||||
}
|
||||
|
||||
QWindow *WidgetItemDelegate::hostWindow(const QObject *host) const {
|
||||
return static_cast<const QWidget *>(host)->windowHandle();
|
||||
}
|
||||
|
||||
bool WidgetItemDelegate::isWindowActive(const QObject *host) const {
|
||||
return static_cast<const QWidget *>(host)->isActiveWindow();
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::resetQtGrabbedControl(QObject *host) const {
|
||||
Q_UNUSED(host);
|
||||
if (!qt_button_down) {
|
||||
return;
|
||||
}
|
||||
static constexpr const auto invalidPos =
|
||||
QPoint{std::numeric_limits<int>::lowest(), std::numeric_limits<int>::lowest()};
|
||||
const auto event = new QMouseEvent(
|
||||
QEvent::MouseButtonRelease, invalidPos, invalidPos, invalidPos, Qt::LeftButton,
|
||||
QGuiApplication::mouseButtons() ^ Qt::LeftButton, QGuiApplication::keyboardModifiers());
|
||||
QCoreApplication::postEvent(qt_button_down, event);
|
||||
qt_button_down = nullptr;
|
||||
}
|
||||
|
||||
Qt::WindowStates WidgetItemDelegate::getWindowState(const QObject *host) const {
|
||||
return static_cast<const QWidget *>(host)->windowState();
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::setWindowState(QObject *host, Qt::WindowStates state) const {
|
||||
static_cast<QWidget *>(host)->setWindowState(state);
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::setCursorShape(QObject *host, Qt::CursorShape shape) const {
|
||||
static_cast<QWidget *>(host)->setCursor(QCursor(shape));
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::restoreCursorShape(QObject *host) const {
|
||||
static_cast<QWidget *>(host)->unsetCursor();
|
||||
}
|
||||
|
||||
Qt::WindowFlags WidgetItemDelegate::getWindowFlags(const QObject *host) const {
|
||||
return static_cast<const QWidget *>(host)->windowFlags();
|
||||
}
|
||||
|
||||
QRect WidgetItemDelegate::getGeometry(const QObject *host) const {
|
||||
return static_cast<const QWidget *>(host)->geometry();
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const {
|
||||
static_cast<QWidget *>(host)->setWindowFlags(flags);
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::setWindowVisible(QObject *host, bool visible) const {
|
||||
static_cast<QWidget *>(host)->setVisible(visible);
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::setGeometry(QObject *host, const QRect &rect) {
|
||||
static_cast<QWidget *>(host)->setGeometry(rect);
|
||||
}
|
||||
|
||||
void WidgetItemDelegate::bringWindowToTop(QObject *host) const {
|
||||
static_cast<QWidget *>(host)->raise();
|
||||
}
|
||||
|
||||
WinIdChangeEventFilter *
|
||||
WidgetItemDelegate::createWinIdEventFilter(QObject *host,
|
||||
AbstractWindowContext *context) const {
|
||||
return new WidgetWinIdChangeEventFilter(host, context);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef WIDGETITEMDELEGATE_P_H
|
||||
#define WIDGETITEMDELEGATE_P_H
|
||||
|
||||
//
|
||||
// W A R N I N G !!!
|
||||
// -----------------
|
||||
//
|
||||
// This file is not part of the QWindowKit API. It is used purely as an
|
||||
// implementation detail. This header file may change from version to
|
||||
// version without notice, or may even be removed.
|
||||
//
|
||||
|
||||
#include <QtCore/QObject>
|
||||
#include <QtGui/QWindow>
|
||||
|
||||
#include <QWKCore/private/windowitemdelegate_p.h>
|
||||
#include <QWKWidgets/qwkwidgetsglobal.h>
|
||||
|
||||
namespace QWK {
|
||||
|
||||
class QWK_WIDGETS_EXPORT WidgetItemDelegate : public WindowItemDelegate {
|
||||
public:
|
||||
WidgetItemDelegate();
|
||||
~WidgetItemDelegate() override;
|
||||
|
||||
public:
|
||||
QWindow *window(const QObject *obj) const override;
|
||||
bool isEnabled(const QObject *obj) const override;
|
||||
bool isVisible(const QObject *obj) const override;
|
||||
QRect mapGeometryToScene(const QObject *obj) const override;
|
||||
|
||||
QWindow *hostWindow(const QObject *host) const override;
|
||||
bool isWindowActive(const QObject *host) const override;
|
||||
Qt::WindowStates getWindowState(const QObject *host) const override;
|
||||
Qt::WindowFlags getWindowFlags(const QObject *host) const override;
|
||||
QRect getGeometry(const QObject *host) const override;
|
||||
|
||||
void resetQtGrabbedControl(QObject *host) const override;
|
||||
void setWindowState(QObject *host, Qt::WindowStates state) const override;
|
||||
void setCursorShape(QObject *host, Qt::CursorShape shape) const override;
|
||||
void restoreCursorShape(QObject *host) const override;
|
||||
void setWindowFlags(QObject *host, Qt::WindowFlags flags) const override;
|
||||
void setWindowVisible(QObject *host, bool visible) const override;
|
||||
void setGeometry(QObject *host, const QRect &rect) override;
|
||||
void bringWindowToTop(QObject *host) const override;
|
||||
|
||||
WinIdChangeEventFilter *
|
||||
createWinIdEventFilter(QObject *host, AbstractWindowContext *context) const override;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // WIDGETITEMDELEGATE_P_H
|
||||
@@ -0,0 +1,163 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "widgetwindowagent.h"
|
||||
#include "widgetwindowagent_p.h"
|
||||
|
||||
#include <QtGui/QtEvents>
|
||||
#include <QtGui/QPainter>
|
||||
#include <QtCore/QDebug>
|
||||
|
||||
#include "widgetitemdelegate_p.h"
|
||||
|
||||
namespace QWK {
|
||||
|
||||
/*!
|
||||
\class WidgetWindowAgent
|
||||
\brief WindowAgentBase is the window agent for QtWidgets.
|
||||
|
||||
It provides interfaces for QtWidgets and processes some Qt events related to the QWidget
|
||||
instance.
|
||||
*/
|
||||
|
||||
WidgetWindowAgentPrivate::WidgetWindowAgentPrivate() = default;
|
||||
|
||||
WidgetWindowAgentPrivate::~WidgetWindowAgentPrivate() = default;
|
||||
|
||||
void WidgetWindowAgentPrivate::init() {
|
||||
}
|
||||
|
||||
/*!
|
||||
Constructs a widget agent, it's better to set the widget to setup as \a parent.
|
||||
*/
|
||||
WidgetWindowAgent::WidgetWindowAgent(QObject *parent)
|
||||
: WidgetWindowAgent(*new WidgetWindowAgentPrivate(), parent) {
|
||||
}
|
||||
|
||||
/*!
|
||||
Destructor.
|
||||
*/
|
||||
WidgetWindowAgent::~WidgetWindowAgent() = default;
|
||||
|
||||
/*!
|
||||
Installs the window agent on the widget. The window agent will take over some of the window
|
||||
events, making the window look frameless.
|
||||
*/
|
||||
bool WidgetWindowAgent::setup(QWidget *w) {
|
||||
Q_ASSERT(w);
|
||||
if (!w) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#if defined(_DEBUG) || !defined(NDEBUG)
|
||||
if (w->inherits("QDockWidget")) {
|
||||
qWarning()
|
||||
<< "QWK::WidgetWindowAgent: QWindowKit does not support QDockWidget as top window.";
|
||||
}
|
||||
#endif
|
||||
|
||||
Q_D(WidgetWindowAgent);
|
||||
if (d->hostWidget) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Qt will create invisible native window container for native QWidget
|
||||
// without this attribute, and this behavior will break QWK functionality.
|
||||
// So far enabling this attribute is a must for QWK users.
|
||||
w->setAttribute(Qt::WA_DontCreateNativeAncestors);
|
||||
// Make sure the native window handle is actually created before we apply
|
||||
// various hooks.
|
||||
// w->setAttribute(Qt::WA_NativeWindow); // ### FIXME: Check
|
||||
|
||||
d->setup(w, new WidgetItemDelegate());
|
||||
d->hostWidget = w;
|
||||
|
||||
#if defined(Q_OS_WINDOWS) && QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS)
|
||||
d->setupWindows10BorderWorkaround();
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the title bar widget.
|
||||
*/
|
||||
QWidget *WidgetWindowAgent::titleBar() const {
|
||||
Q_D(const WidgetWindowAgent);
|
||||
return static_cast<QWidget *>(d->context->titleBar());
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the title bar widget, all system button and hit-test visible widget references that
|
||||
have been set will be removed.
|
||||
*/
|
||||
void WidgetWindowAgent::setTitleBar(QWidget *w) {
|
||||
Q_D(WidgetWindowAgent);
|
||||
if (!d->context->setTitleBar(w)) {
|
||||
return;
|
||||
}
|
||||
#ifdef Q_OS_MAC
|
||||
setSystemButtonArea(nullptr);
|
||||
#endif
|
||||
Q_EMIT titleBarChanged(w);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the system button of the given type.
|
||||
*/
|
||||
QWidget *WidgetWindowAgent::systemButton(SystemButton button) const {
|
||||
Q_D(const WidgetWindowAgent);
|
||||
return static_cast<QWidget *>(d->context->systemButton(button));
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the system button of the given type, the system buttons always receive mouse events so
|
||||
you don't need to call \c setHitTestVisible for them.
|
||||
*/
|
||||
void WidgetWindowAgent::setSystemButton(SystemButton button, QWidget *w) {
|
||||
Q_D(WidgetWindowAgent);
|
||||
if (!d->context->setSystemButton(button, w)) {
|
||||
return;
|
||||
}
|
||||
Q_EMIT systemButtonChanged(button, w);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns \a true if the widget can receive mouse events on title bar.
|
||||
*/
|
||||
bool WidgetWindowAgent::isHitTestVisible(const QWidget *w) const {
|
||||
Q_D(const WidgetWindowAgent);
|
||||
return d->context->isHitTestVisible(w);
|
||||
}
|
||||
|
||||
/*!
|
||||
Makes the widget able to receive mouse events on title bar if \a visible is \c true.
|
||||
You're supposed to make sure that the specified widget \a w is a child or descendant
|
||||
of the title bar widget.
|
||||
*/
|
||||
void WidgetWindowAgent::setHitTestVisible(QWidget *w, bool visible) {
|
||||
Q_D(WidgetWindowAgent);
|
||||
d->context->setHitTestVisible(w, visible);
|
||||
}
|
||||
|
||||
/*!
|
||||
\internal
|
||||
*/
|
||||
WidgetWindowAgent::WidgetWindowAgent(WidgetWindowAgentPrivate &d, QObject *parent)
|
||||
: WindowAgentBase(d, parent) {
|
||||
d.init();
|
||||
}
|
||||
|
||||
/*!
|
||||
\fn void WidgetWindowAgent::titleBarChanged(const QWidget *w)
|
||||
|
||||
This signal is emitted when the title bar widget is replaced.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn void WidgetWindowAgent::systemButtonChanged(SystemButton button, const QWidget *w)
|
||||
|
||||
This signal is emitted when a system button is replaced.
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef WIDGETWINDOWAGENT_H
|
||||
#define WIDGETWINDOWAGENT_H
|
||||
|
||||
#include <QtWidgets/QWidget>
|
||||
|
||||
#include <QWKCore/windowagentbase.h>
|
||||
#include <QWKWidgets/qwkwidgetsglobal.h>
|
||||
|
||||
namespace QWK {
|
||||
|
||||
class WidgetWindowAgentPrivate;
|
||||
|
||||
class QWK_WIDGETS_EXPORT WidgetWindowAgent : public WindowAgentBase {
|
||||
Q_OBJECT
|
||||
Q_DECLARE_PRIVATE(WidgetWindowAgent)
|
||||
public:
|
||||
explicit WidgetWindowAgent(QObject *parent = nullptr);
|
||||
~WidgetWindowAgent() override;
|
||||
|
||||
public:
|
||||
bool setup(QWidget *w);
|
||||
|
||||
QWidget *titleBar() const;
|
||||
void setTitleBar(QWidget *w);
|
||||
|
||||
QWidget *systemButton(SystemButton button) const;
|
||||
void setSystemButton(SystemButton button, QWidget *w);
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
// The system button area APIs are experimental, very likely to change in the future.
|
||||
QWidget *systemButtonArea() const;
|
||||
void setSystemButtonArea(QWidget *widget);
|
||||
|
||||
ScreenRectCallback systemButtonAreaCallback() const;
|
||||
void setSystemButtonAreaCallback(const ScreenRectCallback &callback);
|
||||
#endif
|
||||
|
||||
bool isHitTestVisible(const QWidget *w) const;
|
||||
void setHitTestVisible(QWidget *w, bool visible = true);
|
||||
|
||||
Q_SIGNALS:
|
||||
void titleBarChanged(QWidget *w);
|
||||
void systemButtonChanged(SystemButton button, QWidget *w);
|
||||
|
||||
protected:
|
||||
WidgetWindowAgent(WidgetWindowAgentPrivate &d, QObject *parent = nullptr);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // WIDGETWINDOWAGENT_H
|
||||
@@ -0,0 +1,98 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "widgetwindowagent_p.h"
|
||||
|
||||
#include <QtGui/QtEvents>
|
||||
|
||||
namespace QWK {
|
||||
|
||||
static inline QRect getWidgetSceneRect(QWidget *widget) {
|
||||
return {widget->mapTo(widget->window(), QPoint()), widget->size()};
|
||||
}
|
||||
|
||||
class SystemButtonAreaWidgetEventFilter : public QObject {
|
||||
public:
|
||||
SystemButtonAreaWidgetEventFilter(QWidget *widget, AbstractWindowContext *ctx,
|
||||
QObject *parent = nullptr)
|
||||
: QObject(parent), widget(widget), ctx(ctx) {
|
||||
widget->installEventFilter(this);
|
||||
ctx->setSystemButtonAreaCallback([widget](const QSize &) {
|
||||
return getWidgetSceneRect(widget); //
|
||||
});
|
||||
}
|
||||
~SystemButtonAreaWidgetEventFilter() override = default;
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event) override {
|
||||
Q_UNUSED(obj)
|
||||
switch (event->type()) {
|
||||
case QEvent::Move:
|
||||
case QEvent::Resize: {
|
||||
ctx->virtual_hook(AbstractWindowContext::SystemButtonAreaChangedHook, nullptr);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected:
|
||||
QWidget *widget;
|
||||
AbstractWindowContext *ctx;
|
||||
};
|
||||
|
||||
/*!
|
||||
Returns the widget that acts as the system button area.
|
||||
*/
|
||||
QWidget *WidgetWindowAgent::systemButtonArea() const {
|
||||
Q_D(const WidgetWindowAgent);
|
||||
return d->systemButtonAreaWidget;
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the widget that acts as the system button area. The system button will be centered in
|
||||
its area, it is recommended to place the widget in a layout and set a fixed size policy.
|
||||
|
||||
The system button will be visible in the system title bar area.
|
||||
*/
|
||||
void WidgetWindowAgent::setSystemButtonArea(QWidget *widget) {
|
||||
Q_D(WidgetWindowAgent);
|
||||
if (d->systemButtonAreaWidget == widget)
|
||||
return;
|
||||
|
||||
auto ctx = d->context.get();
|
||||
d->systemButtonAreaWidget = widget;
|
||||
if (!widget) {
|
||||
d->context->setSystemButtonAreaCallback({});
|
||||
d->systemButtonAreaWidgetEventFilter.reset();
|
||||
return;
|
||||
}
|
||||
d->systemButtonAreaWidgetEventFilter =
|
||||
std::make_unique<SystemButtonAreaWidgetEventFilter>(widget, ctx);
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns the the system button area callback.
|
||||
*/
|
||||
ScreenRectCallback WidgetWindowAgent::systemButtonAreaCallback() const {
|
||||
Q_D(const WidgetWindowAgent);
|
||||
return d->systemButtonAreaWidget ? nullptr : d->context->systemButtonAreaCallback();
|
||||
}
|
||||
|
||||
/*!
|
||||
Sets the the system button area callback, the \c size of the callback is the native title
|
||||
bar size.
|
||||
|
||||
The system button position will be updated when the window resizes.
|
||||
*/
|
||||
void WidgetWindowAgent::setSystemButtonAreaCallback(const ScreenRectCallback &callback) {
|
||||
Q_D(WidgetWindowAgent);
|
||||
setSystemButtonArea(nullptr);
|
||||
d->context->setSystemButtonAreaCallback(callback);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#ifndef WIDGETWINDOWAGENTPRIVATE_H
|
||||
#define WIDGETWINDOWAGENTPRIVATE_H
|
||||
|
||||
//
|
||||
// W A R N I N G !!!
|
||||
// -----------------
|
||||
//
|
||||
// This file is not part of the QWindowKit API. It is used purely as an
|
||||
// implementation detail. This header file may change from version to
|
||||
// version without notice, or may even be removed.
|
||||
//
|
||||
|
||||
#include <QWKCore/qwkconfig.h>
|
||||
#include <QWKCore/private/windowagentbase_p.h>
|
||||
#include <QWKWidgets/widgetwindowagent.h>
|
||||
|
||||
namespace QWK {
|
||||
|
||||
class WidgetWindowAgentPrivate : public WindowAgentBasePrivate {
|
||||
Q_DECLARE_PUBLIC(WidgetWindowAgent)
|
||||
public:
|
||||
WidgetWindowAgentPrivate();
|
||||
~WidgetWindowAgentPrivate();
|
||||
|
||||
void init();
|
||||
|
||||
// Host
|
||||
QWidget *hostWidget{};
|
||||
|
||||
#ifdef Q_OS_MAC
|
||||
QWidget *systemButtonAreaWidget{};
|
||||
std::unique_ptr<QObject> systemButtonAreaWidgetEventFilter;
|
||||
#endif
|
||||
|
||||
#if defined(Q_OS_WINDOWS) && QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS)
|
||||
void setupWindows10BorderWorkaround();
|
||||
std::unique_ptr<QObject> borderHandler;
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // WIDGETWINDOWAGENTPRIVATE_H
|
||||
@@ -0,0 +1,179 @@
|
||||
// Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
|
||||
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
#include "widgetwindowagent_p.h"
|
||||
|
||||
#include <QtCore/QDebug>
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtGui/QPainter>
|
||||
|
||||
#include <QtCore/private/qcoreapplication_p.h>
|
||||
|
||||
#include <QWKCore/qwindowkit_windows.h>
|
||||
#include <QWKCore/private/qwkglobal_p.h>
|
||||
#include <QWKCore/private/windows10borderhandler_p.h>
|
||||
|
||||
namespace QWK {
|
||||
|
||||
#if QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS)
|
||||
// https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/plugins/platforms/windows/qwindowsbackingstore.cpp#L42
|
||||
// In QtWidgets applications, when repainting happens, QPA at the last calls
|
||||
// QWindowsBackingStore::flush() to draw the contents of the buffer to the screen, we need to
|
||||
// call GDI drawing the top border after that.
|
||||
|
||||
// After debugging, we know that there are two situations that will lead to repaint.
|
||||
//
|
||||
// 1. Windows sends a WM_PAINT message, after which Qt immediately generates a QExposeEvent or
|
||||
// QResizeEvent and send it to the corresponding QWidgetWindow instance, calling "flush" at the
|
||||
// end of its handler.
|
||||
//
|
||||
// 2. When a timer or user input triggers Qt to repaint spontaneously, the corresponding
|
||||
// QWidget receives a QEvent::UpdateRequest event and also calls "flush" at the end of its
|
||||
// handler.
|
||||
//
|
||||
// The above two cases are mutually exclusive, so we just need to intercept the two events
|
||||
// separately and draw the border area after the "flush" is called.
|
||||
|
||||
// https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/plugins/platforms/windows/qwindowswindow.cpp#L2440
|
||||
// Note that we can not draw the border right after WM_PAINT comes or right before the WndProc
|
||||
// returns, because Qt calls BeginPaint() and EndPaint() itself. We should make sure that we
|
||||
// draw the top border between these two calls, otherwise some display exceptions may arise.
|
||||
|
||||
class WidgetBorderHandler : public QObject, public Windows10BorderHandler {
|
||||
public:
|
||||
explicit WidgetBorderHandler(QWidget *widget, AbstractWindowContext *ctx,
|
||||
QObject *parent = nullptr)
|
||||
: QObject(parent), Windows10BorderHandler(ctx), widget(widget) {
|
||||
widget->installEventFilter(this);
|
||||
|
||||
// First update
|
||||
if (ctx->windowId()) {
|
||||
setupNecessaryAttributes();
|
||||
}
|
||||
WidgetBorderHandler::updateGeometry();
|
||||
}
|
||||
|
||||
void updateGeometry() override {
|
||||
// The window top border is manually painted by QWK so we want to give
|
||||
// some margins to avoid it covering real window contents, however, we
|
||||
// found that there are some rounding issues for the thin border and
|
||||
// thus this small trick doesn't work very well when the DPR is not
|
||||
// integer. So far we haven't found a perfect solution, so just don't
|
||||
// set any margins. In theory the window content will only be covered
|
||||
// by 1px or so, it should not be a serious issue in the real world.
|
||||
//
|
||||
// widget->setContentsMargins(isNormalWindow() ? QMargins(0, borderThickness(), 0, 0)
|
||||
// : QMargins());
|
||||
}
|
||||
|
||||
bool isWindowActive() const override {
|
||||
return widget->isActiveWindow();
|
||||
}
|
||||
|
||||
inline void forwardEventToWidgetAndDraw(QWidget *w, QEvent *event) {
|
||||
// https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/widgets/kernel/qapplication.cpp#L3286
|
||||
// Deliver the event
|
||||
if (!forwardObjectEventFilters(this, w, event)) {
|
||||
// Let the widget paint first
|
||||
std::ignore = static_cast<QObject *>(w)->event(event);
|
||||
QCoreApplicationPrivate::setEventSpontaneous(event, false);
|
||||
}
|
||||
|
||||
// Due to the timer or user action, Qt will repaint some regions spontaneously,
|
||||
// even if there is no WM_PAINT message, we must wait for it to finish painting
|
||||
// and then update the top border area.
|
||||
drawBorderNative();
|
||||
}
|
||||
|
||||
inline void forwardEventToWindowAndDraw(QWindow *window, QEvent *event) {
|
||||
// https://github.com/qt/qtbase/blob/e26a87f1ecc40bc8c6aa5b889fce67410a57a702/src/widgets/kernel/qapplication.cpp#L3286
|
||||
// Deliver the event
|
||||
if (!forwardObjectEventFilters(ctx, window, event)) {
|
||||
// Let Qt paint first
|
||||
std::ignore = static_cast<QObject *>(window)->event(event);
|
||||
QCoreApplicationPrivate::setEventSpontaneous(event, false);
|
||||
}
|
||||
|
||||
// Upon receiving the WM_PAINT message, Qt will repaint the entire view, and we
|
||||
// must wait for it to finish painting before drawing this top border area.
|
||||
drawBorderNative();
|
||||
}
|
||||
|
||||
protected:
|
||||
bool sharedEventFilter(QObject *obj, QEvent *event) override {
|
||||
Q_UNUSED(obj)
|
||||
|
||||
switch (event->type()) {
|
||||
case QEvent::Expose: {
|
||||
// Qt will absolutely send a QExposeEvent or QResizeEvent to the QWindow when it
|
||||
// receives a WM_PAINT message. When the control flow enters the expose handler,
|
||||
// Qt must have already called BeginPaint() and it's the best time for us to
|
||||
// draw the top border.
|
||||
|
||||
// Since a QExposeEvent will be sent immediately after the QResizeEvent, we can
|
||||
// simply ignore it.
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||
struct ExposeEvent : public QExposeEvent {
|
||||
inline const QRegion &getRegion() const { return m_region; }
|
||||
};
|
||||
auto ee = static_cast<ExposeEvent *>(event);
|
||||
bool exposeRegionValid = !ee->getRegion().isNull();
|
||||
#else
|
||||
auto ee = static_cast<QExposeEvent *>(event);
|
||||
bool exposeRegionValid = !ee->region().isNull();
|
||||
#endif
|
||||
auto window = widget->windowHandle();
|
||||
if (window->isExposed() && isNormalWindow() && exposeRegionValid) {
|
||||
forwardEventToWindowAndDraw(window, event);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return Windows10BorderHandler::sharedEventFilter(obj, event);
|
||||
}
|
||||
|
||||
bool eventFilter(QObject *obj, QEvent *event) override {
|
||||
Q_UNUSED(obj)
|
||||
|
||||
switch (event->type()) {
|
||||
case QEvent::UpdateRequest: {
|
||||
if (!isNormalWindow())
|
||||
break;
|
||||
forwardEventToWidgetAndDraw(widget, event);
|
||||
return true;
|
||||
}
|
||||
|
||||
case QEvent::WindowStateChange: {
|
||||
updateGeometry();
|
||||
break;
|
||||
}
|
||||
|
||||
case QEvent::WindowActivate:
|
||||
case QEvent::WindowDeactivate: {
|
||||
widget->update();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QWidget *widget;
|
||||
};
|
||||
|
||||
void WidgetWindowAgentPrivate::setupWindows10BorderWorkaround() {
|
||||
// Install painting hook
|
||||
auto ctx = context.get();
|
||||
if (ctx->windowAttribute(QStringLiteral("win10-border-needed")).toBool()) {
|
||||
borderHandler = std::make_unique<WidgetBorderHandler>(hostWidget, ctx);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user