Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Side by Side Diff: chrome/browser/ui/cocoa/website_settings/chooser_bubble_ui_cocoa.mm

Issue 1528543004: Update chooser bubble ui cocoa code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@modify_base_bubble_controller_dealloc
Patch Set: address rsesek@'s comments Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/website_settings/chooser_bubble_ui_cocoa.h" 5 #import "chrome/browser/ui/cocoa/website_settings/chooser_bubble_ui_cocoa.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 ChooserBubbleUiCocoa* bridge_; 60 ChooserBubbleUiCocoa* bridge_;
61 61
62 base::scoped_nsobject<NSTextField> titleView_; 62 base::scoped_nsobject<NSTextField> titleView_;
63 base::scoped_nsobject<NSScrollView> scrollView_; 63 base::scoped_nsobject<NSScrollView> scrollView_;
64 base::scoped_nsobject<NSTableColumn> tableColumn_; 64 base::scoped_nsobject<NSTableColumn> tableColumn_;
65 base::scoped_nsobject<NSTableView> tableView_; 65 base::scoped_nsobject<NSTableView> tableView_;
66 base::scoped_nsobject<NSButton> connectButton_; 66 base::scoped_nsobject<NSButton> connectButton_;
67 base::scoped_nsobject<NSButton> cancelButton_; 67 base::scoped_nsobject<NSButton> cancelButton_;
68 68
69 Browser* browser_; // Weak. 69 Browser* browser_; // Weak.
70 ChooserBubbleDelegate* chooser_bubble_delegate_; // Weak. 70 ChooserBubbleDelegate* chooserBubbleDelegate_; // Weak.
71 } 71 }
72 72
73 // Designated initializer. |browser| and |bridge| must both be non-nil. 73 // Designated initializer. |browser| and |bridge| must both be non-nil.
74 - (id)initWithBrowser:(Browser*)browser 74 - (id)initWithBrowser:(Browser*)browser
75 initWithChooserBubbleDelegate: 75 initWithChooserBubbleDelegate:(ChooserBubbleDelegate*)chooserBubbleDelegate
76 (ChooserBubbleDelegate*)chooser_bubble_delegate
77 bridge:(ChooserBubbleUiCocoa*)bridge; 76 bridge:(ChooserBubbleUiCocoa*)bridge;
78 77
79 // Makes the bubble visible. 78 // Makes the bubble visible.
80 - (void)show; 79 - (void)show;
81 80
82 // Will reposition the bubble based in case the anchor or parent should change. 81 // Will reposition the bubble based in case the anchor or parent should change.
83 - (void)updateAnchorPosition; 82 - (void)updateAnchorPosition;
84 83
85 // Will calculate the expected anchor point for this bubble. 84 // Will calculate the expected anchor point for this bubble.
86 // Should only be used outside this class for tests. 85 // Should only be used outside this class for tests.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 - (void)onConnect:(id)sender; 124 - (void)onConnect:(id)sender;
126 125
127 // Called when the "Cancel" button is pressed. 126 // Called when the "Cancel" button is pressed.
128 - (void)onCancel:(id)sender; 127 - (void)onCancel:(id)sender;
129 128
130 @end 129 @end
131 130
132 @implementation ChooserBubbleUiController 131 @implementation ChooserBubbleUiController
133 132
134 - (id)initWithBrowser:(Browser*)browser 133 - (id)initWithBrowser:(Browser*)browser
135 initWithChooserBubbleDelegate: 134 initWithChooserBubbleDelegate:(ChooserBubbleDelegate*)chooserBubbleDelegate
136 (ChooserBubbleDelegate*)chooser_bubble_delegate
137 bridge:(ChooserBubbleUiCocoa*)bridge { 135 bridge:(ChooserBubbleUiCocoa*)bridge {
138 DCHECK(browser); 136 DCHECK(browser);
139 DCHECK(chooser_bubble_delegate); 137 DCHECK(chooserBubbleDelegate);
140 DCHECK(bridge); 138 DCHECK(bridge);
141 139
142 if (browser == nil || chooser_bubble_delegate == nil || bridge == nil)
143 return nil;
144
145 browser_ = browser; 140 browser_ = browser;
146 chooser_bubble_delegate_ = chooser_bubble_delegate; 141 chooserBubbleDelegate_ = chooserBubbleDelegate;
147 142
148 base::scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc] 143 base::scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc]
149 initWithContentRect:ui::kWindowSizeDeterminedLater 144 initWithContentRect:ui::kWindowSizeDeterminedLater
150 styleMask:NSBorderlessWindowMask 145 styleMask:NSBorderlessWindowMask
151 backing:NSBackingStoreBuffered 146 backing:NSBackingStoreBuffered
152 defer:NO]); 147 defer:NO]);
153 [window setAllowedAnimations:info_bubble::kAnimateNone]; 148 [window setAllowedAnimations:info_bubble::kAnimateNone];
154 [window setReleasedWhenClosed:NO]; 149 [window setReleasedWhenClosed:NO];
155 if ((self = [super initWithWindow:window 150 if ((self = [super initWithWindow:window
156 parentWindow:[self getExpectedParentWindow] 151 parentWindow:[self getExpectedParentWindow]
157 anchoredAt:NSZeroPoint])) { 152 anchoredAt:NSZeroPoint])) {
158 [self setShouldCloseOnResignKey:NO]; 153 [self setShouldCloseOnResignKey:NO];
159 [self setShouldOpenAsKeyWindow:YES]; 154 [self setShouldOpenAsKeyWindow:YES];
160 [[self bubble] setArrowLocation:[self getExpectedArrowLocation]]; 155 [[self bubble] setArrowLocation:[self getExpectedArrowLocation]];
161 bridge_ = bridge; 156 bridge_ = bridge;
162 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; 157 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
163 [center addObserver:self 158 [center addObserver:self
164 selector:@selector(parentWindowDidMove:) 159 selector:@selector(parentWindowDidMove:)
165 name:NSWindowDidMoveNotification 160 name:NSWindowDidMoveNotification
166 object:[self getExpectedParentWindow]]; 161 object:[self getExpectedParentWindow]];
167 } 162 }
168 return self; 163 return self;
169 } 164 }
170 165
171 - (void)windowWillClose:(NSNotification*)notification { 166 - (void)windowWillClose:(NSNotification*)notification {
167 [[NSNotificationCenter defaultCenter] removeObserver:self];
Robert Sesek 2015/12/15 18:16:00 Limit this too to just the observer that was regis
juncai 2015/12/15 18:24:11 Done.
172 bridge_->OnBubbleClosing(); 168 bridge_->OnBubbleClosing();
173 [super windowWillClose:notification]; 169 [super windowWillClose:notification];
174 } 170 }
175 171
176 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification { 172 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification {
177 // Override the base class implementation, which would have closed the bubble. 173 // Override the base class implementation, which would have closed the bubble.
178 } 174 }
179 175
180 - (void)parentWindowDidResize:(NSNotification*)notification { 176 - (void)parentWindowDidResize:(NSNotification*)notification {
181 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 177 [self setAnchorPoint:[self getExpectedAnchorPoint]];
182 } 178 }
183 179
184 - (void)parentWindowDidMove:(NSNotification*)notification { 180 - (void)parentWindowDidMove:(NSNotification*)notification {
185 DCHECK(bridge_); 181 DCHECK(bridge_);
186 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 182 [self setAnchorPoint:[self getExpectedAnchorPoint]];
187 } 183 }
188 184
189 - (void)show { 185 - (void)show {
190 NSView* view = [[self window] contentView]; 186 NSView* view = [[self window] contentView];
191 [view setSubviews:@[]];
192 187
193 // ------------------------------------ 188 // ------------------------------------
194 // | Chooser bubble title | 189 // | Chooser bubble title |
195 // | -------------------------------- | 190 // | -------------------------------- |
196 // | | option 0 | | 191 // | | option 0 | |
197 // | | option 1 | | 192 // | | option 1 | |
198 // | | option 2 | | 193 // | | option 2 | |
199 // | | | | 194 // | | | |
200 // | | | | 195 // | | | |
201 // | | | | 196 // | | | |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 [[self window] setFrame:bubbleFrame display:NO]; 282 [[self window] setFrame:bubbleFrame display:NO];
288 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 283 [self setAnchorPoint:[self getExpectedAnchorPoint]];
289 [self showWindow:nil]; 284 [self showWindow:nil];
290 [[self window] makeFirstResponder:nil]; 285 [[self window] makeFirstResponder:nil];
291 [[self window] setInitialFirstResponder:connectButton_.get()]; 286 [[self window] setInitialFirstResponder:connectButton_.get()];
292 } 287 }
293 } 288 }
294 289
295 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { 290 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
296 const std::vector<base::string16>& device_names = 291 const std::vector<base::string16>& device_names =
297 chooser_bubble_delegate_->GetOptions(); 292 chooserBubbleDelegate_->GetOptions();
298 if (device_names.empty()) { 293 if (device_names.empty()) {
299 return 1; 294 return 1;
300 } else { 295 } else {
301 return static_cast<NSInteger>(device_names.size()); 296 return static_cast<NSInteger>(device_names.size());
302 } 297 }
303 } 298 }
304 299
305 - (id)tableView:(NSTableView*)tableView 300 - (id)tableView:(NSTableView*)tableView
306 objectValueForTableColumn:(NSTableColumn*)tableColumn 301 objectValueForTableColumn:(NSTableColumn*)tableColumn
307 row:(NSInteger)rowIndex { 302 row:(NSInteger)rowIndex {
308 const std::vector<base::string16>& device_names = 303 const std::vector<base::string16>& device_names =
309 chooser_bubble_delegate_->GetOptions(); 304 chooserBubbleDelegate_->GetOptions();
310 if (device_names.empty()) { 305 if (device_names.empty()) {
311 DCHECK(rowIndex == 0); 306 DCHECK(rowIndex == 0);
312 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); 307 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT);
313 } else { 308 } else {
314 if (rowIndex >= 0 && 309 if (rowIndex >= 0 &&
315 rowIndex < static_cast<NSInteger>(device_names.size())) { 310 rowIndex < static_cast<NSInteger>(device_names.size())) {
316 return base::SysUTF16ToNSString(device_names[rowIndex]); 311 return base::SysUTF16ToNSString(device_names[rowIndex]);
317 } else { 312 } else {
318 return @""; 313 return @"";
319 } 314 }
(...skipping 19 matching lines...) Expand all
339 // item is removed, so here it tracks if the removed item is the item 334 // item is removed, so here it tracks if the removed item is the item
340 // that was previously selected, if so, deselect it. 335 // that was previously selected, if so, deselect it.
341 if ([tableView_ selectedRow] == index) 336 if ([tableView_ selectedRow] == index)
342 [tableView_ deselectRow:index]; 337 [tableView_ deselectRow:index];
343 338
344 [self updateTableView]; 339 [self updateTableView];
345 } 340 }
346 341
347 - (void)updateTableView { 342 - (void)updateTableView {
348 const std::vector<base::string16>& device_names = 343 const std::vector<base::string16>& device_names =
349 chooser_bubble_delegate_->GetOptions(); 344 chooserBubbleDelegate_->GetOptions();
350 [tableView_ setEnabled:!device_names.empty()]; 345 [tableView_ setEnabled:!device_names.empty()];
351 [tableView_ reloadData]; 346 [tableView_ reloadData];
352 } 347 }
353 348
354 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification { 349 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification {
355 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0]; 350 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0];
356 } 351 }
357 352
358 - (void)updateAnchorPosition { 353 - (void)updateAnchorPosition {
359 [self setParentWindow:[self getExpectedParentWindow]]; 354 [self setParentWindow:[self getExpectedParentWindow]];
360 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 355 [self setAnchorPoint:[self getExpectedAnchorPoint]];
361 } 356 }
362 357
363 - (NSPoint)getExpectedAnchorPoint { 358 - (NSPoint)getExpectedAnchorPoint {
364 NSPoint anchor; 359 NSPoint anchor;
365 if ([self hasLocationBar]) { 360 if ([self hasLocationBar]) {
366 LocationBarViewMac* location_bar = 361 LocationBarViewMac* locationBar =
367 [[[self getExpectedParentWindow] windowController] locationBarBridge]; 362 [[[self getExpectedParentWindow] windowController] locationBarBridge];
368 anchor = location_bar->GetPageInfoBubblePoint(); 363 anchor = locationBar->GetPageInfoBubblePoint();
369 } else { 364 } else {
370 // Center the bubble if there's no location bar. 365 // Center the bubble if there's no location bar.
371 NSRect contentFrame = [[[self getExpectedParentWindow] contentView] frame]; 366 NSRect contentFrame = [[[self getExpectedParentWindow] contentView] frame];
372 anchor = NSMakePoint(NSMidX(contentFrame), NSMaxY(contentFrame)); 367 anchor = NSMakePoint(NSMidX(contentFrame), NSMaxY(contentFrame));
373 } 368 }
374 369
375 return [[self getExpectedParentWindow] convertBaseToScreen:anchor]; 370 return [[self getExpectedParentWindow] convertBaseToScreen:anchor];
376 } 371 }
377 372
378 - (bool)hasLocationBar { 373 - (bool)hasLocationBar {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB { 435 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB {
441 NSRect frameA = [viewA frame]; 436 NSRect frameA = [viewA frame];
442 NSRect frameB = [viewB frame]; 437 NSRect frameB = [viewB frame];
443 frameA.origin.y = 438 frameA.origin.y =
444 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); 439 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2);
445 [viewA setFrameOrigin:frameA.origin]; 440 [viewA setFrameOrigin:frameA.origin];
446 } 441 }
447 442
448 - (void)onConnect:(id)sender { 443 - (void)onConnect:(id)sender {
449 NSInteger row = [tableView_ selectedRow]; 444 NSInteger row = [tableView_ selectedRow];
450 chooser_bubble_delegate_->Select(row); 445 chooserBubbleDelegate_->Select(row);
451 [self close]; 446 [self close];
452 } 447 }
453 448
454 - (void)onCancel:(id)sender { 449 - (void)onCancel:(id)sender {
455 chooser_bubble_delegate_->Cancel(); 450 chooserBubbleDelegate_->Cancel();
456 [self close]; 451 [self close];
457 } 452 }
458 453
459 @end 454 @end
460 455
461 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa( 456 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa(
462 Browser* browser, 457 Browser* browser,
463 ChooserBubbleDelegate* chooser_bubble_delegate) 458 ChooserBubbleDelegate* chooser_bubble_delegate)
464 : browser_(browser), 459 : browser_(browser),
465 chooser_bubble_delegate_(chooser_bubble_delegate), 460 chooser_bubble_delegate_(chooser_bubble_delegate),
466 chooser_bubble_ui_controller_(nil) { 461 chooser_bubble_ui_controller_(nil) {
467 DCHECK(browser); 462 DCHECK(browser);
468 DCHECK(chooser_bubble_delegate); 463 DCHECK(chooser_bubble_delegate);
469 chooser_bubble_delegate_->set_observer(this); 464 chooser_bubble_delegate_->set_observer(this);
470 } 465 }
471 466
472 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() { 467 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() {
473 chooser_bubble_delegate_->set_observer(nullptr); 468 chooser_bubble_delegate_->set_observer(nullptr);
474 if (chooser_bubble_ui_controller_) { 469 [chooser_bubble_ui_controller_ close];
475 [chooser_bubble_ui_controller_ close]; 470 chooser_bubble_ui_controller_ = nil;
476 chooser_bubble_ui_controller_ = nil;
477 }
478 } 471 }
479 472
480 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) { 473 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) {
481 if (!chooser_bubble_ui_controller_) { 474 if (!chooser_bubble_ui_controller_) {
482 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc] 475 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc]
483 initWithBrowser:browser_ 476 initWithBrowser:browser_
484 initWithChooserBubbleDelegate:chooser_bubble_delegate_ 477 initWithChooserBubbleDelegate:chooser_bubble_delegate_
485 bridge:this]; 478 bridge:this];
486 } 479 }
487 480
488 if (chooser_bubble_ui_controller_) { 481 [chooser_bubble_ui_controller_ show];
489 [chooser_bubble_ui_controller_ show]; 482 [chooser_bubble_ui_controller_ updateTableView];
490 [chooser_bubble_ui_controller_ updateTableView];
491 }
492 } 483 }
493 484
494 void ChooserBubbleUiCocoa::Close() { 485 void ChooserBubbleUiCocoa::Close() {
495 if (chooser_bubble_ui_controller_) { 486 [chooser_bubble_ui_controller_ close];
496 [chooser_bubble_ui_controller_ close]; 487 chooser_bubble_ui_controller_ = nil;
497 chooser_bubble_ui_controller_ = nil;
498 }
499 } 488 }
500 489
501 void ChooserBubbleUiCocoa::UpdateAnchorPosition() { 490 void ChooserBubbleUiCocoa::UpdateAnchorPosition() {
502 [chooser_bubble_ui_controller_ updateAnchorPosition]; 491 [chooser_bubble_ui_controller_ updateAnchorPosition];
503 } 492 }
504 493
505 void ChooserBubbleUiCocoa::OnOptionsInitialized() { 494 void ChooserBubbleUiCocoa::OnOptionsInitialized() {
506 [chooser_bubble_ui_controller_ onOptionsInitialized]; 495 [chooser_bubble_ui_controller_ onOptionsInitialized];
507 } 496 }
508 497
509 void ChooserBubbleUiCocoa::OnOptionAdded(int index) { 498 void ChooserBubbleUiCocoa::OnOptionAdded(int index) {
510 [chooser_bubble_ui_controller_ onOptionAdded:index]; 499 [chooser_bubble_ui_controller_ onOptionAdded:index];
511 } 500 }
512 501
513 void ChooserBubbleUiCocoa::OnOptionRemoved(int index) { 502 void ChooserBubbleUiCocoa::OnOptionRemoved(int index) {
514 [chooser_bubble_ui_controller_ onOptionRemoved:index]; 503 [chooser_bubble_ui_controller_ onOptionRemoved:index];
515 } 504 }
516 505
517 void ChooserBubbleUiCocoa::OnBubbleClosing() { 506 void ChooserBubbleUiCocoa::OnBubbleClosing() {
518 chooser_bubble_ui_controller_ = nil; 507 chooser_bubble_ui_controller_ = nil;
519 } 508 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698