OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/search_widget_extension/search_widget_view_controller.h" | 5 #import "ios/chrome/search_widget_extension/search_widget_view_controller.h" |
6 | 6 |
7 #import <NotificationCenter/NotificationCenter.h> | 7 #import <NotificationCenter/NotificationCenter.h> |
8 | 8 |
9 #include "base/ios/ios_util.h" | 9 #include "base/ios/ios_util.h" |
10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
12 #include "components/open_from_clipboard/clipboard_recent_content_impl_ios.h" | 12 #include "components/open_from_clipboard/clipboard_recent_content_impl_ios.h" |
13 #include "ios/chrome/common/app_group/app_group_constants.h" | 13 #include "ios/chrome/common/app_group/app_group_constants.h" |
14 #import "ios/chrome/search_widget_extension/search_widget_view.h" | 14 #import "ios/chrome/search_widget_extension/search_widget_view.h" |
| 15 #import "ios/chrome/search_widget_extension/ui_util.h" |
15 | 16 |
16 #if !defined(__has_feature) || !__has_feature(objc_arc) | 17 #if !defined(__has_feature) || !__has_feature(objc_arc) |
17 #error "This file requires ARC support." | 18 #error "This file requires ARC support." |
18 #endif | 19 #endif |
19 | 20 |
20 namespace { | 21 namespace { |
21 // Using GURL in the extension is not wanted as it includes ICU which makes the | 22 // Using GURL in the extension is not wanted as it includes ICU which makes the |
22 // extension binary much larger; therefore, ios/chrome/common/x_callback_url.h | 23 // extension binary much larger; therefore, ios/chrome/common/x_callback_url.h |
23 // cannot be used. This class makes a very basic use of x-callback-url, so no | 24 // cannot be used. This class makes a very basic use of x-callback-url, so no |
24 // full implementation is required. | 25 // full implementation is required. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 delegate:nil]; | 62 delegate:nil]; |
62 } | 63 } |
63 return self; | 64 return self; |
64 } | 65 } |
65 | 66 |
66 #pragma mark - UIViewController | 67 #pragma mark - UIViewController |
67 | 68 |
68 - (void)viewDidLoad { | 69 - (void)viewDidLoad { |
69 [super viewDidLoad]; | 70 [super viewDidLoad]; |
70 | 71 |
| 72 UIVibrancyEffect* primary; |
| 73 UIVibrancyEffect* secondary; |
| 74 if (base::ios::IsRunningOnIOS10OrLater()) { |
| 75 primary = [UIVibrancyEffect widgetPrimaryVibrancyEffect]; |
| 76 secondary = [UIVibrancyEffect widgetSecondaryVibrancyEffect]; |
| 77 } else { |
| 78 primary = [UIVibrancyEffect notificationCenterVibrancyEffect]; |
| 79 secondary = [UIVibrancyEffect notificationCenterVibrancyEffect]; |
| 80 } |
| 81 |
71 // A local variable is necessary here as the property is declared weak and the | 82 // A local variable is necessary here as the property is declared weak and the |
72 // object would be deallocated before being retained by the addSubview call. | 83 // object would be deallocated before being retained by the addSubview call. |
73 SearchWidgetView* widgetView = | 84 SearchWidgetView* widgetView = |
74 [[SearchWidgetView alloc] initWithActionTarget:self]; | 85 [[SearchWidgetView alloc] initWithActionTarget:self |
| 86 primaryVibrancyEffect:primary |
| 87 secondaryVibrancyEffect:secondary]; |
75 self.widgetView = widgetView; | 88 self.widgetView = widgetView; |
76 [self.view addSubview:self.widgetView]; | 89 [self.view addSubview:self.widgetView]; |
77 | 90 |
78 if (base::ios::IsRunningOnIOS10OrLater()) { | 91 if (base::ios::IsRunningOnIOS10OrLater()) { |
79 self.extensionContext.widgetLargestAvailableDisplayMode = | 92 self.extensionContext.widgetLargestAvailableDisplayMode = |
80 NCWidgetDisplayModeExpanded; | 93 NCWidgetDisplayModeExpanded; |
81 } | 94 } |
82 | 95 |
83 self.widgetView.translatesAutoresizingMaskIntoConstraints = NO; | 96 self.widgetView.translatesAutoresizingMaskIntoConstraints = NO; |
84 | 97 |
85 NSLayoutConstraint* heightAnchor = [self.widgetView.heightAnchor | 98 [NSLayoutConstraint activateConstraints:ui_util::CreateSameConstraints( |
86 constraintEqualToAnchor:self.view.heightAnchor]; | 99 self.view, self.widgetView)]; |
87 heightAnchor.priority = 900; | |
88 | |
89 [NSLayoutConstraint activateConstraints:@[ | |
90 [self.widgetView.leadingAnchor | |
91 constraintEqualToAnchor:self.view.leadingAnchor], | |
92 [self.widgetView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor], | |
93 [self.widgetView.trailingAnchor | |
94 constraintEqualToAnchor:self.view.trailingAnchor], | |
95 heightAnchor, | |
96 [self.widgetView.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
97 ]]; | |
98 } | 100 } |
99 | 101 |
100 - (void)viewWillAppear:(BOOL)animated { | 102 - (void)viewWillAppear:(BOOL)animated { |
101 [super viewWillAppear:animated]; | 103 [super viewWillAppear:animated]; |
102 [self updateWidget]; | 104 [self updateWidget]; |
103 } | 105 } |
104 | 106 |
105 - (void)widgetPerformUpdateWithCompletionHandler: | 107 - (void)widgetPerformUpdateWithCompletionHandler: |
106 (void (^)(NCUpdateResult))completionHandler { | 108 (void (^)(NCUpdateResult))completionHandler { |
107 completionHandler([self updateWidget] ? NCUpdateResultNewData | 109 completionHandler([self updateWidget] ? NCUpdateResultNewData |
108 : NCUpdateResultNoData); | 110 : NCUpdateResultNoData); |
109 } | 111 } |
110 | 112 |
111 - (BOOL)updateWidget { | 113 - (BOOL)updateWidget { |
112 NSURL* url = [_clipboardRecentContent recentURLFromClipboard]; | 114 NSURL* url = [_clipboardRecentContent recentURLFromClipboard]; |
113 | 115 |
114 if (![url isEqual:self.copiedURL]) { | 116 if (![url isEqual:self.copiedURL]) { |
115 self.copiedURL = url; | 117 self.copiedURL = url; |
116 [self.widgetView updateCopiedURL:self.copiedURL.absoluteString]; | 118 [self.widgetView setCopiedURLString:self.copiedURL.absoluteString]; |
117 return YES; | 119 return YES; |
118 } | 120 } |
119 return NO; | 121 return NO; |
120 } | 122 } |
121 | 123 |
122 #pragma mark - NCWidgetProviding | 124 #pragma mark - NCWidgetProviding |
123 | 125 |
124 - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode | 126 - (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode |
125 withMaximumSize:(CGSize)maxSize { | 127 withMaximumSize:(CGSize)maxSize { |
126 CGSize fittingSize = [self.widgetView | 128 BOOL isVariableHeight = (activeDisplayMode == NCWidgetDisplayModeExpanded); |
127 systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; | 129 // Set the copied URL section here so that the fitting size is correctly |
128 if (fittingSize.height > maxSize.height) { | 130 // calculated. |
129 self.preferredContentSize = maxSize; | 131 [self.widgetView setCopiedURLVisible:isVariableHeight]; |
130 } else { | 132 |
131 self.preferredContentSize = fittingSize; | 133 // If the widget's height is not variable, the preferredContentSize is the |
| 134 // maxSize. Widgets cannot be shrunk, and this ensures the view will lay |
| 135 // itself out according to the actual screen size. (This is only likely to |
| 136 // happen if the accessibility option for larger font is used.) If the widget |
| 137 // is not a fixed size, if the fitting size for the widget's contents is |
| 138 // larger than the maximum size for the current widget display mode, this |
| 139 // maximum size is used for the widget. Otherwise, the preferredContentSize is |
| 140 // set to the fitting size so that the widget gets the correct height. |
| 141 if (isVariableHeight) { |
| 142 CGSize fittingSize = [self.widgetView |
| 143 systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; |
| 144 if (fittingSize.height < maxSize.height) { |
| 145 self.preferredContentSize = fittingSize; |
| 146 return; |
| 147 } |
132 } | 148 } |
| 149 self.preferredContentSize = maxSize; |
133 } | 150 } |
134 | 151 |
135 #pragma mark - WidgetViewActionTarget | 152 #pragma mark - WidgetViewActionTarget |
136 | 153 |
137 - (void)openSearch:(id)sender { | 154 - (void)openSearch:(id)sender { |
138 [self openAppWithCommand:base::SysUTF8ToNSString( | 155 [self openAppWithCommand:base::SysUTF8ToNSString( |
139 app_group::kChromeAppGroupFocusOmniboxCommand)]; | 156 app_group::kChromeAppGroupFocusOmniboxCommand)]; |
140 } | 157 } |
141 | 158 |
142 - (void)openIncognito:(id)sender { | 159 - (void)openIncognito:(id)sender { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 }; | 232 }; |
216 } | 233 } |
217 return @{ | 234 return @{ |
218 timePrefKey : [NSDate date], | 235 timePrefKey : [NSDate date], |
219 appPrefKey : @"TodayExtension", | 236 appPrefKey : @"TodayExtension", |
220 commandPrefKey : command, | 237 commandPrefKey : command, |
221 }; | 238 }; |
222 } | 239 } |
223 | 240 |
224 @end | 241 @end |
OLD | NEW |