Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support breakpoints when iPadOS window frame size changes #209

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions ios/platform/Platform_iOS.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ @implementation Platform
- (instancetype)init {
self = [super init];
if (self) {
UIScreen *screen = [UIScreen mainScreen];
UIViewController *presentedViewController = RCTPresentedViewController();
jpudysz marked this conversation as resolved.
Show resolved Hide resolved
CGRect windowFrame = presentedViewController.view.window.frame;

UIContentSizeCategory contentSizeCategory = [[UIApplication sharedApplication] preferredContentSizeCategory];

self.initialScreen = {(int)screen.bounds.size.width, (int)screen.bounds.size.height};
self.initialScreen = {(int)windowFrame.size.width, (int)windowFrame.size.height};
self.initialColorScheme = [self getColorScheme];
self.initialContentSizeCategory = [self getContentSizeCategory:contentSizeCategory];
self.initialStatusBar = [self getStatusBarDimensions];
self.initialInsets = [self getInsets];

[self setupListeners];
}
return self;
Expand All @@ -31,6 +33,9 @@ - (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name: UIContentSizeCategoryDidChangeNotification
object: nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:RCTWindowFrameDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name: RCTUserInterfaceStyleDidChangeNotification
object: nil];
Expand All @@ -41,9 +46,13 @@ - (void)dealloc {

- (void)setupListeners {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onOrientationChange:)
selector:@selector(onWindowChange:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onWindowChange:)
name:RCTWindowFrameDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(onAppearanceChange:)
name:RCTUserInterfaceStyleDidChangeNotification
Expand All @@ -70,10 +79,12 @@ - (void)onContentSizeCategoryChange:(NSNotification *)notification {
}
}

- (void)onOrientationChange:(NSNotification *)notification {
- (void)onWindowChange:(NSNotification *)notification {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIScreen *mainScreen = [UIScreen mainScreen];
Dimensions screen = {(int)mainScreen.bounds.size.width, (int)mainScreen.bounds.size.height};
UIViewController *presentedViewController = RCTPresentedViewController();

CGRect windowFrame = presentedViewController.view.frame;
Dimensions screen = {(int)windowFrame.size.width, (int)windowFrame.size.height};
Insets insets = [self getInsets];
Dimensions statusBar = [self getStatusBarDimensions];
Dimensions navigationBar = [self getNavigationBarDimensions];
Expand Down Expand Up @@ -101,13 +112,13 @@ - (void)onOrientationChange:(NSNotification *)notification {
- (Insets)getInsets {
UIWindow *window = UIApplication.sharedApplication.windows.firstObject;
UIEdgeInsets safeArea = window.safeAreaInsets;
jpudysz marked this conversation as resolved.
Show resolved Hide resolved

return {(int)safeArea.top, (int)safeArea.bottom, (int)safeArea.left, (int)safeArea.right};
}

- (Dimensions)getStatusBarDimensions {
CGRect statusBarFrame = UIApplication.sharedApplication.statusBarFrame;

return {(int)statusBarFrame.size.width, (int)statusBarFrame.size.height};
}

Expand All @@ -119,51 +130,51 @@ - (Dimensions)getNavigationBarDimensions {
if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraExtraLarge]) {
return std::string([@"xxxLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraExtraLarge]) {
return std::string([@"xxLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraLarge]) {
return std::string([@"xLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryLarge]) {
return std::string([@"Large" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryMedium]) {
return std::string([@"Medium" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategorySmall]) {
return std::string([@"Small" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryExtraSmall]) {
return std::string([@"xSmall" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityMedium]) {
return std::string([@"accessibilityMedium" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityLarge]) {
return std::string([@"accessibilityLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraLarge]) {
return std::string([@"accessibilityExtraLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraLarge]) {
return std::string([@"accessibilityExtraExtraLarge" UTF8String]);
}

if ([contentSizeCategory isEqualToString:UIContentSizeCategoryAccessibilityExtraExtraExtraLarge]) {
return std::string([@"accessibilityExtraExtraExtraLarge" UTF8String]);
}

return std::string([@"unspecified" UTF8String]);
}

Expand Down
Loading