view_utils.mm 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/ios/app/view_utils.h"
  5. namespace {
  6. UIWindow* GetAnyKeyWindow() {
  7. #if !defined(__IPHONE_13_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_13_0
  8. return [UIApplication sharedApplication].keyWindow;
  9. #else
  10. NSArray<UIWindow*>* windows = [UIApplication sharedApplication].windows;
  11. for (UIWindow* window in windows) {
  12. if (window.isKeyWindow)
  13. return window;
  14. }
  15. return nil;
  16. #endif
  17. }
  18. } // namespace
  19. namespace remoting {
  20. UIViewController* TopPresentingVC() {
  21. UIViewController* topController = GetAnyKeyWindow().rootViewController;
  22. while (topController.presentedViewController) {
  23. topController = topController.presentedViewController;
  24. }
  25. return topController;
  26. }
  27. UILayoutGuide* SafeAreaLayoutGuideForView(UIView* view) {
  28. if (@available(iOS 11, *)) {
  29. return view.safeAreaLayoutGuide;
  30. } else {
  31. NSString* kChromeSafeAreaLayoutGuideShim =
  32. @"ChromotingSafeAreaLayoutGuideShim";
  33. // Search for an existing shim safe area layout guide:
  34. for (UILayoutGuide* guide in view.layoutGuides) {
  35. if ([guide.identifier isEqualToString:kChromeSafeAreaLayoutGuideShim]) {
  36. return guide;
  37. }
  38. }
  39. // If no existing shim exist, create and return a new one.
  40. UILayoutGuide* safeAreaLayoutShim = [[UILayoutGuide alloc] init];
  41. safeAreaLayoutShim.identifier = kChromeSafeAreaLayoutGuideShim;
  42. [view addLayoutGuide:safeAreaLayoutShim];
  43. [NSLayoutConstraint activateConstraints:@[
  44. [safeAreaLayoutShim.leadingAnchor
  45. constraintEqualToAnchor:view.leadingAnchor],
  46. [safeAreaLayoutShim.trailingAnchor
  47. constraintEqualToAnchor:view.trailingAnchor],
  48. [safeAreaLayoutShim.topAnchor constraintEqualToAnchor:view.topAnchor],
  49. [safeAreaLayoutShim.bottomAnchor
  50. constraintEqualToAnchor:view.bottomAnchor]
  51. ]];
  52. return safeAreaLayoutShim;
  53. }
  54. }
  55. UIEdgeInsets SafeAreaInsetsForView(UIView* view) {
  56. if (@available(iOS 11, *)) {
  57. return view.safeAreaInsets;
  58. }
  59. return UIEdgeInsetsZero;
  60. }
  61. void PostDelayedAccessibilityNotification(NSString* announcement) {
  62. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC),
  63. dispatch_get_main_queue(), ^{
  64. UIAccessibilityPostNotification(
  65. UIAccessibilityAnnouncementNotification, announcement);
  66. });
  67. }
  68. void SetAccessibilityInfoFromImage(UIBarButtonItem* button) {
  69. button.accessibilityLabel = button.image.accessibilityLabel;
  70. }
  71. void SetAccessibilityInfoFromImage(UIButton* button) {
  72. button.accessibilityLabel =
  73. [button imageForState:UIControlStateNormal].accessibilityLabel;
  74. }
  75. void SetAccessibilityFocusElement(id element) {
  76. UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification,
  77. element);
  78. }
  79. } // namespace remoting