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