-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathinput.cpp
510 lines (429 loc) · 13.2 KB
/
input.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "input.h"
#include "drag.h"
#include "focus.h"
#include "funcs.h"
#include "layout.h"
#include "view.h"
namespace Karm::Ui {
// MARK: Button -----------------------------------------------------------------
ButtonStyle ButtonStyle::none() {
return {};
}
ButtonStyle ButtonStyle::regular(Gfx::ColorRamp ramp) {
return {
.idleStyle = {
.borderRadii = RADIUS,
.backgroundFill = ramp[8],
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = ramp[7],
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = ramp[7],
.backgroundFill = ramp[8],
},
};
}
ButtonStyle ButtonStyle::secondary() {
return {
.idleStyle = {
.borderRadii = RADIUS,
.backgroundFill = GRAY900,
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = GRAY800,
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = GRAY800,
.backgroundFill = GRAY900,
},
};
}
ButtonStyle ButtonStyle::primary() {
return {
.idleStyle = {
.borderRadii = RADIUS,
.backgroundFill = ACCENT700,
.foregroundFill = Gfx::WHITE,
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = ACCENT600,
.foregroundFill = Gfx::WHITE,
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = ACCENT600,
.backgroundFill = ACCENT700,
.foregroundFill = Gfx::WHITE,
},
};
}
ButtonStyle ButtonStyle::outline() {
return {
.idleStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = GRAY800,
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = GRAY700,
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = GRAY700,
.backgroundFill = GRAY800,
},
};
}
ButtonStyle ButtonStyle::subtle() {
return {
.idleStyle = {
.foregroundFill = GRAY300,
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = GRAY700,
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = GRAY700,
.backgroundFill = GRAY800,
},
};
}
ButtonStyle ButtonStyle::text() {
return {
.idleStyle = {
.foregroundFill = GRAY300,
},
.pressStyle = {
.foregroundFill = GRAY300,
},
};
}
ButtonStyle ButtonStyle::destructive() {
return {
.idleStyle = {
.borderRadii = RADIUS,
.foregroundFill = Gfx::RED500,
},
.hoverStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.backgroundFill = Gfx::RED600,
},
.pressStyle = {
.borderRadii = RADIUS,
.borderWidth = 1,
.borderFill = Gfx::RED600,
.backgroundFill = Gfx::RED700,
},
};
}
ButtonStyle ButtonStyle::withRadii(Math::Radiif radii) const {
return {
idleStyle.withRadii(radii),
hoverStyle.withRadii(radii),
pressStyle.withRadii(radii),
};
}
ButtonStyle ButtonStyle::withForegroundFill(Gfx::Fill fill) const {
return {
idleStyle.withForegroundFill(fill),
hoverStyle.withForegroundFill(fill),
pressStyle.withForegroundFill(fill),
};
}
ButtonStyle ButtonStyle::withPadding(Math::Insetsi insets) const {
return {
idleStyle.withPadding(insets),
hoverStyle.withPadding(insets),
pressStyle.withPadding(insets),
};
}
ButtonStyle ButtonStyle::withMargin(Math::Insetsi insets) const {
return {
idleStyle.withMargin(insets),
hoverStyle.withMargin(insets),
pressStyle.withMargin(insets),
};
}
struct Button : public _Box<Button> {
OnPress _onPress;
ButtonStyle _buttonStyle = ButtonStyle::regular();
MouseListener _mouseListener;
Button(OnPress onPress, ButtonStyle style, Child child)
: _Box<Button>(child),
_onPress(std::move(onPress)),
_buttonStyle(style) {}
void reconcile(Button& o) override {
_buttonStyle = o._buttonStyle;
_onPress = std::move(o._onPress);
if (not _onPress) {
// Reset the mouse listener if the button is disabled.
_mouseListener = {};
}
_Box<Button>::reconcile(o);
}
BoxStyle& boxStyle() override {
if (not _onPress) {
return _buttonStyle.disabledStyle;
} else if (_mouseListener.isIdle()) {
return _buttonStyle.idleStyle;
} else if (_mouseListener.isHover()) {
return _buttonStyle.hoverStyle;
} else {
return _buttonStyle.pressStyle;
}
}
void event(App::Event& e) override {
_Box<Button>::event(e);
if (e.accepted())
return;
if (_onPress and _mouseListener.listen(*this, e)) {
_onPress(*this);
}
};
};
Child button(OnPress onPress, ButtonStyle style, Child child) {
return makeRc<Button>(std::move(onPress), style, child);
}
Child button(OnPress onPress, ButtonStyle style, Str t) {
return text(t) |
insets({6, 16}) |
center() |
minSize({UNCONSTRAINED, 36}) |
button(std::move(onPress), style);
}
Child button(OnPress onPress, ButtonStyle style, Gfx::Icon i) {
return icon(i) |
insets(6) |
center() |
minSize({36, 36}) |
button(std::move(onPress), style);
}
Child button(OnPress onPress, ButtonStyle style, Gfx::Icon i, Str t) {
return hflow(8, Math::Align::CENTER, icon(i), text(t)) |
insets({6, 16, 6, 12}) |
minSize({UNCONSTRAINED, 36}) |
button(std::move(onPress), style);
}
Child button(OnPress onPress, Child child) {
return button(std::move(onPress), ButtonStyle::regular(), child);
}
Child button(OnPress onPress, Str t) {
return button(std::move(onPress), ButtonStyle::regular(), t);
}
Child button(OnPress onPress, Mdi::Icon i) {
return button(std::move(onPress), ButtonStyle::regular(), i);
}
Child button(OnPress onPress, Mdi::Icon i, Str t) {
return button(std::move(onPress), ButtonStyle::regular(), i, t);
}
// MARK: Input -----------------------------------------------------------------
struct Input : public View<Input> {
Text::ProseStyle _style;
Rc<Text::Model> _model;
OnChange<Text::Action> _onChange;
Opt<Rc<Text::Prose>> _text;
Input(Text::ProseStyle style, Rc<Text::Model> model, OnChange<Text::Action> onChange)
: _style(style), _model(model), _onChange(std::move(onChange)) {}
void reconcile(Input& o) override {
_style = o._style;
_model = o._model;
_onChange = std::move(o._onChange);
// NOTE: The model might have changed,
// so we need to invalidate the presentation.
_text = NONE;
}
Text::Prose& _ensureText() {
if (not _text) {
_text = makeRc<Text::Prose>(_style);
(*_text)->append(_model->runes());
}
return **_text;
}
void paint(Gfx::Canvas& g, Math::Recti) override {
g.push();
g.clip(bound());
g.origin(bound().xy.cast<f64>());
auto& text = _ensureText();
text.paintCaret(g, _model->_cur.head, _style.color.unwrapOr(Ui::GRAY100));
g.fill(text);
g.pop();
}
void event(App::Event& e) override {
auto a = Text::Action::fromEvent(e);
if (a) {
e.accept();
_onChange(*this, *a);
}
}
void layout(Math::Recti bound) override {
_ensureText().layout(Au{bound.width});
View<Input>::layout(bound);
}
Math::Vec2i size(Math::Vec2i s, Hint) override {
auto size = _ensureText().layout(Au{s.width});
return size.ceil().cast<isize>();
}
};
Child input(Text::ProseStyle style, Rc<Text::Model> text, OnChange<Text::Action> onChange) {
return makeRc<Input>(style, text, std::move(onChange));
}
Child input(Rc<Text::Model> text, OnChange<Text::Action> onChange) {
return makeRc<Input>(TextStyles::bodyMedium(), text, std::move(onChange));
}
struct SimpleInput : public View<SimpleInput> {
Text::ProseStyle _style;
String _text;
OnChange<String> _onChange;
FocusListener _focus;
Opt<Text::Model> _model;
Opt<Rc<Text::Prose>> _prose;
SimpleInput(Text::ProseStyle style, String text, OnChange<String> onChange)
: _style(style),
_text(text),
_onChange(std::move(onChange)) {}
void reconcile(SimpleInput& o) override {
_style = o._style;
_model = o._model;
_onChange = std::move(o._onChange);
// NOTE: The model might have changed,
// so we need to invalidate the presentation.
_prose = NONE;
}
Text::Model& _ensureModel() {
if (not _model)
_model = Text::Model(_text);
return *_model;
}
Text::Prose& _ensureText() {
if (not _prose) {
_prose = makeRc<Text::Prose>(_style);
(*_prose)->append(_ensureModel().runes());
}
return **_prose;
}
void paint(Gfx::Canvas& g, Math::Recti) override {
g.push();
g.clip(bound());
g.origin(bound().xy.cast<f64>());
auto& text = _ensureText();
if (_focus)
text.paintCaret(g, _ensureModel()._cur.head, _style.color.unwrapOr(Ui::GRAY100));
g.fill(text);
g.pop();
}
void event(App::Event& e) override {
_focus.event(*this, e);
auto a = Text::Action::fromEvent(e);
if (a) {
e.accept();
_ensureModel().reduce(*a);
_text = _ensureModel().string();
_prose = NONE;
if (_onChange)
_onChange(*this, _text);
else
Ui::shouldLayout(*this);
}
}
void layout(Math::Recti bound) override {
_ensureText().layout(Au{bound.width});
View<SimpleInput>::layout(bound);
}
Math::Vec2i size(Math::Vec2i s, Hint) override {
auto size = _ensureText().layout(Au{s.width});
return size.ceil().cast<isize>();
}
};
Child input(Text::ProseStyle style, String text, OnChange<String> onChange) {
return makeRc<SimpleInput>(style, text, std::move(onChange));
}
// MARK: Slider -----------------------------------------------------------------
struct Slider : public ProxyNode<Slider> {
f64 _value = 0.0f;
OnChange<f64> _onChange;
Math::Recti _bound;
Slider(f64 value, OnChange<f64> onChange, Child child)
: ProxyNode<Slider>(std::move(child)),
_value(value),
_onChange(std::move(onChange)) {
}
void reconcile(Slider& o) override {
_value = o._value;
_onChange = o._onChange;
ProxyNode<Slider>::reconcile(o);
}
void layout(Math::Recti r) override {
_bound = r;
child().layout(_bound.hsplit(((r.width - r.height) * _value) + r.height).v0);
}
Math::Recti bound() override {
return _bound;
}
void bubble(App::Event& e) override {
if (auto dv = e.is<DragEvent>()) {
if (dv->type == DragEvent::DRAG) {
auto max = bound().width - bound().height;
auto value = max * _value;
value = clamp(value + dv->delta.x, 0.0f, max);
_value = value / max;
if (_onChange) {
_onChange(*this, _value);
} else {
child().layout(_bound.hsplit(((_bound.width - _bound.height) * _value) + _bound.height).v0);
shouldRepaint(*this);
}
}
e.accept();
}
ProxyNode<Slider>::bubble(e);
}
};
Child slider(f64 value, OnChange<f64> onChange, Child child) {
return makeRc<Slider>(value, std::move(onChange), std::move(child));
}
// MARK: Intent ----------------------------------------------------------------
struct Intent : public ProxyNode<Intent> {
Func<void(Node&, App::Event& e)> _map;
Intent(Func<void(Node&, App::Event& e)> map, Child child)
: ProxyNode<Intent>(std::move(child)), _map(std::move(map)) {}
void reconcile(Intent& o) override {
_map = std::move(o._map);
ProxyNode<Intent>::reconcile(o);
}
void event(App::Event& e) override {
if (e.accepted())
return;
_map(*this, e);
ProxyNode<Intent>::event(e);
}
void bubble(App::Event& e) override {
if (e.accepted())
return;
_map(*this, e);
ProxyNode<Intent>::bubble(e);
}
};
Child intent(Func<void(Node&, App::Event& e)> map, Child child) {
return makeRc<Intent>(std::move(map), std::move(child));
}
} // namespace Karm::Ui