react-native-nitro-input 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (187) hide show
  1. package/CHANGELOG.md +63 -0
  2. package/LICENSE +21 -0
  3. package/NitroInput.podspec +54 -0
  4. package/README.md +524 -0
  5. package/android/CMakeLists.txt +52 -0
  6. package/android/build.gradle +158 -0
  7. package/android/fix-prefab.gradle +51 -0
  8. package/android/gradle.properties +4 -0
  9. package/android/src/main/AndroidManifest.xml +2 -0
  10. package/android/src/main/cpp/JAmountFormatter.cpp +70 -0
  11. package/android/src/main/cpp/JAmountFormatter.hpp +45 -0
  12. package/android/src/main/cpp/JMaskEngine.cpp +81 -0
  13. package/android/src/main/cpp/JMaskEngine.hpp +52 -0
  14. package/android/src/main/cpp/JMorphEngine.cpp +132 -0
  15. package/android/src/main/cpp/JMorphEngine.hpp +54 -0
  16. package/android/src/main/cpp/JNitroInputWorklets.cpp +83 -0
  17. package/android/src/main/cpp/JNitroInputWorklets.hpp +40 -0
  18. package/android/src/main/cpp/JOutlineGeometry.cpp +58 -0
  19. package/android/src/main/cpp/JOutlineGeometry.hpp +55 -0
  20. package/android/src/main/cpp/cpp-adapter.cpp +19 -0
  21. package/android/src/main/java/com/margelo/nitro/nitroinput/AmountFormatter.kt +35 -0
  22. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +730 -0
  23. package/android/src/main/java/com/margelo/nitro/nitroinput/MaskEngine.kt +50 -0
  24. package/android/src/main/java/com/margelo/nitro/nitroinput/MorphEngine.kt +47 -0
  25. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputPackage.kt +24 -0
  26. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +2176 -0
  27. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputWorklets.kt +34 -0
  28. package/android/src/main/java/com/margelo/nitro/nitroinput/OutlineGeometry.kt +71 -0
  29. package/cpp/AmountFormatter.cpp +346 -0
  30. package/cpp/AmountFormatter.hpp +88 -0
  31. package/cpp/MaskEngine.cpp +596 -0
  32. package/cpp/MaskEngine.hpp +136 -0
  33. package/cpp/MorphEngine.cpp +904 -0
  34. package/cpp/MorphEngine.hpp +213 -0
  35. package/cpp/OutlineGeometry.cpp +159 -0
  36. package/cpp/OutlineGeometry.hpp +107 -0
  37. package/cpp/worklets/HybridNitroInputWorklets.cpp +30 -0
  38. package/cpp/worklets/HybridNitroInputWorklets.hpp +30 -0
  39. package/cpp/worklets/NitroInputWorkletsBridge.cpp +203 -0
  40. package/cpp/worklets/NitroInputWorkletsBridge.hpp +52 -0
  41. package/cpp/worklets/NitroInputWorkletsBridgeJSI.hpp +23 -0
  42. package/ios/HybridMorphInputView.swift +710 -0
  43. package/ios/NitroInputCommands.mm +66 -0
  44. package/ios/NitroInputView.swift +2600 -0
  45. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
  46. package/lib/commonjs/src/MorphInput.js +42 -0
  47. package/lib/commonjs/src/NitroInput.js +623 -0
  48. package/lib/commonjs/src/index.js +12 -0
  49. package/lib/commonjs/src/specs/NitroInput.nitro.js +2 -0
  50. package/lib/commonjs/src/specs/NitroInputWorklets.nitro.js +2 -0
  51. package/lib/commonjs/src/useNitroInputState.js +58 -0
  52. package/lib/commonjs/src/worklets.js +226 -0
  53. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
  54. package/lib/module/package.json +1 -0
  55. package/lib/module/src/MorphInput.js +6 -0
  56. package/lib/module/src/NitroInput.js +584 -0
  57. package/lib/module/src/index.js +4 -0
  58. package/lib/module/src/specs/NitroInput.nitro.js +1 -0
  59. package/lib/module/src/specs/NitroInputWorklets.nitro.js +1 -0
  60. package/lib/module/src/useNitroInputState.js +55 -0
  61. package/lib/module/src/worklets.js +214 -0
  62. package/lib/typescript/src/MorphInput.d.ts +18 -0
  63. package/lib/typescript/src/NitroInput.d.ts +411 -0
  64. package/lib/typescript/src/index.d.ts +6 -0
  65. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +390 -0
  66. package/lib/typescript/src/specs/NitroInputWorklets.nitro.d.ts +15 -0
  67. package/lib/typescript/src/useNitroInputState.d.ts +55 -0
  68. package/lib/typescript/src/worklets.d.ts +117 -0
  69. package/nitro.json +37 -0
  70. package/nitrogen/generated/.gitattributes +1 -0
  71. package/nitrogen/generated/android/NitroInput+autolinking.cmake +84 -0
  72. package/nitrogen/generated/android/NitroInput+autolinking.gradle +27 -0
  73. package/nitrogen/generated/android/NitroInputOnLoad.cpp +78 -0
  74. package/nitrogen/generated/android/NitroInputOnLoad.hpp +34 -0
  75. package/nitrogen/generated/android/c++/JFunc_void_bool.hpp +75 -0
  76. package/nitrogen/generated/android/c++/JFunc_void_double.hpp +75 -0
  77. package/nitrogen/generated/android/c++/JFunc_void_double_double.hpp +75 -0
  78. package/nitrogen/generated/android/c++/JFunc_void_std__string.hpp +76 -0
  79. package/nitrogen/generated/android/c++/JFunc_void_std__string_double.hpp +76 -0
  80. package/nitrogen/generated/android/c++/JFunc_void_std__string_std__string_std__string_bool.hpp +76 -0
  81. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +1073 -0
  82. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +254 -0
  83. package/nitrogen/generated/android/c++/JNitroInputAffixAlign.hpp +64 -0
  84. package/nitrogen/generated/android/c++/JNitroInputAutoCapitalize.hpp +64 -0
  85. package/nitrogen/generated/android/c++/JNitroInputEasing.hpp +67 -0
  86. package/nitrogen/generated/android/c++/JNitroInputEffect.hpp +61 -0
  87. package/nitrogen/generated/android/c++/JNitroInputKeyboardAppearance.hpp +61 -0
  88. package/nitrogen/generated/android/c++/JNitroInputKeyboardType.hpp +79 -0
  89. package/nitrogen/generated/android/c++/JNitroInputLabelBehavior.hpp +58 -0
  90. package/nitrogen/generated/android/c++/JNitroInputMode.hpp +61 -0
  91. package/nitrogen/generated/android/c++/JNitroInputNotation.hpp +65 -0
  92. package/nitrogen/generated/android/c++/JNitroInputReturnKeyType.hpp +70 -0
  93. package/nitrogen/generated/android/c++/JNitroInputSignPlacement.hpp +58 -0
  94. package/nitrogen/generated/android/c++/JNitroInputSubmitBehavior.hpp +61 -0
  95. package/nitrogen/generated/android/c++/JNitroInputTextAlign.hpp +64 -0
  96. package/nitrogen/generated/android/c++/JNitroInputTextAlignVertical.hpp +64 -0
  97. package/nitrogen/generated/android/c++/JNitroInputVariant.hpp +61 -0
  98. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +529 -0
  99. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.hpp +54 -0
  100. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_bool.kt +78 -0
  101. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double.kt +78 -0
  102. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_double_double.kt +78 -0
  103. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string.kt +78 -0
  104. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string_double.kt +78 -0
  105. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/Func_void_std__string_std__string_std__string_bool.kt +78 -0
  106. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +711 -0
  107. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputAffixAlign.kt +25 -0
  108. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputAutoCapitalize.kt +25 -0
  109. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputEasing.kt +26 -0
  110. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputEffect.kt +24 -0
  111. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputKeyboardAppearance.kt +24 -0
  112. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputKeyboardType.kt +30 -0
  113. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputLabelBehavior.kt +23 -0
  114. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputMode.kt +24 -0
  115. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputNotation.kt +61 -0
  116. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputOnLoad.kt +35 -0
  117. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputReturnKeyType.kt +27 -0
  118. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSignPlacement.kt +23 -0
  119. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputSubmitBehavior.kt +24 -0
  120. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextAlign.kt +25 -0
  121. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputTextAlignVertical.kt +25 -0
  122. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroInputVariant.kt +24 -0
  123. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroInputViewManager.kt +96 -0
  124. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/views/HybridNitroInputViewStateUpdater.kt +23 -0
  125. package/nitrogen/generated/ios/NitroInput+autolinking.rb +62 -0
  126. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +81 -0
  127. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +348 -0
  128. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +92 -0
  129. package/nitrogen/generated/ios/NitroInputAutolinking.mm +43 -0
  130. package/nitrogen/generated/ios/NitroInputAutolinking.swift +26 -0
  131. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.cpp +11 -0
  132. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +769 -0
  133. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +690 -0
  134. package/nitrogen/generated/ios/swift/Func_void_bool.swift +46 -0
  135. package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
  136. package/nitrogen/generated/ios/swift/Func_void_double_double.swift +46 -0
  137. package/nitrogen/generated/ios/swift/Func_void_std__string.swift +46 -0
  138. package/nitrogen/generated/ios/swift/Func_void_std__string_double.swift +46 -0
  139. package/nitrogen/generated/ios/swift/Func_void_std__string_std__string_std__string_bool.swift +46 -0
  140. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +154 -0
  141. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +1454 -0
  142. package/nitrogen/generated/ios/swift/NitroInputAffixAlign.swift +48 -0
  143. package/nitrogen/generated/ios/swift/NitroInputAutoCapitalize.swift +48 -0
  144. package/nitrogen/generated/ios/swift/NitroInputEasing.swift +52 -0
  145. package/nitrogen/generated/ios/swift/NitroInputEffect.swift +44 -0
  146. package/nitrogen/generated/ios/swift/NitroInputKeyboardAppearance.swift +44 -0
  147. package/nitrogen/generated/ios/swift/NitroInputKeyboardType.swift +68 -0
  148. package/nitrogen/generated/ios/swift/NitroInputLabelBehavior.swift +40 -0
  149. package/nitrogen/generated/ios/swift/NitroInputMode.swift +44 -0
  150. package/nitrogen/generated/ios/swift/NitroInputNotation.swift +39 -0
  151. package/nitrogen/generated/ios/swift/NitroInputReturnKeyType.swift +56 -0
  152. package/nitrogen/generated/ios/swift/NitroInputSignPlacement.swift +40 -0
  153. package/nitrogen/generated/ios/swift/NitroInputSubmitBehavior.swift +44 -0
  154. package/nitrogen/generated/ios/swift/NitroInputTextAlign.swift +48 -0
  155. package/nitrogen/generated/ios/swift/NitroInputTextAlignVertical.swift +48 -0
  156. package/nitrogen/generated/ios/swift/NitroInputVariant.swift +44 -0
  157. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +213 -0
  158. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +300 -0
  159. package/nitrogen/generated/shared/c++/HybridNitroInputWorkletsSpec.cpp +21 -0
  160. package/nitrogen/generated/shared/c++/HybridNitroInputWorkletsSpec.hpp +62 -0
  161. package/nitrogen/generated/shared/c++/NitroInputAffixAlign.hpp +84 -0
  162. package/nitrogen/generated/shared/c++/NitroInputAutoCapitalize.hpp +84 -0
  163. package/nitrogen/generated/shared/c++/NitroInputEasing.hpp +88 -0
  164. package/nitrogen/generated/shared/c++/NitroInputEffect.hpp +80 -0
  165. package/nitrogen/generated/shared/c++/NitroInputKeyboardAppearance.hpp +80 -0
  166. package/nitrogen/generated/shared/c++/NitroInputKeyboardType.hpp +104 -0
  167. package/nitrogen/generated/shared/c++/NitroInputLabelBehavior.hpp +76 -0
  168. package/nitrogen/generated/shared/c++/NitroInputMode.hpp +80 -0
  169. package/nitrogen/generated/shared/c++/NitroInputNotation.hpp +91 -0
  170. package/nitrogen/generated/shared/c++/NitroInputReturnKeyType.hpp +92 -0
  171. package/nitrogen/generated/shared/c++/NitroInputSignPlacement.hpp +76 -0
  172. package/nitrogen/generated/shared/c++/NitroInputSubmitBehavior.hpp +80 -0
  173. package/nitrogen/generated/shared/c++/NitroInputTextAlign.hpp +84 -0
  174. package/nitrogen/generated/shared/c++/NitroInputTextAlignVertical.hpp +84 -0
  175. package/nitrogen/generated/shared/c++/NitroInputVariant.hpp +80 -0
  176. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +216 -0
  177. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +374 -0
  178. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +101 -0
  179. package/package.json +141 -0
  180. package/react-native.config.js +16 -0
  181. package/src/MorphInput.tsx +30 -0
  182. package/src/NitroInput.tsx +1364 -0
  183. package/src/index.ts +55 -0
  184. package/src/specs/NitroInput.nitro.ts +436 -0
  185. package/src/specs/NitroInputWorklets.nitro.ts +13 -0
  186. package/src/useNitroInputState.ts +101 -0
  187. package/src/worklets.ts +300 -0
@@ -0,0 +1,213 @@
1
+ //
2
+ // MorphEngine.hpp
3
+ // NitroInput
4
+ //
5
+ // The platform-independent text-morph state machine behind the input view.
6
+ // Both native views (NitroInputView.swift, NitroInputView.kt) hand it the new
7
+ // text as a list of glyphs (character, role, kind, advance width) plus where
8
+ // the edit happened, and draw whatever it reports: one glyph per character
9
+ // with a position, vertical slide, opacity and scale, including the glyphs
10
+ // that are still on their way out. No rendering, no fonts, no threading.
11
+ //
12
+ // Matching follows Torph (https://torph.lochie.me): an edit at a caret pairs
13
+ // the characters on either side of the caret by position (grouping separators
14
+ // aside, which reflow with the magnitude and pair from the units end); a value
15
+ // set without a caret pairs digits by place value and other characters by
16
+ // longest common subsequence. Characters that persist glide to their new
17
+ // place; new ones enter next to their neighbour; removed ones travel with
18
+ // their neighbour while they leave.
19
+ //
20
+ // Time is in seconds on any monotonic clock. Plain ints/doubles/strings so it
21
+ // imports into Swift (C++ interop), bridges over JNI and compiles to WebAssembly.
22
+ //
23
+
24
+ #pragma once
25
+
26
+ #include <cstdint>
27
+ #include <vector>
28
+
29
+ namespace margelo::nitro::nitroinput {
30
+
31
+ class MorphEngine final {
32
+ public:
33
+ /// What a character is; decides how it enters, leaves and is matched.
34
+ enum Kind : int {
35
+ Text = 0,
36
+ Digit = 1,
37
+ /// Grouping separator: pairs by distance from the units end; enters from below.
38
+ Separator = 2,
39
+ /// Decimal separator: part of the typed sequence; enters from below.
40
+ Decimal = 3,
41
+ };
42
+ /// Where a glyph sits. Prefix and suffix are static decorations around the editable body.
43
+ enum Role : int { Prefix = 0, Body = 1, Suffix = 2 };
44
+ // Easing: 0 expo (cubic-bezier 0.19, 1, 0.22, 1), 1 easeOut, 2 easeInOut, 3 linear, 4 spring.
45
+ // Effect: 0 auto (digits/separators slide, text fades), 1 slide, 2 fade.
46
+
47
+ struct Glyph {
48
+ /// Stable identity across updates; a renderer keeps one layer per id.
49
+ int64_t id;
50
+ /// UTF-32 code point.
51
+ uint32_t character;
52
+ int role;
53
+ int kind;
54
+ /// Advance width, in the caller's layout units.
55
+ double width;
56
+ /// Drawn in the placeholder colour.
57
+ bool placeholder;
58
+ /// Left edge, animated.
59
+ double x;
60
+ /// Vertical offset in line heights (the slide), 0 at rest.
61
+ double y;
62
+ /// 0…1.
63
+ double opacity;
64
+ /// About the glyph's centre.
65
+ double scale;
66
+ /// On its way out: drawn, but not part of the text.
67
+ bool exiting;
68
+ };
69
+
70
+ /// One glyph of a pending text (see `addGlyph`).
71
+ struct Input {
72
+ uint32_t character;
73
+ int role;
74
+ int kind;
75
+ double width;
76
+ bool placeholder;
77
+ };
78
+
79
+ MorphEngine();
80
+
81
+ // MARK: Configuration
82
+
83
+ void setTiming(double durationSeconds, int easing, double bounce);
84
+ void setEffect(int effect);
85
+ /// Reduce Motion / "remove animations": every change snaps.
86
+ void setReduceMotion(bool reduceMotion);
87
+ /// A right-to-left layout: the prefix moves to the right edge and the suffix
88
+ /// to the left, while the body stays a left-to-right run (digits read the
89
+ /// same way in every script). Applied to the published frames and `caretX`;
90
+ /// the layout itself is always computed left-to-right.
91
+ void setRightToLeft(bool rightToLeft);
92
+
93
+ // MARK: Text
94
+
95
+ /// Starts describing the new text. Add every glyph in order (prefix, body,
96
+ /// suffix), then commit.
97
+ void beginText();
98
+ void addGlyph(uint32_t character, int role, int kind, double width, bool placeholder);
99
+ /// `caretIndex`: the body index the edit ended at (caret matching), or -1
100
+ /// when the value was replaced (place matching). The first commit shows the
101
+ /// text without an animation.
102
+ void commitText(int caretIndex, double now);
103
+
104
+ // MARK: Frames
105
+
106
+ /// Advances every animation. Returns true when something is still moving.
107
+ bool tick(double now);
108
+ bool needsFrames() const;
109
+ bool isAnimating() const { return animating_; }
110
+
111
+ // MARK: Render state
112
+
113
+ /// Every glyph in draw order, exiting ones included.
114
+ const std::vector<Glyph>& glyphs() const { return glyphs_; }
115
+ int glyphCount() const { return static_cast<int>(glyphs_.size()); }
116
+ Glyph glyphAt(int index) const { return glyphs_[static_cast<size_t>(index)]; }
117
+ /// Width of the whole content (prefix + body + suffix), animated.
118
+ double contentWidth() const { return contentWidth_; }
119
+ /// Width the content settles at.
120
+ double targetWidth() const { return targetWidth_; }
121
+ /// Number of live body glyphs.
122
+ int bodyCount() const;
123
+ /// Left edge of the caret placed before body glyph `index` (`bodyCount()` =
124
+ /// after the last one), following the animation.
125
+ double caretX(int index) const;
126
+ bool hasText() const { return committed_; }
127
+
128
+ void reset();
129
+
130
+ private:
131
+ struct Slot {
132
+ Glyph g;
133
+ double start = 0;
134
+ double fromX = 0, toX = 0;
135
+ double fromY = 0, toY = 0;
136
+ double fromOpacity = 1, toOpacity = 1;
137
+ double fromScale = 1, toScale = 1;
138
+ /// Opacity ramps linearly between these fractions of the duration.
139
+ double fadeFrom = 0, fadeTo = 1;
140
+ /// A glyph entering or leaving rides along with this live glyph.
141
+ int64_t anchorId = -1;
142
+ double anchorBaseX = 0;
143
+ bool entering = false;
144
+ bool slide = false;
145
+ /// Part of a wholly-replaced run: scales about `groupCentre` instead of sliding.
146
+ bool grouped = false;
147
+ /// The run's centre, in layout units, shared by every member.
148
+ double groupCentre = 0;
149
+ };
150
+
151
+ /// Scratch for finding wholly-replaced runs, kept between commits so a
152
+ /// keystroke allocates nothing once the buffers have grown.
153
+ std::vector<size_t> runBuf_;
154
+ std::vector<unsigned char> groupFlag_;
155
+ std::vector<double> groupCentre_;
156
+
157
+
158
+ double ease(double t) const;
159
+ bool slides(int kind) const;
160
+ void snapTo(const std::vector<Input>& inputs);
161
+ void matchBody(const std::vector<Input>& inputs, const std::vector<int>& oldBody, const std::vector<int>& newBody,
162
+ int caretIndex, std::vector<int>& matchOfNew) const;
163
+ void matchByCaret(const std::vector<Input>& inputs, const std::vector<int>& oldBody, const std::vector<int>& newBody,
164
+ int caretIndex, std::vector<int>& matchOfNew) const;
165
+ void matchByPlace(const std::vector<Input>& inputs, const std::vector<int>& oldBody, const std::vector<int>& newBody,
166
+ std::vector<int>& matchOfNew) const;
167
+ void matchBySequence(const std::vector<Input>& inputs, const std::vector<int>& oldIdx, const std::vector<int>& newIdx,
168
+ std::vector<int>& matchOfNew) const;
169
+ bool same(const Input& in, const Slot& slot) const;
170
+ int slotIndexOf(int64_t id) const;
171
+ /// Copies the slots' frames into `glyphs_`, mirrored when right-to-left.
172
+ void publish();
173
+
174
+ /// The horizontal extent of one block of the run (see `Blocks` in the .cpp).
175
+ struct Extent {
176
+ double start = 0;
177
+ double end = 0;
178
+ bool any = false;
179
+ };
180
+ /// The two blocks a caret can sit in - a sign laid out ahead of the prefix,
181
+ /// and the rest of the body - measured over the live glyphs each time a
182
+ /// frame is published, so `caretX` under right-to-left neither allocates
183
+ /// nor re-measures the run on every call.
184
+ struct CaretBlocks {
185
+ Extent sign;
186
+ Extent rest;
187
+ };
188
+ CaretBlocks caretBlocks_;
189
+ /// Scratch for measuring the live glyphs in `publish`, kept so a frame
190
+ /// allocates nothing once it has grown.
191
+ std::vector<Glyph> liveBuf_;
192
+
193
+ double duration_ = 0.4;
194
+ int easing_ = 0;
195
+ double bounce_ = 0.15;
196
+ int effect_ = 0;
197
+ bool reduceMotion_ = false;
198
+ bool rightToLeft_ = false;
199
+
200
+ std::vector<Input> pending_;
201
+ std::vector<Slot> slots_;
202
+ std::vector<Glyph> glyphs_;
203
+ bool committed_ = false;
204
+ bool animating_ = false;
205
+ double now_ = 0;
206
+ double contentWidth_ = 0;
207
+ double targetWidth_ = 0;
208
+ double widthFrom_ = 0;
209
+ double widthStart_ = 0;
210
+ int64_t nextId_ = 1;
211
+ };
212
+
213
+ } // namespace margelo::nitro::nitroinput
@@ -0,0 +1,159 @@
1
+ //
2
+ // OutlineGeometry.cpp
3
+ // NitroInput
4
+ //
5
+
6
+ #include "OutlineGeometry.hpp"
7
+
8
+ #include <algorithm>
9
+ #include <cmath>
10
+
11
+ namespace margelo::nitro::nitroinput {
12
+
13
+ namespace {
14
+
15
+ double clamp01(double value) {
16
+ if (!(value > 0)) return 0; // also catches NaN
17
+ return value < 1 ? value : 1;
18
+ }
19
+
20
+ double finite(double value, double fallback) {
21
+ return std::isfinite(value) ? value : fallback;
22
+ }
23
+
24
+ } // namespace
25
+
26
+ OutlineGeometry::Rect OutlineGeometry::lerp(const Rect& from, const Rect& to, double progress) {
27
+ const double t = clamp01(finite(progress, 0));
28
+ return Rect{
29
+ from.x + (to.x - from.x) * t,
30
+ from.y + (to.y - from.y) * t,
31
+ from.width + (to.width - from.width) * t,
32
+ from.height + (to.height - from.height) * t,
33
+ };
34
+ }
35
+
36
+ OutlineGeometry::Gap OutlineGeometry::gapFor(const Rect& floatedLabel, double padding) {
37
+ Gap gap;
38
+ gap.left = finite(floatedLabel.x, 0);
39
+ gap.width = std::max(0.0, finite(floatedLabel.width, 0));
40
+ gap.padding = std::max(0.0, finite(padding, 0));
41
+ return gap;
42
+ }
43
+
44
+ namespace {
45
+
46
+ using Box = OutlineGeometry::Box;
47
+ using Gap = OutlineGeometry::Gap;
48
+ using Verb = OutlineGeometry::Verb;
49
+
50
+ /// Appends `Segment`s to a vector: the form iOS and the docs consume.
51
+ struct SegmentSink {
52
+ std::vector<OutlineGeometry::Segment>& path;
53
+ void move(double x, double y) { path.push_back({Verb::Move, {x, y}, 0, 0, 0}); }
54
+ void line(double x, double y) { path.push_back({Verb::Line, {x, y}, 0, 0, 0}); }
55
+ void arc(double cx, double cy, double r, double start, double sweep) {
56
+ path.push_back({Verb::Arc, {cx, cy}, r, start, sweep});
57
+ }
58
+ };
59
+
60
+ /// Appends `kFlatStride` doubles per segment: the form the Android bridge
61
+ /// copies straight into a `double[]`.
62
+ struct FlatSink {
63
+ std::vector<double>& out;
64
+ void emit(Verb verb, double x, double y, double r, double start, double sweep) {
65
+ out.push_back(static_cast<double>(static_cast<int32_t>(verb)));
66
+ out.push_back(x);
67
+ out.push_back(y);
68
+ out.push_back(r);
69
+ out.push_back(start);
70
+ out.push_back(sweep);
71
+ }
72
+ void move(double x, double y) { emit(Verb::Move, x, y, 0, 0, 0); }
73
+ void line(double x, double y) { emit(Verb::Line, x, y, 0, 0, 0); }
74
+ void arc(double cx, double cy, double r, double start, double sweep) { emit(Verb::Arc, cx, cy, r, start, sweep); }
75
+ };
76
+
77
+ /// The one tracing of the outline; the public overloads only differ in the
78
+ /// sink the segments land in.
79
+ template <typename Sink>
80
+ void trace(const Box& box, const Gap& gap, double progress, Sink& sink) {
81
+ const double width = std::max(0.0, finite(box.width, 0));
82
+ const double height = std::max(0.0, finite(box.height, 0));
83
+ const double stroke = std::max(0.0, finite(box.strokeWidth, 0));
84
+ if (width <= stroke || height <= stroke) return;
85
+
86
+ // The path runs down the middle of the stroke, so it is inset by half of it.
87
+ const double inset = stroke / 2;
88
+ const double left = inset;
89
+ const double top = inset;
90
+ const double right = width - inset;
91
+ const double bottom = height - inset;
92
+ const double maxRadius = std::min(right - left, bottom - top) / 2;
93
+ const double topRadius = std::clamp(finite(box.radius, 0), 0.0, maxRadius);
94
+ // A negative or non-finite bottom radius means "the same as the top".
95
+ const double requestedBottom =
96
+ std::isfinite(box.bottomRadius) && box.bottomRadius >= 0 ? box.bottomRadius : finite(box.radius, 0);
97
+ const double bottomRadius = std::clamp(requestedBottom, 0.0, maxRadius);
98
+
99
+ const double t = clamp01(finite(progress, 0));
100
+ const double padded = gap.width > 0 ? gap.width + gap.padding * 2 : 0;
101
+ double gapStart = finite(gap.left, 0) - gap.padding;
102
+ double gapWidth = padded * t;
103
+
104
+ // The gap has to sit on the straight part of the top edge, between the two
105
+ // corner arcs. A label too wide for that just closes the gap rather than
106
+ // producing a path that doubles back on itself.
107
+ const double straightStart = left + topRadius;
108
+ const double straightEnd = right - topRadius;
109
+ const double bottomStart = left + bottomRadius;
110
+ const double bottomEnd = right - bottomRadius;
111
+ if (gapWidth > 0) {
112
+ gapStart = std::max(gapStart, straightStart);
113
+ const double gapEnd = gapStart + gapWidth;
114
+ if (gapEnd > straightEnd || straightEnd - straightStart <= 0) {
115
+ gapWidth = 0;
116
+ }
117
+ }
118
+
119
+ // The gap is always expressed, even when it is zero wide, so the path has the
120
+ // same verbs and the same point count at every progress. That is what lets
121
+ // Core Animation and ValueAnimator interpolate between two of them: a path
122
+ // that changed shape mid-animation could not be tweened, and would need a
123
+ // display link redrawing it every frame instead.
124
+ if (gapWidth <= 0) gapStart = straightStart;
125
+ sink.move(gapStart + gapWidth, top);
126
+ sink.line(straightEnd, top);
127
+
128
+ // Clockwise from the top-right corner. Angles are measured from the positive
129
+ // x axis with y growing downwards, which is what both platforms use.
130
+ if (topRadius > 0) sink.arc(straightEnd, top + topRadius, topRadius, -90, 90);
131
+ sink.line(right, bottom - bottomRadius);
132
+ if (bottomRadius > 0) sink.arc(bottomEnd, bottom - bottomRadius, bottomRadius, 0, 90);
133
+ sink.line(bottomStart, bottom);
134
+ if (bottomRadius > 0) sink.arc(bottomStart, bottom - bottomRadius, bottomRadius, 90, 90);
135
+ sink.line(left, top + topRadius);
136
+ if (topRadius > 0) sink.arc(straightStart, top + topRadius, topRadius, 180, 90);
137
+
138
+ // Back to the near edge of the gap - the same point we started from when the
139
+ // gap is closed, so the rectangle reads as unbroken.
140
+ sink.line(gapStart, top);
141
+ }
142
+
143
+ } // namespace
144
+
145
+ std::vector<OutlineGeometry::Segment> OutlineGeometry::outline(const Box& box, const Gap& gap, double progress) {
146
+ std::vector<Segment> path;
147
+ SegmentSink sink{path};
148
+ trace(box, gap, progress, sink);
149
+ return path;
150
+ }
151
+
152
+ size_t OutlineGeometry::outline(const Box& box, const Gap& gap, double progress, std::vector<double>& out) {
153
+ out.clear();
154
+ FlatSink sink{out};
155
+ trace(box, gap, progress, sink);
156
+ return out.size() / kFlatStride;
157
+ }
158
+
159
+ } // namespace margelo::nitro::nitroinput
@@ -0,0 +1,107 @@
1
+ //
2
+ // OutlineGeometry.hpp
3
+ // NitroInput
4
+ //
5
+ // The outlined-field frame: a rounded rectangle whose top edge has a gap cut
6
+ // out of it for a floating label.
7
+ //
8
+ // The gap is a real hole in the stroked path, not a patch of background drawn
9
+ // over the line, so whatever is behind the field shows through it. That is the
10
+ // part people reach for Skia to get; it needs nothing more than describing the
11
+ // border as a path instead of a filled box, which both UIBezierPath and
12
+ // android.graphics.Path draw natively.
13
+ //
14
+ // This header emits the path as a short list of move / line / arc commands so
15
+ // iOS and Android replay exactly the same geometry. Text measurement stays on
16
+ // the platform side: the caller passes in the label's measured width, and gets
17
+ // back where the gap goes.
18
+ //
19
+
20
+ #pragma once
21
+
22
+ #include <cstddef>
23
+ #include <cstdint>
24
+ #include <vector>
25
+
26
+ namespace margelo::nitro::nitroinput {
27
+
28
+ class OutlineGeometry final {
29
+ public:
30
+ struct Point {
31
+ double x = 0;
32
+ double y = 0;
33
+ };
34
+
35
+ /// The field's frame, in points.
36
+ struct Box {
37
+ double width = 0;
38
+ double height = 0;
39
+ /// Corner radius, clamped to half the shorter side.
40
+ double radius = 0;
41
+ /// Stroke width; the path runs down the middle of it, as both platforms stroke.
42
+ double strokeWidth = 1;
43
+ /// The bottom corners, when they differ from the top ones. Negative - the
44
+ /// default - means they match `radius`. A filled field squares them off so
45
+ /// its indicator rule meets the fill edge to edge rather than overhanging
46
+ /// the curve.
47
+ double bottomRadius = -1;
48
+ };
49
+
50
+ /// The hole in the top edge. `width` is the label's measured width at its
51
+ /// floated size; `padding` is the breathing room added on each side.
52
+ struct Gap {
53
+ double left = 0;
54
+ double width = 0;
55
+ double padding = 4;
56
+ };
57
+
58
+ enum class Verb : int32_t {
59
+ Move = 0,
60
+ Line = 1,
61
+ /// A corner. `center` and `radius` with `startAngle` → `startAngle + sweepAngle`,
62
+ /// degrees, clockwise, 0° pointing right - the convention both platforms take.
63
+ Arc = 2,
64
+ };
65
+
66
+ struct Segment {
67
+ Verb verb = Verb::Move;
68
+ /// Move and Line: the destination. Arc: the arc's centre.
69
+ Point point;
70
+ double radius = 0;
71
+ double startAngle = 0;
72
+ double sweepAngle = 0;
73
+ };
74
+
75
+ /// The outline. `progress` interpolates the gap: 0 closes it (an unbroken
76
+ /// rounded rectangle, for a resting label sitting inside the field), 1 opens
77
+ /// it fully. A zero-width gap, or one that cannot fit between the corners,
78
+ /// also yields an unbroken rectangle.
79
+ static std::vector<Segment> outline(const Box& box, const Gap& gap, double progress);
80
+
81
+ /// Doubles per segment in the flat form below: verb, x, y, radius,
82
+ /// startAngle, sweepAngle.
83
+ static constexpr size_t kFlatStride = 6;
84
+
85
+ /// The same outline, flattened into a caller-owned vector - `kFlatStride`
86
+ /// doubles per segment, the verb as its integer value - and the number of
87
+ /// segments written. `out` is cleared and refilled, never shrunk, so a
88
+ /// caller that keeps one vector allocates nothing once it has grown to fit:
89
+ /// that is the form the Android bridge redraws from every frame.
90
+ static size_t outline(const Box& box, const Gap& gap, double progress, std::vector<double>& out);
91
+
92
+ /// Linear interpolation of a rectangle, for animating the label between its
93
+ /// resting place inside the field and its floated place on the outline.
94
+ /// Both rects are measured by the platform.
95
+ struct Rect {
96
+ double x = 0;
97
+ double y = 0;
98
+ double width = 0;
99
+ double height = 0;
100
+ };
101
+ static Rect lerp(const Rect& from, const Rect& to, double progress);
102
+
103
+ /// The gap the given floated label rect needs, in the box's coordinates.
104
+ static Gap gapFor(const Rect& floatedLabel, double padding);
105
+ };
106
+
107
+ } // namespace margelo::nitro::nitroinput
@@ -0,0 +1,30 @@
1
+ //
2
+ // HybridNitroInputWorklets.cpp
3
+ // NitroInput
4
+ //
5
+
6
+ #include "HybridNitroInputWorklets.hpp"
7
+
8
+ #include "NitroInputWorkletsBridge.hpp"
9
+ #include "NitroInputWorkletsBridgeJSI.hpp"
10
+
11
+ namespace margelo::nitro::nitroinput {
12
+
13
+ bool HybridNitroInputWorklets::getIsAvailable() {
14
+ return nitroinputworklets::isAvailable();
15
+ }
16
+
17
+ void HybridNitroInputWorklets::loadHybridMethods() {
18
+ HybridNitroInputWorkletsSpec::loadHybridMethods();
19
+ registerHybrids(this, [](Prototype& prototype) {
20
+ prototype.registerRawHybridMethod("install", 1, &HybridNitroInputWorklets::install);
21
+ });
22
+ }
23
+
24
+ jsi::Value HybridNitroInputWorklets::install(jsi::Runtime& runtime, const jsi::Value& /* thisValue */, const jsi::Value* args,
25
+ size_t count) {
26
+ if (count < 1) return jsi::Value(false);
27
+ return jsi::Value(nitroinputworklets::installRuntime(runtime, args[0]));
28
+ }
29
+
30
+ } // namespace margelo::nitro::nitroinput
@@ -0,0 +1,30 @@
1
+ //
2
+ // HybridNitroInputWorklets.hpp
3
+ // NitroInput
4
+ //
5
+ // The `NitroInputWorklets` hybrid object: JS hands it the worklets UI runtime
6
+ // (`install(getUIRuntimeHolder())`, a raw JSI method because the holder is
7
+ // an opaque object) and the native views run registered worklets through
8
+ // `NitroInputWorkletsBridge`.
9
+ //
10
+
11
+ #pragma once
12
+
13
+ #include "HybridNitroInputWorkletsSpec.hpp"
14
+
15
+ namespace margelo::nitro::nitroinput {
16
+
17
+ class HybridNitroInputWorklets final : public HybridNitroInputWorkletsSpec {
18
+ public:
19
+ HybridNitroInputWorklets() : HybridObject(TAG) {}
20
+
21
+ bool getIsAvailable() override;
22
+
23
+ protected:
24
+ void loadHybridMethods() override;
25
+
26
+ private:
27
+ jsi::Value install(jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* args, size_t count);
28
+ };
29
+
30
+ } // namespace margelo::nitro::nitroinput