| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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] |
| 168 removeObserver:self |
| 169 name:NSWindowDidMoveNotification |
| 170 object:nil]; |
| 172 bridge_->OnBubbleClosing(); | 171 bridge_->OnBubbleClosing(); |
| 173 [super windowWillClose:notification]; | 172 [super windowWillClose:notification]; |
| 174 } | 173 } |
| 175 | 174 |
| 176 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification { | 175 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification { |
| 177 // Override the base class implementation, which would have closed the bubble. | 176 // Override the base class implementation, which would have closed the bubble. |
| 178 } | 177 } |
| 179 | 178 |
| 180 - (void)parentWindowDidResize:(NSNotification*)notification { | 179 - (void)parentWindowDidResize:(NSNotification*)notification { |
| 181 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 180 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 182 } | 181 } |
| 183 | 182 |
| 184 - (void)parentWindowDidMove:(NSNotification*)notification { | 183 - (void)parentWindowDidMove:(NSNotification*)notification { |
| 185 DCHECK(bridge_); | 184 DCHECK(bridge_); |
| 186 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 185 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 187 } | 186 } |
| 188 | 187 |
| 189 - (void)show { | 188 - (void)show { |
| 190 NSView* view = [[self window] contentView]; | 189 NSView* view = [[self window] contentView]; |
| 191 [view setSubviews:@[]]; | |
| 192 | 190 |
| 193 // ------------------------------------ | 191 // ------------------------------------ |
| 194 // | Chooser bubble title | | 192 // | Chooser bubble title | |
| 195 // | -------------------------------- | | 193 // | -------------------------------- | |
| 196 // | | option 0 | | | 194 // | | option 0 | | |
| 197 // | | option 1 | | | 195 // | | option 1 | | |
| 198 // | | option 2 | | | 196 // | | option 2 | | |
| 199 // | | | | | 197 // | | | | |
| 200 // | | | | | 198 // | | | | |
| 201 // | | | | | 199 // | | | | |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 [[self window] setFrame:bubbleFrame display:NO]; | 285 [[self window] setFrame:bubbleFrame display:NO]; |
| 288 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 286 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 289 [self showWindow:nil]; | 287 [self showWindow:nil]; |
| 290 [[self window] makeFirstResponder:nil]; | 288 [[self window] makeFirstResponder:nil]; |
| 291 [[self window] setInitialFirstResponder:connectButton_.get()]; | 289 [[self window] setInitialFirstResponder:connectButton_.get()]; |
| 292 } | 290 } |
| 293 } | 291 } |
| 294 | 292 |
| 295 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { | 293 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { |
| 296 const std::vector<base::string16>& device_names = | 294 const std::vector<base::string16>& device_names = |
| 297 chooser_bubble_delegate_->GetOptions(); | 295 chooserBubbleDelegate_->GetOptions(); |
| 298 if (device_names.empty()) { | 296 if (device_names.empty()) { |
| 299 return 1; | 297 return 1; |
| 300 } else { | 298 } else { |
| 301 return static_cast<NSInteger>(device_names.size()); | 299 return static_cast<NSInteger>(device_names.size()); |
| 302 } | 300 } |
| 303 } | 301 } |
| 304 | 302 |
| 305 - (id)tableView:(NSTableView*)tableView | 303 - (id)tableView:(NSTableView*)tableView |
| 306 objectValueForTableColumn:(NSTableColumn*)tableColumn | 304 objectValueForTableColumn:(NSTableColumn*)tableColumn |
| 307 row:(NSInteger)rowIndex { | 305 row:(NSInteger)rowIndex { |
| 308 const std::vector<base::string16>& device_names = | 306 const std::vector<base::string16>& device_names = |
| 309 chooser_bubble_delegate_->GetOptions(); | 307 chooserBubbleDelegate_->GetOptions(); |
| 310 if (device_names.empty()) { | 308 if (device_names.empty()) { |
| 311 DCHECK(rowIndex == 0); | 309 DCHECK(rowIndex == 0); |
| 312 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); | 310 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); |
| 313 } else { | 311 } else { |
| 314 if (rowIndex >= 0 && | 312 if (rowIndex >= 0 && |
| 315 rowIndex < static_cast<NSInteger>(device_names.size())) { | 313 rowIndex < static_cast<NSInteger>(device_names.size())) { |
| 316 return base::SysUTF16ToNSString(device_names[rowIndex]); | 314 return base::SysUTF16ToNSString(device_names[rowIndex]); |
| 317 } else { | 315 } else { |
| 318 return @""; | 316 return @""; |
| 319 } | 317 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 339 // item is removed, so here it tracks if the removed item is the item | 337 // item is removed, so here it tracks if the removed item is the item |
| 340 // that was previously selected, if so, deselect it. | 338 // that was previously selected, if so, deselect it. |
| 341 if ([tableView_ selectedRow] == index) | 339 if ([tableView_ selectedRow] == index) |
| 342 [tableView_ deselectRow:index]; | 340 [tableView_ deselectRow:index]; |
| 343 | 341 |
| 344 [self updateTableView]; | 342 [self updateTableView]; |
| 345 } | 343 } |
| 346 | 344 |
| 347 - (void)updateTableView { | 345 - (void)updateTableView { |
| 348 const std::vector<base::string16>& device_names = | 346 const std::vector<base::string16>& device_names = |
| 349 chooser_bubble_delegate_->GetOptions(); | 347 chooserBubbleDelegate_->GetOptions(); |
| 350 [tableView_ setEnabled:!device_names.empty()]; | 348 [tableView_ setEnabled:!device_names.empty()]; |
| 351 [tableView_ reloadData]; | 349 [tableView_ reloadData]; |
| 352 } | 350 } |
| 353 | 351 |
| 354 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification { | 352 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification { |
| 355 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0]; | 353 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0]; |
| 356 } | 354 } |
| 357 | 355 |
| 358 - (void)updateAnchorPosition { | 356 - (void)updateAnchorPosition { |
| 359 [self setParentWindow:[self getExpectedParentWindow]]; | 357 [self setParentWindow:[self getExpectedParentWindow]]; |
| 360 [self setAnchorPoint:[self getExpectedAnchorPoint]]; | 358 [self setAnchorPoint:[self getExpectedAnchorPoint]]; |
| 361 } | 359 } |
| 362 | 360 |
| 363 - (NSPoint)getExpectedAnchorPoint { | 361 - (NSPoint)getExpectedAnchorPoint { |
| 364 NSPoint anchor; | 362 NSPoint anchor; |
| 365 if ([self hasLocationBar]) { | 363 if ([self hasLocationBar]) { |
| 366 LocationBarViewMac* location_bar = | 364 LocationBarViewMac* locationBar = |
| 367 [[[self getExpectedParentWindow] windowController] locationBarBridge]; | 365 [[[self getExpectedParentWindow] windowController] locationBarBridge]; |
| 368 anchor = location_bar->GetPageInfoBubblePoint(); | 366 anchor = locationBar->GetPageInfoBubblePoint(); |
| 369 } else { | 367 } else { |
| 370 // Center the bubble if there's no location bar. | 368 // Center the bubble if there's no location bar. |
| 371 NSRect contentFrame = [[[self getExpectedParentWindow] contentView] frame]; | 369 NSRect contentFrame = [[[self getExpectedParentWindow] contentView] frame]; |
| 372 anchor = NSMakePoint(NSMidX(contentFrame), NSMaxY(contentFrame)); | 370 anchor = NSMakePoint(NSMidX(contentFrame), NSMaxY(contentFrame)); |
| 373 } | 371 } |
| 374 | 372 |
| 375 return [[self getExpectedParentWindow] convertBaseToScreen:anchor]; | 373 return [[self getExpectedParentWindow] convertBaseToScreen:anchor]; |
| 376 } | 374 } |
| 377 | 375 |
| 378 - (bool)hasLocationBar { | 376 - (bool)hasLocationBar { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB { | 438 + (void)alignCenterOf:(NSView*)viewA verticallyToCenterOf:(NSView*)viewB { |
| 441 NSRect frameA = [viewA frame]; | 439 NSRect frameA = [viewA frame]; |
| 442 NSRect frameB = [viewB frame]; | 440 NSRect frameB = [viewB frame]; |
| 443 frameA.origin.y = | 441 frameA.origin.y = |
| 444 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); | 442 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); |
| 445 [viewA setFrameOrigin:frameA.origin]; | 443 [viewA setFrameOrigin:frameA.origin]; |
| 446 } | 444 } |
| 447 | 445 |
| 448 - (void)onConnect:(id)sender { | 446 - (void)onConnect:(id)sender { |
| 449 NSInteger row = [tableView_ selectedRow]; | 447 NSInteger row = [tableView_ selectedRow]; |
| 450 chooser_bubble_delegate_->Select(row); | 448 chooserBubbleDelegate_->Select(row); |
| 451 [self close]; | 449 [self close]; |
| 452 } | 450 } |
| 453 | 451 |
| 454 - (void)onCancel:(id)sender { | 452 - (void)onCancel:(id)sender { |
| 455 chooser_bubble_delegate_->Cancel(); | 453 chooserBubbleDelegate_->Cancel(); |
| 456 [self close]; | 454 [self close]; |
| 457 } | 455 } |
| 458 | 456 |
| 459 @end | 457 @end |
| 460 | 458 |
| 461 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa( | 459 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa( |
| 462 Browser* browser, | 460 Browser* browser, |
| 463 ChooserBubbleDelegate* chooser_bubble_delegate) | 461 ChooserBubbleDelegate* chooser_bubble_delegate) |
| 464 : browser_(browser), | 462 : browser_(browser), |
| 465 chooser_bubble_delegate_(chooser_bubble_delegate), | 463 chooser_bubble_delegate_(chooser_bubble_delegate), |
| 466 chooser_bubble_ui_controller_(nil) { | 464 chooser_bubble_ui_controller_(nil) { |
| 467 DCHECK(browser); | 465 DCHECK(browser); |
| 468 DCHECK(chooser_bubble_delegate); | 466 DCHECK(chooser_bubble_delegate); |
| 469 chooser_bubble_delegate_->set_observer(this); | 467 chooser_bubble_delegate_->set_observer(this); |
| 470 } | 468 } |
| 471 | 469 |
| 472 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() { | 470 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() { |
| 473 chooser_bubble_delegate_->set_observer(nullptr); | 471 chooser_bubble_delegate_->set_observer(nullptr); |
| 474 if (chooser_bubble_ui_controller_) { | 472 [chooser_bubble_ui_controller_ close]; |
| 475 [chooser_bubble_ui_controller_ close]; | 473 chooser_bubble_ui_controller_ = nil; |
| 476 chooser_bubble_ui_controller_ = nil; | |
| 477 } | |
| 478 } | 474 } |
| 479 | 475 |
| 480 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) { | 476 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) { |
| 481 if (!chooser_bubble_ui_controller_) { | 477 if (!chooser_bubble_ui_controller_) { |
| 482 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc] | 478 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc] |
| 483 initWithBrowser:browser_ | 479 initWithBrowser:browser_ |
| 484 initWithChooserBubbleDelegate:chooser_bubble_delegate_ | 480 initWithChooserBubbleDelegate:chooser_bubble_delegate_ |
| 485 bridge:this]; | 481 bridge:this]; |
| 486 } | 482 } |
| 487 | 483 |
| 488 if (chooser_bubble_ui_controller_) { | 484 [chooser_bubble_ui_controller_ show]; |
| 489 [chooser_bubble_ui_controller_ show]; | 485 [chooser_bubble_ui_controller_ updateTableView]; |
| 490 [chooser_bubble_ui_controller_ updateTableView]; | |
| 491 } | |
| 492 } | 486 } |
| 493 | 487 |
| 494 void ChooserBubbleUiCocoa::Close() { | 488 void ChooserBubbleUiCocoa::Close() { |
| 495 if (chooser_bubble_ui_controller_) { | 489 [chooser_bubble_ui_controller_ close]; |
| 496 [chooser_bubble_ui_controller_ close]; | 490 chooser_bubble_ui_controller_ = nil; |
| 497 chooser_bubble_ui_controller_ = nil; | |
| 498 } | |
| 499 } | 491 } |
| 500 | 492 |
| 501 void ChooserBubbleUiCocoa::UpdateAnchorPosition() { | 493 void ChooserBubbleUiCocoa::UpdateAnchorPosition() { |
| 502 [chooser_bubble_ui_controller_ updateAnchorPosition]; | 494 [chooser_bubble_ui_controller_ updateAnchorPosition]; |
| 503 } | 495 } |
| 504 | 496 |
| 505 void ChooserBubbleUiCocoa::OnOptionsInitialized() { | 497 void ChooserBubbleUiCocoa::OnOptionsInitialized() { |
| 506 [chooser_bubble_ui_controller_ onOptionsInitialized]; | 498 [chooser_bubble_ui_controller_ onOptionsInitialized]; |
| 507 } | 499 } |
| 508 | 500 |
| 509 void ChooserBubbleUiCocoa::OnOptionAdded(int index) { | 501 void ChooserBubbleUiCocoa::OnOptionAdded(int index) { |
| 510 [chooser_bubble_ui_controller_ onOptionAdded:index]; | 502 [chooser_bubble_ui_controller_ onOptionAdded:index]; |
| 511 } | 503 } |
| 512 | 504 |
| 513 void ChooserBubbleUiCocoa::OnOptionRemoved(int index) { | 505 void ChooserBubbleUiCocoa::OnOptionRemoved(int index) { |
| 514 [chooser_bubble_ui_controller_ onOptionRemoved:index]; | 506 [chooser_bubble_ui_controller_ onOptionRemoved:index]; |
| 515 } | 507 } |
| 516 | 508 |
| 517 void ChooserBubbleUiCocoa::OnBubbleClosing() { | 509 void ChooserBubbleUiCocoa::OnBubbleClosing() { |
| 518 chooser_bubble_ui_controller_ = nil; | 510 chooser_bubble_ui_controller_ = nil; |
| 519 } | 511 } |
| OLD | NEW |