From 96999339b6a7aeabd0cd706ef7736fd91d9ecf80 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Tue, 28 Apr 2020 23:16:58 -0700 Subject: [PATCH] Add hostname to loading banner on iOS Summary: This diff adds a hostname to the loading banner on iOS so it's clear which server you're loading from. Changelog: [Added] [iOS] Added hostname to loading banner. Reviewed By: PeteTheHeat Differential Revision: D21280252 fbshipit-source-id: d7733c056f5fb63e32b247a4fa1476ab42c7da17 --- React/CoreModules/RCTDevLoadingView.mm | 46 ++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/React/CoreModules/RCTDevLoadingView.mm b/React/CoreModules/RCTDevLoadingView.mm index d150607ab233aa..a17614e87d77fe 100644 --- a/React/CoreModules/RCTDevLoadingView.mm +++ b/React/CoreModules/RCTDevLoadingView.mm @@ -29,6 +29,7 @@ @interface RCTDevLoadingView () @implementation RCTDevLoadingView { UIWindow *_window; UILabel *_label; + UILabel *_host; NSDate *_showDate; } @@ -64,6 +65,24 @@ - (void)setBridge:(RCTBridge *)bridge } } +- (UIColor *)dimColor:(UIColor *)c +{ + // Given a color, return a slightly lighter or darker color for dim effect. + CGFloat h, s, b, a; + if ([c getHue:&h saturation:&s brightness:&b alpha:&a]) + return [UIColor colorWithHue:h saturation:s brightness:b < 0.5 ? b * 1.25 : b * 0.75 alpha:a]; + return nil; +} + +- (NSString *)getTextForHost +{ + if (self->_bridge.bundleURL == nil || self->_bridge.bundleURL.fileURL) { + return @"React Native"; + } + + return [NSString stringWithFormat:@"%@:%@", self->_bridge.bundleURL.host, self->_bridge.bundleURL.port]; +} + - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor { if (!RCTDevLoadingViewGetEnabled()) { @@ -78,18 +97,25 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:( if (@available(iOS 11.0, *)) { UIWindow *window = RCTSharedApplication().keyWindow; self->_window = - [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, window.safeAreaInsets.top + 30)]; - self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top, screenSize.width, 30)]; + [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, window.safeAreaInsets.top + 52)]; + self->_label = [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top, screenSize.width, 22)]; + self->_host = + [[UILabel alloc] initWithFrame:CGRectMake(0, window.safeAreaInsets.top + 20, screenSize.width, 22)]; + self->_host.font = [UIFont monospacedDigitSystemFontOfSize:10.0 weight:UIFontWeightRegular]; + self->_host.textAlignment = NSTextAlignmentCenter; + + [self->_window addSubview:self->_label]; + [self->_window addSubview:self->_host]; + } else { self->_window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, 22)]; self->_label = [[UILabel alloc] initWithFrame:self->_window.bounds]; + + [self->_window addSubview:self->_label]; + // TODO: Add host to iOS < 11.0 } - [self->_window addSubview:self->_label]; -#if TARGET_OS_TV - self->_window.windowLevel = UIWindowLevelNormal + 1; -#else + self->_window.windowLevel = UIWindowLevelStatusBar + 1; -#endif // set a root VC so rotation is supported self->_window.rootViewController = [UIViewController new]; @@ -99,6 +125,12 @@ - (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:( self->_label.text = message; self->_label.textColor = color; + + if (self->_host != nil) { + self->_host.text = [self getTextForHost]; + self->_host.textColor = [self dimColor:color]; + } + self->_window.backgroundColor = backgroundColor; self->_window.hidden = NO;