/
CanvasHandler.h
705 lines (561 loc) · 16.5 KB
/
CanvasHandler.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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
Copyright 2016 Nidium Inc. All rights reserved.
Use of this source code is governed by a MIT license
that can be found in the LICENSE file.
*/
#ifndef graphics_canvashandler_h__
#define graphics_canvashandler_h__
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <jsapi.h>
#include "Core/Events.h"
#include "Graphics/Geometry.h"
#include <Yoga.h>
#include <YGStringEnums.h>
#ifndef NAN
static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};
#define NAN (*(const float *) __nan)
#endif
/*
- Handle a canvas layer.
- Agnostic to any renderer.
- All size are in logical pixels (device ratio is handled by CanvasContext)
*/
namespace Nidium {
namespace Interface {
class UIInterface;
}
namespace Binding {
class JSCanvas;
}
namespace Frontend {
class Context;
class InputEvent;
}
namespace Graphics {
class CanvasHandler;
class SkiaContext;
class CanvasContext;
struct ComposeContext
{
CanvasHandler *handler;
float left;
float top;
double opacity;
double zoom;
bool needClip;
Rect clip;
};
struct LayerizeContext
{
CanvasHandler *m_Layer;
float m_pLeft;
float m_pTop;
double m_aOpacity;
double m_aZoom;
Rect *m_Clip;
void reset()
{
m_Layer = NULL;
m_pLeft = 0.;
m_pTop = 0.;
m_aOpacity = 1.0;
m_aZoom = 1.0;
m_Clip = NULL;
}
};
#define CANVAS_DEF_CLASS_PROPERTY(name, type, default_value, state) \
CanvasProperty<type> p_##name = {#name, default_value, CanvasProperty<type>::state, this}; \
virtual void setProp##name(type value) { \
this->p_##name.set(value); \
} \
virtual type getProp##name() { \
return this->p_##name.get(); \
}
class CanvasHandlerBase
{
private:
/*
This need to be initialized before properties
*/
std::vector<void *> m_PropertyList;
public:
/*
* This class holds two different value for the property
* - The 'computed' value : the actual value used for the layout
* - the 'user' value : the value set by the user */
template <typename T>
class CanvasProperty
{
public:
enum class State {
kDefault,
kSet,
kInherit,
kUndefined
};
static constexpr float kUndefined_Value = NAN;
CanvasProperty(const char *name, T val, State state, CanvasHandlerBase *h) :
m_Name(name), m_Canvas(h), m_Value(val), m_CachedValue(val) {
position = m_Canvas->m_PropertyList.size();
m_Canvas->m_PropertyList.push_back((void *)this);
};
inline T get() const {
#if 0
if (m_State == State::kInherit) {
if (m_Canvas->getParentBase()) {
CanvasProperty<T> *ref =
static_cast<CanvasProperty<T> *>
(m_Canvas->getParentBase()->m_PropertyList.at(position));
return ref->get();
}
}
#endif
return m_Value;
}
inline T getCachedValue() const {
return m_CachedValue;
}
inline operator T() const {
return get();
}
/* Change the computed value */
inline void set(T val) {
m_Value = val;
}
inline void setCachedValue(T val) {
m_CachedValue = val;
}
inline CanvasProperty<T> operator=(const T& val) {
set(val);
return *this;
}
void setInherit() {
m_State = State::kInherit;
}
void reset() {
m_State = State::kDefault;
}
void setIsPercentageValue(bool val) {
m_IsPercentage = val;
}
bool isPercentageValue() const {
return m_IsPercentage;
}
private:
/* Used for debug purpose
* TODO: ifdef DEBUG */
const char *m_Name;
CanvasHandlerBase *m_Canvas;
/* Actual value used for computation */
T m_Value;
T m_CachedValue;
bool m_IsPercentage = false;
State m_State = State::kDefault;
/* Position of the property in the Canvas properyList
* This is used in order to lookup for parent same property */
int position;
};
CANVAS_DEF_CLASS_PROPERTY(Right, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Bottom, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Top, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Left, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Width, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Height, float, NAN, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(MinWidth, float, -1, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(MinHeight, float, -1, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(MaxWidth, float, 0, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(MaxHeight, float, 0, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Coating, float, 0, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(EventReceiver,bool, true, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Display, bool, true, State::kDefault);
CANVAS_DEF_CLASS_PROPERTY(Opacity, float, 1.0, State::kDefault);
virtual CanvasHandlerBase *getParentBase()=0;
};
// {{{ CanvasHandler
class CanvasHandler : public CanvasHandlerBase, public Core::Events
{
private:
/*
This need to be initialized before properties
*/
std::vector<void *> m_PropertyList;
public:
friend class SkiaContext;
friend class Nidium::Frontend::Context;
friend class Binding::JSCanvas;
static const uint8_t EventID = 1;
enum COORD_MODE
{
kLeft_Coord = 1 << 0,
kRight_Coord = 1 << 1,
kTop_Coord = 1 << 2,
kBottom_Coord = 1 << 3
};
enum Flags
{
kDrag_Flag = 1 << 0
};
enum EventsChangedProperty
{
kContentHeight_Changed,
kContentWidth_Changed
};
enum Events
{
RESIZE_EVENT = 1,
LOADED_EVENT,
CHANGE_EVENT,
MOUSE_EVENT,
DRAG_EVENT,
PAINT_EVENT,
MOUNT_EVENT,
UNMOUNT_EVENT
};
enum Position
{
POSITION_FRONT,
POSITION_BACK
};
enum COORD_POSITION
{
COORD_RELATIVE,
COORD_ABSOLUTE,
COORD_FIXED,
COORD_DEFAULT
};
enum Visibility
{
CANVAS_VISIBILITY_VISIBLE,
CANVAS_VISIBILITY_HIDDEN
};
CanvasContext *m_Context;
JS::TenuredHeap<JSObject *> m_JsObj;
JSContext *m_JsCx;
struct
{
float top;
float right;
float bottom;
float left;
} m_Margin;
struct
{
float top;
float right;
float bottom;
float left;
} m_Padding;
struct
{
int width;
int height;
int scrollTop;
int scrollLeft;
int _width, _height;
} m_Content;
struct
{
int x, y, xrel, yrel;
bool consumed;
} m_MousePosition;
CanvasContext *getContext() const
{
return m_Context;
}
double getZoom() const
{
return m_Zoom;
}
inline float getPropLeftAbsolute()
{
return p_Left.getCachedValue();
}
inline float getPropTopAbsolute()
{
return p_Top.getCachedValue();
}
float getTopScrolled()
{
float top = getPropTop();
if (m_CoordPosition == COORD_RELATIVE && m_Parent != NULL) {
top -= m_Parent->m_Content.scrollTop;
}
return top;
}
float getLeftScrolled()
{
float left = getPropLeft();
if (m_CoordPosition == COORD_RELATIVE && m_Parent != NULL) {
left -= m_Parent->m_Content.scrollLeft;
}
return left;
}
/*
Get the real dimensions computed by Yoga
*/
bool getDimensions(float *width, float *height,
float *left = nullptr, float *top = nullptr)
{
*width = YGNodeLayoutGetWidth(m_YogaRef);
*height = YGNodeLayoutGetHeight(m_YogaRef);
if (isnan(*width) || isnan(*height)) {
return false;
}
if (left) {
*left = YGNodeLayoutGetLeft(m_YogaRef);
if (isnan(*left)) {
return false;
}
}
if (top) {
*top = YGNodeLayoutGetTop(m_YogaRef);
if (isnan(*top)) {
return false;
}
}
return true;
}
inline float getComputedTop() const {
return YGNodeLayoutGetTop(m_YogaRef);
}
inline float getComputedLeft() const {
return YGNodeLayoutGetLeft(m_YogaRef);
}
inline float getComputedRight() const {
return YGNodeLayoutGetRight(m_YogaRef);
}
inline float getComputedBottom() const {
return YGNodeLayoutGetBottom(m_YogaRef);
}
inline float getComputedWidth() const {
return YGNodeLayoutGetWidth(m_YogaRef);
}
inline float getComputedHeight() const {
return YGNodeLayoutGetHeight(m_YogaRef);
}
inline float getComputedAbsoluteLeft() const {
return p_Left.getCachedValue();
}
inline float getComputedAbsoluteTop() const {
return p_Top.getCachedValue();
}
Frontend::Context *getNidiumContext() const
{
return m_NidiumContext;
}
void setMargin(float top, float right, float bottom, float left)
{
YGNodeStyleSetMargin(m_YogaRef, YGEdgeTop, top);
YGNodeStyleSetMargin(m_YogaRef, YGEdgeRight, right);
YGNodeStyleSetMargin(m_YogaRef, YGEdgeBottom, bottom);
YGNodeStyleSetMargin(m_YogaRef, YGEdgeLeft, left);
m_Margin.top = top;
m_Margin.right = right;
m_Margin.bottom = bottom;
m_Margin.left = left;
}
void setPadding(float top, float right, float bottom, float left)
{
YGNodeStyleSetPadding(m_YogaRef, YGEdgeTop, top);
YGNodeStyleSetPadding(m_YogaRef, YGEdgeRight, right);
YGNodeStyleSetPadding(m_YogaRef, YGEdgeBottom, bottom);
YGNodeStyleSetPadding(m_YogaRef, YGEdgeLeft, left);
m_Padding.top = top;
m_Padding.right = right;
m_Padding.bottom = bottom;
m_Padding.left = left;
}
void setPropDisplay(bool state) override
{
p_Display.set(state);
YGNodeStyleSetDisplay(m_YogaRef, state ? YGDisplayFlex : YGDisplayNone);
}
void setPropLeft(float val) override
{
p_Left.set(val);
if (p_Left.isPercentageValue()) {
YGNodeStyleSetPositionPercent(m_YogaRef, YGEdgeLeft, isnan(val) ? YGUndefined : val);
} else {
YGNodeStyleSetPosition(m_YogaRef, YGEdgeLeft, isnan(val) ? YGUndefined : val);
}
}
void setPropTop(float val) override
{
p_Top.set(val);
if (p_Top.isPercentageValue()) {
YGNodeStyleSetPositionPercent(m_YogaRef, YGEdgeTop, isnan(val) ? YGUndefined : val);
} else {
YGNodeStyleSetPosition(m_YogaRef, YGEdgeTop, isnan(val) ? YGUndefined : val);
}
}
void setPropRight(float val) override
{
p_Right.set(val);
if (p_Right.isPercentageValue()) {
YGNodeStyleSetPositionPercent(m_YogaRef, YGEdgeRight, isnan(val) ? YGUndefined : val);
} else {
YGNodeStyleSetPosition(m_YogaRef, YGEdgeRight, isnan(val) ? YGUndefined : val);
}
}
void setPropBottom(float val) override
{
p_Bottom.set(val);
if (p_Bottom.isPercentageValue()) {
YGNodeStyleSetPositionPercent(m_YogaRef, YGEdgeBottom, isnan(val) ? YGUndefined : val);
} else {
YGNodeStyleSetPosition(m_YogaRef, YGEdgeBottom, isnan(val) ? YGUndefined : val);
}
}
void setOverflow(bool state) {
m_Overflow = state;
/* TODO: We should set YGOverflowScroll only if the view is scrollable */
YGNodeStyleSetOverflow(m_YogaRef, state ? YGOverflowScroll : YGOverflowVisible);
}
bool canOverflow() const {
return m_Overflow;
}
void setPropCoating(float value) override;
void setScale(double x, double y);
void setId(const char *str);
double getScaleX() const
{
return m_ScaleX;
}
double getScaleY() const
{
return m_ScaleY;
}
uint64_t getIdentifier(char **str = NULL)
{
if (str != NULL) {
*str = m_Identifier.str;
}
return m_Identifier.idx;
}
void setAllowNegativeScroll(bool val)
{
m_AllowNegativeScroll = val;
}
bool getAllowNegativeScroll() const
{
return m_AllowNegativeScroll;
}
COORD_POSITION getPositioning() const
{
return m_CoordPosition;
}
CanvasHandler(float width,
float height,
Frontend::Context *nctx,
bool lazyLoad = false);
virtual ~CanvasHandler();
void unrootHierarchy();
void setContext(CanvasContext *context);
bool setWidth(float width, bool force = false);
bool setHeight(float height, bool force = false);
void setPropMinWidth(float width) override;
void setPropMinHeight(float height) override;
void setPropMaxWidth(float width) override;
void setPropMaxHeight(float height) override;
void setSize(float width, float height, bool redraw = true);
void setPositioning(CanvasHandler::COORD_POSITION mode);
void setScrollTop(int value);
void setScrollLeft(int value);
void computeAbsolutePosition();
bool isOutOfBound();
Rect getViewport();
Rect getVisibleRect();
void bringToFront();
void sendToBack();
void addChild(CanvasHandler *insert,
CanvasHandler::Position position = POSITION_FRONT);
void insertBefore(CanvasHandler *insert, CanvasHandler *ref);
void insertAfter(CanvasHandler *insert, CanvasHandler *ref);
int getContentWidth();
int getContentHeight();
void setHidden(bool val);
bool isDisplayed() const;
bool isHidden() const;
bool hasAFixedAncestor() const;
void setPropOpacity(float val) override;
void setZoom(double val);
void removeFromParent(bool willBeAdopted = false);
void getChildren(CanvasHandler **out) const;
bool checkLoaded(bool async = false);
void setCursor(int cursor);
int getCursor();
void invalidate()
{
m_NeedPaint = true;
}
CanvasHandler *getParent() const
{
return m_Parent;
}
CanvasHandlerBase *getParentBase() override
{
return m_Parent;
}
CanvasHandler *getFirstChild() const
{
return m_Children;
}
CanvasHandler *getLastChild() const
{
return m_Last;
}
CanvasHandler *getNextSibling() const
{
return m_Next;
}
CanvasHandler *getPrevSibling() const
{
return m_Prev;
}
int32_t countChildren() const;
bool containsPoint(float x, float y);
void layerize(LayerizeContext &layerContext,
std::vector<ComposeContext> &compList, bool draw);
CanvasHandler *m_Parent;
CanvasHandler *m_Children;
CanvasHandler *m_Next;
CanvasHandler *m_Prev;
CanvasHandler *m_Last;
bool _handleEvent(Frontend::InputEvent *ev);
uint32_t m_Flags;
void computeLayoutPositions();
protected:
void paint();
void propertyChanged(EventsChangedProperty property);
private:
void deviceSetSize(float width, float height);
void onMouseEvent(Frontend::InputEvent *ev);
void onDrag(Frontend::InputEvent *ev, CanvasHandler *target, bool end = false);
void onDrop(Frontend::InputEvent *ev, CanvasHandler *droped);
int32_t m_nChildren;
void dispatchMouseEvents(LayerizeContext &layerContext);
COORD_POSITION m_CoordPosition;
Visibility m_Visibility;
double m_Zoom;
double m_ScaleX, m_ScaleY;
bool m_AllowNegativeScroll;
Frontend::Context *m_NidiumContext;
struct
{
uint64_t idx;
char *str;
} m_Identifier;
void recursiveScale(double x, double y, double oldX, double oldY);
bool m_Overflow;
bool m_Loaded;
int m_Cursor;
bool m_NeedPaint = true;
/* Reference to the Yoga node */
YGNodeRef m_YogaRef;
};
} // namespace Graphics
} // namespace Nidium
#endif