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

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

Issue 1724183005: Change ChooserBubbleDelegate class name to ChooserBubbleController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed typo Created 4 years, 10 months 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
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
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 "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_finder.h" 15 #include "chrome/browser/ui/browser_finder.h"
16 #include "chrome/browser/ui/browser_window.h" 16 #include "chrome/browser/ui/browser_window.h"
17 #import "chrome/browser/ui/chrome_style.h" 17 #import "chrome/browser/ui/chrome_style.h"
18 #import "chrome/browser/ui/cocoa/base_bubble_controller.h" 18 #import "chrome/browser/ui/cocoa/base_bubble_controller.h"
19 #import "chrome/browser/ui/cocoa/browser_window_controller.h" 19 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
20 #import "chrome/browser/ui/cocoa/browser_window_utils.h" 20 #import "chrome/browser/ui/cocoa/browser_window_utils.h"
21 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" 21 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h"
22 #import "chrome/browser/ui/cocoa/hover_close_button.h" 22 #import "chrome/browser/ui/cocoa/hover_close_button.h"
23 #import "chrome/browser/ui/cocoa/info_bubble_view.h" 23 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
24 #import "chrome/browser/ui/cocoa/info_bubble_window.h" 24 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
25 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" 25 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
26 #include "chrome/browser/ui/website_settings/chooser_bubble_delegate.h"
27 #include "chrome/grit/generated_resources.h" 26 #include "chrome/grit/generated_resources.h"
28 #include "content/public/browser/native_web_keyboard_event.h" 27 #include "content/public/browser/native_web_keyboard_event.h"
29 #include "ui/base/cocoa/window_size_constants.h" 28 #include "ui/base/cocoa/window_size_constants.h"
30 #include "ui/base/l10n/l10n_util_mac.h" 29 #include "ui/base/l10n/l10n_util_mac.h"
31 30
32 namespace { 31 namespace {
33 32
34 // Chooser bubble width. 33 // Chooser bubble width.
35 const CGFloat kChooserBubbleWidth = 320.0f; 34 const CGFloat kChooserBubbleWidth = 320.0f;
36 35
37 // Chooser bubble height. 36 // Chooser bubble height.
38 const CGFloat kChooserBubbleHeight = 220.0f; 37 const CGFloat kChooserBubbleHeight = 220.0f;
39 38
40 // Distance between the bubble border and the view that is closest to the 39 // Distance between the bubble border and the view that is closest to the
41 // border. 40 // border.
42 const CGFloat kMarginX = 20.0f; 41 const CGFloat kMarginX = 20.0f;
43 const CGFloat kMarginY = 20.0f; 42 const CGFloat kMarginY = 20.0f;
44 43
45 // Distance between two views inside the bubble. 44 // Distance between two views inside the bubble.
46 const CGFloat kHorizontalPadding = 10.0f; 45 const CGFloat kHorizontalPadding = 10.0f;
47 const CGFloat kVerticalPadding = 10.0f; 46 const CGFloat kVerticalPadding = 10.0f;
48 47
49 const CGFloat kTitlePaddingX = 50.0f; 48 const CGFloat kTitlePaddingX = 50.0f;
50 } 49 }
51 50
52 scoped_ptr<BubbleUi> ChooserBubbleDelegate::BuildBubbleUi() { 51 scoped_ptr<BubbleUi> ChooserBubbleController::BuildBubbleUi() {
53 return make_scoped_ptr(new ChooserBubbleUiCocoa(browser_, this)); 52 return make_scoped_ptr(new ChooserBubbleUiCocoa(browser_, this));
54 } 53 }
55 54
56 @interface ChooserBubbleUiController 55 @interface ChooserBubbleUiController
57 : BaseBubbleController<NSTableViewDataSource, NSTableViewDelegate> { 56 : BaseBubbleController<NSTableViewDataSource, NSTableViewDelegate> {
58 @private 57 @private
59 // Bridge to the C++ class that created this object. 58 // Bridge to the C++ class that created this object.
60 ChooserBubbleUiCocoa* bridge_; 59 ChooserBubbleUiCocoa* bridge_;
61 60
62 base::scoped_nsobject<NSTextField> titleView_; 61 base::scoped_nsobject<NSTextField> titleView_;
63 base::scoped_nsobject<NSScrollView> scrollView_; 62 base::scoped_nsobject<NSScrollView> scrollView_;
64 base::scoped_nsobject<NSTableColumn> tableColumn_; 63 base::scoped_nsobject<NSTableColumn> tableColumn_;
65 base::scoped_nsobject<NSTableView> tableView_; 64 base::scoped_nsobject<NSTableView> tableView_;
66 base::scoped_nsobject<NSButton> connectButton_; 65 base::scoped_nsobject<NSButton> connectButton_;
67 base::scoped_nsobject<NSButton> cancelButton_; 66 base::scoped_nsobject<NSButton> cancelButton_;
68 bool buttonPressed_; 67 bool buttonPressed_;
69 68
70 Browser* browser_; // Weak. 69 Browser* browser_; // Weak.
71 ChooserBubbleDelegate* chooserBubbleDelegate_; // Weak. 70 ChooserBubbleController* chooserBubbleController_; // Weak.
72 } 71 }
73 72
74 // Designated initializer. |browser| and |bridge| must both be non-nil. 73 // Designated initializer. |browser| and |bridge| must both be non-nil.
75 - (id)initWithBrowser:(Browser*)browser 74 - (id)initWithBrowser:(Browser*)browser
76 initWithChooserBubbleDelegate:(ChooserBubbleDelegate*)chooserBubbleDelegate 75 initWithChooserBubbleController:
77 bridge:(ChooserBubbleUiCocoa*)bridge; 76 (ChooserBubbleController*)chooserBubbleController
77 bridge:(ChooserBubbleUiCocoa*)bridge;
78 78
79 // Makes the bubble visible. 79 // Makes the bubble visible.
80 - (void)show; 80 - (void)show;
81 81
82 // Will reposition the bubble based in case the anchor or parent should change. 82 // Will reposition the bubble based in case the anchor or parent should change.
83 - (void)updateAnchorPosition; 83 - (void)updateAnchorPosition;
84 84
85 // Will calculate the expected anchor point for this bubble. 85 // Will calculate the expected anchor point for this bubble.
86 // Should only be used outside this class for tests. 86 // Should only be used outside this class for tests.
87 - (NSPoint)getExpectedAnchorPoint; 87 - (NSPoint)getExpectedAnchorPoint;
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 - (void)onConnect:(id)sender; 125 - (void)onConnect:(id)sender;
126 126
127 // Called when the "Cancel" button is pressed. 127 // Called when the "Cancel" button is pressed.
128 - (void)onCancel:(id)sender; 128 - (void)onCancel:(id)sender;
129 129
130 @end 130 @end
131 131
132 @implementation ChooserBubbleUiController 132 @implementation ChooserBubbleUiController
133 133
134 - (id)initWithBrowser:(Browser*)browser 134 - (id)initWithBrowser:(Browser*)browser
135 initWithChooserBubbleDelegate:(ChooserBubbleDelegate*)chooserBubbleDelegate 135 initWithChooserBubbleController:
136 bridge:(ChooserBubbleUiCocoa*)bridge { 136 (ChooserBubbleController*)chooserBubbleController
137 bridge:(ChooserBubbleUiCocoa*)bridge {
137 DCHECK(browser); 138 DCHECK(browser);
138 DCHECK(chooserBubbleDelegate); 139 DCHECK(chooserBubbleController);
139 DCHECK(bridge); 140 DCHECK(bridge);
140 141
141 browser_ = browser; 142 browser_ = browser;
142 chooserBubbleDelegate_ = chooserBubbleDelegate; 143 chooserBubbleController_ = chooserBubbleController;
143 144
144 base::scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc] 145 base::scoped_nsobject<InfoBubbleWindow> window([[InfoBubbleWindow alloc]
145 initWithContentRect:ui::kWindowSizeDeterminedLater 146 initWithContentRect:ui::kWindowSizeDeterminedLater
146 styleMask:NSBorderlessWindowMask 147 styleMask:NSBorderlessWindowMask
147 backing:NSBackingStoreBuffered 148 backing:NSBackingStoreBuffered
148 defer:NO]); 149 defer:NO]);
149 [window setAllowedAnimations:info_bubble::kAnimateNone]; 150 [window setAllowedAnimations:info_bubble::kAnimateNone];
150 [window setReleasedWhenClosed:NO]; 151 [window setReleasedWhenClosed:NO];
151 if ((self = [super initWithWindow:window 152 if ((self = [super initWithWindow:window
152 parentWindow:[self getExpectedParentWindow] 153 parentWindow:[self getExpectedParentWindow]
(...skipping 10 matching lines...) Expand all
163 } 164 }
164 return self; 165 return self;
165 } 166 }
166 167
167 - (void)windowWillClose:(NSNotification*)notification { 168 - (void)windowWillClose:(NSNotification*)notification {
168 [[NSNotificationCenter defaultCenter] 169 [[NSNotificationCenter defaultCenter]
169 removeObserver:self 170 removeObserver:self
170 name:NSWindowDidMoveNotification 171 name:NSWindowDidMoveNotification
171 object:nil]; 172 object:nil];
172 if (!buttonPressed_) 173 if (!buttonPressed_)
173 chooserBubbleDelegate_->Close(); 174 chooserBubbleController_->Close();
174 bridge_->OnBubbleClosing(); 175 bridge_->OnBubbleClosing();
175 [super windowWillClose:notification]; 176 [super windowWillClose:notification];
176 } 177 }
177 178
178 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification { 179 - (void)parentWindowWillToggleFullScreen:(NSNotification*)notification {
179 // Override the base class implementation, which would have closed the bubble. 180 // Override the base class implementation, which would have closed the bubble.
180 } 181 }
181 182
182 - (void)parentWindowDidResize:(NSNotification*)notification { 183 - (void)parentWindowDidResize:(NSNotification*)notification {
183 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 184 [self setAnchorPoint:[self getExpectedAnchorPoint]];
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 290 [self setAnchorPoint:[self getExpectedAnchorPoint]];
290 [self showWindow:nil]; 291 [self showWindow:nil];
291 [[self window] makeFirstResponder:nil]; 292 [[self window] makeFirstResponder:nil];
292 [[self window] setInitialFirstResponder:connectButton_.get()]; 293 [[self window] setInitialFirstResponder:connectButton_.get()];
293 } 294 }
294 } 295 }
295 296
296 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView { 297 - (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView {
297 // When there are no devices, the table contains a message saying there are 298 // When there are no devices, the table contains a message saying there are
298 // no devices, so the number of rows is always at least 1. 299 // no devices, so the number of rows is always at least 1.
299 return std::max(static_cast<NSInteger>(chooserBubbleDelegate_->NumOptions()), 300 return std::max(
300 static_cast<NSInteger>(1)); 301 static_cast<NSInteger>(chooserBubbleController_->NumOptions()),
302 static_cast<NSInteger>(1));
301 } 303 }
302 304
303 - (id)tableView:(NSTableView*)tableView 305 - (id)tableView:(NSTableView*)tableView
304 objectValueForTableColumn:(NSTableColumn*)tableColumn 306 objectValueForTableColumn:(NSTableColumn*)tableColumn
305 row:(NSInteger)rowIndex { 307 row:(NSInteger)rowIndex {
306 NSInteger num_options = 308 NSInteger num_options =
307 static_cast<NSInteger>(chooserBubbleDelegate_->NumOptions()); 309 static_cast<NSInteger>(chooserBubbleController_->NumOptions());
308 if (num_options == 0) { 310 if (num_options == 0) {
309 DCHECK_EQ(0, rowIndex); 311 DCHECK_EQ(0, rowIndex);
310 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT); 312 return l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT);
311 } 313 }
312 314
313 DCHECK_GE(rowIndex, 0); 315 DCHECK_GE(rowIndex, 0);
314 DCHECK_LT(rowIndex, num_options); 316 DCHECK_LT(rowIndex, num_options);
315 return base::SysUTF16ToNSString( 317 return base::SysUTF16ToNSString(
316 chooserBubbleDelegate_->GetOption(static_cast<size_t>(rowIndex))); 318 chooserBubbleController_->GetOption(static_cast<size_t>(rowIndex)));
317 } 319 }
318 320
319 - (BOOL)tableView:(NSTableView*)aTableView 321 - (BOOL)tableView:(NSTableView*)aTableView
320 shouldEditTableColumn:(NSTableColumn*)aTableColumn 322 shouldEditTableColumn:(NSTableColumn*)aTableColumn
321 row:(NSInteger)rowIndex { 323 row:(NSInteger)rowIndex {
322 return NO; 324 return NO;
323 } 325 }
324 326
325 - (void)onOptionsInitialized { 327 - (void)onOptionsInitialized {
326 [self updateTableView]; 328 [self updateTableView];
327 } 329 }
328 330
329 - (void)onOptionAdded:(NSInteger)index { 331 - (void)onOptionAdded:(NSInteger)index {
330 [self updateTableView]; 332 [self updateTableView];
331 } 333 }
332 334
333 - (void)onOptionRemoved:(NSInteger)index { 335 - (void)onOptionRemoved:(NSInteger)index {
334 // |tableView_| will automatically selects the next item if the current 336 // |tableView_| will automatically selects the next item if the current
335 // 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
336 // that was previously selected, if so, deselect it. 338 // that was previously selected, if so, deselect it.
337 if ([tableView_ selectedRow] == index) 339 if ([tableView_ selectedRow] == index)
338 [tableView_ deselectRow:index]; 340 [tableView_ deselectRow:index];
339 341
340 [self updateTableView]; 342 [self updateTableView];
341 } 343 }
342 344
343 - (void)updateTableView { 345 - (void)updateTableView {
344 [tableView_ setEnabled:chooserBubbleDelegate_->NumOptions() > 0]; 346 [tableView_ setEnabled:chooserBubbleController_->NumOptions() > 0];
345 [tableView_ reloadData]; 347 [tableView_ reloadData];
346 } 348 }
347 349
348 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification { 350 - (void)tableViewSelectionDidChange:(NSNotification*)aNotification {
349 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0]; 351 [connectButton_ setEnabled:[tableView_ numberOfSelectedRows] > 0];
350 } 352 }
351 353
352 - (void)updateAnchorPosition { 354 - (void)updateAnchorPosition {
353 [self setParentWindow:[self getExpectedParentWindow]]; 355 [self setParentWindow:[self getExpectedParentWindow]];
354 [self setAnchorPoint:[self getExpectedAnchorPoint]]; 356 [self setAnchorPoint:[self getExpectedAnchorPoint]];
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 NSRect frameA = [viewA frame]; 437 NSRect frameA = [viewA frame];
436 NSRect frameB = [viewB frame]; 438 NSRect frameB = [viewB frame];
437 frameA.origin.y = 439 frameA.origin.y =
438 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2); 440 NSMinY(frameB) + std::floor((NSHeight(frameB) - NSHeight(frameA)) / 2);
439 [viewA setFrameOrigin:frameA.origin]; 441 [viewA setFrameOrigin:frameA.origin];
440 } 442 }
441 443
442 - (void)onConnect:(id)sender { 444 - (void)onConnect:(id)sender {
443 buttonPressed_ = true; 445 buttonPressed_ = true;
444 NSInteger row = [tableView_ selectedRow]; 446 NSInteger row = [tableView_ selectedRow];
445 chooserBubbleDelegate_->Select(row); 447 chooserBubbleController_->Select(row);
446 [self close]; 448 [self close];
447 } 449 }
448 450
449 - (void)onCancel:(id)sender { 451 - (void)onCancel:(id)sender {
450 buttonPressed_ = true; 452 buttonPressed_ = true;
451 chooserBubbleDelegate_->Cancel(); 453 chooserBubbleController_->Cancel();
452 [self close]; 454 [self close];
453 } 455 }
454 456
455 @end 457 @end
456 458
457 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa( 459 ChooserBubbleUiCocoa::ChooserBubbleUiCocoa(
458 Browser* browser, 460 Browser* browser,
459 ChooserBubbleDelegate* chooser_bubble_delegate) 461 ChooserBubbleController* chooser_bubble_controller)
460 : browser_(browser), 462 : browser_(browser),
461 chooser_bubble_delegate_(chooser_bubble_delegate), 463 chooser_bubble_controller_(chooser_bubble_controller),
462 chooser_bubble_ui_controller_(nil) { 464 chooser_bubble_ui_controller_(nil) {
463 DCHECK(browser); 465 DCHECK(browser);
464 DCHECK(chooser_bubble_delegate); 466 DCHECK(chooser_bubble_controller);
465 chooser_bubble_delegate_->set_observer(this); 467 chooser_bubble_controller_->set_observer(this);
466 } 468 }
467 469
468 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() { 470 ChooserBubbleUiCocoa::~ChooserBubbleUiCocoa() {
469 chooser_bubble_delegate_->set_observer(nullptr); 471 chooser_bubble_controller_->set_observer(nullptr);
470 [chooser_bubble_ui_controller_ close]; 472 [chooser_bubble_ui_controller_ close];
471 chooser_bubble_ui_controller_ = nil; 473 chooser_bubble_ui_controller_ = nil;
472 } 474 }
473 475
474 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) { 476 void ChooserBubbleUiCocoa::Show(BubbleReference bubble_reference) {
475 if (!chooser_bubble_ui_controller_) { 477 if (!chooser_bubble_ui_controller_) {
476 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc] 478 chooser_bubble_ui_controller_ = [[ChooserBubbleUiController alloc]
477 initWithBrowser:browser_ 479 initWithBrowser:browser_
478 initWithChooserBubbleDelegate:chooser_bubble_delegate_ 480 initWithChooserBubbleController:chooser_bubble_controller_
479 bridge:this]; 481 bridge:this];
480 } 482 }
481 483
482 [chooser_bubble_ui_controller_ show]; 484 [chooser_bubble_ui_controller_ show];
483 [chooser_bubble_ui_controller_ updateTableView]; 485 [chooser_bubble_ui_controller_ updateTableView];
484 } 486 }
485 487
486 void ChooserBubbleUiCocoa::Close() { 488 void ChooserBubbleUiCocoa::Close() {
487 [chooser_bubble_ui_controller_ close]; 489 [chooser_bubble_ui_controller_ close];
488 chooser_bubble_ui_controller_ = nil; 490 chooser_bubble_ui_controller_ = nil;
489 } 491 }
(...skipping 10 matching lines...) Expand all
500 [chooser_bubble_ui_controller_ onOptionAdded:static_cast<NSInteger>(index)]; 502 [chooser_bubble_ui_controller_ onOptionAdded:static_cast<NSInteger>(index)];
501 } 503 }
502 504
503 void ChooserBubbleUiCocoa::OnOptionRemoved(size_t index) { 505 void ChooserBubbleUiCocoa::OnOptionRemoved(size_t index) {
504 [chooser_bubble_ui_controller_ onOptionRemoved:static_cast<NSInteger>(index)]; 506 [chooser_bubble_ui_controller_ onOptionRemoved:static_cast<NSInteger>(index)];
505 } 507 }
506 508
507 void ChooserBubbleUiCocoa::OnBubbleClosing() { 509 void ChooserBubbleUiCocoa::OnBubbleClosing() {
508 chooser_bubble_ui_controller_ = nil; 510 chooser_bubble_ui_controller_ = nil;
509 } 511 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698