forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueUnit.h
More file actions
65 lines (55 loc) · 1.36 KB
/
Copy pathValueUnit.h
File metadata and controls
65 lines (55 loc) · 1.36 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#ifdef RN_SERIALIZABLE_STATE
#include <folly/dynamic.h>
#endif
namespace facebook::react {
enum class UnitType {
Undefined,
Point,
Percent,
};
struct ValueUnit {
float value{0.0f};
UnitType unit{UnitType::Undefined};
ValueUnit() = default;
ValueUnit(float v, UnitType u) : value(v), unit(u) {}
bool operator==(const ValueUnit& other) const {
return value == other.value && unit == other.unit;
}
bool operator!=(const ValueUnit& other) const {
return !(*this == other);
}
constexpr float resolve(float referenceLength) const {
switch (unit) {
case UnitType::Point:
return value;
case UnitType::Percent:
return value * referenceLength * 0.01f;
case UnitType::Undefined:
default:
return 0.0f;
}
}
constexpr operator bool() const {
return unit != UnitType::Undefined;
}
#ifdef RN_SERIALIZABLE_STATE
folly::dynamic toDynamic() const {
switch (unit) {
case UnitType::Undefined:
return nullptr;
case UnitType::Point:
return value;
case UnitType::Percent:
return std::format("{}%", value);
}
}
#endif
};
} // namespace facebook::react