OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/media_picker/desktop_media_picker_controller.h" | 5 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #import "base/mac/bundle_locations.h" | 11 #import "base/mac/bundle_locations.h" |
12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
13 #include "chrome/browser/media/combined_desktop_media_list.h" | 13 #include "chrome/browser/media/combined_desktop_media_list.h" |
14 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h" | 14 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h" |
15 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_item.h" | 15 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_item.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/grit/chromium_strings.h" | |
17 #include "chrome/grit/generated_resources.h" | 18 #include "chrome/grit/generated_resources.h" |
19 #include "chrome/grit/google_chrome_strings.h" | |
18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
20 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
21 #include "content/public/browser/web_contents_delegate.h" | 23 #include "content/public/browser/web_contents_delegate.h" |
22 #include "grit/components_strings.h" | 24 #include "grit/components_strings.h" |
23 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw eaker.h" | 25 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw eaker.h" |
24 #import "ui/base/cocoa/flipped_view.h" | 26 #import "ui/base/cocoa/flipped_view.h" |
25 #import "ui/base/cocoa/window_size_constants.h" | 27 #import "ui/base/cocoa/window_size_constants.h" |
26 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
27 #include "ui/gfx/image/image_skia_util_mac.h" | 29 #include "ui/gfx/image/image_skia_util_mac.h" |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 const int kInitialContentWidth = 620; | 33 const CGFloat kInitialContentWidth = 620; |
32 const int kMinimumContentWidth = 500; | 34 const CGFloat kMinimumContentWidth = 500; |
33 const int kMinimumContentHeight = 390; | 35 const CGFloat kMinimumContentHeight = 390; |
34 const int kThumbnailWidth = 150; | 36 const CGFloat kThumbnailWidth = 150; |
35 const int kThumbnailHeight = 150; | 37 const CGFloat kThumbnailHeight = 150; |
36 const int kFramePadding = 20; | 38 const CGFloat kSingleScreenWidth = 300; |
37 const int kControlSpacing = 10; | 39 const CGFloat kSingleScreenHeight = 300; |
38 const int kExcessButtonPadding = 6; | 40 const CGFloat kFramePadding = 20; |
41 const CGFloat kControlSpacing = 10; | |
42 const CGFloat kExcessButtonPadding = 6; | |
43 const CGFloat kRowHeight = 20; | |
44 const CGFloat kRowWidth = 500; | |
45 const CGFloat kIconWidth = 20; | |
46 const CGFloat kPaddedWidth = kInitialContentWidth - (kFramePadding * 2); | |
47 | |
48 NSString* const kIconId = @"icon"; | |
49 NSString* const kTitleId = @"title"; | |
39 | 50 |
40 } // namespace | 51 } // namespace |
41 | 52 |
42 @interface DesktopMediaPickerController (Private) | 53 using content::DesktopMediaID; |
tapted
2016/06/23 03:38:14
nit: this typically comes immediately after the #i
qiangchen
2016/06/23 17:49:37
Done.
| |
54 | |
55 @interface DesktopMediaPickerController () | |
43 | 56 |
44 // Populate the window with controls and views. | 57 // Populate the window with controls and views. |
45 - (void)initializeContentsWithAppName:(const base::string16&)appName; | 58 - (void)initializeContentsWithAppName:(const base::string16&)appName |
59 targetName:(const base::string16&)targetName | |
60 requestAudio:(bool)requestAudio; | |
61 | |
62 // Add |NSSegmentControl| for source type switch. | |
63 - (void)createTypeButtonAtOrigin:(NSPoint)origin; | |
64 | |
65 // Add |IKImageBrowerView| for screen and window source view. | |
66 // Add |NSTableView| for tab source view. | |
67 - (void)createSourceViewsAtOrigin:(NSPoint)origin; | |
68 | |
69 // Add check box for audio sharing. | |
70 - (void)createAudioCheckboxAtOrigin:(NSPoint)origin; | |
71 | |
72 // Create the share and cancel button. | |
73 - (void)createActionButtonsAtOrigin:(NSPoint)origin; | |
46 | 74 |
47 // Create a |NSTextField| with label traits given |width|. Frame height is | 75 // Create a |NSTextField| with label traits given |width|. Frame height is |
48 // automatically adjusted to fit. | 76 // automatically adjusted to fit. |
49 - (NSTextField*)createTextFieldWithText:(NSString*)text | 77 - (NSTextField*)createTextFieldWithText:(NSString*)text |
50 frameWidth:(CGFloat)width; | 78 frameWidth:(CGFloat)width; |
51 | 79 |
52 // Create a button with |title|, with size adjusted to fit. | 80 // Create a button with |title|, with size adjusted to fit. |
53 - (NSButton*)createButtonWithTitle:(NSString*)title; | 81 - (NSButton*)createButtonWithTitle:(NSString*)title; |
54 | 82 |
83 - (IKImageBrowserView*)createImageBrowserWithSize:(NSSize)size; | |
84 | |
55 // Report result by invoking |doneCallback_|. The callback is invoked only on | 85 // Report result by invoking |doneCallback_|. The callback is invoked only on |
56 // the first call to |reportResult:|. Subsequent calls will be no-ops. | 86 // the first call to |reportResult:|. Subsequent calls will be no-ops. |
57 - (void)reportResult:(content::DesktopMediaID)sourceID; | 87 - (void)reportResult:(DesktopMediaID)sourceID; |
58 | 88 |
59 // Action handlers. | 89 // Action handlers. |
60 - (void)sharePressed:(id)sender; | 90 - (void)sharePressed:(id)sender; |
61 - (void)cancelPressed:(id)sender; | 91 - (void)cancelPressed:(id)sender; |
62 | 92 |
93 // Helper functions to get source type, or get data entities based on source | |
94 // type. | |
95 - (DesktopMediaID::Type)selectedSourceType; | |
96 - (DesktopMediaID::Type)sourceTypeForList:(DesktopMediaList*)list; | |
97 - (DesktopMediaID::Type)sourceTypeForBrowser:(id)browser; | |
98 - (id)browserViewForType:(DesktopMediaID::Type)sourceType; | |
99 - (NSMutableArray*)itemSetForType:(DesktopMediaID::Type)sourceType; | |
100 - (NSInteger)selectedIndexForType:(DesktopMediaID::Type)sourceType; | |
101 - (void)setTabBrowserIndex:(NSInteger)index; | |
102 | |
63 @end | 103 @end |
64 | 104 |
65 @implementation DesktopMediaPickerController | 105 @implementation DesktopMediaPickerController |
66 | 106 |
67 - (id)initWithScreenList:(std::unique_ptr<DesktopMediaList>)screen_list | 107 - (id)initWithScreenList:(std::unique_ptr<DesktopMediaList>)screenList |
68 windowList:(std::unique_ptr<DesktopMediaList>)window_list | 108 windowList:(std::unique_ptr<DesktopMediaList>)windowList |
69 tabList:(std::unique_ptr<DesktopMediaList>)tab_list | 109 tabList:(std::unique_ptr<DesktopMediaList>)tabList |
70 parent:(NSWindow*)parent | 110 parent:(NSWindow*)parent |
71 callback:(const DesktopMediaPicker::DoneCallback&)callback | 111 callback:(const DesktopMediaPicker::DoneCallback&)callback |
72 appName:(const base::string16&)appName | 112 appName:(const base::string16&)appName |
73 targetName:(const base::string16&)targetName | 113 targetName:(const base::string16&)targetName |
74 requestAudio:(bool)requestAudio { | 114 requestAudio:(bool)requestAudio { |
75 const NSUInteger kStyleMask = | 115 const NSUInteger kStyleMask = |
76 NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask; | 116 NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask; |
77 base::scoped_nsobject<NSWindow> window( | 117 base::scoped_nsobject<NSWindow> window( |
78 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater | 118 [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater |
79 styleMask:kStyleMask | 119 styleMask:kStyleMask |
80 backing:NSBackingStoreBuffered | 120 backing:NSBackingStoreBuffered |
81 defer:NO]); | 121 defer:NO]); |
82 | 122 |
83 if ((self = [super initWithWindow:window])) { | 123 if ((self = [super initWithWindow:window])) { |
84 [parent addChildWindow:window ordered:NSWindowAbove]; | 124 [parent addChildWindow:window ordered:NSWindowAbove]; |
85 [window setDelegate:self]; | 125 [window setDelegate:self]; |
126 if (screenList) { | |
127 screenList_ = std::move(screenList); | |
128 screenItems_.reset([[NSMutableArray alloc] init]); | |
129 } | |
130 | |
131 if (windowList) { | |
132 windowList_ = std::move(windowList); | |
133 windowList_->SetViewDialogWindowId( | |
134 DesktopMediaID(DesktopMediaID::TYPE_WINDOW, [window windowNumber])); | |
135 windowItems_.reset([[NSMutableArray alloc] init]); | |
136 } | |
137 | |
138 if (tabList) { | |
139 tabList_ = std::move(tabList); | |
140 tabItems_.reset([[NSMutableArray alloc] init]); | |
141 } | |
142 | |
86 [self initializeContentsWithAppName:appName | 143 [self initializeContentsWithAppName:appName |
87 targetName:targetName | 144 targetName:targetName |
88 requestAudio:requestAudio]; | 145 requestAudio:requestAudio]; |
89 std::vector<std::unique_ptr<DesktopMediaList>> media_lists; | 146 doneCallback_ = callback; |
90 if (screen_list) | |
91 media_lists.push_back(std::move(screen_list)); | |
92 | 147 |
93 if (window_list) | |
94 media_lists.push_back(std::move(window_list)); | |
95 | |
96 if (tab_list) | |
97 media_lists.push_back(std::move(tab_list)); | |
98 | |
99 if (media_lists.size() > 1) | |
100 media_list_.reset(new CombinedDesktopMediaList(media_lists)); | |
101 else | |
102 media_list_ = std::move(media_lists[0]); | |
103 media_list_->SetViewDialogWindowId(content::DesktopMediaID( | |
104 content::DesktopMediaID::TYPE_WINDOW, [window windowNumber])); | |
105 doneCallback_ = callback; | |
106 items_.reset([[NSMutableArray alloc] init]); | |
107 bridge_.reset(new DesktopMediaPickerBridge(self)); | 148 bridge_.reset(new DesktopMediaPickerBridge(self)); |
108 } | 149 } |
109 return self; | 150 return self; |
110 } | 151 } |
111 | 152 |
112 - (void)dealloc { | 153 - (void)dealloc { |
113 [shareButton_ setTarget:nil]; | 154 [shareButton_ setTarget:nil]; |
114 [cancelButton_ setTarget:nil]; | 155 [cancelButton_ setTarget:nil]; |
115 [sourceBrowser_ setDelegate:nil]; | 156 [screenBrowser_ setDelegate:nil]; |
116 [sourceBrowser_ setDataSource:nil]; | 157 [screenBrowser_ setDataSource:nil]; |
158 [windowBrowser_ setDelegate:nil]; | |
159 [windowBrowser_ setDataSource:nil]; | |
160 [tabBrowser_ setDataSource:nil]; | |
161 [tabBrowser_ setDelegate:nil]; | |
117 [[self window] close]; | 162 [[self window] close]; |
118 [super dealloc]; | 163 [super dealloc]; |
119 } | 164 } |
120 | 165 |
121 - (void)initializeContentsWithAppName:(const base::string16&)appName | 166 - (void)initializeContentsWithAppName:(const base::string16&)appName |
122 targetName:(const base::string16&)targetName | 167 targetName:(const base::string16&)targetName |
123 requestAudio:(bool)requestAudio { | 168 requestAudio:(bool)requestAudio { |
124 // Use flipped coordinates to facilitate manual layout. | 169 // Use flipped coordinates to facilitate manual layout. |
125 const CGFloat kPaddedWidth = kInitialContentWidth - (kFramePadding * 2); | |
126 base::scoped_nsobject<FlippedView> content( | 170 base::scoped_nsobject<FlippedView> content( |
127 [[FlippedView alloc] initWithFrame:NSZeroRect]); | 171 [[FlippedView alloc] initWithFrame:NSZeroRect]); |
128 [[self window] setContentView:content]; | 172 [[self window] setContentView:content]; |
129 NSPoint origin = NSMakePoint(kFramePadding, kFramePadding); | 173 NSPoint origin = NSMakePoint(kFramePadding, kFramePadding); |
130 | 174 |
131 // Set the dialog's title. | 175 // Set the dialog's title. |
132 NSString* titleText = l10n_util::GetNSStringF( | 176 NSString* titleText = l10n_util::GetNSString(IDS_DESKTOP_MEDIA_PICKER_TITLE); |
133 IDS_DESKTOP_MEDIA_PICKER_TITLE_DEPRECATED, appName); | |
134 [[self window] setTitle:titleText]; | 177 [[self window] setTitle:titleText]; |
135 | 178 |
136 // Set the dialog's description. | 179 // Set the dialog's description. |
137 NSString* descriptionText; | 180 NSString* descriptionText; |
138 if (appName == targetName) { | 181 if (appName == targetName) { |
139 descriptionText = l10n_util::GetNSStringF( | 182 descriptionText = l10n_util::GetNSStringF( |
140 IDS_DESKTOP_MEDIA_PICKER_TEXT, appName); | 183 IDS_DESKTOP_MEDIA_PICKER_TEXT, appName); |
141 } else { | 184 } else { |
142 descriptionText = l10n_util::GetNSStringF( | 185 descriptionText = l10n_util::GetNSStringF( |
143 IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, appName, targetName); | 186 IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, appName, targetName); |
144 } | 187 } |
145 NSTextField* description = [self createTextFieldWithText:descriptionText | 188 NSTextField* description = [self createTextFieldWithText:descriptionText |
146 frameWidth:kPaddedWidth]; | 189 frameWidth:kPaddedWidth]; |
147 [description setFrameOrigin:origin]; | 190 [description setFrameOrigin:origin]; |
148 [content addSubview:description]; | 191 [content addSubview:description]; |
149 origin.y += NSHeight([description frame]) + kControlSpacing; | 192 origin.y += NSHeight([description frame]) + kControlSpacing; |
150 | 193 |
151 // Create the image browser. | 194 [self createTypeButtonAtOrigin:origin]; |
152 sourceBrowser_.reset([[IKImageBrowserView alloc] initWithFrame:NSZeroRect]); | 195 origin.y += NSHeight([sourceTypeControl_ frame]) + kControlSpacing; |
153 NSUInteger cellStyle = IKCellsStyleShadowed | IKCellsStyleTitled; | |
154 [sourceBrowser_ setDelegate:self]; | |
155 [sourceBrowser_ setDataSource:self]; | |
156 [sourceBrowser_ setCellsStyleMask:cellStyle]; | |
157 [sourceBrowser_ setCellSize:NSMakeSize(kThumbnailWidth, kThumbnailHeight)]; | |
158 [sourceBrowser_ setAllowsMultipleSelection:NO]; | |
159 | 196 |
160 // Create a scroll view to host the image browser. | 197 [self createSourceViewsAtOrigin:origin]; |
161 NSRect imageBrowserScrollFrame = NSMakeRect( | 198 origin.y += NSHeight([imageBrowserScroll_ frame]) + kControlSpacing; |
162 origin.x, origin.y, kPaddedWidth, 350); | |
163 base::scoped_nsobject<NSScrollView> imageBrowserScroll( | |
164 [[NSScrollView alloc] initWithFrame:imageBrowserScrollFrame]); | |
165 [imageBrowserScroll setHasVerticalScroller:YES]; | |
166 [imageBrowserScroll setDocumentView:sourceBrowser_]; | |
167 [imageBrowserScroll setBorderType:NSBezelBorder]; | |
168 [imageBrowserScroll setAutoresizingMask: | |
169 NSViewWidthSizable | NSViewHeightSizable]; | |
170 [content addSubview:imageBrowserScroll]; | |
171 origin.y += NSHeight(imageBrowserScrollFrame) + kControlSpacing; | |
172 | 199 |
173 // Create a checkbox for audio sharing. | |
174 if (requestAudio) { | 200 if (requestAudio) { |
175 audioShareCheckbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]); | 201 [self createAudioCheckboxAtOrigin:origin]; |
176 [audioShareCheckbox_ setFrameOrigin:origin]; | |
177 [audioShareCheckbox_ | |
178 setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin]; | |
179 [audioShareCheckbox_ setButtonType:NSSwitchButton]; | |
180 audioShareState_ = NSOnState; | |
181 [audioShareCheckbox_ | |
182 setTitle:l10n_util::GetNSString(IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE)]; | |
183 [audioShareCheckbox_ sizeToFit]; | |
184 [audioShareCheckbox_ setEnabled:NO]; | |
185 [audioShareCheckbox_ | |
186 setToolTip:l10n_util::GetNSString( | |
187 IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE_TOOLTIP_MAC)]; | |
188 [content addSubview:audioShareCheckbox_]; | |
189 origin.y += NSHeight([audioShareCheckbox_ frame]) + kControlSpacing; | 202 origin.y += NSHeight([audioShareCheckbox_ frame]) + kControlSpacing; |
190 } | 203 } |
191 | 204 |
205 [self createActionButtonsAtOrigin:origin]; | |
206 origin.y += | |
207 kFramePadding + (NSHeight([cancelButton_ frame]) - kExcessButtonPadding); | |
208 | |
209 // Resize window to fit. | |
210 [content setAutoresizesSubviews:NO]; | |
211 [[self window] setContentSize:NSMakeSize(kInitialContentWidth, origin.y)]; | |
212 [[self window] setContentMinSize:NSMakeSize(kMinimumContentWidth, | |
213 kMinimumContentHeight)]; | |
214 [content setAutoresizesSubviews:YES]; | |
215 | |
216 // Initialize the type selection at the first segment. | |
217 [sourceTypeControl_ setSelected:YES forSegment:0]; | |
218 [self typeButtonPressed:sourceTypeControl_]; | |
219 [[self window] | |
220 makeFirstResponder:[self browserViewForType:[self selectedSourceType]]]; | |
221 } | |
222 | |
223 - (void)createTypeButtonAtOrigin:(NSPoint)origin { | |
224 FlippedView* content = [[self window] contentView]; | |
225 // Create segmented button. | |
226 sourceTypeControl_.reset([[NSSegmentedControl alloc] init]); | |
tapted
2016/06/23 03:38:15
nit: initWthFrame:NSZeroRect (to invoke the desig
qiangchen
2016/06/23 17:49:38
Done.
| |
227 | |
228 NSInteger segmentCount = | |
229 (screenList_ ? 1 : 0) + (windowList_ ? 1 : 0) + (tabList_ ? 1 : 0); | |
230 [sourceTypeControl_ setSegmentCount:segmentCount]; | |
231 NSInteger segmentIndex = 0; | |
232 | |
233 if (screenList_) { | |
234 [sourceTypeControl_ | |
235 setLabel:l10n_util::GetNSString( | |
236 IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_SCREEN) | |
237 forSegment:segmentIndex]; | |
238 | |
239 [[sourceTypeControl_ cell] setTag:DesktopMediaID::TYPE_SCREEN | |
240 forSegment:segmentIndex]; | |
241 ++segmentIndex; | |
242 } | |
243 | |
244 if (windowList_) { | |
245 [sourceTypeControl_ | |
246 setLabel:l10n_util::GetNSString( | |
247 IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_WINDOW) | |
248 forSegment:segmentIndex]; | |
249 [[sourceTypeControl_ cell] setTag:DesktopMediaID::TYPE_WINDOW | |
250 forSegment:segmentIndex]; | |
251 ++segmentIndex; | |
252 } | |
253 | |
254 if (tabList_) { | |
255 [sourceTypeControl_ setLabel:l10n_util::GetNSString( | |
256 IDS_DESKTOP_MEDIA_PICKER_SOURCE_TYPE_TAB) | |
257 forSegment:segmentIndex]; | |
258 [[sourceTypeControl_ cell] setTag:DesktopMediaID::TYPE_WEB_CONTENTS | |
259 forSegment:segmentIndex]; | |
260 } | |
261 [sourceTypeControl_ setAction:@selector(typeButtonPressed:)]; | |
262 | |
263 [[sourceTypeControl_ cell] setTrackingMode:NSSegmentSwitchTrackingSelectOne]; | |
264 | |
265 [content addSubview:sourceTypeControl_]; | |
tapted
2016/06/23 03:38:14
nit: just make this [[self window] contentView] ad
qiangchen
2016/06/23 17:49:37
Done.
| |
266 | |
267 [sourceTypeControl_ sizeToFit]; | |
268 [sourceTypeControl_ setAutoresizingMask:NSViewMaxXMargin | NSViewMinXMargin]; | |
269 CGFloat controlWidth = NSWidth([sourceTypeControl_ frame]); | |
270 CGFloat controlHeight = NSHeight([sourceTypeControl_ frame]); | |
271 NSRect centerFrame = NSMakeRect((kInitialContentWidth - controlWidth) / 2, | |
272 origin.y, controlWidth, controlHeight); | |
273 | |
274 [sourceTypeControl_ setFrame:NSIntegralRect(centerFrame)]; | |
275 } | |
276 | |
277 - (void)createSourceViewsAtOrigin:(NSPoint)origin { | |
278 if (screenList_) { | |
279 screenBrowser_.reset( | |
tapted
2016/06/23 02:49:48
the scoped_nsobject constructor, and reset, expect
qiangchen
2016/06/23 17:49:37
Done.
| |
280 [self createImageBrowserWithSize:NSMakeSize(kSingleScreenWidth, | |
281 kSingleScreenHeight)]); | |
282 } | |
283 | |
284 if (windowList_) { | |
285 windowBrowser_.reset( | |
286 [self createImageBrowserWithSize:NSMakeSize(kThumbnailWidth, | |
287 kThumbnailHeight)]); | |
288 } | |
289 | |
290 if (tabList_) { | |
291 tabBrowser_.reset([[NSTableView alloc] initWithFrame:NSZeroRect]); | |
292 [tabBrowser_ setDelegate:self]; | |
293 [tabBrowser_ setDataSource:self]; | |
294 [tabBrowser_ setAllowsMultipleSelection:NO]; | |
295 [tabBrowser_ setRowHeight:kRowHeight]; | |
296 [tabBrowser_ setDoubleAction:@selector(sharePressed:)]; | |
297 base::scoped_nsobject<NSTableColumn> iconColumn( | |
298 [[NSTableColumn alloc] initWithIdentifier:kIconId]); | |
299 [iconColumn setEditable:NO]; | |
300 [iconColumn setWidth:kIconWidth]; | |
301 [tabBrowser_ addTableColumn:iconColumn]; | |
302 base::scoped_nsobject<NSTableColumn> titleColumn( | |
303 [[NSTableColumn alloc] initWithIdentifier:kTitleId]); | |
304 [titleColumn setEditable:NO]; | |
305 [titleColumn setWidth:kRowWidth]; | |
306 [tabBrowser_ addTableColumn:titleColumn]; | |
307 [tabBrowser_ setHeaderView:nil]; | |
308 } | |
309 | |
310 // Create a scroll view to host the image browsers. | |
311 NSRect imageBrowserScrollFrame = | |
312 NSMakeRect(origin.x, origin.y, kPaddedWidth, 350); | |
313 imageBrowserScroll_.reset( | |
314 [[NSScrollView alloc] initWithFrame:imageBrowserScrollFrame]); | |
315 [imageBrowserScroll_ setHasVerticalScroller:YES]; | |
316 [imageBrowserScroll_ setBorderType:NSBezelBorder]; | |
317 [imageBrowserScroll_ | |
318 setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
319 [[[self window] contentView] addSubview:imageBrowserScroll_]; | |
320 } | |
321 | |
322 - (void)createAudioCheckboxAtOrigin:(NSPoint)origin { | |
323 audioShareCheckbox_.reset([[NSButton alloc] initWithFrame:NSZeroRect]); | |
324 [audioShareCheckbox_ setFrameOrigin:origin]; | |
325 [audioShareCheckbox_ setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin]; | |
326 [audioShareCheckbox_ setButtonType:NSSwitchButton]; | |
327 [audioShareCheckbox_ setState:NSOnState]; | |
328 [audioShareCheckbox_ | |
329 setTitle:l10n_util::GetNSString(IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE)]; | |
330 [audioShareCheckbox_ sizeToFit]; | |
331 [[[self window] contentView] addSubview:audioShareCheckbox_]; | |
332 } | |
333 | |
334 - (void)createActionButtonsAtOrigin:(NSPoint)origin { | |
335 FlippedView* content = [[self window] contentView]; | |
336 | |
192 // Create the share button. | 337 // Create the share button. |
193 shareButton_ = [self createButtonWithTitle:l10n_util::GetNSString( | 338 shareButton_ = |
194 IDS_DESKTOP_MEDIA_PICKER_SHARE)]; | 339 [self createButtonWithTitle:l10n_util::GetNSString( |
340 IDS_DESKTOP_MEDIA_PICKER_SHARE)]; | |
195 origin.x = kInitialContentWidth - kFramePadding - | 341 origin.x = kInitialContentWidth - kFramePadding - |
196 (NSWidth([shareButton_ frame]) - kExcessButtonPadding); | 342 (NSWidth([shareButton_ frame]) - kExcessButtonPadding); |
197 [shareButton_ setEnabled:NO]; | 343 [shareButton_ setEnabled:NO]; |
198 [shareButton_ setFrameOrigin:origin]; | 344 [shareButton_ setFrameOrigin:origin]; |
199 [shareButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin]; | 345 [shareButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin]; |
200 [shareButton_ setTarget:self]; | 346 [shareButton_ setTarget:self]; |
201 [shareButton_ setKeyEquivalent:kKeyEquivalentReturn]; | 347 [shareButton_ setKeyEquivalent:kKeyEquivalentReturn]; |
202 [shareButton_ setAction:@selector(sharePressed:)]; | 348 [shareButton_ setAction:@selector(sharePressed:)]; |
203 [content addSubview:shareButton_]; | 349 [content addSubview:shareButton_]; |
204 | 350 |
205 // Create the cancel button. | 351 // Create the cancel button. |
206 cancelButton_ = | 352 cancelButton_ = |
207 [self createButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)]; | 353 [self createButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)]; |
208 origin.x -= kControlSpacing + | 354 origin.x -= kControlSpacing + |
209 (NSWidth([cancelButton_ frame]) - (kExcessButtonPadding * 2)); | 355 (NSWidth([cancelButton_ frame]) - (kExcessButtonPadding * 2)); |
210 [cancelButton_ setFrameOrigin:origin]; | 356 [cancelButton_ setFrameOrigin:origin]; |
211 [cancelButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin]; | 357 [cancelButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin]; |
212 [cancelButton_ setTarget:self]; | 358 [cancelButton_ setTarget:self]; |
213 [cancelButton_ setKeyEquivalent:kKeyEquivalentEscape]; | 359 [cancelButton_ setKeyEquivalent:kKeyEquivalentEscape]; |
214 [cancelButton_ setAction:@selector(cancelPressed:)]; | 360 [cancelButton_ setAction:@selector(cancelPressed:)]; |
215 [content addSubview:cancelButton_]; | 361 [content addSubview:cancelButton_]; |
216 origin.y += kFramePadding + | |
217 (NSHeight([cancelButton_ frame]) - kExcessButtonPadding); | |
218 | |
219 // Resize window to fit. | |
220 [[[self window] contentView] setAutoresizesSubviews:NO]; | |
221 [[self window] setContentSize:NSMakeSize(kInitialContentWidth, origin.y)]; | |
222 [[self window] setContentMinSize: | |
223 NSMakeSize(kMinimumContentWidth, kMinimumContentHeight)]; | |
224 [[[self window] contentView] setAutoresizesSubviews:YES]; | |
225 | |
226 // Make sourceBrowser_ get keyboard focus. | |
227 [[self window] makeFirstResponder:sourceBrowser_]; | |
228 } | 362 } |
229 | 363 |
364 - (NSTextField*)createTextFieldWithText:(NSString*)text | |
365 frameWidth:(CGFloat)width { | |
366 NSRect frame = NSMakeRect(0, 0, width, 1); | |
367 base::scoped_nsobject<NSTextField> textField( | |
368 [[NSTextField alloc] initWithFrame:frame]); | |
369 [textField setEditable:NO]; | |
370 [textField setSelectable:YES]; | |
371 [textField setDrawsBackground:NO]; | |
372 [textField setBezeled:NO]; | |
373 [textField setStringValue:text]; | |
374 [textField setFont:[NSFont systemFontOfSize:13]]; | |
tapted
2016/06/23 03:38:14
nit: 13 should be a constant, if it's coming from
qiangchen
2016/06/23 17:49:38
Done.
Actually this is the existing code.
As we h
| |
375 [textField setAutoresizingMask:NSViewWidthSizable]; | |
376 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField]; | |
377 return textField.autorelease(); | |
378 } | |
379 | |
380 - (NSButton*)createButtonWithTitle:(NSString*)title { | |
381 base::scoped_nsobject<NSButton> button( | |
382 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
383 [button setButtonType:NSMomentaryPushInButton]; | |
384 [button setBezelStyle:NSRoundedBezelStyle]; | |
385 [button setTitle:title]; | |
386 [GTMUILocalizerAndLayoutTweaker sizeToFitView:button]; | |
387 return button.autorelease(); | |
388 } | |
389 | |
390 - (IKImageBrowserView*)createImageBrowserWithSize:(NSSize)size { | |
391 NSUInteger cellStyle = IKCellsStyleShadowed | IKCellsStyleTitled; | |
392 IKImageBrowserView* browser = | |
tapted
2016/06/23 02:49:48
Use scoped_nsobject and autorelease - it should be
qiangchen
2016/06/23 17:49:37
Done.
| |
393 [[IKImageBrowserView alloc] initWithFrame:NSZeroRect]; | |
394 [browser setDelegate:self]; | |
395 [browser setDataSource:self]; | |
396 [browser setCellsStyleMask:cellStyle]; | |
397 [browser setCellSize:size]; | |
398 [browser setAllowsMultipleSelection:NO]; | |
399 return browser; | |
400 } | |
401 | |
402 #pragma mark Event Actions | |
403 | |
230 - (void)showWindow:(id)sender { | 404 - (void)showWindow:(id)sender { |
231 // Signal the media_list to start sending thumbnails. |bridge_| is used as the | 405 // Signal the source lists to start sending thumbnails. |bridge_| is used as |
232 // observer, and will forward notifications to this object. | 406 // the observer, and will forward notifications to this object. |
233 media_list_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); | 407 if (screenList_) { |
234 media_list_->StartUpdating(bridge_.get()); | 408 screenList_->SetThumbnailSize( |
409 gfx::Size(kSingleScreenWidth, kSingleScreenHeight)); | |
410 screenList_->StartUpdating(bridge_.get()); | |
411 } | |
412 | |
413 if (windowList_) { | |
414 windowList_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); | |
415 windowList_->StartUpdating(bridge_.get()); | |
416 } | |
417 | |
418 if (tabList_) { | |
419 tabList_->SetThumbnailSize(gfx::Size(kIconWidth, kRowHeight)); | |
420 tabList_->StartUpdating(bridge_.get()); | |
421 } | |
235 | 422 |
236 [self.window center]; | 423 [self.window center]; |
237 [super showWindow:sender]; | 424 [super showWindow:sender]; |
238 } | 425 } |
239 | 426 |
240 - (void)reportResult:(content::DesktopMediaID)sourceID { | 427 - (void)reportResult:(DesktopMediaID)sourceID { |
241 if (doneCallback_.is_null()) { | 428 if (doneCallback_.is_null()) { |
242 return; | 429 return; |
243 } | 430 } |
244 | 431 |
245 sourceID.audio_share = [audioShareCheckbox_ isEnabled] && | 432 sourceID.audio_share = ![audioShareCheckbox_ isHidden] && |
246 [audioShareCheckbox_ state] == NSOnState; | 433 [audioShareCheckbox_ state] == NSOnState; |
247 | 434 |
248 // If the media source is an tab, activate it. | 435 // If the media source is an tab, activate it. |
249 if (sourceID.type == content::DesktopMediaID::TYPE_WEB_CONTENTS) { | 436 if (sourceID.type == DesktopMediaID::TYPE_WEB_CONTENTS) { |
250 content::WebContents* tab = content::WebContents::FromRenderFrameHost( | 437 content::WebContents* tab = content::WebContents::FromRenderFrameHost( |
251 content::RenderFrameHost::FromID( | 438 content::RenderFrameHost::FromID( |
252 sourceID.web_contents_id.render_process_id, | 439 sourceID.web_contents_id.render_process_id, |
253 sourceID.web_contents_id.main_render_frame_id)); | 440 sourceID.web_contents_id.main_render_frame_id)); |
254 if (tab) | 441 if (tab) |
255 tab->GetDelegate()->ActivateContents(tab); | 442 tab->GetDelegate()->ActivateContents(tab); |
256 } | 443 } |
257 | 444 |
258 // Notify the |callback_| asynchronously because it may release the | 445 // Notify the |callback_| asynchronously because it may release the |
259 // controller. | 446 // controller. |
260 content::BrowserThread::PostTask( | 447 content::BrowserThread::PostTask( |
261 content::BrowserThread::UI, FROM_HERE, | 448 content::BrowserThread::UI, FROM_HERE, |
262 base::Bind(doneCallback_, sourceID)); | 449 base::Bind(doneCallback_, sourceID)); |
263 doneCallback_.Reset(); | 450 doneCallback_.Reset(); |
264 } | 451 } |
265 | 452 |
266 - (void)sharePressed:(id)sender { | 453 - (void)sharePressed:(id)sender { |
267 NSIndexSet* indexes = [sourceBrowser_ selectionIndexes]; | 454 DesktopMediaID::Type selectedType = [self selectedSourceType]; |
268 NSUInteger selectedIndex = [indexes firstIndex]; | 455 NSMutableArray* items = [self itemSetForType:selectedType]; |
269 DesktopMediaPickerItem* item = | 456 NSInteger selectedIndex = [self selectedIndexForType:selectedType]; |
270 [items_ objectAtIndex:selectedIndex]; | 457 DesktopMediaPickerItem* item = [items objectAtIndex:selectedIndex]; |
271 [self reportResult:[item sourceID]]; | 458 [self reportResult:[item sourceID]]; |
272 [self close]; | 459 [self close]; |
273 } | 460 } |
274 | 461 |
275 - (void)cancelPressed:(id)sender { | 462 - (void)cancelPressed:(id)sender { |
276 [self reportResult:content::DesktopMediaID()]; | 463 [self reportResult:DesktopMediaID()]; |
277 [self close]; | 464 [self close]; |
278 } | 465 } |
279 | 466 |
280 - (NSTextField*)createTextFieldWithText:(NSString*)text | 467 - (void)typeButtonPressed:(id)sender { |
281 frameWidth:(CGFloat)width { | 468 DesktopMediaID::Type selectedType = [self selectedSourceType]; |
282 NSRect frame = NSMakeRect(0, 0, width, 1); | 469 id browser = [self browserViewForType:selectedType]; |
283 base::scoped_nsobject<NSTextField> textField( | 470 |
284 [[NSTextField alloc] initWithFrame:frame]); | 471 [audioShareCheckbox_ |
285 [textField setEditable:NO]; | 472 setHidden:selectedType != DesktopMediaID::TYPE_WEB_CONTENTS]; |
286 [textField setSelectable:YES]; | 473 [imageBrowserScroll_ setDocumentView:browser]; |
287 [textField setDrawsBackground:NO]; | 474 |
288 [textField setBezeled:NO]; | 475 if (selectedType == DesktopMediaID::TYPE_WEB_CONTENTS) { |
289 [textField setStringValue:text]; | 476 NSInteger selectedIndex = [self selectedIndexForType:selectedType]; |
290 [textField setFont:[NSFont systemFontOfSize:13]]; | 477 [tabBrowser_ reloadData]; |
291 [textField setAutoresizingMask:NSViewWidthSizable]; | 478 [self setTabBrowserIndex:selectedIndex]; |
292 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField]; | 479 } else { |
293 return textField.autorelease(); | 480 [browser reloadData]; |
481 [self imageBrowserSelectionDidChange:browser]; | |
482 } | |
294 } | 483 } |
295 | 484 |
296 - (NSButton*)createButtonWithTitle:(NSString*)title { | 485 #pragma mark Data Retrieve Helper |
297 base::scoped_nsobject<NSButton> button( | 486 |
298 [[NSButton alloc] initWithFrame:NSZeroRect]); | 487 - (DesktopMediaID::Type)selectedSourceType { |
299 [button setButtonType:NSMomentaryPushInButton]; | 488 NSInteger segment = [sourceTypeControl_ selectedSegment]; |
300 [button setBezelStyle:NSRoundedBezelStyle]; | 489 return static_cast<DesktopMediaID::Type>( |
301 [button setTitle:title]; | 490 [[sourceTypeControl_ cell] tagForSegment:segment]); |
302 [GTMUILocalizerAndLayoutTweaker sizeToFitView:button]; | 491 } |
303 return button.autorelease(); | 492 |
493 - (DesktopMediaID::Type)sourceTypeForList:(DesktopMediaList*)list { | |
494 if (list == screenList_.get()) | |
495 return DesktopMediaID::TYPE_SCREEN; | |
496 if (list == windowList_.get()) | |
497 return DesktopMediaID::TYPE_WINDOW; | |
498 return DesktopMediaID::TYPE_WEB_CONTENTS; | |
499 } | |
500 | |
501 - (DesktopMediaID::Type)sourceTypeForBrowser:(id)browser { | |
502 if (browser == screenBrowser_.get()) | |
503 return DesktopMediaID::TYPE_SCREEN; | |
504 if (browser == windowBrowser_.get()) | |
505 return DesktopMediaID::TYPE_WINDOW; | |
506 return DesktopMediaID::TYPE_WEB_CONTENTS; | |
507 } | |
508 | |
509 - (id)browserViewForType:(DesktopMediaID::Type)sourceType { | |
510 switch (sourceType) { | |
511 case DesktopMediaID::TYPE_SCREEN: | |
512 return screenBrowser_; | |
513 case DesktopMediaID::TYPE_WINDOW: | |
514 return windowBrowser_; | |
515 case DesktopMediaID::TYPE_WEB_CONTENTS: | |
516 return tabBrowser_; | |
517 case DesktopMediaID::TYPE_NONE: | |
518 NOTREACHED(); | |
519 return nil; | |
520 } | |
521 } | |
522 | |
523 - (NSMutableArray*)itemSetForType:(DesktopMediaID::Type)sourceType { | |
524 switch (sourceType) { | |
525 case DesktopMediaID::TYPE_SCREEN: | |
526 return screenItems_; | |
527 case DesktopMediaID::TYPE_WINDOW: | |
528 return windowItems_; | |
529 case DesktopMediaID::TYPE_WEB_CONTENTS: | |
530 return tabItems_; | |
531 case DesktopMediaID::TYPE_NONE: | |
532 NOTREACHED(); | |
533 return nil; | |
534 } | |
535 } | |
536 | |
537 - (NSInteger)selectedIndexForType:(DesktopMediaID::Type)sourceType { | |
538 NSIndexSet* indexes = nil; | |
539 switch (sourceType) { | |
540 case DesktopMediaID::TYPE_SCREEN: | |
541 indexes = [screenBrowser_ selectionIndexes]; | |
542 break; | |
543 case DesktopMediaID::TYPE_WINDOW: | |
544 indexes = [windowBrowser_ selectionIndexes]; | |
545 break; | |
546 case DesktopMediaID::TYPE_WEB_CONTENTS: | |
547 indexes = [tabBrowser_ selectedRowIndexes]; | |
548 break; | |
549 case DesktopMediaID::TYPE_NONE: | |
550 NOTREACHED(); | |
551 } | |
552 | |
553 if ([indexes count] == 0) | |
554 return -1; | |
555 return [indexes firstIndex]; | |
556 } | |
557 | |
558 - (void)setTabBrowserIndex:(NSInteger)index { | |
559 NSIndexSet* indexes; | |
560 | |
561 if (index < 0) | |
562 indexes = [NSIndexSet indexSet]; | |
563 else | |
564 indexes = [NSIndexSet indexSetWithIndex:index]; | |
565 | |
566 [tabBrowser_ selectRowIndexes:indexes byExtendingSelection:NO]; | |
304 } | 567 } |
305 | 568 |
306 #pragma mark NSWindowDelegate | 569 #pragma mark NSWindowDelegate |
307 | 570 |
308 - (void)windowWillClose:(NSNotification*)notification { | 571 - (void)windowWillClose:(NSNotification*)notification { |
309 // Report the result if it hasn't been reported yet. |reportResult:| ensures | 572 // Report the result if it hasn't been reported yet. |reportResult:| ensures |
310 // that the result is only reported once. | 573 // that the result is only reported once. |
311 [self reportResult:content::DesktopMediaID()]; | 574 [self reportResult:DesktopMediaID()]; |
312 | 575 |
313 // Remove self from the parent. | 576 // Remove self from the parent. |
314 NSWindow* window = [self window]; | 577 NSWindow* window = [self window]; |
315 [[window parentWindow] removeChildWindow:window]; | 578 [[window parentWindow] removeChildWindow:window]; |
316 } | 579 } |
317 | 580 |
318 #pragma mark IKImageBrowserDataSource | 581 #pragma mark IKImageBrowserDataSource |
319 | 582 |
320 - (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView*)browser { | 583 - (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView*)browser { |
321 return [items_ count]; | 584 DesktopMediaID::Type sourceType = [self sourceTypeForBrowser:browser]; |
585 NSMutableArray* items = [self itemSetForType:sourceType]; | |
586 return [items count]; | |
322 } | 587 } |
323 | 588 |
324 - (id)imageBrowser:(IKImageBrowserView *)browser | 589 - (id)imageBrowser:(IKImageBrowserView*)browser itemAtIndex:(NSUInteger)index { |
325 itemAtIndex:(NSUInteger)index { | 590 DesktopMediaID::Type sourceType = [self sourceTypeForBrowser:browser]; |
326 return [items_ objectAtIndex:index]; | 591 NSMutableArray* items = [self itemSetForType:sourceType]; |
592 return [items objectAtIndex:index]; | |
327 } | 593 } |
328 | 594 |
329 #pragma mark IKImageBrowserDelegate | 595 #pragma mark IKImageBrowserDelegate |
330 | 596 |
331 - (void)imageBrowser:(IKImageBrowserView *)browser | 597 - (void)imageBrowser:(IKImageBrowserView*)browser |
332 cellWasDoubleClickedAtIndex:(NSUInteger)index { | 598 cellWasDoubleClickedAtIndex:(NSUInteger)index { |
333 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | 599 DesktopMediaPickerItem* item; |
600 if (browser == screenBrowser_) | |
601 item = [screenItems_ objectAtIndex:index]; | |
602 else | |
603 item = [windowItems_ objectAtIndex:index]; | |
334 [self reportResult:[item sourceID]]; | 604 [self reportResult:[item sourceID]]; |
335 [self close]; | 605 [self close]; |
336 } | 606 } |
337 | 607 |
338 - (void)imageBrowserSelectionDidChange:(IKImageBrowserView*)browser { | 608 - (void)imageBrowserSelectionDidChange:(IKImageBrowserView*)browser { |
339 NSIndexSet* indexes = [sourceBrowser_ selectionIndexes]; | 609 DesktopMediaID::Type selectedType = [self selectedSourceType]; |
610 NSInteger selectedIndex = [self selectedIndexForType:selectedType]; | |
611 // Enable or disable the OK button based on whether we have a selection. | |
612 [shareButton_ setEnabled:(selectedIndex >= 0)]; | |
613 } | |
614 | |
615 #pragma mark NSTableViewDataSource | |
616 | |
617 - (NSInteger)numberOfRowsInTableView:(NSTableView*)table { | |
618 return [tabItems_ count]; | |
619 } | |
620 | |
621 #pragma mark NSTableViewDelegate | |
622 | |
623 - (NSView*)tableView:(NSTableView*)table | |
624 viewForTableColumn:(NSTableColumn*)column | |
625 row:(NSInteger)rowIndex { | |
626 if ([[column identifier] isEqualToString:kIconId]) { | |
627 NSImage* image = [[tabItems_ objectAtIndex:rowIndex] imageRepresentation]; | |
628 NSImageView* iconView = [ | |
629 [[table makeViewWithIdentifier:kIconId owner:self] retain] autorelease]; | |
630 if (!iconView) { | |
631 iconView = [[[NSImageView alloc] | |
tapted
2016/06/23 02:49:48
things should always be `alloced` into a scoped_ns
qiangchen
2016/06/23 17:49:37
Acknowledged.
| |
632 initWithFrame:NSMakeRect(0, 0, kIconWidth, kRowWidth)] autorelease]; | |
633 [iconView setIdentifier:kIconId]; | |
634 } | |
635 [iconView setImage:image]; | |
636 return iconView; | |
637 } | |
638 | |
639 NSString* string = [[tabItems_ objectAtIndex:rowIndex] imageTitle]; | |
640 NSTextField* titleView = | |
tapted
2016/06/23 02:49:48
so removing scoped_nsobject here is OK, since noth
qiangchen
2016/06/23 17:49:37
Done.
| |
641 [[[table makeViewWithIdentifier:kTitleId owner:self] retain] autorelease]; | |
tapted
2016/06/23 02:49:48
but here the retain] autorelease] does nothing. Th
qiangchen
2016/06/23 17:49:38
Done.
| |
642 if (!titleView) { | |
643 titleView = | |
644 [self createTextFieldWithText:string frameWidth:kMinimumContentWidth]; | |
645 [titleView setIdentifier:kTitleId]; | |
646 } else { | |
647 [titleView setStringValue:string]; | |
648 } | |
649 return titleView; | |
650 } | |
651 | |
652 - (void)tableViewSelectionDidChange:(NSNotification*)notification { | |
653 NSIndexSet* indexes = [tabBrowser_ selectedRowIndexes]; | |
340 | 654 |
341 // Enable or disable the OK button based on whether we have a selection. | 655 // Enable or disable the OK button based on whether we have a selection. |
342 [shareButton_ setEnabled:([indexes count] > 0)]; | 656 [shareButton_ setEnabled:([indexes count] > 0)]; |
343 | |
344 // Enable or disable the checkbox based on whether we can support audio for | |
345 // the selected source. | |
346 // On Mac, the checkbox will enabled for tab sharing, namely | |
347 // TYPE_WEB_CONTENTS. | |
348 if ([indexes count] == 0) { | |
349 if ([audioShareCheckbox_ isEnabled]) { | |
350 [audioShareCheckbox_ setEnabled:NO]; | |
351 audioShareState_ = [audioShareCheckbox_ state]; | |
352 [audioShareCheckbox_ setState:NSOffState]; | |
353 } | |
354 [audioShareCheckbox_ | |
355 setToolTip:l10n_util::GetNSString( | |
356 IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE_TOOLTIP_MAC)]; | |
357 return; | |
358 } | |
359 | |
360 NSUInteger selectedIndex = [indexes firstIndex]; | |
361 DesktopMediaPickerItem* item = [items_ objectAtIndex:selectedIndex]; | |
362 switch ([item sourceID].type) { | |
363 case content::DesktopMediaID::TYPE_SCREEN: | |
364 case content::DesktopMediaID::TYPE_WINDOW: | |
365 if ([audioShareCheckbox_ isEnabled]) { | |
366 [audioShareCheckbox_ setEnabled:NO]; | |
367 audioShareState_ = [audioShareCheckbox_ state]; | |
368 [audioShareCheckbox_ setState:NSOffState]; | |
369 } | |
370 [audioShareCheckbox_ | |
371 setToolTip:l10n_util::GetNSString( | |
372 IDS_DESKTOP_MEDIA_PICKER_AUDIO_SHARE_TOOLTIP_MAC)]; | |
373 break; | |
374 case content::DesktopMediaID::TYPE_WEB_CONTENTS: | |
375 if (![audioShareCheckbox_ isEnabled]) { | |
376 [audioShareCheckbox_ setEnabled:YES]; | |
377 [audioShareCheckbox_ setState:audioShareState_]; | |
378 } | |
379 [audioShareCheckbox_ setToolTip:@""]; | |
380 break; | |
381 case content::DesktopMediaID::TYPE_NONE: | |
382 NOTREACHED(); | |
383 } | |
384 } | 657 } |
385 | 658 |
386 #pragma mark DesktopMediaPickerObserver | 659 #pragma mark DesktopMediaPickerObserver |
387 | 660 |
388 - (void)sourceAddedAtIndex:(int)index { | 661 - (void)sourceAddedForList:(DesktopMediaList*)list atIndex:(int)index { |
389 const DesktopMediaList::Source& source = media_list_->GetSource(index); | 662 DesktopMediaID::Type sourceType = [self sourceTypeForList:list]; |
663 NSMutableArray* items = [self itemSetForType:sourceType]; | |
664 id browser = [self browserViewForType:sourceType]; | |
665 NSInteger selectedIndex = [self selectedIndexForType:sourceType]; | |
666 if (selectedIndex >= index) | |
667 ++selectedIndex; | |
668 | |
669 const DesktopMediaList::Source& source = list->GetSource(index); | |
390 NSString* imageTitle = base::SysUTF16ToNSString(source.name); | 670 NSString* imageTitle = base::SysUTF16ToNSString(source.name); |
391 base::scoped_nsobject<DesktopMediaPickerItem> item( | 671 base::scoped_nsobject<DesktopMediaPickerItem> item( |
392 [[DesktopMediaPickerItem alloc] initWithSourceId:source.id | 672 [[DesktopMediaPickerItem alloc] initWithSourceId:source.id |
393 imageUID:++lastImageUID_ | 673 imageUID:++lastImageUID_ |
394 imageTitle:imageTitle]); | 674 imageTitle:imageTitle]); |
395 [items_ insertObject:item atIndex:index]; | 675 [items insertObject:item atIndex:index]; |
396 [sourceBrowser_ reloadData]; | 676 [browser reloadData]; |
677 if (sourceType == DesktopMediaID::TYPE_WEB_CONTENTS) | |
678 [self setTabBrowserIndex:selectedIndex]; | |
397 | 679 |
398 NSString* autoselectSource = base::SysUTF8ToNSString( | 680 NSString* autoselectSource = base::SysUTF8ToNSString( |
399 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 681 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
400 switches::kAutoSelectDesktopCaptureSource)); | 682 switches::kAutoSelectDesktopCaptureSource)); |
401 | 683 |
402 if ([autoselectSource isEqualToString:imageTitle]) { | 684 if ([autoselectSource isEqualToString:imageTitle]) { |
403 [self reportResult:[item sourceID]]; | 685 [self reportResult:[item sourceID]]; |
404 [self close]; | 686 [self close]; |
405 } | 687 } |
406 } | 688 } |
407 | 689 |
408 - (void)sourceRemovedAtIndex:(int)index { | 690 - (void)sourceRemovedForList:(DesktopMediaList*)list atIndex:(int)index { |
409 if ([[sourceBrowser_ selectionIndexes] containsIndex:index]) { | 691 DesktopMediaID::Type sourceType = [self sourceTypeForList:list]; |
692 NSMutableArray* items = [self itemSetForType:sourceType]; | |
693 id browser = [self browserViewForType:sourceType]; | |
694 | |
695 if (sourceType == DesktopMediaID::TYPE_WEB_CONTENTS) { | |
696 NSInteger selectedIndex = [self selectedIndexForType:sourceType]; | |
697 if (selectedIndex > index) | |
698 --selectedIndex; | |
699 else if (selectedIndex == index) | |
700 selectedIndex = -1; | |
701 [tabItems_ removeObjectAtIndex:index]; | |
702 [tabBrowser_ reloadData]; | |
703 [self setTabBrowserIndex:selectedIndex]; | |
704 return; | |
705 } | |
706 | |
707 if ([[browser selectionIndexes] containsIndex:index]) { | |
410 // Selected item was removed. Clear selection. | 708 // Selected item was removed. Clear selection. |
411 [sourceBrowser_ setSelectionIndexes:[NSIndexSet indexSet] | 709 [browser setSelectionIndexes:[NSIndexSet indexSet] byExtendingSelection:NO]; |
412 byExtendingSelection:FALSE]; | |
413 } | 710 } |
414 [items_ removeObjectAtIndex:index]; | 711 [items removeObjectAtIndex:index]; |
415 [sourceBrowser_ reloadData]; | 712 [browser reloadData]; |
416 } | 713 } |
417 | 714 |
418 - (void)sourceMovedFrom:(int)oldIndex to:(int)newIndex { | 715 - (void)sourceMovedForList:(DesktopMediaList*)list |
716 from:(int)oldIndex | |
717 to:(int)newIndex { | |
718 DesktopMediaID::Type sourceType = [self sourceTypeForList:list]; | |
719 NSMutableArray* items = [self itemSetForType:sourceType]; | |
720 id browser = [self browserViewForType:sourceType]; | |
721 NSInteger selectedIndex = [self selectedIndexForType:sourceType]; | |
722 if (selectedIndex > oldIndex && selectedIndex <= newIndex) | |
723 --selectedIndex; | |
724 else if (selectedIndex < oldIndex && selectedIndex >= newIndex) | |
725 ++selectedIndex; | |
726 else if (selectedIndex == oldIndex) | |
727 selectedIndex = newIndex; | |
728 | |
419 base::scoped_nsobject<DesktopMediaPickerItem> item( | 729 base::scoped_nsobject<DesktopMediaPickerItem> item( |
420 [[items_ objectAtIndex:oldIndex] retain]); | 730 [[items objectAtIndex:oldIndex] retain]); |
421 [items_ removeObjectAtIndex:oldIndex]; | 731 [items removeObjectAtIndex:oldIndex]; |
422 [items_ insertObject:item atIndex:newIndex]; | 732 [items insertObject:item atIndex:newIndex]; |
423 [sourceBrowser_ reloadData]; | 733 [browser reloadData]; |
734 | |
735 if (sourceType == DesktopMediaID::TYPE_WEB_CONTENTS) | |
736 [self setTabBrowserIndex:selectedIndex]; | |
424 } | 737 } |
425 | 738 |
426 - (void)sourceNameChangedAtIndex:(int)index { | 739 - (void)sourceNameChangedForList:(DesktopMediaList*)list atIndex:(int)index { |
427 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | 740 DesktopMediaID::Type sourceType = [self sourceTypeForList:list]; |
428 const DesktopMediaList::Source& source = media_list_->GetSource(index); | 741 NSMutableArray* items = [self itemSetForType:sourceType]; |
742 id browser = [self browserViewForType:sourceType]; | |
743 NSInteger selectedIndex = [self selectedIndexForType:sourceType]; | |
744 | |
745 DesktopMediaPickerItem* item = [items objectAtIndex:index]; | |
746 const DesktopMediaList::Source& source = list->GetSource(index); | |
429 [item setImageTitle:base::SysUTF16ToNSString(source.name)]; | 747 [item setImageTitle:base::SysUTF16ToNSString(source.name)]; |
430 [sourceBrowser_ reloadData]; | 748 [browser reloadData]; |
749 if (sourceType == DesktopMediaID::TYPE_WEB_CONTENTS) | |
750 [self setTabBrowserIndex:selectedIndex]; | |
431 } | 751 } |
432 | 752 |
433 - (void)sourceThumbnailChangedAtIndex:(int)index { | 753 - (void)sourceThumbnailChangedForList:(DesktopMediaList*)list |
434 const DesktopMediaList::Source& source = media_list_->GetSource(index); | 754 atIndex:(int)index { |
755 DesktopMediaID::Type sourceType = [self sourceTypeForList:list]; | |
756 NSMutableArray* items = [self itemSetForType:sourceType]; | |
757 id browser = [self browserViewForType:sourceType]; | |
758 NSInteger selectedIndex = [self selectedIndexForType:sourceType]; | |
759 | |
760 const DesktopMediaList::Source& source = list->GetSource(index); | |
435 NSImage* image = gfx::NSImageFromImageSkia(source.thumbnail); | 761 NSImage* image = gfx::NSImageFromImageSkia(source.thumbnail); |
436 | 762 |
437 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | 763 DesktopMediaPickerItem* item = [items objectAtIndex:index]; |
438 [item setImageRepresentation:image]; | 764 [item setImageRepresentation:image]; |
439 [sourceBrowser_ reloadData]; | 765 [browser reloadData]; |
766 | |
767 if (sourceType == DesktopMediaID::TYPE_WEB_CONTENTS) | |
768 [self setTabBrowserIndex:selectedIndex]; | |
440 } | 769 } |
441 | 770 |
442 @end // @interface DesktopMediaPickerController | 771 @end // @interface DesktopMediaPickerController |
OLD | NEW |