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

Side by Side Diff: chrome/browser/ui/cocoa/chooser_content_view_cocoa.mm

Issue 2086663003: Change ChooserController ownership model (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removed temporary unique ptr at ChooserBubbleUiCocoa Created 4 years, 5 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/chooser_content_view_cocoa.h" 5 #import "chrome/browser/ui/cocoa/chooser_content_view_cocoa.h"
6 6
7 #include <algorithm>
8
9 #include "base/macros.h"
10 #include "base/strings/sys_string_conversions.h"
7 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" 11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h"
8 #include "chrome/grit/generated_resources.h" 12 #include "chrome/grit/generated_resources.h"
13 #include "components/chooser_controller/chooser_controller.h"
9 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw eaker.h" 14 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw eaker.h"
10 #import "ui/base/cocoa/controls/hyperlink_button_cell.h" 15 #import "ui/base/cocoa/controls/hyperlink_button_cell.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/base/l10n/l10n_util_mac.h" 16 #include "ui/base/l10n/l10n_util_mac.h"
13 17
14 namespace { 18 namespace {
15 19
16 // Chooser width. 20 // Chooser width.
17 const CGFloat kChooserWidth = 320.0f; 21 const CGFloat kChooserWidth = 320.0f;
18 22
19 // Chooser height. 23 // Chooser height.
20 const CGFloat kChooserHeight = 280.0f; 24 const CGFloat kChooserHeight = 280.0f;
21 25
22 // Distance between the chooser border and the view that is closest to the 26 // Distance between the chooser border and the view that is closest to the
23 // border. 27 // border.
24 const CGFloat kMarginX = 20.0f; 28 const CGFloat kMarginX = 20.0f;
25 const CGFloat kMarginY = 20.0f; 29 const CGFloat kMarginY = 20.0f;
26 30
27 // Distance between two views inside the chooser. 31 // Distance between two views inside the chooser.
28 const CGFloat kHorizontalPadding = 10.0f; 32 const CGFloat kHorizontalPadding = 10.0f;
29 const CGFloat kVerticalPadding = 10.0f; 33 const CGFloat kVerticalPadding = 10.0f;
30 34
31 // Separator alpha value. 35 // Separator alpha value.
32 const CGFloat kSeparatorAlphaValue = 0.6f; 36 const CGFloat kSeparatorAlphaValue = 0.6f;
33 37
34 // Separator height. 38 // Separator height.
35 const CGFloat kSeparatorHeight = 1.0f; 39 const CGFloat kSeparatorHeight = 1.0f;
36 40
37 } // namespace 41 } // namespace
38 42
43 class TableViewController : public ChooserController::Observer {
44 public:
45 TableViewController(ChooserController* chooser_controller,
46 NSTableView* table_view);
47 ~TableViewController() override;
48
49 // ChooserController::Observer:
50 void OnOptionsInitialized() override;
51 void OnOptionAdded(size_t index) override;
52 void OnOptionRemoved(size_t index) override;
53
54 void UpdateTableView();
55
56 private:
57 ChooserController* chooser_controller_;
58 NSTableView* table_view_;
59
60 DISALLOW_COPY_AND_ASSIGN(TableViewController);
61 };
62
63 TableViewController::TableViewController(ChooserController* chooser_controller,
64 NSTableView* table_view)
65 : chooser_controller_(chooser_controller), table_view_(table_view) {
66 DCHECK(chooser_controller_);
67 DCHECK(table_view_);
68 chooser_controller_->set_observer(this);
69 }
70
71 TableViewController::~TableViewController() {
72 chooser_controller_->set_observer(nullptr);
73 }
74
75 void TableViewController::OnOptionsInitialized() {
76 UpdateTableView();
77 }
78
79 void TableViewController::OnOptionAdded(size_t index) {
80 UpdateTableView();
81 }
82
83 void TableViewController::OnOptionRemoved(size_t index) {
84 // |table_view_| will automatically select the removed item's next item.
85 // So here it tracks if the removed item is the item that was currently
86 // selected, if so, deselect it. Also if the removed item is before the
87 // currently selected item, the currently selected item's index needs to
88 // be adjusted by one.
89 NSInteger idx = static_cast<NSInteger>(index);
90 NSInteger selected_row = [table_view_ selectedRow];
91 if (selected_row == idx)
92 [table_view_ deselectRow:idx];
93 else if (selected_row > idx)
94 [table_view_
95 selectRowIndexes:[NSIndexSet indexSetWithIndex:selected_row - 1]
96 byExtendingSelection:NO];
97
98 UpdateTableView();
99 }
100
101 void TableViewController::UpdateTableView() {
102 [table_view_ setEnabled:chooser_controller_->NumOptions() > 0];
103 [table_view_ reloadData];
104 }
105
39 @implementation ChooserContentViewCocoa 106 @implementation ChooserContentViewCocoa
40 107
41 - (instancetype)initWithChooserTitle:(NSString*)chooserTitle { 108 - (instancetype)initWithChooserTitle:(NSString*)chooserTitle
109 chooserController:
110 (std::unique_ptr<ChooserController>)chooserController {
42 // ------------------------------------ 111 // ------------------------------------
43 // | Chooser title | 112 // | Chooser title |
44 // | -------------------------------- | 113 // | -------------------------------- |
45 // | | option 0 | | 114 // | | option 0 | |
46 // | | option 1 | | 115 // | | option 1 | |
47 // | | option 2 | | 116 // | | option 2 | |
48 // | | | | 117 // | | | |
49 // | | | | 118 // | | | |
50 // | | | | 119 // | | | |
51 // | -------------------------------- | 120 // | -------------------------------- |
52 // | [ Connect ] [ Cancel ] | 121 // | [ Connect ] [ Cancel ] |
53 // |----------------------------------| 122 // |----------------------------------|
54 // | Not seeing your device? Get help | 123 // | Not seeing your device? Get help |
55 // ------------------------------------ 124 // ------------------------------------
56 125
57 // Determine the dimensions of the chooser. 126 // Determine the dimensions of the chooser.
58 // Once the height and width are set, the buttons and permission menus can 127 // Once the height and width are set, the buttons and permission menus can
59 // be laid out correctly. 128 // be laid out correctly.
60 NSRect chooserFrame = NSMakeRect(0, 0, kChooserWidth, kChooserHeight); 129 NSRect chooserFrame = NSMakeRect(0, 0, kChooserWidth, kChooserHeight);
61 130
62 if ((self = [super initWithFrame:chooserFrame])) { 131 if ((self = [super initWithFrame:chooserFrame])) {
132 chooserController_ = std::move(chooserController);
133
63 // Create the views. 134 // Create the views.
64 // Title. 135 // Title.
65 titleView_ = [self createChooserTitle:chooserTitle]; 136 titleView_ = [self createChooserTitle:chooserTitle];
66 CGFloat titleHeight = NSHeight([titleView_ frame]); 137 CGFloat titleHeight = NSHeight([titleView_ frame]);
67 138
68 // Connect button. 139 // Connect button.
69 connectButton_ = [self createConnectButton]; 140 connectButton_ = [self createConnectButton];
70 CGFloat connectButtonWidth = NSWidth([connectButton_ frame]); 141 CGFloat connectButtonWidth = NSWidth([connectButton_ frame]);
71 CGFloat connectButtonHeight = NSHeight([connectButton_ frame]); 142 CGFloat connectButtonHeight = NSHeight([connectButton_ frame]);
72 143
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 CGFloat messageOriginY = kMarginY; 220 CGFloat messageOriginY = kMarginY;
150 [message_ setFrameOrigin:NSMakePoint(messageOriginX, messageOriginY)]; 221 [message_ setFrameOrigin:NSMakePoint(messageOriginX, messageOriginY)];
151 [self addSubview:message_]; 222 [self addSubview:message_];
152 223
153 // Help button. 224 // Help button.
154 CGFloat helpButtonOriginX = 225 CGFloat helpButtonOriginX =
155 kMarginX + messageWidth - kHorizontalPadding / 2; 226 kMarginX + messageWidth - kHorizontalPadding / 2;
156 CGFloat helpButtonOriginY = kMarginY; 227 CGFloat helpButtonOriginY = kMarginY;
157 [helpButton_ 228 [helpButton_
158 setFrameOrigin:NSMakePoint(helpButtonOriginX, helpButtonOriginY)]; 229 setFrameOrigin:NSMakePoint(helpButtonOriginX, helpButtonOriginY)];
230 [helpButton_ setTarget:self];
231 [helpButton_ setAction:@selector(onHelpPressed:)];
159 [self addSubview:helpButton_]; 232 [self addSubview:helpButton_];
233
234 tableViewController_.reset(
235 new TableViewController(chooserController_.get(), tableView_.get()));
160 } 236 }
161 237
162 return self; 238 return self;
163 } 239 }
164 240
165 - (base::scoped_nsobject<NSTextField>)createChooserTitle:(NSString*)title { 241 - (base::scoped_nsobject<NSTextField>)createChooserTitle:(NSString*)title {
166 base::scoped_nsobject<NSTextField> titleView( 242 base::scoped_nsobject<NSTextField> titleView(
167 [[NSTextField alloc] initWithFrame:NSZeroRect]); 243 [[NSTextField alloc] initWithFrame:NSZeroRect]);
168 [titleView setDrawsBackground:NO]; 244 [titleView setDrawsBackground:NO];
169 [titleView setBezeled:NO]; 245 [titleView setBezeled:NO];
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 } 318 }
243 319
244 - (NSButton*)cancelButton { 320 - (NSButton*)cancelButton {
245 return cancelButton_.get(); 321 return cancelButton_.get();
246 } 322 }
247 323
248 - (NSButton*)helpButton { 324 - (NSButton*)helpButton {
249 return helpButton_.get(); 325 return helpButton_.get();
250 } 326 }
251 327
328 - (NSInteger)numberOfOptions {
329 // When there are no devices, the table contains a message saying there are
330 // no devices, so the number of rows is always at least 1.
331 return std::max(static_cast<NSInteger>(chooserController_->NumOptions()),
332 static_cast<NSInteger>(1));
333 }
334
335 - (NSString*)optionAtIndex:(NSInteger)index {
336 NSInteger numOptions =
337 static_cast<NSInteger>(chooserController_->NumOptions());
338 if (numOptions == 0) {
339 DCHECK_EQ(0, index);
340 return l10n_util::GetNSString(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT);
341 }
342
343 DCHECK_GE(index, 0);
344 DCHECK_LT(index, numOptions);
345
346 return base::SysUTF16ToNSString(
347 chooserController_->GetOption(static_cast<size_t>(index)));
348 }
349
350 - (void)updateTableView {
351 tableViewController_->UpdateTableView();
352 }
353
354 - (void)accept {
355 chooserController_->Select([tableView_ selectedRow]);
356 }
357
358 - (void)cancel {
359 chooserController_->Cancel();
360 }
361
362 - (void)close {
363 chooserController_->Close();
364 }
365
366 - (void)onHelpPressed:(id)sender {
367 chooserController_->OpenHelpCenterUrl();
368 }
369
252 @end 370 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/chooser_content_view_cocoa.h ('k') | chrome/browser/ui/cocoa/extensions/chooser_dialog_cocoa.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698