Sine Striker
2023-12-19 ed5f9693b3cd7ddcdc746c4bb65dcd9cf7a8268b
Add window attributes
6个文件已修改
91 ■■■■ 已修改文件
README.md 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext.cpp 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext_p.h 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext.cpp 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/windowagentbase.cpp 36 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/windowagentbase.h 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
README.md
@@ -10,7 +10,6 @@
+ Fix window 10 top border color in dark background
+ Fix `isFixedSize` code
+ Support customized system button area on Mac
+ Make Linux system move/resize more robust
## Supported Platforms
@@ -95,7 +94,7 @@
MyWidget::MyWidget(QWidget *parent) {
    // ...
    auto agent = new WidgetWindowAgent(w);
    auto agent = new QWK::WidgetWindowAgent(w);
    agent->setup(w);
    // ...
}
@@ -104,10 +103,12 @@
You can also initialize the agent after the window constructs.
```c++
auto w = new MyWidget();
auto agent = new WidgetWindowAgent(w);
auto agent = new QWK::WidgetWindowAgent(w);
agent->setup(w);
```
Then, construct your title bar widget, without which the window lacks the basic interaction feature, and it's better to put it into the window's layout.
You can use the [`WindowBar`](examples/shared/widgetframe/windowbar.h) provided by `WidgetFrame` in the examples as the container of your title bar components.
src/core/contexts/abstractwindowcontext.cpp
@@ -23,6 +23,21 @@
        }
    }
    void AbstractWindowContext::setWindowAttribute(const QString &key, const QVariant &var) {
        auto it = m_windowAttributes.find(key);
        if (it.value() == var)
            return;
        auto newVar = var;
        auto oldVar = it.value();
        void *a[] = {
            &const_cast<QString &>(key),
            &newVar,
            &oldVar,
        };
        virtual_hook(WindowAttributeChangedHook, a);
    }
    bool AbstractWindowContext::setHitTestVisible(const QObject *obj, bool visible) {
        Q_ASSERT(obj);
        if (!obj) {
src/core/contexts/abstractwindowcontext_p.h
@@ -28,6 +28,9 @@
        inline QWindow *window() const;
        inline WindowItemDelegate *delegate() const;
        inline QVariant windowAttribute(const QString &key) const;
        void setWindowAttribute(const QString &key, const QVariant &var);
        inline bool isHitTestVisible(const QObject *obj) const;
        bool setHitTestVisible(const QObject *obj, bool visible);
@@ -49,8 +52,10 @@
        enum WindowContextHook {
            CentralizeHook = 1,
            RaiseWindowHook,
            ShowSystemMenuHook,
            DefaultColorsHook,
            WindowAttributeChangedHook,
            DrawWindows10BorderHook,     // Only works on Windows 10
            SystemButtonAreaChangedHook, // Only works on Mac
        };
@@ -74,6 +79,8 @@
        QObject *m_titleBar{};
        std::array<QObject *, WindowAgentBase::NumSystemButton> m_systemButtons{};
        QVariantHash m_windowAttributes;
    };
    inline QObject *AbstractWindowContext::host() const {
@@ -88,6 +95,10 @@
        return m_delegate.get();
    }
    inline QVariant AbstractWindowContext::windowAttribute(const QString &key) const {
        return m_windowAttributes.value(key);
    }
    inline bool AbstractWindowContext::isHitTestVisible(const QObject *obj) const {
        return m_hitTestVisibleItems.contains(obj);
    }
src/core/contexts/win32windowcontext.cpp
@@ -824,6 +824,23 @@
                showSystemMenu2(hWnd, qpoint2point(nativeGlobalPos), false,
                                m_delegate->isHostSizeFixed(m_host));
                return;
            }
            case WindowAttributeChangedHook: {
                auto args = static_cast<void **>(data);
                const auto &key = *static_cast<const QString *>(args[0]);
                const auto &newVar = *static_cast<const QVariant *>(args[1]);
                const auto &oldVar = *static_cast<const QVariant *>(args[2]);
                if (key == QStringLiteral("no-frame-shadow")) {
                    if (newVar.toBool()) {
                        // TODO: set off
                    } else {
                        // TODO: set on
                    }
                }
                break;
            }
            case DefaultColorsHook: {
src/core/windowagentbase.cpp
@@ -50,37 +50,29 @@
    WindowAgentBase::~WindowAgentBase() = default;
    QVariant WindowAgentBase::windowAttribute(const QString &key) const {
        Q_D(const WindowAgentBase);
        return d->context->windowAttribute(key);
    }
    void WindowAgentBase::setWindowAttribute(const QString &key, const QVariant &var) {
        Q_D(WindowAgentBase);
        d->context->setWindowAttribute(key, var);
    }
    void WindowAgentBase::showSystemMenu(const QPoint &pos) {
        Q_D(WindowAgentBase);
        d->context->showSystemMenu(pos);
    }
    void WindowAgentBase::startSystemMove(const QPoint &pos) {
        Q_D(WindowAgentBase);
        auto win = d->context->window();
        if (!win) {
            return;
        }
        Q_UNUSED(pos)
        win->startSystemMove();
    }
    void WindowAgentBase::startSystemResize(Qt::Edges edges, const QPoint &pos) {
        Q_D(WindowAgentBase);
        auto win = d->context->window();
        if (!win) {
            return;
        }
        Q_UNUSED(pos)
        win->startSystemResize(edges);
    }
    void WindowAgentBase::centralize() {
        Q_D(WindowAgentBase);
        d->context->virtual_hook(AbstractWindowContext::CentralizeHook, nullptr);
    }
    void WindowAgentBase::raise() {
        Q_D(WindowAgentBase);
        d->context->virtual_hook(AbstractWindowContext::RaiseWindowHook, nullptr);
    }
    WindowAgentBase::WindowAgentBase(WindowAgentBasePrivate &d, QObject *parent)
src/core/windowagentbase.h
@@ -28,10 +28,11 @@
        };
        Q_ENUM(SystemButton)
        QVariant windowAttribute(const QString &key) const;
        void setWindowAttribute(const QString &key, const QVariant &var);
    public Q_SLOTS:
        void showSystemMenu(const QPoint &pos);
        void startSystemMove(const QPoint &pos);
        void startSystemResize(Qt::Edges edges, const QPoint &pos);
        void centralize();
        void raise();