QmlMaterial 0.1.0
Loading...
Searching...
No Matches
qml_util.hpp
1#pragma once
2#include <QObject>
3#include <QQmlEngine>
4#include <QColor>
5#include <QQuickItem>
6#include <QPointer>
7#include <QQuickWindow>
8#include <memory>
9
10#include "qml_material/util/corner.hpp"
11#include "qml_material/core/enum.hpp"
12#include "qml_material/token/token.hpp"
13#include "qml_material/core.hpp"
14#include "qml_material/export.hpp"
15#include "qml_material/control/icon_spec.hpp"
16
17namespace qml_material
18{
19
20struct QML_MATERIAL_API ComponentSource {
21 QPointer<QQmlComponent> component;
22 std::unique_ptr<QQmlComponent> ownedComponent;
23 QString errorString;
24
25 explicit operator bool() const { return component != nullptr; }
26};
27
28class Util : public QObject {
29 Q_OBJECT
30 QML_NAMED_ELEMENT(UtilCpp)
31public:
32 Util(QObject* parent = nullptr);
33 ~Util();
34
35 enum Track
36 {
37 TrackCreate = 0,
38 TrackDelete
39 };
40 Q_ENUMS(Track)
41
42 Q_INVOKABLE void track(QVariant, Track);
43
48 Q_INVOKABLE void openUrlExternally(const QString& url);
49 Q_INVOKABLE void openFolderExternally(const QString& url);
50
51 Q_INVOKABLE bool hasIcon(ActionIcon* icon) const;
52
53 Q_INVOKABLE static void closePopup(QObject* obj);
54
55 Q_INVOKABLE static QColor transparent(QColor in, float alpha) noexcept;
56 Q_INVOKABLE static QColor hoverColor(QColor in) noexcept;
57 Q_INVOKABLE static QColor pressColor(QColor in) noexcept;
58 // QColor channels are interpreted as sRGB; alpha is interpolated linearly.
59 Q_INVOKABLE static QML_MATERIAL_API QColor mixColorOklab(QColor from, QColor to,
60 qreal progress) noexcept;
61
62 Q_INVOKABLE static qreal devicePixelRatio(QQuickItem* in);
63
64 Q_INVOKABLE static CornersGroup listCorners(qint32 idx, qint32 count, qint32 radius) noexcept;
65 Q_INVOKABLE static CornersGroup tableCorners(qint32 row, qint32 column, qint32 rows,
66 qint32 columns, qint32 radius) noexcept;
67 Q_INVOKABLE static CornersGroup tableWithHeaderCorners(qint32 row, qint32 column, qint32 rows,
68 qint32 columns, qint32 radius) noexcept;
69
70 Q_INVOKABLE static CornersGroup cornerArray(QVariant in) noexcept;
71 Q_INVOKABLE static CornersGroup corners(qreal in) noexcept;
72 Q_INVOKABLE static CornersGroup corners(qreal, qreal) noexcept;
73 Q_INVOKABLE static CornersGroup corners(qreal tl, qreal tr, qreal bl, qreal br) noexcept;
74
75 QString type_str(const QJSValue&);
76
77 Q_INVOKABLE void print_parents(const QJSValue&);
78
79 Q_INVOKABLE static qreal lightness(QColor color) noexcept;
80
81 Q_INVOKABLE static token::Elevation tokenElevation() noexcept;
82 Q_INVOKABLE static token::Shape tokenShape() noexcept;
83 Q_INVOKABLE static token::State tokenState() noexcept;
84
85 Q_INVOKABLE QObject* createItem(const QJSValue& url_or_comp, const QVariantMap& props,
86 QObject* parent = nullptr);
87
88 Q_INVOKABLE QObject* showPopup(const QJSValue& url_or_comp, const QVariantMap& props,
89 QObject* parent = nullptr, bool open_and_destry = true);
90
91 Q_INVOKABLE static QString paramsString(const QVariantMap& props);
92 Q_INVOKABLE static void setCursor(QQuickItem* item, Qt::CursorShape shape);
93
94 Q_INVOKABLE static double clamp(double t, double low, double high);
95 Q_INVOKABLE static double teleportCurve(double t, double left, double right);
96
97 // Cubic Bezier ease — given a normalized fraction `t` in [0,1] and a curve
98 // (0,0) → (p1x,p1y) → (p2x,p2y) → (1,1), returns the eased y. Matches
99 // Android's pathInterpolator.
100 Q_INVOKABLE static double bezierY(double t, double p1x, double p1y, double p2x,
101 double p2y) noexcept;
102 // Convenience for the M3 indeterminate animation drivers: clamp
103 // `(playtime - delay) / duration` to [0,1] then ease via bezierY.
104 Q_INVOKABLE static double segFrac(double playtime, double delay, double duration, double p1x,
105 double p1y, double p2x, double p2y) noexcept;
106
107 Q_INVOKABLE static QString stateText(bool enabled, bool pressed, bool hovered,
108 bool focused) noexcept;
109
110 Q_INVOKABLE static void forceSetImplicitHeight(QQuickItem* item, qreal height);
111 Q_INVOKABLE static void forceSetImplicitWidth(QQuickItem* item, qreal width);
112
113 Q_INVOKABLE static void cellHoveredOn(QQuickItem* item, bool hovered, qint32 row,
114 qint32 column);
115 Q_INVOKABLE static QObject* getParent(QObject* obj);
116 Q_INVOKABLE static QQuickItem* controlAncestor(QQuickItem* item);
117 Q_INVOKABLE static bool disconnectAll(QObject* obj, const QString&);
118
119 Q_INVOKABLE static quint32 poolObjectCount() noexcept;
120 Q_INVOKABLE static qint32 i32Max() noexcept;
121
122private:
123 Q_SLOT void onPopupFinished();
124
125private:
126 usize m_tracked { 0 };
127};
128
129QML_MATERIAL_API auto resolveComponentSource(const QVariant& source, QQmlEngine* engine,
130 QQmlContext* context,
131 QQmlComponent::CompilationMode mode)
132 -> ComponentSource;
133
137void sysOpenUrl(const QString& url);
138void sysOpenFolder(const QString& url);
139} // namespace qml_material
140
141namespace qcm
142{
143auto qml_dyn_count() -> std::atomic<i32>&;
144auto createItem(QQmlEngine* engine, const QJSValue& url_or_comp, const QVariantMap& props,
145 QObject* parent, QQmlContext* context) -> QObject*;
146
147} // namespace qcm
Definition Util.qml:4