QmlMaterial 0.1.0
Loading...
Searching...
No Matches
button_interaction_state.hpp
1#pragma once
2#include "qml_material/style/common_state.hpp"
3#include <optional>
4namespace qml_material
5{
6class AbstractButton;
7class QML_MATERIAL_API ButtonInteractionState : public CommonState {
8 Q_OBJECT
9 QML_ANONYMOUS
10public:
11 ~ButtonInteractionState() override;
12 Q_INVOKABLE qreal groupOpticalOffset(const qml_material::CornersGroup&) const;
13 Q_SIGNAL void itemChanged();
14
15protected:
16 void enableGroupShape();
17 int groupSize() const;
18 enum class Interaction
19 {
20 Base,
21 Disabled,
22 Pressed,
23 Hovered,
24 Focus
25 };
26 using BindingSet = StateBindingSet<Interaction>;
27 using BindingLifetime = BindingSet::Lifetime;
28 BindingSet& bindingSet() { return m_bindings; }
29 StateBindings baseBindings() { return m_bindings.base(); }
30 StateBindings stateBindings(Interaction state) { return m_bindings.state(state); }
31 void stopBindings() {
32 m_inputReady = false;
33 m_selection.takeBinding();
34 m_hovered.takeBinding();
35 m_bindings.abandon();
36 }
37 void setHoverBinding(const QPropertyBinding<bool>& binding) { m_hovered.setBinding(binding); }
38 enum class FocusTreatment
39 {
40 Pressed,
41 Separate,
42 Ignored
43 };
44 enum class PressSource
45 {
46 Down,
47 Pressed
48 };
49 explicit ButtonInteractionState(QObject* parent = nullptr,
50 FocusTreatment focus = FocusTreatment::Pressed,
51 PressSource press = PressSource::Down);
52 AbstractButton* inputItem() const;
53 void setInputItem(AbstractButton*);
54 bool checked() const;
55 bool checkable() const;
56 bool down() const;
57 bool hovered() const;
58 bool visualFocus() const;
59 bool disabled() const;
60 std::optional<qreal> backgroundHeight() const;
61 void bindStateLayerOpacity();
62
63private:
64 void updateGroupContext();
65 CornersGroup groupCorners() const;
66 QList<QMetaObject::Connection> m_groupConnections;
67 QProperty<quint64> m_groupRevision { 0 };
68 static QString stateName(Interaction);
69 BindingSet m_bindings { Interaction::Base };
70 struct Selection {
71 Interaction state = Interaction::Base;
72 quint64 generation = 0;
73 bool operator==(const Selection&) const = default;
74 };
75 void selectionChanged();
76 bool m_inputReady = false;
77 QProperty<AbstractButton*> m_item { nullptr };
78 QList<QMetaObject::Connection> m_connections;
79 QProperty<bool> m_disabled { false };
80 QProperty<bool> m_hovered { false };
81 Q_OBJECT_BINDABLE_PROPERTY(ButtonInteractionState, Selection, m_selection,
82 &ButtonInteractionState::selectionChanged)
83};
84} // namespace qml_material