Skip to content

Commit 37cfa12

Browse files
committed
feat: support resizing in RCTDevLoadingView using automatic constraints and animating the container transform, drop RCTResizeThrottler
1 parent a75ed5b commit 37cfa12

3 files changed

Lines changed: 6 additions & 196 deletions

File tree

packages/react-native/React/Base/RCTResizeThrottler.h

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages/react-native/React/Base/RCTResizeThrottler.mm

Lines changed: 0 additions & 96 deletions
This file was deleted.

packages/react-native/React/CoreModules/RCTDevLoadingView.mm

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@
1717
#import <React/RCTConvert.h>
1818
#import <React/RCTDefines.h>
1919
#import <React/RCTDevLoadingViewSetEnabled.h>
20-
#import <React/RCTResizeThrottler.h>
2120
#import <React/RCTUtils.h>
2221

2322
#import "CoreModulesPlugins.h"
2423

2524
using namespace facebook::react;
2625

27-
const NSInteger LOADING_VIEW_HEIGHT = 25;
28-
2926
@interface RCTDevLoadingView () <NativeDevLoadingViewSpec>
3027
@end
3128

@@ -39,9 +36,6 @@ @implementation RCTDevLoadingView {
3936
NSDate *_showDate;
4037
BOOL _hiding;
4138
dispatch_block_t _initialMessageBlock;
42-
NSLayoutConstraint *_containerHeightConstraint;
43-
NSLayoutConstraint *_windowWidthConstraint;
44-
RCTResizeThrottler *_resizeThrottler;
4539
}
4640

4741
RCT_EXPORT_MODULE()
@@ -57,24 +51,6 @@ - (instancetype)init
5751
selector:@selector(hide)
5852
name:RCTJavaScriptDidFailToLoadNotification
5953
object:nil];
60-
self->_resizeThrottler = [[RCTResizeThrottler alloc] initWithTrailingDelay:0];
61-
62-
__weak __typeof(self) weakSelf = self;
63-
self->_resizeThrottler.updateBlock = ^(CGFloat width, CGFloat height) {
64-
UIWindow *mainWindow = RCTKeyWindow();
65-
auto *strongSelf = weakSelf;
66-
if (!mainWindow || !strongSelf->_window || !strongSelf->_containerHeightConstraint)
67-
return;
68-
69-
CGRect newFrame = CGRectMake(0, 0, width, height);
70-
71-
// update if the size has actually changed
72-
if (!CGRectEqualToRect(strongSelf->_window.frame, newFrame)) {
73-
strongSelf->_window.frame = newFrame;
74-
strongSelf->_containerHeightConstraint.constant = height;
75-
[strongSelf->_window layoutIfNeeded];
76-
}
77-
};
7854
}
7955
return self;
8056
}
@@ -207,28 +183,24 @@ - (void)showMessage:(NSString *)message
207183
} else {
208184
self->_window = [[UIWindow alloc] init];
209185
}
186+
187+
self->_window.frame = self->_window.windowScene.coordinateSpace.bounds;
188+
210189
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
211190
self->_window.rootViewController = [UIViewController new];
212191
[self->_window.rootViewController.view addSubview:self->_container];
213192
}
214193

215194
CGFloat topSafeAreaHeight = mainWindow.safeAreaInsets.top;
216-
CGFloat height = topSafeAreaHeight + LOADING_VIEW_HEIGHT;
217-
self->_window.frame = CGRectMake(0, 0, mainWindow.frame.size.width, height);
218-
219195
self->_window.hidden = NO;
220196

221197
[self->_window layoutIfNeeded];
222198

223-
// Store constraints that need to be updated on resize
224-
self->_containerHeightConstraint = [self->_container.heightAnchor constraintEqualToConstant:height];
225-
226199
NSMutableArray *constraints = [NSMutableArray arrayWithArray:@[
227200
// Container constraints
228201
[self->_container.topAnchor constraintEqualToAnchor:self->_window.rootViewController.view.topAnchor],
229202
[self->_container.leadingAnchor constraintEqualToAnchor:self->_window.rootViewController.view.leadingAnchor],
230203
[self->_container.trailingAnchor constraintEqualToAnchor:self->_window.rootViewController.view.trailingAnchor],
231-
self -> _containerHeightConstraint,
232204

233205
// Label constraints
234206
[self->_label.topAnchor constraintEqualToAnchor:self->_container.topAnchor constant:topSafeAreaHeight + 8],
@@ -252,10 +224,6 @@ - (void)showMessage:(NSString *)message
252224
[NSLayoutConstraint activateConstraints:constraints];
253225

254226
[self->_window layoutIfNeeded];
255-
self->_window.frame = CGRectMake(0, 0, mainWindow.frame.size.width, self->_container.frame.size.height);
256-
257-
// Observe window frame changes using KVO
258-
[mainWindow addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
259227
});
260228
}
261229

@@ -267,7 +235,6 @@ - (void)showMessage:(NSString *)message
267235
backgroundColor:[RCTConvert UIColor:backgroundColor]
268236
dismissButton:[dismissButton boolValue]];
269237
}
270-
271238
RCT_EXPORT_METHOD(hide)
272239
{
273240
if (!RCTDevLoadingViewGetEnabled()) {
@@ -282,28 +249,17 @@ - (void)showMessage:(NSString *)message
282249
const NSTimeInterval MIN_PRESENTED_TIME = 0.6;
283250
NSTimeInterval presentedTime = [[NSDate date] timeIntervalSinceDate:self->_showDate];
284251
NSTimeInterval delay = MAX(0, MIN_PRESENTED_TIME - presentedTime);
285-
CGRect windowFrame = self->_window.frame;
252+
CGFloat height = self->_container.bounds.size.height;
286253
[UIView animateWithDuration:0.25
287254
delay:delay
288255
options:0
289256
animations:^{
290-
self->_window.frame = CGRectOffset(windowFrame, 0, -windowFrame.size.height);
257+
self->_container.transform = CGAffineTransformMakeTranslation(0, -height);
291258
}
292259
completion:^(__unused BOOL finished) {
293-
// Remove KVO observers before cleanup
294-
UIWindow *mainWindow = RCTKeyWindow();
295-
if (mainWindow != nil) {
296-
@try {
297-
[mainWindow removeObserver:self forKeyPath:@"frame"];
298-
} @catch (NSException *exception) {
299-
// Observer might not have been added, ignore
300-
}
301-
}
302-
303-
self->_window.frame = windowFrame;
260+
self->_container.transform = CGAffineTransformIdentity;
304261
self->_window.hidden = YES;
305262
self->_window = nil;
306-
self->_containerHeightConstraint = nil;
307263
self->_container = nil;
308264
self->_label = nil;
309265
self->_dismissButton = nil;
@@ -355,20 +311,6 @@ - (BOOL)isDarkModeEnabled
355311
// (which always passes nil). This would result in an inconsistent UI.
356312
return [RCTColorSchemePreference(nil) isEqualToString:@"dark"];
357313
}
358-
359-
- (void)observeValueForKeyPath:(NSString *)keyPath
360-
ofObject:(id)object
361-
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
362-
context:(void *)context
363-
{
364-
if ([keyPath isEqualToString:@"frame"]) {
365-
dispatch_async(dispatch_get_main_queue(), ^{
366-
UIWindow *mainWindow = RCTKeyWindow();
367-
[self->_resizeThrottler sourceSizeChangedToWidth:mainWindow.frame.size.width
368-
height:mainWindow.safeAreaInsets.top + LOADING_VIEW_HEIGHT];
369-
});
370-
}
371-
}
372314
- (void)showWithURL:(NSURL *)URL
373315
{
374316
if (URL.fileURL) {

0 commit comments

Comments
 (0)