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
45 changes: 45 additions & 0 deletions examples/qml/fpscounter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "fpscounter.h"
#include <QtCore/qtimer.h>
#include <QtQuick/qquickwindow.h>

class FPSCounterPrivate final {
Q_DISABLE_COPY(FPSCounterPrivate)
Q_DECLARE_PUBLIC(FPSCounter)

public:
FPSCounterPrivate(FPSCounter* qq);
~FPSCounterPrivate();

FPSCounter* q_ptr{ nullptr };
int frameCount{ 0 };
QMetaObject::Connection connection{};
};

FPSCounterPrivate::FPSCounterPrivate(FPSCounter* qq) : q_ptr{ qq } {
QObject::connect(q_ptr, &FPSCounter::windowChanged, q_ptr, [this](QQuickWindow* window){
if (connection) {
QObject::disconnect(std::exchange(connection, {}));
}
if (window) {
connection = QObject::connect(window, &QQuickWindow::frameSwapped, q_ptr, [this](){ ++frameCount; });
}
});
auto timer = new QTimer(q_ptr);
timer->setTimerType(Qt::PreciseTimer);
QObject::connect(timer, &QTimer::timeout, q_ptr, [this](){
Q_EMIT q_ptr->valueChanged();
frameCount = 0;
});
timer->start(1000);
}

FPSCounterPrivate::~FPSCounterPrivate() = default;

FPSCounter::FPSCounter(QQuickItem* parent) : QQuickItem{ parent }, d_ptr{ std::make_unique<FPSCounterPrivate>(this) } {}

FPSCounter::~FPSCounter() = default;

int FPSCounter::value() const {
Q_D(const FPSCounter);
return d->frameCount;
}
26 changes: 26 additions & 0 deletions examples/qml/fpscounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <QtQuick/qquickitem.h>
#include <memory>

class FPSCounterPrivate;
class FPSCounter : public QQuickItem {
Q_OBJECT
Q_DECLARE_PRIVATE(FPSCounter)
Q_PROPERTY(int value READ value NOTIFY valueChanged FINAL)
#ifdef QML_ELEMENT
QML_ELEMENT
#endif

public:
explicit FPSCounter(QQuickItem* parent = nullptr);
~FPSCounter() override;

int value() const;

Q_SIGNALS:
void valueChanged();

private:
const std::unique_ptr<FPSCounterPrivate> d_ptr;
};
20 changes: 16 additions & 4 deletions examples/qml/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
// SPDX-License-Identifier: Apache-2.0

#include "fpscounter.h"
#include <QtCore/qloggingcategory.h>
#include <QtGui/QGuiApplication>
#include <QtQml/QQmlApplicationEngine>
#include <QtQml/QQmlContext>
Expand All @@ -20,7 +22,10 @@ extern "C" {
int main(int argc, char *argv[]) {
qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); // or "new": create a separate console window
qputenv("QSG_INFO", "1");
qputenv("QT_NO_OPENGL_BUGLIST", "1");
qputenv("QSG_NO_VSYNC", "1");
qputenv("QT_D3D_NO_VBLANK_THREAD", "1");
qputenv("QT_QPA_UPDATE_IDLE_TIME", "0");

#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
qputenv("QT_QUICK_CONTROLS_STYLE", "Basic");
Expand All @@ -29,7 +34,7 @@ int main(int argc, char *argv[]) {
#endif

#ifdef Q_OS_WINDOWS
qputenv("QSG_RHI_BACKEND", "d3d11"); // options: d3d11, d3d12, opengl, vulkan
qputenv("QSG_RHI_BACKEND", "d3d11"); // options: d3d11, d3d12, opengl, vulkan
qputenv("QT_QPA_DISABLE_REDIRECTION_SURFACE", "1");
#endif
//qputenv("QSG_RHI_HDR", "scrgb"); // other options: hdr10, p3
Expand All @@ -38,17 +43,24 @@ int main(int argc, char *argv[]) {
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif

QGuiApplication application(argc, argv);

QLoggingCategory::setFilterRules(QStringLiteral("qt.qpa.screen.updates=true"));

// Make sure alpha channel is requested, our special effects on Windows depends on it.
QQuickWindow::setDefaultAlphaBuffer(true);
QQmlApplicationEngine engine;

QQmlApplicationEngine engine{};
#if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
const bool curveRenderingAvailable = true;
constexpr bool curveRenderingAvailable = true;
#else
const bool curveRenderingAvailable = false;
constexpr bool curveRenderingAvailable = false;
#endif
engine.rootContext()->setContextProperty(QStringLiteral("$curveRenderingAvailable"), QVariant(curveRenderingAvailable));
QWK::registerTypes(&engine);
qmlRegisterType<FPSCounter>("QWK.Demo", 1, 0, "FPSCounter");
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

return application.exec();
}
23 changes: 23 additions & 0 deletions examples/qml/main.qml
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QWK.Demo 1.0

FramelessWindow {
property FramelessWindow childWindow: FramelessWindow {
showWhenReady: false
}

FPSCounter {
property int maxVal: 0

id: fps
onValueChanged: fps.maxVal = Math.max(fps.value, fps.maxVal)
}

Text {
anchors {
bottom: buttonsRow.top
bottomMargin: 10
horizontalCenter: parent.horizontalCenter
}
font {
pixelSize: 25
bold: true
}
color: "green"
text: qsTr("FPS: ") + fps.value + qsTr(", Max: ") + fps.maxVal
}

Drawer {
id: drawer
width: 0.66 * parent.width
Expand All @@ -22,6 +44,7 @@ FramelessWindow {
}

Row {
id: buttonsRow
anchors {
horizontalCenter: parent.horizontalCenter
bottom: parent.bottom
Expand Down
20 changes: 15 additions & 5 deletions src/core/contexts/abstractwindowcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ namespace QWK {

AbstractWindowContext::~AbstractWindowContext() = default;

void AbstractWindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
if (m_host || !host || !delegate) {
void AbstractWindowContext::setup(QObject *host, WindowItemDelegate *delegate, WindowAgentBase* agent) {
Q_ASSERT(host);
Q_ASSERT(delegate);
Q_ASSERT(agent);
if (m_host || !host || !delegate || !agent) {
return;
}
m_host = host;
m_delegate.reset(delegate);
m_agent = agent;
m_winIdChangeEventFilter.reset(delegate->createWinIdEventFilter(host, this));
notifyWinIdChange();
}
Expand Down Expand Up @@ -49,7 +53,8 @@ namespace QWK {
bool AbstractWindowContext::setSystemButton(WindowAgentBase::SystemButton button,
QObject *obj) {
Q_ASSERT(button != WindowAgentBase::Unknown);
if (button == WindowAgentBase::Unknown) {
Q_ASSERT(obj);
if (button == WindowAgentBase::Unknown || !obj) {
return false;
}

Expand All @@ -63,6 +68,10 @@ namespace QWK {

bool AbstractWindowContext::setTitleBar(QObject *item) {
Q_ASSERT(item);
if (!item) {
return false;
}

auto org = m_titleBar;
if (org == item) {
return false;
Expand All @@ -85,6 +94,7 @@ namespace QWK {

bool AbstractWindowContext::isInSystemButtons(const QPoint &pos,
WindowAgentBase::SystemButton *button) const {
Q_ASSERT(button);
*button = WindowAgentBase::Unknown;
for (int i = WindowAgentBase::WindowIcon; i <= WindowAgentBase::Close; ++i) {
auto currentButton = m_systemButtons[i];
Expand Down Expand Up @@ -144,7 +154,7 @@ namespace QWK {
const quint32 activeDark = MAKE_RGBA_COLOR(177, 205, 190, 240);
const quint32 inactiveLight = MAKE_RGBA_COLOR(193, 195, 211, 203);
const quint32 inactiveDark = MAKE_RGBA_COLOR(240, 240, 250, 255);
} kSampleColorSet;
} kSampleColorSet{};

void AbstractWindowContext::virtual_hook(int id, void *data) {
switch (id) {
Expand Down Expand Up @@ -273,7 +283,7 @@ namespace QWK {
}

bool AbstractWindowContext::eventFilter(QObject *obj, QEvent *event) {
if (obj == m_windowHandle && sharedDispatch(obj, event)) {
if (obj && event && obj == m_windowHandle && sharedDispatch(obj, event)) {
return true;
}
return QObject::eventFilter(obj, event);
Expand Down
58 changes: 35 additions & 23 deletions src/core/contexts/abstractwindowcontext_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ namespace QWK {
~AbstractWindowContext() override;

public:
void setup(QObject *host, WindowItemDelegate *delegate);
void setup(QObject *host, WindowItemDelegate *delegate, WindowAgentBase* agent);

inline QObject *host() const;
inline QWindow *window() const;
inline WId windowId() const;
inline WindowItemDelegate *delegate() const;
inline WindowAgentBase* agent() const;

inline bool isHitTestVisible(const QObject *obj) const;
bool setHitTestVisible(QObject *obj, bool visible);
Expand All @@ -65,23 +66,9 @@ namespace QWK {
bool isInSystemButtons(const QPoint &pos, WindowAgentBase::SystemButton *button) const;
bool isInTitleBarDraggableArea(const QPoint &pos) const;

inline bool isHostWidthFixed() const {
return m_windowHandle
? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumWidth() == m_windowHandle->maximumWidth())
: false;
}
inline bool isHostHeightFixed() const {
return m_windowHandle
? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumHeight() == m_windowHandle->maximumHeight())
: false;
}
inline bool isHostSizeFixed() const {
return m_windowHandle ? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumSize() == m_windowHandle->maximumSize())
: false;
}
inline bool isHostWidthFixed() const;
inline bool isHostHeightFixed() const;
inline bool isHostSizeFixed() const;

virtual QString key() const;

Expand Down Expand Up @@ -111,18 +98,19 @@ namespace QWK {
const QVariant &oldAttribute);

protected:
QObject *m_host{};
QObject *m_host{ nullptr };
std::unique_ptr<WindowItemDelegate> m_delegate;
WindowAgentBase* m_agent{ nullptr };
QPointer<QWindow> m_windowHandle;
WId m_windowId{};
WId m_windowId{ 0 };

QVector<QPointer<QObject>> m_hitTestVisibleItems;
#ifdef Q_OS_MAC
ScreenRectCallback m_systemButtonAreaCallback;
ScreenRectCallback m_systemButtonAreaCallback{};
#endif

QPointer<QObject> m_titleBar{};
std::array<QPointer<QObject>, WindowAgentBase::Close + 1> m_systemButtons{};
QPointer<QObject> m_titleBar;
std::array<QPointer<QObject>, WindowAgentBase::Close + 1> m_systemButtons;

std::list<std::pair<QString, QVariant>> m_windowAttributesOrder;
QHash<QString, decltype(m_windowAttributesOrder)::iterator> m_windowAttributes;
Expand All @@ -148,6 +136,10 @@ namespace QWK {
return m_delegate.get();
}

inline WindowAgentBase* AbstractWindowContext::agent() const {
return m_agent;
}

inline bool AbstractWindowContext::isHitTestVisible(const QObject *obj) const {
return m_hitTestVisibleItems.contains(const_cast<QObject *>(obj));
}
Expand All @@ -167,6 +159,26 @@ namespace QWK {
}
#endif

inline bool AbstractWindowContext::isHostWidthFixed() const {
return m_windowHandle
? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumWidth() == m_windowHandle->maximumWidth())
: false;
}

inline bool AbstractWindowContext::isHostHeightFixed() const {
return m_windowHandle
? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumHeight() == m_windowHandle->maximumHeight())
: false;
}

inline bool AbstractWindowContext::isHostSizeFixed() const {
return m_windowHandle ? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
m_windowHandle->minimumSize() == m_windowHandle->maximumSize())
: false;
}

}

#endif // ABSTRACTWINDOWCONTEXT_P_H
2 changes: 1 addition & 1 deletion src/core/contexts/cocoawindowcontext_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace QWK {

class CocoaWindowContext : public AbstractWindowContext {
class QWK_CORE_EXPORT CocoaWindowContext final : public AbstractWindowContext {
Q_OBJECT
public:
CocoaWindowContext();
Expand Down
3 changes: 1 addition & 2 deletions src/core/contexts/linuxwaylandcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ namespace QWK {
seat, serial, x, y);
}

LinuxWaylandContext::LinuxWaylandContext() : QtWindowContext() {
}
LinuxWaylandContext::LinuxWaylandContext() = default;

LinuxWaylandContext::~LinuxWaylandContext() = default;

Expand Down
2 changes: 1 addition & 1 deletion src/core/contexts/linuxwaylandcontext_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace QWK {

class LinuxWaylandContext : public QtWindowContext {
class QWK_CORE_EXPORT LinuxWaylandContext final : public QtWindowContext {
Q_OBJECT
public:
LinuxWaylandContext();
Expand Down
3 changes: 1 addition & 2 deletions src/core/contexts/linuxx11context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ union _XEvent {

namespace QWK {

LinuxX11Context::LinuxX11Context() : QtWindowContext() {
}
LinuxX11Context::LinuxX11Context() = default;

LinuxX11Context::~LinuxX11Context() = default;

Expand Down
2 changes: 1 addition & 1 deletion src/core/contexts/linuxx11context_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
namespace QWK {

class LinuxX11Context : public QtWindowContext {
class QWK_CORE_EXPORT LinuxX11Context final : public QtWindowContext {
Q_OBJECT
public:
LinuxX11Context();
Expand Down
Loading