Skip to content

Commit 7120d1c

Browse files
committed
fix: avoid reallocating views on RCTDevLoadingView progress updates
1 parent 0ee9be8 commit 7120d1c

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

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

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,40 @@ - (void)showMessage:(NSString *)message color:(RCTUIColor *)color backgroundColo
123123

124124
self->_showDate = [NSDate date];
125125

126-
#if !TARGET_OS_OSX // [macOS]
127-
UIWindow *mainWindow = RCTKeyWindow();
128-
self->_window = [[UIWindow alloc] initWithWindowScene:mainWindow.windowScene];
129-
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
130-
self->_window.rootViewController = [UIViewController new];
131-
#else // [macOS
132-
self->_window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 375, 20)
133-
styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskFullSizeContentView
134-
backing:NSBackingStoreBuffered
135-
defer:YES];
136-
[self->_window setIdentifier:sRCTDevLoadingViewWindowIdentifier];
137-
#endif // macOS]
138-
139-
self->_container = [[RCTUIView alloc] init]; // [macOS]
140-
self->_container.backgroundColor = backgroundColor;
141-
self->_container.translatesAutoresizingMaskIntoConstraints = NO;
142-
143-
self->_label = [[RCTUILabel alloc] init]; // [macOS]
144-
self->_label.translatesAutoresizingMaskIntoConstraints = NO;
145-
self->_label.font = [UIFont monospacedDigitSystemFontOfSize:12.0 weight:UIFontWeightRegular];
146-
self->_label.textAlignment = NSTextAlignmentCenter;
126+
if (!self->label) {
127+
self->_label = [[RCTUILabel alloc] init]; // [macOS]
128+
self->_label.translatesAutoresizingMaskIntoConstraints = NO;
129+
self->_label.font = [UIFont monospacedDigitSystemFontOfSize:12.0 weight:UIFontWeightRegular];
130+
self->_label.textAlignment = NSTextAlignmentCenter;
131+
}
147132
self->_label.textColor = color;
148133
self->_label.text = message;
149134

135+
if (!self->_container) {
136+
self->_container = [[RCTUIView alloc] init]; // [macOS]
137+
self->_container.translatesAutoresizingMaskIntoConstraints = NO;
138+
[self->_container addSubview:self->_label];
139+
}
140+
self->_container.backgroundColor = backgroundColor;
141+
150142
#if !TARGET_OS_OSX // [macOS]
151-
[self->_window.rootViewController.view addSubview:self->_container];
143+
UIWindow *mainWindow = RCTKeyWindow();
144+
if (!self->_window) {
145+
self->_window = [[UIWindow alloc] initWithWindowScene:mainWindow.windowScene];
146+
self->_window.windowLevel = UIWindowLevelStatusBar + 1;
147+
self->_window.rootViewController = [UIViewController new];
148+
[self->_window.rootViewController.view addSubview:self->_container];
149+
}
152150
#else // [macOS
153-
[self->_window.contentView addSubview:self->_container];
151+
if (!self->_window) {
152+
self->_window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 375, 20)
153+
styleMask:NSWindowStyleMaskBorderless | NSWindowStyleMaskFullSizeContentView
154+
backing:NSBackingStoreBuffered
155+
defer:YES];
156+
[self->_window setIdentifier:sRCTDevLoadingViewWindowIdentifier];
157+
[self->_window.contentView addSubview:self->_container];
158+
}
154159
#endif // macOS]
155-
[self->_container addSubview:self->_label];
156160

157161
#if !TARGET_OS_OSX // [macOS]
158162
CGFloat topSafeAreaHeight = mainWindow.safeAreaInsets.top;

0 commit comments

Comments
 (0)