Skip to content

Commit

Permalink
[MDCKeyboardWatcher] Migrate off of deprecated mainScreen usage.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 596024734
  • Loading branch information
Nobody authored and material-automation committed Jan 5, 2024
1 parent 00fac7e commit 10161d4
Showing 1 changed file with 4 additions and 31 deletions.
35 changes: 4 additions & 31 deletions components/private/KeyboardWatcher/src/MDCKeyboardWatcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#import <CoreGraphics/CoreGraphics.h>

#import "MDCAvailability.h"
#import "MaterialApplication.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down Expand Up @@ -95,44 +94,18 @@ - (void)updateKeyboardOffsetWithKeyboardUserInfo:(NSDictionary *)userInfo {
return;
}

UIWindow *keyWindow = nil;
#if MDC_AVAILABLE_SDK_IOS(15_0)
if (@available(ios 15.0, *)) {
for (UIScene *scene in [UIApplication mdc_safeSharedApplication].connectedScenes) {
if ([scene isKindOfClass:[UIWindowScene class]] &&
scene.activationState == UISceneActivationStateForegroundActive) {
keyWindow = ((UIWindowScene *)scene).keyWindow;
break;
}
}
} else {
for (UIWindow *window in [UIApplication mdc_safeSharedApplication].windows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
}
#else
for (UIWindow *window in [UIApplication mdc_safeSharedApplication].windows) {
if (window.isKeyWindow) {
keyWindow = window;
break;
}
}
#endif
CGRect screenBounds = keyWindow.screen.bounds;
// Keyboard occupies full-width of the screen under both single scene and multiple scenes.
CGRect keyWindowBounds = [UIApplication mdc_safeSharedApplication].keyWindow.bounds;
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGRect intersection = CGRectIntersection(screenBounds, keyboardRect);

// If the extent of the keyboard is at or below the bottom of the screen it is docked.
// This handles the case of an external keyboard on iOS8+ where the entire frame of the keyboard
// view is used, but on the top, the input accessory section is show.
BOOL dockedKeyboard = CGRectGetMaxY(keyWindow.bounds) <= CGRectGetMaxY(keyboardRect);
BOOL dockedKeyboard = CGRectGetMaxY(keyWindowBounds) <= CGRectGetMaxY(keyboardRect);

// If the bottom of the keyboard isn't at the bottom of the screen, then it is undocked, and we
// shouldn't try to account for it.
if (keyWindow != nil && dockedKeyboard && !CGRectIsEmpty(intersection)) {
if (dockedKeyboard && !CGRectIsEmpty(intersection)) {
self.keyboardFrame = intersection;
} else {
self.keyboardFrame = CGRectZero;
Expand Down

0 comments on commit 10161d4

Please sign in to comment.