-
-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathinput.h
190 lines (132 loc) · 4.66 KB
/
input.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
#include <karm-text/edit.h>
#include "box.h"
#include "funcs.h"
#include "view.h"
namespace Karm::Ui {
template <typename T = None>
using OnChange = Meta::Cond<
Meta::Same<T, None>,
Opt<SharedFunc<void(Node&)>>,
Opt<SharedFunc<void(Node&, T value)>>>;
template <typename T>
[[gnu::used]] static auto IGNORE(Ui::Node&, T) {}
// MARK: Button ----------------------------------------------------------------
struct MouseListener {
enum MouseState {
IDLE,
HOVER,
PRESS,
};
MouseState _state = IDLE;
Math::Vec2i _pos = {0, 0};
auto state() const {
return _state;
}
bool isIdle() const {
return _state == IDLE;
}
bool isHover() const {
return _state == HOVER;
}
bool isPress() const {
return _state == PRESS;
}
auto pos() const {
return _pos;
}
bool listen(Node& node, App::Event& event) {
bool result = false;
MouseState state = _state;
if (auto e = event.is<App::MouseEvent>()) {
if (not node.bound().contains(e->pos)) {
state = IDLE;
} else {
if (state != PRESS) {
state = HOVER;
}
_pos = e->pos - node.bound().topStart();
if (e->type == App::MouseEvent::PRESS and
e->button == App::MouseButton::LEFT) {
state = PRESS;
event.accept();
} else if (e->type == App::MouseEvent::RELEASE and
e->button == App::MouseButton::LEFT) {
if (state == PRESS) {
state = HOVER;
result = true;
event.accept();
}
}
}
} else if (auto e = event.is<App::MouseLeaveEvent>()) {
state = IDLE;
}
if (state != _state) {
_state = state;
shouldRepaint(node);
}
return result;
}
};
struct ButtonStyle {
static constexpr isize RADIUS = 4;
BoxStyle idleStyle{};
BoxStyle hoverStyle{};
BoxStyle pressStyle{};
BoxStyle disabledStyle = {
.foregroundFill = GRAY600,
};
static ButtonStyle none();
static ButtonStyle regular(Gfx::ColorRamp ramp = GRAYS);
static ButtonStyle secondary();
static ButtonStyle primary();
static ButtonStyle outline();
static ButtonStyle subtle();
static ButtonStyle text();
static ButtonStyle destructive();
ButtonStyle withRadii(Math::Radiif radii) const;
ButtonStyle withForegroundFill(Gfx::Fill fill) const;
ButtonStyle withPadding(Math::Insetsi insets) const;
ButtonStyle withMargin(Math::Insetsi insets) const;
};
using OnPress = Opt<Func<void(Node&)>>;
[[gnu::used]] static auto NOP(Ui::Node&) {}
Child button(OnPress onPress, ButtonStyle style, Child child);
inline auto button(OnPress onPress, ButtonStyle style) {
return [onPress = std::move(onPress), style](Child child) mutable {
return button(std::move(onPress), style, child);
};
}
Child button(OnPress onPress, ButtonStyle style, Str t);
Child button(OnPress onPress, ButtonStyle style, Gfx::Icon i);
Child button(OnPress onPress, ButtonStyle style, Gfx::Icon i, Str t);
Child button(OnPress onPress, Child child);
inline auto button(OnPress onPress) {
return [onPress = std::move(onPress)](Child child) mutable {
return button(std::move(onPress), child);
};
}
Child button(OnPress onPress, Str t);
Child button(OnPress onPress, Mdi::Icon i);
Child button(OnPress onPress, Mdi::Icon i, Str t);
// MARK: Input -----------------------------------------------------------------
Child input(Text::ProseStyle style, Rc<Text::Model> text, OnChange<Text::Action> onChange);
Child input(Text::ProseStyle, String text, OnChange<String> onChange);
Child input(Rc<Text::Model> text, OnChange<Text::Action> onChange);
// MARK: Slider ----------------------------------------------------------------
Child slider(f64 value, OnChange<f64> onChange, Child child);
static inline auto slider(f64 value, OnChange<f64> onChange) {
return [value, onChange = std::move(onChange)](Child child) mutable {
return slider(value, std::move(onChange), std::move(child));
};
}
// MARK: Intent ----------------------------------------------------------------
using Filter = Func<void(Node&, App::Event& e)>;
Child intent(Filter map, Child child);
static inline auto intent(Filter filter) {
return [filter = std::move(filter)](Child child) mutable {
return intent(std::move(filter), std::move(child));
};
}
} // namespace Karm::Ui