OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h" | 5 #import "chrome/browser/ui/cocoa/autofill/autofill_notification_controller.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/mac/foundation_util.h" | 10 #include "base/mac/foundation_util.h" |
11 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #include "chrome/browser/ui/autofill/autofill_dialog_types.h" | 13 #include "chrome/browser/ui/autofill/autofill_dialog_types.h" |
14 #include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h" | 14 #include "chrome/browser/ui/autofill/autofill_dialog_view_delegate.h" |
15 #include "chrome/browser/ui/chrome_style.h" | 15 #include "chrome/browser/ui/chrome_style.h" |
16 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h" | 16 #include "chrome/browser/ui/cocoa/autofill/autofill_dialog_constants.h" |
| 17 #import "chrome/browser/ui/cocoa/autofill/autofill_tooltip_controller.h" |
17 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" | 18 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
19 #include "skia/ext/skia_utils_mac.h" | 20 #include "skia/ext/skia_utils_mac.h" |
20 | 21 |
21 @interface AutofillNotificationView : NSView { | 22 @interface AutofillNotificationView : NSView { |
22 @private | 23 @private |
23 // Weak, determines anchor point for arrow. | 24 // Weak, determines anchor point for arrow. |
24 NSView* arrowAnchorView_; | 25 NSView* arrowAnchorView_; |
25 BOOL hasArrow_; | 26 BOOL hasArrow_; |
26 base::scoped_nsobject<NSColor> backgroundColor_; | 27 base::scoped_nsobject<NSColor> backgroundColor_; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 [checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)]; | 135 [checkbox_ setState:(notification->checked() ? NSOnState : NSOffState)]; |
135 [checkbox_ setAttributedTitle:[textview_ textStorage]]; | 136 [checkbox_ setAttributedTitle:[textview_ textStorage]]; |
136 [checkbox_ setTarget:self]; | 137 [checkbox_ setTarget:self]; |
137 [checkbox_ setAction:@selector(checkboxClicked:)]; | 138 [checkbox_ setAction:@selector(checkboxClicked:)]; |
138 // Set the size that preferredSizeForWidth will use. Do this here because | 139 // Set the size that preferredSizeForWidth will use. Do this here because |
139 // (1) preferredSizeForWidth is logically const, and so shouldn't have a | 140 // (1) preferredSizeForWidth is logically const, and so shouldn't have a |
140 // side-effect of updating the checkbox's frame, and | 141 // side-effect of updating the checkbox's frame, and |
141 // (2) this way, the sizing computation can be cached. | 142 // (2) this way, the sizing computation can be cached. |
142 [checkbox_ sizeToFit]; | 143 [checkbox_ sizeToFit]; |
143 | 144 |
144 tooltipIcon_.reset([[NSImageView alloc] initWithFrame:NSZeroRect]); | 145 tooltipController_.reset([[AutofillTooltipController alloc] init]); |
145 [tooltipIcon_ setImage: | 146 [tooltipController_ setImage: |
146 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( | 147 ui::ResourceBundle::GetSharedInstance().GetNativeImageNamed( |
147 IDR_AUTOFILL_TOOLTIP_ICON).ToNSImage()]; | 148 IDR_AUTOFILL_TOOLTIP_ICON).ToNSImage()]; |
148 [tooltipIcon_ setFrameSize:[[tooltipIcon_ image] size]]; | 149 [tooltipController_ setMessage: |
149 [tooltipIcon_ setToolTip: | |
150 base::SysUTF16ToNSString(notification->tooltip_text())]; | 150 base::SysUTF16ToNSString(notification->tooltip_text())]; |
151 [tooltipIcon_ setHidden:[[tooltipIcon_ toolTip] length] == 0]; | 151 [[tooltipController_ view] setHidden: |
| 152 [[tooltipController_ message] length] == 0]; |
152 | 153 |
153 [view setSubviews:@[textview_, checkbox_, tooltipIcon_]]; | 154 [view setSubviews:@[ textview_, checkbox_, [tooltipController_ view] ]]; |
154 } | 155 } |
155 return self; | 156 return self; |
156 } | 157 } |
157 | 158 |
158 - (AutofillNotificationView*)notificationView { | 159 - (AutofillNotificationView*)notificationView { |
159 return base::mac::ObjCCastStrict<AutofillNotificationView>([self view]); | 160 return base::mac::ObjCCastStrict<AutofillNotificationView>([self view]); |
160 } | 161 } |
161 | 162 |
162 - (void)setHasArrow:(BOOL)hasArrow withAnchorView:(NSView*)anchorView { | 163 - (void)setHasArrow:(BOOL)hasArrow withAnchorView:(NSView*)anchorView { |
163 [[self notificationView] setAnchorView:anchorView]; | 164 [[self notificationView] setAnchorView:anchorView]; |
164 [[self notificationView] setHasArrow:hasArrow]; | 165 [[self notificationView] setHasArrow:hasArrow]; |
165 } | 166 } |
166 | 167 |
167 - (BOOL)hasArrow { | 168 - (BOOL)hasArrow { |
168 return [[self notificationView] hasArrow]; | 169 return [[self notificationView] hasArrow]; |
169 } | 170 } |
170 | 171 |
171 - (NSTextView*)textview { | 172 - (NSTextView*)textview { |
172 return textview_; | 173 return textview_; |
173 } | 174 } |
174 | 175 |
175 - (NSButton*)checkbox { | 176 - (NSButton*)checkbox { |
176 return checkbox_; | 177 return checkbox_; |
177 } | 178 } |
178 | 179 |
179 - (NSImageView*)tooltipIcon { | 180 - (NSView*)tooltipView { |
180 return tooltipIcon_; | 181 return [tooltipController_ view]; |
181 } | 182 } |
182 | 183 |
183 - (NSSize)preferredSizeForWidth:(CGFloat)width { | 184 - (NSSize)preferredSizeForWidth:(CGFloat)width { |
184 width -= 2 * chrome_style::kHorizontalPadding; | 185 width -= 2 * chrome_style::kHorizontalPadding; |
185 if (![tooltipIcon_ isHidden]) | 186 if (![[tooltipController_ view] isHidden]) { |
186 width -= [tooltipIcon_ frame].size.width + chrome_style::kHorizontalPadding; | 187 width -= NSWidth([[tooltipController_ view] frame]) + |
| 188 chrome_style::kHorizontalPadding; |
| 189 } |
187 // TODO(isherman): Restore the DCHECK below once I figure out why it causes | 190 // TODO(isherman): Restore the DCHECK below once I figure out why it causes |
188 // unit tests to fail. | 191 // unit tests to fail. |
189 //DCHECK_GT(width, 0); | 192 //DCHECK_GT(width, 0); |
190 | 193 |
191 NSSize preferredSize; | 194 NSSize preferredSize; |
192 if (![textview_ isHidden]) { | 195 if (![textview_ isHidden]) { |
193 // This method is logically const. Hence, cache the original frame so that | 196 // This method is logically const. Hence, cache the original frame so that |
194 // it can be restored once the preferred size has been computed. | 197 // it can be restored once the preferred size has been computed. |
195 NSRect frame = [textview_ frame]; | 198 NSRect frame = [textview_ frame]; |
196 | 199 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 - (void)performLayout { | 231 - (void)performLayout { |
229 NSRect bounds = [[self view] bounds]; | 232 NSRect bounds = [[self view] bounds]; |
230 if ([[self notificationView] hasArrow]) | 233 if ([[self notificationView] hasArrow]) |
231 bounds.size.height -= autofill::kArrowHeight; | 234 bounds.size.height -= autofill::kArrowHeight; |
232 | 235 |
233 // Calculate the frame size, leaving room for padding around the notification, | 236 // Calculate the frame size, leaving room for padding around the notification, |
234 // as well as for the tooltip if it is visible. | 237 // as well as for the tooltip if it is visible. |
235 NSRect labelFrame = NSInsetRect(bounds, | 238 NSRect labelFrame = NSInsetRect(bounds, |
236 chrome_style::kHorizontalPadding, | 239 chrome_style::kHorizontalPadding, |
237 autofill::kNotificationPadding); | 240 autofill::kNotificationPadding); |
238 if (![tooltipIcon_ isHidden]) { | 241 NSView* tooltipView = [tooltipController_ view]; |
| 242 if (![tooltipView isHidden]) { |
239 labelFrame.size.width -= | 243 labelFrame.size.width -= |
240 [tooltipIcon_ frame].size.width + chrome_style::kHorizontalPadding; | 244 NSWidth([tooltipView frame]) + chrome_style::kHorizontalPadding; |
241 } | 245 } |
242 | 246 |
243 NSView* label = [checkbox_ isHidden] ? textview_.get() : checkbox_.get(); | 247 NSView* label = [checkbox_ isHidden] ? textview_.get() : checkbox_.get(); |
244 [label setFrame:labelFrame]; | 248 [label setFrame:labelFrame]; |
245 | 249 |
246 if (![tooltipIcon_ isHidden]) { | 250 if (![tooltipView isHidden]) { |
247 NSPoint tooltipOrigin = | 251 NSPoint tooltipOrigin = |
248 NSMakePoint( | 252 NSMakePoint( |
249 NSMaxX(labelFrame) + chrome_style::kHorizontalPadding, | 253 NSMaxX(labelFrame) + chrome_style::kHorizontalPadding, |
250 NSMidY(labelFrame) - (NSHeight([tooltipIcon_ frame]) / 2.0)); | 254 NSMidY(labelFrame) - (NSHeight([tooltipView frame]) / 2.0)); |
251 [tooltipIcon_ setFrameOrigin:tooltipOrigin]; | 255 [tooltipView setFrameOrigin:tooltipOrigin]; |
252 } | 256 } |
253 } | 257 } |
254 | 258 |
255 - (IBAction)checkboxClicked:(id)sender { | 259 - (IBAction)checkboxClicked:(id)sender { |
256 DCHECK(sender == checkbox_.get()); | 260 DCHECK(sender == checkbox_.get()); |
257 BOOL isChecked = ([checkbox_ state] == NSOnState); | 261 BOOL isChecked = ([checkbox_ state] == NSOnState); |
258 delegate_->NotificationCheckboxStateChanged(notificationType_, isChecked); | 262 delegate_->NotificationCheckboxStateChanged(notificationType_, isChecked); |
259 } | 263 } |
260 | 264 |
261 - (BOOL)textView:(NSTextView *)textView | 265 - (BOOL)textView:(NSTextView *)textView |
262 clickedOnLink:(id)link | 266 clickedOnLink:(id)link |
263 atIndex:(NSUInteger)charIndex { | 267 atIndex:(NSUInteger)charIndex { |
264 delegate_->LinkClicked(linkURL_); | 268 delegate_->LinkClicked(linkURL_); |
265 return YES; | 269 return YES; |
266 } | 270 } |
267 | 271 |
268 @end | 272 @end |
OLD | NEW |