OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 | |
5 #include "chrome/browser/ui/cocoa/autofill/generated_credit_card_bubble_cocoa.h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #include "base/mac/scoped_nsobject.h" | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h" | |
11 #include "chrome/browser/ui/autofill/generated_credit_card_bubble_view.h" | |
12 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" | |
13 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" | |
14 #import "chrome/browser/ui/cocoa/info_bubble_view.h" | |
15 #import "chrome/browser/ui/cocoa/info_bubble_window.h" | |
16 #include "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | |
17 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | |
18 #include "content/public/browser/web_contents_view.h" | |
19 #include "skia/ext/skia_utils_mac.h" | |
20 #include "ui/native_theme/native_theme.h" | |
21 | |
22 using autofill::GeneratedCreditCardBubbleCocoa; | |
23 | |
24 namespace { | |
25 | |
26 const CGFloat kTitlePadding = 20.0; | |
27 const CGFloat kInset = 20.0; | |
28 | |
29 } // namespace | |
30 | |
31 // The Cocoa side of the bubble controller. | |
32 @interface GeneratedCreditCardBubbleControllerCocoa : | |
33 BaseBubbleController<NSTextViewDelegate> { | |
34 // A block that is run when the bubble is being closed. That allows wiping | |
35 // out weak references. (BaseBubbleController self-destroys on close). | |
36 | |
37 // A block that is run when the user clicks on a link. | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
Can probably get rid of the comments, now :-).
groby-ooo-7-16
2013/11/01 00:28:59
Yep. Kept them to copy them over to declarations,
| |
38 | |
39 // Bridge to C++ side. Owned. | |
40 scoped_ptr<GeneratedCreditCardBubbleCocoa> bridge_; | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
I'm not sure this works, as currently written. No
groby-ooo-7-16
2013/11/01 00:28:59
Let's put it that way - if super init fails, we ha
| |
41 } | |
42 | |
43 // Designate initializer. Runs |closeObserver| when -windowWillClose: runs. | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
Can probably drop the comment entirely, since it's
groby-ooo-7-16
2013/11/01 00:28:59
Done.
| |
44 - (id)initWithParentWindow:(NSWindow*)parentWindow | |
45 bridge:(GeneratedCreditCardBubbleCocoa*)bridge; | |
46 | |
47 // Show the bubble at the given |anchor| point. Coordinates are in screen space. | |
48 - (void)showAtAnchor:(NSPoint)anchor; | |
49 | |
50 // Return true if the bubble is in the process of hiding. | |
51 - (BOOL)isHiding; | |
52 | |
53 // Build the window contents and lay them out. | |
54 - (void)performLayoutWithController: | |
55 (autofill::GeneratedCreditCardBubbleController*)controller; | |
56 | |
57 @end | |
58 | |
59 | |
60 @implementation GeneratedCreditCardBubbleControllerCocoa | |
61 | |
62 - (id)initWithParentWindow:(NSWindow*)parentWindow | |
63 bridge:(GeneratedCreditCardBubbleCocoa*)bridge { | |
64 DCHECK(bridge); | |
65 base::scoped_nsobject<InfoBubbleWindow> window( | |
66 [[InfoBubbleWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 100) | |
67 styleMask:NSBorderlessWindowMask | |
68 backing:NSBackingStoreBuffered | |
69 defer:NO]); | |
70 if ((self = [super initWithWindow:window | |
71 parentWindow:parentWindow | |
72 anchoredAt:NSZeroPoint])) { | |
73 [window setCanBecomeKeyWindow:NO]; | |
74 bridge_.reset(bridge); | |
75 | |
76 ui::NativeTheme* nativeTheme = ui::NativeTheme::instance(); | |
77 [[self bubble] setAlignment:info_bubble::kAlignArrowToAnchor]; | |
78 [[self bubble] setArrowLocation:info_bubble::kTopRight]; | |
79 [[self bubble] setBackgroundColor: | |
80 gfx::SkColorToCalibratedNSColor(nativeTheme->GetSystemColor( | |
81 ui::NativeTheme::kColorId_DialogBackground))]; | |
82 [self performLayoutWithController:bridge->controller().get()]; | |
83 } | |
84 return self; | |
85 } | |
86 | |
87 - (void)windowWillClose:(NSNotification*)notification { | |
88 bridge_->OnBubbleClosing(); | |
89 [super windowWillClose:notification]; | |
90 } | |
91 | |
92 // Called when embedded links are clicked. | |
93 - (BOOL)textView:(NSTextView*)textView | |
94 clickedOnLink:(id)link | |
95 atIndex:(NSUInteger)charIndex { | |
96 bridge_->OnLinkClicked(); | |
97 return YES; | |
98 } | |
99 | |
100 - (void)showAtAnchor:(NSPoint)anchorPoint { | |
101 [self setAnchorPoint:anchorPoint]; | |
102 [self showWindow:nil]; | |
103 } | |
104 | |
105 - (BOOL)isHiding { | |
106 InfoBubbleWindow* window = | |
107 base::mac::ObjCCastStrict<InfoBubbleWindow>([self window]); | |
108 return [window isClosing]; | |
109 } | |
110 | |
111 | |
112 - (void)performLayoutWithController: | |
113 (autofill::GeneratedCreditCardBubbleController*)controller { | |
114 CGFloat bubbleWidth = autofill::GeneratedCreditCardBubbleView::kContentsWidth; | |
115 | |
116 // Build the bubble title. | |
117 NSFont* titleFont = [NSFont systemFontOfSize:15.0]; | |
118 NSString* title = base::SysUTF16ToNSString(controller->TitleText()); | |
119 base::scoped_nsobject<NSTextField> titleView( | |
120 [[NSTextField alloc] initWithFrame:NSZeroRect]); | |
121 [titleView setDrawsBackground:NO]; | |
122 [titleView setBezeled:NO]; | |
123 [titleView setEditable:NO]; | |
124 [titleView setSelectable:NO]; | |
125 [titleView setStringValue:title]; | |
126 [titleView setFont:titleFont]; | |
127 [titleView sizeToFit]; | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
That'll accomplish it, too :-).
groby-ooo-7-16
2013/11/01 00:28:59
And much nicer, too :)
| |
128 | |
129 bubbleWidth = std::max(bubbleWidth, NSWidth([titleView frame]) + 2 * kInset); | |
130 | |
131 // Build the contents view. | |
132 base::scoped_nsobject<HyperlinkTextView> contentsView( | |
133 [[HyperlinkTextView alloc] initWithFrame:NSZeroRect]); | |
134 | |
135 [contentsView setEditable:NO]; | |
136 [contentsView setDelegate:self]; | |
137 | |
138 NSFont* font = [NSFont systemFontOfSize:[NSFont systemFontSize]]; | |
139 NSFont* boldFont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]]; | |
140 [contentsView setMessage:base::SysUTF16ToNSString(controller->ContentsText()) | |
141 withFont:font | |
142 messageColor:[NSColor blackColor]]; | |
143 | |
144 const std::vector<autofill::TextRange>& text_ranges = | |
145 controller->ContentsTextRanges(); | |
146 for (size_t i = 0; i < text_ranges.size(); ++i) { | |
147 NSRange range = text_ranges[i].range.ToNSRange(); | |
148 if (text_ranges[i].is_link) { | |
149 [contentsView addLinkRange:range | |
150 withName:@(i) | |
151 linkColor:[NSColor blueColor]]; | |
152 } else { | |
153 [[contentsView textStorage] | |
154 addAttributes:@{ NSFontAttributeName : boldFont } | |
155 range:range]; | |
156 } | |
157 } | |
158 | |
159 [contentsView setFrameSizeForWidth:bubbleWidth - 2 * kInset]; | |
160 // Sizes are computed, now lay out the individual parts. | |
161 [contentsView setFrameOrigin:NSMakePoint(kInset, kInset)]; | |
162 [titleView setFrameOrigin: | |
163 NSMakePoint(kInset, NSMaxY([contentsView frame]) + kTitlePadding)]; | |
164 [[[self window] contentView] setSubviews:@[ contentsView, titleView ]]; | |
165 | |
166 // Update window frame. | |
167 NSRect windowFrame = [[self window] frame]; | |
168 windowFrame.size = | |
169 NSMakeSize(bubbleWidth, NSMaxY([titleView frame]) + kInset); | |
170 [[self window] setFrame:windowFrame display:YES]; | |
171 } | |
172 | |
173 @end | |
174 | |
175 | |
176 namespace autofill { | |
177 | |
178 // static | |
179 base::WeakPtr<GeneratedCreditCardBubbleView> | |
180 GeneratedCreditCardBubbleView::Create( | |
181 const base::WeakPtr<GeneratedCreditCardBubbleController>& controller) { | |
182 return (new GeneratedCreditCardBubbleCocoa(controller))->weak_ptr_factory_. | |
183 GetWeakPtr(); | |
184 } | |
185 | |
186 GeneratedCreditCardBubbleCocoa::GeneratedCreditCardBubbleCocoa( | |
187 const base::WeakPtr<GeneratedCreditCardBubbleController>& controller) | |
188 : bubbleController_(NULL), | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
nil for objective-c object.
groby-ooo-7-16
2013/11/01 00:28:59
Done.
| |
189 controller_(controller), | |
190 weak_ptr_factory_(this) { | |
191 } | |
192 | |
193 GeneratedCreditCardBubbleCocoa::~GeneratedCreditCardBubbleCocoa() {} | |
194 | |
195 void GeneratedCreditCardBubbleCocoa::Show() { | |
196 DCHECK(controller_.get()); | |
197 NSView* browser_view = | |
198 controller_->web_contents()->GetView()->GetNativeView(); | |
199 NSWindow* parent_window = [browser_view window]; | |
200 LocationBarViewMac* location_bar = | |
201 [[parent_window windowController] locationBarBridge]; | |
202 | |
203 // |location_bar| can be NULL during initialization stages. | |
204 if (!location_bar) | |
205 return; | |
206 | |
207 if (!bubbleController_) { | |
208 bubbleController_ = [[GeneratedCreditCardBubbleControllerCocoa alloc] | |
209 initWithParentWindow:parent_window | |
210 bridge:this]; | |
Scott Hess - ex-Googler
2013/10/31 23:33:38
: align.
groby-ooo-7-16
2013/11/01 00:28:59
Done.
| |
211 } | |
212 | |
213 NSRect frame = NSZeroRect; | |
214 frame.origin = location_bar->GetGeneratedCreditCardBubblePoint(); | |
215 NSPoint anchor = [parent_window convertRectToScreen:frame].origin; | |
216 [bubbleController_ showAtAnchor:anchor]; | |
217 } | |
218 | |
219 void GeneratedCreditCardBubbleCocoa::Hide() { | |
220 [bubbleController_ close]; | |
221 } | |
222 | |
223 bool GeneratedCreditCardBubbleCocoa::IsHiding() const { | |
224 return [bubbleController_ isHiding]; | |
225 } | |
226 | |
227 void GeneratedCreditCardBubbleCocoa::OnBubbleClosing() { | |
228 bubbleController_ = nil; | |
229 } | |
230 | |
231 void GeneratedCreditCardBubbleCocoa::OnLinkClicked() { | |
232 if (controller_) | |
233 controller_->OnLinkClicked(); | |
234 } | |
235 | |
236 } // autofill | |
OLD | NEW |