@rafaelaugusto/jolt 0.1.0-rc.3 → 0.1.0-rc.4

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 (45) hide show
  1. package/README.md +2 -2
  2. package/dist/bottom-sheet/geometry.d.ts +7 -2
  3. package/dist/bottom-sheet/geometry.d.ts.map +1 -1
  4. package/dist/bottom-sheet/geometry.js +7 -3
  5. package/dist/bottom-sheet/geometry.js.map +1 -1
  6. package/dist/bottom-sheet/index.d.ts +10 -0
  7. package/dist/bottom-sheet/index.d.ts.map +1 -1
  8. package/dist/bottom-sheet/index.js +72 -22
  9. package/dist/bottom-sheet/index.js.map +1 -1
  10. package/dist/bottom-sheet/native.d.ts +5 -0
  11. package/dist/bottom-sheet/native.d.ts.map +1 -1
  12. package/dist/bottom-sheet/native.js +6 -0
  13. package/dist/bottom-sheet/native.js.map +1 -1
  14. package/dist/live-spring-driver.d.ts +5 -0
  15. package/dist/live-spring-driver.d.ts.map +1 -1
  16. package/dist/live-spring-driver.js +13 -0
  17. package/dist/live-spring-driver.js.map +1 -1
  18. package/dist/mixer.d.ts +7 -1
  19. package/dist/mixer.d.ts.map +1 -1
  20. package/dist/mixer.js +57 -22
  21. package/dist/mixer.js.map +1 -1
  22. package/dist/motion-value.d.ts +12 -1
  23. package/dist/motion-value.d.ts.map +1 -1
  24. package/dist/motion-value.js +115 -7
  25. package/dist/motion-value.js.map +1 -1
  26. package/dist/value-track.d.ts +9 -0
  27. package/dist/value-track.d.ts.map +1 -1
  28. package/dist/value-track.js +28 -16
  29. package/dist/value-track.js.map +1 -1
  30. package/package.json +1 -1
  31. package/platforms/android/java/org/rafaelaugusto/jolt/BlurEffect.java +14 -2
  32. package/platforms/android/java/org/rafaelaugusto/jolt/GradientLayer.java +30 -8
  33. package/platforms/android/java/org/rafaelaugusto/jolt/LiveSpring.java +36 -9
  34. package/platforms/android/java/org/rafaelaugusto/jolt/ShapeLayer.java +17 -7
  35. package/platforms/ios/src/JoltLiveSpring.h +1 -0
  36. package/platforms/ios/src/JoltLiveSpring.m +183 -67
  37. package/src/android-plugin.d.ts +1 -0
  38. package/src/bottom-sheet/geometry.ts +9 -2
  39. package/src/bottom-sheet/index.ts +94 -20
  40. package/src/bottom-sheet/native.ts +6 -0
  41. package/src/ios-plugin.d.ts +1 -0
  42. package/src/live-spring-driver.ts +15 -0
  43. package/src/mixer.ts +62 -22
  44. package/src/motion-value.ts +121 -9
  45. package/src/value-track.ts +25 -14
@@ -2,67 +2,133 @@
2
2
  #import "JoltSpringState.h"
3
3
  #import <QuartzCore/QuartzCore.h>
4
4
 
5
+ /* Sampled inputs and mappings are decoded once; frames read plain C arrays. */
6
+ typedef struct {
7
+ double value, elapsed, duration, delay, repeat, to;
8
+ BOOL reverse, playing;
9
+ NSUInteger count;
10
+ double *times, *values;
11
+ } JoltSampled;
12
+ typedef struct {
13
+ NSUInteger count;
14
+ double *input, *output;
15
+ BOOL clamp;
16
+ } JoltMapping;
17
+
5
18
  static id decode(NSString *json) {
6
19
  return [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
7
20
  }
8
- static double mapped(double value, NSArray *mappings) {
9
- for (NSDictionary *mapping in mappings) {
10
- NSArray *input = mapping[@"input"], *output = mapping[@"output"];
11
- BOOL clamp = [mapping[@"clamp"] boolValue];
12
- if (clamp && value <= [input.firstObject doubleValue]) { value = [output.firstObject doubleValue]; continue; }
13
- if (clamp && value >= [input.lastObject doubleValue]) { value = [output.lastObject doubleValue]; continue; }
14
- NSUInteger index = 0;
15
- while (index < input.count - 2 && value > [input[index + 1] doubleValue]) index++;
16
- double span = [input[index + 1] doubleValue] - [input[index] doubleValue];
17
- double fraction = span > 0 ? (value - [input[index] doubleValue]) / span : value < [input[index] doubleValue] ? 0 : 1;
18
- value = [output[index] doubleValue] + ([output[index + 1] doubleValue] - [output[index] doubleValue]) * fraction;
21
+ static double *doubles(NSArray *numbers, NSUInteger count) {
22
+ double *result = malloc(sizeof(double) * MAX(1, count));
23
+ for (NSUInteger i = 0; i < count; i++) result[i] = [numbers[i] doubleValue];
24
+ return result;
25
+ }
26
+ static JoltSampled *decodeSampled(NSArray *list, NSUInteger *count) {
27
+ *count = list.count;
28
+ if (!*count) return NULL;
29
+ JoltSampled *result = calloc(*count, sizeof(JoltSampled));
30
+ for (NSUInteger i = 0; i < *count; i++) {
31
+ NSDictionary *input = list[i];
32
+ NSArray *values = input[@"values"], *times = input[@"times"];
33
+ JoltSampled *s = &result[i];
34
+ s->value = [input[@"value"] doubleValue]; s->elapsed = [input[@"elapsed"] doubleValue];
35
+ s->duration = [input[@"duration"] doubleValue]; s->delay = [input[@"delay"] doubleValue];
36
+ s->repeat = [input[@"repeat"] doubleValue]; s->to = [input[@"to"] doubleValue];
37
+ s->reverse = [input[@"reverse"] boolValue]; s->playing = [input[@"running"] boolValue];
38
+ s->count = MIN(values.count, times.count);
39
+ s->times = doubles(times, s->count);
40
+ s->values = doubles(values, s->count);
19
41
  }
20
- return value;
42
+ return result;
21
43
  }
22
- static BOOL inputRunning(NSDictionary *input, double anchor, double now) {
23
- double repeat = [input[@"repeat"] doubleValue];
24
- return [input[@"running"] boolValue] && (repeat < 0 || now - anchor + [input[@"elapsed"] doubleValue] <
25
- [input[@"delay"] doubleValue] + [input[@"duration"] doubleValue] * (repeat + 1));
26
- }
27
- static double inputValue(NSDictionary *input, double anchor, double now) {
28
- NSArray *values = input[@"values"], *times = input[@"times"];
29
- if (![input[@"running"] boolValue] || values.count < 2) return [input[@"value"] doubleValue];
30
- if (!inputRunning(input, anchor, now)) return [input[@"to"] doubleValue];
31
- double time = MAX(0, now - anchor + [input[@"elapsed"] doubleValue] - [input[@"delay"] doubleValue]);
32
- double duration = MAX(1, [input[@"duration"] doubleValue]);
44
+ static void freeSampled(JoltSampled *list, NSUInteger count) {
45
+ for (NSUInteger i = 0; i < count; i++) { free(list[i].times); free(list[i].values); }
46
+ free(list);
47
+ }
48
+ static BOOL sampledRunning(const JoltSampled *s, double anchor, double now) {
49
+ return s->playing && (s->repeat < 0 || now - anchor + s->elapsed < s->delay + s->duration * (s->repeat + 1));
50
+ }
51
+ static double sampledValue(const JoltSampled *s, double anchor, double now) {
52
+ if (!s->playing || s->count < 2) return s->value;
53
+ if (!sampledRunning(s, anchor, now)) return s->to;
54
+ double time = MAX(0, now - anchor + s->elapsed - s->delay);
55
+ double duration = MAX(1, s->duration);
33
56
  double iteration = floor(time / duration), progress = fmod(time, duration) / duration;
34
- if ([input[@"reverse"] boolValue] && fmod(iteration, 2) >= 1) progress = 1 - progress;
35
- NSUInteger lo = 0, hi = times.count - 1;
36
- while (lo + 1 < hi) { NSUInteger mid = (lo + hi) / 2; if ([times[mid] doubleValue] <= progress) lo = mid; else hi = mid; }
37
- double start = [times[lo] doubleValue], end = [times[hi] doubleValue];
57
+ if (s->reverse && fmod(iteration, 2) >= 1) progress = 1 - progress;
58
+ NSUInteger lo = 0, hi = s->count - 1;
59
+ while (lo + 1 < hi) { NSUInteger mid = (lo + hi) / 2; if (s->times[mid] <= progress) lo = mid; else hi = mid; }
60
+ double start = s->times[lo], end = s->times[hi];
38
61
  double fraction = end > start ? (progress - start) / (end - start) : 1;
39
- return [values[lo] doubleValue] + ([values[hi] doubleValue] - [values[lo] doubleValue]) * MAX(0, MIN(1, fraction));
62
+ return s->values[lo] + (s->values[hi] - s->values[lo]) * MAX(0, MIN(1, fraction));
40
63
  }
64
+ static JoltMapping *decodeMappings(NSString *json, NSUInteger *count) {
65
+ NSArray *list = decode(json);
66
+ *count = list.count;
67
+ if (!*count) return NULL;
68
+ JoltMapping *result = calloc(*count, sizeof(JoltMapping));
69
+ for (NSUInteger i = 0; i < *count; i++) {
70
+ NSDictionary *mapping = list[i];
71
+ NSArray *input = mapping[@"input"], *output = mapping[@"output"];
72
+ result[i].count = MIN(input.count, output.count);
73
+ result[i].input = doubles(input, result[i].count);
74
+ result[i].output = doubles(output, result[i].count);
75
+ result[i].clamp = [mapping[@"clamp"] boolValue];
76
+ }
77
+ return result;
78
+ }
79
+ static void freeMappings(JoltMapping *maps, NSUInteger count) {
80
+ for (NSUInteger i = 0; i < count; i++) { free(maps[i].input); free(maps[i].output); }
81
+ free(maps);
82
+ }
83
+ static double mapped(double value, const JoltMapping *maps, NSUInteger count) {
84
+ for (NSUInteger m = 0; m < count; m++) {
85
+ const JoltMapping *mapping = &maps[m];
86
+ if (mapping->count < 2) continue;
87
+ if (mapping->clamp && value <= mapping->input[0]) { value = mapping->output[0]; continue; }
88
+ if (mapping->clamp && value >= mapping->input[mapping->count - 1]) { value = mapping->output[mapping->count - 1]; continue; }
89
+ NSUInteger index = 0;
90
+ while (index < mapping->count - 2 && value > mapping->input[index + 1]) index++;
91
+ double span = mapping->input[index + 1] - mapping->input[index];
92
+ double fraction = span > 0 ? (value - mapping->input[index]) / span : value < mapping->input[index] ? 0 : 1;
93
+ value = mapping->output[index] + (mapping->output[index + 1] - mapping->output[index]) * fraction;
94
+ }
95
+ return value;
96
+ }
97
+
98
+ @protocol JoltShadowedView
99
+ - (CALayer *)outerShadowContainerLayer;
100
+ @end
41
101
 
42
102
  @interface JoltLiveSpring () {
43
103
  JoltSpringState _state;
104
+ JoltSampled *_inputs;
105
+ NSUInteger _inputCount;
106
+ JoltMapping *_sourceMaps;
107
+ NSUInteger _sourceMapCount;
44
108
  }
45
109
  @property(nonatomic) double stiffness, damping, mass, restDelta, restSpeed;
46
110
  @property(nonatomic, readwrite) BOOL moving;
47
111
  @property(nonatomic) BOOL reduced, paused, disposed, direct, inputHeld, sensorDriven;
48
112
  @property(nonatomic) double targetValue;
49
113
  @property(nonatomic, nullable) JoltLiveSpring *source;
50
- @property(nonatomic) NSArray *sourceMaps;
51
- @property(nonatomic) NSDictionary *input;
52
- @property(nonatomic) double anchor;
114
+ @property(nonatomic) double inputRest, anchor;
53
115
  @property(nonatomic) NSMutableArray<JoltLiveSpringBinding *> *bindings;
54
116
  - (void)step:(double)now seconds:(double)seconds;
55
117
  - (void)apply;
56
118
  - (void)wake;
57
119
  @end
58
- @interface JoltLiveSpringBinding ()
120
+ @interface JoltLiveSpringBinding () {
121
+ JoltMapping *_maps;
122
+ NSUInteger _mapCount;
123
+ JoltSampled *_layers;
124
+ NSUInteger _layerCount;
125
+ }
59
126
  @property(nonatomic, weak) UIView *view;
60
127
  @property(nonatomic, weak) JoltLiveSpring *node;
61
- @property(nonatomic) NSString *property;
62
- @property(nonatomic) NSArray *mappings;
63
- @property(nonatomic) BOOL stopped;
64
- @property(nonatomic) NSArray *sampledLayers;
128
+ @property(nonatomic) NSString *property, *keyPath;
129
+ @property(nonatomic) BOOL stopped, multiply, radians, alpha, shadowed;
65
130
  @property(nonatomic) double layerAnchor;
131
+ - (void)useMappings:(NSString *)mappings;
66
132
  - (BOOL)running:(double)now;
67
133
  - (void)apply;
68
134
  @end
@@ -90,7 +156,9 @@ static double previousFrame;
90
156
  while (remaining > 1e-9) {
91
157
  double dt = MIN(remaining, 1.0 / 240);
92
158
  remaining -= dt;
93
- for (JoltLiveSpring *node in nodes) [node step:now - remaining * 1000 seconds:dt];
159
+ // An instant node only needs the frame's final time; springs integrate every substep.
160
+ for (JoltLiveSpring *node in nodes)
161
+ if (!node.reduced || remaining <= 1e-9) [node step:now - remaining * 1000 seconds:dt];
94
162
  }
95
163
  BOOL active = NO;
96
164
  [CATransaction begin]; [CATransaction setDisableActions:YES];
@@ -108,36 +176,68 @@ static double previousFrame;
108
176
  node.stiffness = stiffness; node.damping = damping; node.mass = mass;
109
177
  node.restDelta = restDelta; node.restSpeed = restSpeed; node->_reduced = reduced;
110
178
  node.direct = YES; node.targetValue = initial;
111
- node.bindings = [NSMutableArray new]; node.sourceMaps = @[];
179
+ node.bindings = [NSMutableArray new];
112
180
  [nodes addObject:node];
113
181
  return node;
114
182
  }
183
+ - (void)dealloc {
184
+ freeSampled(_inputs, _inputCount);
185
+ freeMappings(_sourceMaps, _sourceMapCount);
186
+ }
115
187
  - (double)position { return _state.position; }
116
188
  - (double)velocity { return _state.velocity; }
117
189
  + (NSUInteger)activeCount { return displayLink ? 1 : 0; }
118
190
  + (NSUInteger)nodeCount { return nodes.count; }
191
+ - (void)replaceInputs:(NSArray *)list rest:(double)rest {
192
+ freeSampled(_inputs, _inputCount);
193
+ _inputs = decodeSampled(list, &_inputCount);
194
+ self.inputRest = rest; self.anchor = CACurrentMediaTime() * 1000;
195
+ }
119
196
  - (void)follow:(JoltLiveSpring *)source mappings:(NSString *)mappings {
120
197
  if (self.disposed || source.disposed || [nodes indexOfObject:source] >= [nodes indexOfObject:self])
121
198
  [NSException raise:NSInvalidArgumentException format:@"A spring can only follow an existing live node"];
122
- self.sourceMaps = decode(mappings); self.source = source; [self wake];
199
+ freeMappings(_sourceMaps, _sourceMapCount);
200
+ _sourceMaps = decodeMappings(mappings, &_sourceMapCount);
201
+ self.source = source; [self wake];
123
202
  }
124
203
  - (void)updateInput:(NSString *)snapshot {
125
204
  if (self.disposed) return;
126
- self.sensorDriven = NO; self.input = decode(snapshot); self.direct = NO; self.anchor = CACurrentMediaTime() * 1000; [self wake];
205
+ self.sensorDriven = NO; self.direct = NO;
206
+ [self replaceInputs:@[decode(snapshot)] rest:0];
207
+ [self wake];
208
+ }
209
+ - (void)setInputs:(NSString *)snapshots rest:(double)rest {
210
+ if (self.disposed) return;
211
+ self.sensorDriven = NO; self.direct = NO;
212
+ [self replaceInputs:decode(snapshots) rest:rest];
213
+ [self wake];
214
+ }
215
+ - (double)inputValue:(double)now {
216
+ double value = self.inputRest;
217
+ for (NSUInteger i = 0; i < _inputCount; i++) value += sampledValue(&_inputs[i], self.anchor, now);
218
+ return value;
219
+ }
220
+ - (BOOL)inputMoving:(double)now {
221
+ for (NSUInteger i = 0; i < _inputCount; i++) if (sampledRunning(&_inputs[i], self.anchor, now)) return YES;
222
+ return NO;
127
223
  }
128
224
  - (void)setSensorInput:(double)value {
129
225
  if (self.disposed || !isfinite(value) || (self.sensorDriven && self.inputHeld && _state.position == value)) return;
130
- self.sensorDriven = YES; self.inputHeld = YES; self.direct = YES; self.targetValue = value; self.input = nil;
226
+ self.sensorDriven = YES; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
227
+ [self replaceInputs:@[] rest:0];
131
228
  _state = (JoltSpringState){value, 0}; self.moving = NO;
132
229
  [JoltLiveSpring schedule];
133
230
  }
134
231
  - (void)setTarget:(double)value {
135
232
  if (self.disposed) return;
136
- self.sensorDriven = NO; self.direct = YES; self.targetValue = value; self.input = nil; [self wake];
233
+ self.sensorDriven = NO; self.direct = YES; self.targetValue = value;
234
+ [self replaceInputs:@[] rest:0];
235
+ [self wake];
137
236
  }
138
237
  - (void)holdInput:(double)value velocity:(double)velocity {
139
238
  if (self.disposed) return;
140
- self.sensorDriven = NO; self.inputHeld = YES; self.direct = YES; self.targetValue = value; self.input = nil;
239
+ self.sensorDriven = NO; self.inputHeld = YES; self.direct = YES; self.targetValue = value;
240
+ [self replaceInputs:@[] rest:0];
141
241
  _state = (JoltSpringState){value, velocity}; self.moving = YES;
142
242
  [CATransaction begin]; [CATransaction setDisableActions:YES]; [self apply]; [CATransaction commit];
143
243
  [JoltLiveSpring schedule];
@@ -163,8 +263,8 @@ static double previousFrame;
163
263
  }
164
264
  - (void)step:(double)now seconds:(double)seconds {
165
265
  if (self.disposed || self.paused || self.inputHeld) return;
166
- double target = self.source ? mapped(self.source.position, self.sourceMaps) : (self.direct ? self.targetValue : inputValue(self.input, self.anchor, now));
167
- BOOL inputMoving = self.source ? self.source.moving : (!self.direct && inputRunning(self.input, self.anchor, now));
266
+ double target = self.source ? mapped(self.source.position, _sourceMaps, _sourceMapCount) : (self.direct ? self.targetValue : [self inputValue:now]);
267
+ BOOL inputMoving = self.source ? self.source.moving : (!self.direct && [self inputMoving:now]);
168
268
  if (self.reduced) _state = (JoltSpringState){target, 0};
169
269
  else _state = JoltSpringAdvance(_state, target, self.stiffness, self.damping, self.mass, seconds);
170
270
  BOOL rest = fabs(_state.position - target) <= self.restDelta && fabs(_state.velocity) <= self.restSpeed;
@@ -174,7 +274,20 @@ static double previousFrame;
174
274
  - (JoltLiveSpringBinding *)bind:(UIView *)view property:(NSString *)property mappings:(NSString *)mappings {
175
275
  if (self.disposed) [NSException raise:NSInvalidArgumentException format:@"Spring disposed"];
176
276
  JoltLiveSpringBinding *binding = [JoltLiveSpringBinding new];
177
- binding.node = self; binding.view = view; binding.property = property; binding.mappings = decode(mappings);
277
+ binding.node = self; binding.view = view; binding.property = property;
278
+ [binding useMappings:mappings];
279
+ NSString *key = property;
280
+ if ([key isEqualToString:@"opacity"]) binding.alpha = YES;
281
+ else if ([key isEqualToString:@"x"]) key = @"transform.translation.x";
282
+ else if ([key isEqualToString:@"y"]) key = @"transform.translation.y";
283
+ else if ([key isEqualToString:@"scaleX"]) key = @"transform.scale.x";
284
+ else if ([key isEqualToString:@"scaleY"]) key = @"transform.scale.y";
285
+ else if ([key isEqualToString:@"rotate"]) { key = @"transform.rotation.z"; binding.radians = YES; }
286
+ else if ([key isEqualToString:@"rotateX"]) { key = @"transform.rotation.x"; binding.radians = YES; }
287
+ else if ([key isEqualToString:@"rotateY"]) { key = @"transform.rotation.y"; binding.radians = YES; }
288
+ binding.keyPath = key;
289
+ binding.multiply = binding.alpha || [property hasPrefix:@"scale"];
290
+ binding.shadowed = [view respondsToSelector:@selector(outerShadowContainerLayer)];
178
291
  [self.bindings addObject:binding];
179
292
  [CATransaction begin]; [CATransaction setDisableActions:YES]; [binding apply]; [CATransaction commit];
180
293
  return binding;
@@ -196,7 +309,7 @@ static double previousFrame;
196
309
  if (self.disposed) return;
197
310
  self.disposed = YES; self.moving = NO; [self.bindings removeAllObjects]; [nodes removeObject:self];
198
311
  for (JoltLiveSpring *child in nodes) if (child.source == self) {
199
- child.source = nil; [child setTarget:mapped(self.position, child.sourceMaps)];
312
+ child.source = nil; [child setTarget:mapped(self.position, child->_sourceMaps, child->_sourceMapCount)];
200
313
  }
201
314
  self.source = nil;
202
315
  if (!nodes.count) { [displayLink invalidate]; displayLink = nil; previousFrame = 0; }
@@ -204,40 +317,43 @@ static double previousFrame;
204
317
  @end
205
318
 
206
319
  @implementation JoltLiveSpringBinding
207
- - (double)value { return mapped(self.node.position, self.mappings); }
320
+ - (void)dealloc {
321
+ freeMappings(_maps, _mapCount);
322
+ freeSampled(_layers, _layerCount);
323
+ }
324
+ - (void)useMappings:(NSString *)mappings {
325
+ freeMappings(_maps, _mapCount);
326
+ _maps = decodeMappings(mappings, &_mapCount);
327
+ }
328
+ - (double)value { return mapped(self.node.position, _maps, _mapCount); }
208
329
  - (void)setLayers:(NSString *)inputs {
209
- self.sampledLayers = decode(inputs); self.layerAnchor = CACurrentMediaTime() * 1000;
330
+ freeSampled(_layers, _layerCount);
331
+ _layers = decodeSampled(decode(inputs), &_layerCount);
332
+ self.layerAnchor = CACurrentMediaTime() * 1000;
210
333
  [CATransaction begin]; [CATransaction setDisableActions:YES]; [self apply]; [CATransaction commit];
211
334
  [JoltLiveSpring schedule];
212
335
  }
213
336
  - (BOOL)running:(double)now {
214
- for (NSDictionary *layer in self.sampledLayers) if (inputRunning(layer, self.layerAnchor, now)) return YES;
337
+ for (NSUInteger i = 0; i < _layerCount; i++) if (sampledRunning(&_layers[i], self.layerAnchor, now)) return YES;
215
338
  return NO;
216
339
  }
217
340
  - (void)cancel { self.stopped = YES; [self.node.bindings removeObject:self]; }
218
341
  - (void)apply {
219
342
  UIView *view = self.view;
220
343
  if (self.stopped || !view.window) return;
221
- NSString *key = self.property;
222
344
  double value = self.value;
223
- BOOL multiply = [key isEqualToString:@"opacity"] || [key hasPrefix:@"scale"];
224
345
  double now = CACurrentMediaTime() * 1000;
225
- for (NSDictionary *layer in self.sampledLayers) {
226
- double part = inputValue(layer, self.layerAnchor, now);
227
- value = multiply ? value * part : value + part;
346
+ for (NSUInteger i = 0; i < _layerCount; i++) {
347
+ double part = sampledValue(&_layers[i], self.layerAnchor, now);
348
+ value = self.multiply ? value * part : value + part;
228
349
  }
229
- if ([key isEqualToString:@"opacity"]) { view.alpha = MAX(0, MIN(1, value)); return; }
230
- if ([key isEqualToString:@"x"]) key = @"transform.translation.x";
231
- else if ([key isEqualToString:@"y"]) key = @"transform.translation.y";
232
- else if ([key isEqualToString:@"scaleX"]) key = @"transform.scale.x";
233
- else if ([key isEqualToString:@"scaleY"]) key = @"transform.scale.y";
234
- else if ([key isEqualToString:@"rotate"]) { key = @"transform.rotation.z"; value *= M_PI / 180; }
235
- else if ([key isEqualToString:@"rotateX"]) { key = @"transform.rotation.x"; value *= M_PI / 180; }
236
- else if ([key isEqualToString:@"rotateY"]) { key = @"transform.rotation.y"; value *= M_PI / 180; }
237
- [view.layer setValue:@(value) forKeyPath:key];
238
- if ([view respondsToSelector:NSSelectorFromString(@"outerShadowContainerLayer")]) {
239
- CALayer *shadow = [view valueForKey:@"outerShadowContainerLayer"];
240
- [shadow setValue:@(value) forKeyPath:key];
350
+ if (self.alpha) { view.alpha = MAX(0, MIN(1, value)); return; }
351
+ if (self.radians) value *= M_PI / 180;
352
+ NSNumber *boxed = @(value);
353
+ [view.layer setValue:boxed forKeyPath:self.keyPath];
354
+ if (self.shadowed) {
355
+ CALayer *shadow = [(id<JoltShadowedView>)view outerShadowContainerLayer];
356
+ if (shadow) [shadow setValue:boxed forKeyPath:self.keyPath];
241
357
  }
242
358
  }
243
359
  @end
@@ -171,6 +171,7 @@ declare namespace org.rafaelaugusto.jolt {
171
171
  isMoving(): boolean
172
172
  follow(source: LiveSpring, mappings: string): void
173
173
  updateInput(snapshot: string): void
174
+ setInputs(rest: number, snapshots: string): void
174
175
  setTarget(value: number): void
175
176
  setReduced(value: boolean): void
176
177
  setPaused(value: boolean): void
@@ -10,6 +10,8 @@ export interface SheetGeometry {
10
10
  height: number
11
11
  closedY: number
12
12
  top: number
13
+ /** How far the panel extends below the usable area, under a home indicator the keyboard does not cover. */
14
+ extension: number
13
15
  stops: SheetStop[]
14
16
  }
15
17
 
@@ -31,7 +33,10 @@ export function validateSnaps(points: readonly SheetSnap[]): void {
31
33
  throw new RangeError('Sheet snap points must not repeat.')
32
34
  }
33
35
 
34
- /** Keyboard overlap is relative to the host, so adjustResize is not subtracted twice. */
36
+ /**
37
+ * Keyboard overlap is relative to the host, so adjustResize is not subtracted twice.
38
+ * The panel extends under the bottom inset only when its host lets it reach that edge.
39
+ */
35
40
  export function sheetGeometry(
36
41
  points: readonly SheetSnap[],
37
42
  hostHeight: number,
@@ -41,6 +46,7 @@ export function sheetGeometry(
41
46
  safeTop: number,
42
47
  safeBottom: number,
43
48
  contentHeight: number,
49
+ extendable = true,
44
50
  ): SheetGeometry {
45
51
  const top = Math.max(0, safeTop)
46
52
  const overlap = Math.max(
@@ -48,6 +54,7 @@ export function sheetGeometry(
48
54
  Math.min(hostHeight, hostBottom - (windowHeight - keyboardHeight)),
49
55
  )
50
56
  const bottom = Math.max(Math.max(0, safeBottom), overlap)
57
+ const extension = extendable ? Math.max(0, Math.max(0, safeBottom) - overlap) : 0
51
58
  const height = Math.max(1, hostHeight - top - bottom)
52
59
  const closedY = Math.max(height, hostHeight - top)
53
60
  const stops = points.map((point) => {
@@ -60,7 +67,7 @@ export function sheetGeometry(
60
67
  const visible = Math.max(1, Math.min(height, desired))
61
68
  return { point, height: visible, y: height - visible }
62
69
  })
63
- return { height, closedY, top, stops }
70
+ return { height, closedY, top, extension, stops }
64
71
  }
65
72
 
66
73
  /** Projection selects a stop; the native spring retains the actual release velocity. */
@@ -3,6 +3,7 @@ import {
3
3
  GridLayout,
4
4
  ScrollView,
5
5
  Trace,
6
+ Utils,
6
7
  View,
7
8
  isAndroid,
8
9
  } from '@nativescript/core'
@@ -19,6 +20,7 @@ import {
19
20
  import {
20
21
  captureFocus,
21
22
  dismissKeyboard,
23
+ hostInsets,
22
24
  revealFocusedField,
23
25
  sheetActions,
24
26
  windowHeight,
@@ -42,10 +44,25 @@ export interface BottomSheetOptions {
42
44
  keyboard?: 'follow' | 'none'
43
45
  /** Whether dragging, the backdrop, back and accessibility can dismiss. Defaults to true. */
44
46
  dismissible?: boolean
47
+ /**
48
+ * Overrides the host's safe-area insets, in DIPs. Core already lays the host's children
49
+ * out inside its insets, so a top override only adds to that placement.
50
+ */
45
51
  safeArea?: { top?: number; bottom?: number }
46
52
  returnFocus?: View
47
53
  spring?: Pick<SpringValueOptions, 'stiffness' | 'damping' | 'mass'>
48
- accessibility?: { label?: string; expand?: string; collapse?: string; dismiss?: string }
54
+ /**
55
+ * Spoken labels for the handle. Expand and Collapse appear only where a larger or smaller
56
+ * stop exists; `value` formats the spoken value per stop, with `null` when closed, and
57
+ * replaces the default, which is omitted for a single stop.
58
+ */
59
+ accessibility?: {
60
+ label?: string
61
+ expand?: string
62
+ collapse?: string
63
+ dismiss?: string
64
+ value?: (point: SheetSnap | null) => string | undefined
65
+ }
49
66
  onChange?: (point: SheetSnap | null) => void
50
67
  /** Called after a close settles; interrupted closes do not dismiss. */
51
68
  onDismiss?: () => void
@@ -142,6 +159,8 @@ export function bottomSheet(
142
159
  const original = {
143
160
  contentAlignment: content?.verticalAlignment,
144
161
  height: panel.height,
162
+ ignoreSafeArea: panel.iosIgnoreSafeArea,
163
+ paddingBottom: panel.paddingBottom,
145
164
  marginTop: panel.marginTop,
146
165
  marginBottom: panel.marginBottom,
147
166
  alignment: panel.verticalAlignment,
@@ -155,6 +174,7 @@ export function bottomSheet(
155
174
  value: handle.accessibilityValue,
156
175
  }
157
176
  let geometry: SheetGeometry | undefined, input: Gestures | undefined
177
+ let extended = 0
158
178
  let opened = false,
159
179
  point = points[0]!,
160
180
  ended = false,
@@ -173,6 +193,44 @@ export function bottomSheet(
173
193
  restoreFocus = () => {}
174
194
  const dismissible = options.dismissible !== false
175
195
  const followKeyboard = options.keyboard !== 'none'
196
+ const labels = options.accessibility
197
+ const spoken = (next: SheetSnap | null) =>
198
+ labels?.value
199
+ ? labels.value(next)
200
+ : points.length > 1
201
+ ? next === null
202
+ ? 'Closed'
203
+ : String(next)
204
+ : undefined
205
+ const announce = (next: SheetSnap | null) => {
206
+ handle.accessibilityValue = spoken(next) as string
207
+ }
208
+ let installed: string | undefined
209
+ // Only actions that can do something are offered; the set follows the selected stop.
210
+ function installActions() {
211
+ if (ended || !handle.isLoaded) return
212
+ const current = geometry?.stops.find((stop) => stop.point === point)
213
+ const larger =
214
+ !!current && geometry!.stops.some((stop) => stop.height > current.height + 0.01)
215
+ const smaller =
216
+ !!current && geometry!.stops.some((stop) => stop.height < current.height - 0.01)
217
+ const actions = [
218
+ ...(larger
219
+ ? [{ label: labels?.expand ?? 'Expand', run: () => adjacent(true) }]
220
+ : []),
221
+ ...(smaller
222
+ ? [{ label: labels?.collapse ?? 'Collapse', run: () => adjacent(false) }]
223
+ : []),
224
+ ...(dismissible
225
+ ? [{ label: labels?.dismiss ?? 'Dismiss', run: () => setTarget(null) }]
226
+ : []),
227
+ ]
228
+ const signature = actions.map((action) => action.label).join('\n')
229
+ if (signature === installed) return
230
+ releaseActions()
231
+ releaseActions = sheetActions(handle, actions)
232
+ installed = signature
233
+ }
176
234
  const keyboardOpen = () => followKeyboard && keyboard.visible
177
235
  const targetY = () =>
178
236
  !opened
@@ -253,7 +311,8 @@ export function bottomSheet(
253
311
  pendingDismiss ||= wasOpen
254
312
  dismissKeyboard(panel)
255
313
  }
256
- handle.accessibilityValue = opened ? String(point) : 'Closed'
314
+ announce(opened ? point : null)
315
+ installActions()
257
316
  move()
258
317
  if (changed) report(() => options.onChange?.(next))
259
318
  }
@@ -280,11 +339,16 @@ export function bottomSheet(
280
339
  return
281
340
  const size = host.getActualSize()
282
341
  if (!(size.height > 0)) return
283
- const safe = host.getSafeAreaInsets()
284
- const top = options.safeArea?.top ?? safe.top
342
+ const safe = hostInsets(host)
343
+ // The host places its children below its own top inset; the panel can only start lower.
344
+ const top = Math.max(safe.top, options.safeArea?.top ?? safe.top)
285
345
  const bottom = options.safeArea?.bottom ?? safe.bottom
346
+ // The padding that keeps rows above the bottom inset is not sheet chrome.
286
347
  const chrome = scroll
287
- ? Math.max(0, panel.getActualSize().height - scroll.getActualSize().height)
348
+ ? Math.max(
349
+ 0,
350
+ panel.getActualSize().height - scroll.getActualSize().height - extended,
351
+ )
288
352
  : 0
289
353
  const contentHeight = (content?.getActualSize().height ?? 0) + chrome
290
354
  const next = sheetGeometry(
@@ -296,14 +360,24 @@ export function bottomSheet(
296
360
  top,
297
361
  bottom,
298
362
  contentHeight,
363
+ safe.bottom === 0,
299
364
  )
300
365
  const previous = geometry
301
366
  geometry = next
302
- const panelHeight = keyboardOpen()
303
- ? next.height
304
- : Math.max(...next.stops.map((stop) => stop.height))
367
+ const panelHeight =
368
+ (keyboardOpen()
369
+ ? next.height
370
+ : Math.max(...next.stops.map((stop) => stop.height))) + next.extension
305
371
  if (panel.height !== panelHeight) panel.height = panelHeight
306
- if (panel.marginTop !== next.top) panel.marginTop = next.top
372
+ const margin = next.top - safe.top
373
+ if (panel.marginTop !== margin) panel.marginTop = margin
374
+ if (extended !== next.extension) {
375
+ const base =
376
+ Utils.layout.toDeviceIndependentPixels(panel.effectivePaddingBottom ?? 0) -
377
+ extended
378
+ panel.paddingBottom = base + next.extension
379
+ extended = next.extension
380
+ }
307
381
  if (!input) {
308
382
  input = gestures(panel, {
309
383
  pan: { axis: 'y', bounds: { top: minimum(), bottom: next.closedY } },
@@ -317,7 +391,7 @@ export function bottomSheet(
317
391
  releaseClose = () => {}
318
392
  opened = true
319
393
  pendingDismiss = false
320
- handle.accessibilityValue = String(point)
394
+ announce(point)
321
395
  if (!wasOpen) report(() => options.onChange?.(point))
322
396
  },
323
397
  onContactEnd(event) {
@@ -345,6 +419,7 @@ export function bottomSheet(
345
419
  releasePanel = bind(panel, { y: input.y })
346
420
  if (held || !opened) input.pause()
347
421
  }
422
+ installActions()
348
423
  if (
349
424
  !previous ||
350
425
  previous.closedY !== next.closedY ||
@@ -393,15 +468,8 @@ export function bottomSheet(
393
468
  restoreFocus = captureFocus(panel, options.returnFocus)
394
469
  focusCaptured = true
395
470
  }
396
- releaseActions()
397
- const labels = options.accessibility
398
- releaseActions = sheetActions(handle, [
399
- { label: labels?.expand ?? 'Expand', run: () => adjacent(true) },
400
- { label: labels?.collapse ?? 'Collapse', run: () => adjacent(false) },
401
- ...(dismissible
402
- ? [{ label: labels?.dismiss ?? 'Dismiss', run: () => setTarget(null) }]
403
- : []),
404
- ])
471
+ installed = undefined
472
+ installActions()
405
473
  releaseKeyboard()
406
474
  releaseKeyboard = onKeyboard((state) => {
407
475
  keyboard = state
@@ -421,6 +489,7 @@ export function bottomSheet(
421
489
  if (backdrop) backdrop.visibility = 'hidden'
422
490
  releaseActions()
423
491
  releaseActions = () => {}
492
+ installed = undefined
424
493
  releaseKeyboard()
425
494
  releaseKeyboard = () => {}
426
495
  releasePanel()
@@ -486,6 +555,8 @@ export function bottomSheet(
486
555
  Application.android.off(Application.android.activityBackPressedEvent, back)
487
556
  if (content) content.verticalAlignment = original.contentAlignment!
488
557
  panel.height = original.height
558
+ panel.iosIgnoreSafeArea = original.ignoreSafeArea
559
+ panel.paddingBottom = original.paddingBottom
489
560
  panel.marginTop = original.marginTop
490
561
  panel.marginBottom = original.marginBottom
491
562
  panel.verticalAlignment = original.alignment
@@ -504,6 +575,9 @@ export function bottomSheet(
504
575
  }
505
576
  owners.set(panel, control)
506
577
  if (content && points.includes('content')) content.verticalAlignment = 'top'
578
+ // The geometry owns the panel's placement and its rows' distance from the bottom inset;
579
+ // Core would otherwise stretch it to the screen edges or inset its rows a second time.
580
+ panel.iosIgnoreSafeArea = true
507
581
  panel.verticalAlignment = 'top'
508
582
  panel.marginBottom = 0
509
583
  panel.visibility = 'hidden'
@@ -511,7 +585,7 @@ export function bottomSheet(
511
585
  handle.accessible = true
512
586
  handle.accessibilityLabel =
513
587
  options.accessibility?.label ?? (handle.accessibilityLabel || 'Bottom sheet')
514
- handle.accessibilityValue = 'Closed'
588
+ announce(null)
515
589
  if (backdrop) backdrop.visibility = 'hidden'
516
590
  for (const view of [panel, handle, ...(scroll ? [scroll] : [])])
517
591
  view.on('loaded', attach)
@@ -16,6 +16,12 @@ function firstResponder(view: UIView): UIView | undefined {
16
16
  return undefined
17
17
  }
18
18
 
19
+ /** Core reports a view's safe-area insets in device pixels; the sheet works in DIPs. */
20
+ export function hostInsets(view: View): { top: number; bottom: number } {
21
+ const insets = view.getSafeAreaInsets()
22
+ const dip = Utils.layout.toDeviceIndependentPixels
23
+ return { top: dip(insets.top), bottom: dip(insets.bottom) }
24
+ }
19
25
  export function windowHeight(view: View): number {
20
26
  if (isIOS)
21
27
  return (