sc_omnibox_popup_container_view_controller.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2019 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. #import "ios/showcase/omnibox_popup/sc_omnibox_popup_container_view_controller.h"
  5. #import "ios/chrome/browser/ui/omnibox/popup/omnibox_popup_view_controller.h"
  6. #import "ios/chrome/browser/ui/toolbar/buttons/toolbar_configuration.h"
  7. #import "ios/chrome/browser/ui/util/named_guide.h"
  8. #import "ios/chrome/browser/ui/util/named_guide_util.h"
  9. #import "ios/chrome/common/ui/util/constraints_ui_util.h"
  10. #if !defined(__has_feature) || !__has_feature(objc_arc)
  11. #error "This file requires ARC support."
  12. #endif
  13. namespace {
  14. CGFloat kFakeImageWidth = 30;
  15. CGFloat kFakeSpacing = 16;
  16. CGFloat kFakeImageLeadingSpacing = 15;
  17. CGFloat kFakeImageToTextSpacing = 14;
  18. CGFloat kFakeTextBoxWidth = 240;
  19. } // namespace
  20. @implementation SCOmniboxPopupContainerViewController
  21. - (instancetype)initWithPopupViewController:
  22. (OmniboxPopupViewController*)popupViewController {
  23. self = [super initWithNibName:nil bundle:nil];
  24. if (self) {
  25. _popupViewController = popupViewController;
  26. }
  27. return self;
  28. }
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // The omnibox popup uses layout guides from the omnibox container view to
  32. // position its elements. Showcase needs to set up these layout guides as well
  33. // so the positioning can be correct.
  34. UIView* fakeImageView = [[UIView alloc] initWithFrame:CGRectZero];
  35. fakeImageView.translatesAutoresizingMaskIntoConstraints = NO;
  36. fakeImageView.backgroundColor = UIColor.blueColor;
  37. UIView* fakeTextView = [[UIView alloc] initWithFrame:CGRectZero];
  38. fakeTextView.translatesAutoresizingMaskIntoConstraints = NO;
  39. fakeTextView.backgroundColor = UIColor.redColor;
  40. [self.view addSubview:fakeImageView];
  41. [self.view addSubview:fakeTextView];
  42. [NSLayoutConstraint activateConstraints:@[
  43. [fakeImageView.heightAnchor constraintEqualToConstant:kFakeImageWidth],
  44. [fakeImageView.widthAnchor
  45. constraintEqualToAnchor:fakeImageView.heightAnchor],
  46. [fakeImageView.topAnchor constraintEqualToAnchor:self.view.topAnchor
  47. constant:kFakeSpacing],
  48. [fakeImageView.leadingAnchor
  49. constraintEqualToAnchor:self.view.leadingAnchor
  50. constant:kFakeImageLeadingSpacing],
  51. [fakeTextView.heightAnchor
  52. constraintEqualToAnchor:fakeImageView.heightAnchor],
  53. [fakeTextView.widthAnchor constraintEqualToConstant:kFakeTextBoxWidth],
  54. [fakeTextView.leadingAnchor
  55. constraintEqualToAnchor:fakeImageView.trailingAnchor
  56. constant:kFakeImageToTextSpacing],
  57. [fakeTextView.topAnchor constraintEqualToAnchor:fakeImageView.topAnchor],
  58. ]];
  59. AddNamedGuidesToView(@[ kOmniboxLeadingImageGuide, kOmniboxTextFieldGuide ],
  60. self.view);
  61. [NamedGuide guideWithName:kOmniboxLeadingImageGuide view:self.view]
  62. .constrainedView = fakeImageView;
  63. [NamedGuide guideWithName:kOmniboxTextFieldGuide view:self.view]
  64. .constrainedView = fakeTextView;
  65. // Popup uses same colors as the toolbar, so the ToolbarConfiguration is
  66. // used to get the style.
  67. ToolbarConfiguration* configuration =
  68. [[ToolbarConfiguration alloc] initWithStyle:NORMAL];
  69. UIView* containerView = [[UIView alloc] init];
  70. [containerView addSubview:self.popupViewController.view];
  71. containerView.backgroundColor = [configuration backgroundColor];
  72. containerView.translatesAutoresizingMaskIntoConstraints = NO;
  73. self.popupViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
  74. AddSameConstraints(self.popupViewController.view, containerView);
  75. self.view.backgroundColor = UIColor.whiteColor;
  76. [self addChildViewController:self.popupViewController];
  77. [self.view addSubview:containerView];
  78. [self.popupViewController didMoveToParentViewController:self];
  79. [NSLayoutConstraint activateConstraints:@[
  80. [self.view.leadingAnchor
  81. constraintEqualToAnchor:containerView.leadingAnchor],
  82. [self.view.trailingAnchor
  83. constraintEqualToAnchor:containerView.trailingAnchor],
  84. [self.view.bottomAnchor constraintEqualToAnchor:containerView.bottomAnchor],
  85. [containerView.topAnchor constraintEqualToAnchor:fakeImageView.bottomAnchor
  86. constant:kFakeSpacing],
  87. ]];
  88. }
  89. @end