Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / media_picker / desktop_media_picker_controller.mm
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
3 // found in the LICENSE file.
4
5 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h"
6
7 #include "base/bind.h"
8 #import "base/mac/bundle_locations.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_item.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "grit/generated_resources.h"
13 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
14 #import "ui/base/cocoa/flipped_view.h"
15 #import "ui/base/cocoa/window_size_constants.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/gfx/image/image_skia_util_mac.h"
18
19 namespace {
20
21 const int kInitialContentWidth = 620;
22 const int kMinimumContentWidth = 500;
23 const int kMinimumContentHeight = 390;
24 const int kThumbnailWidth = 150;
25 const int kThumbnailHeight = 150;
26 const int kFramePadding = 20;
27 const int kControlSpacing = 10;
28 const int kExcessButtonPadding = 6;
29
30 }  // namespace
31
32 @interface DesktopMediaPickerController (Private)
33
34 // Populate the window with controls and views.
35 - (void)initializeContentsWithAppName:(const base::string16&)appName;
36
37 // Create a |NSTextField| with label traits given |width|. Frame height is
38 // automatically adjusted to fit.
39 - (NSTextField*)createTextFieldWithText:(NSString*)text
40                              frameWidth:(CGFloat)width;
41
42 // Create a button with |title|, with size adjusted to fit.
43 - (NSButton*)createButtonWithTitle:(NSString*)title;
44
45 // Report result by invoking |doneCallback_|. The callback is invoked only on
46 // the first call to |reportResult:|. Subsequent calls will be no-ops.
47 - (void)reportResult:(content::DesktopMediaID)sourceID;
48
49 // Action handlers.
50 - (void)sharePressed:(id)sender;
51 - (void)cancelPressed:(id)sender;
52
53 @end
54
55 @implementation DesktopMediaPickerController
56
57 - (id)initWithMediaList:(scoped_ptr<DesktopMediaList>)media_list
58                  parent:(NSWindow*)parent
59                callback:(const DesktopMediaPicker::DoneCallback&)callback
60                 appName:(const base::string16&)appName
61              targetName:(const base::string16&)targetName {
62   const NSUInteger kStyleMask =
63       NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask;
64   base::scoped_nsobject<NSWindow> window(
65       [[NSWindow alloc] initWithContentRect:ui::kWindowSizeDeterminedLater
66                                   styleMask:kStyleMask
67                                     backing:NSBackingStoreBuffered
68                                       defer:NO]);
69
70   if ((self = [super initWithWindow:window])) {
71     [parent addChildWindow:window ordered:NSWindowAbove];
72     [window setDelegate:self];
73     [self initializeContentsWithAppName:appName targetName:targetName];
74     media_list_ = media_list.Pass();
75     media_list_->SetViewDialogWindowId([window windowNumber]);
76     doneCallback_ = callback;
77     items_.reset([[NSMutableArray alloc] init]);
78     bridge_.reset(new DesktopMediaPickerBridge(self));
79   }
80   return self;
81 }
82
83 - (void)dealloc {
84   [shareButton_ setTarget:nil];
85   [cancelButton_ setTarget:nil];
86   [sourceBrowser_ setDelegate:nil];
87   [sourceBrowser_ setDataSource:nil];
88   [super dealloc];
89 }
90
91 - (void)initializeContentsWithAppName:(const base::string16&)appName
92                            targetName:(const base::string16&)targetName {
93   // Use flipped coordinates to facilitate manual layout.
94   const CGFloat kPaddedWidth = kInitialContentWidth - (kFramePadding * 2);
95   base::scoped_nsobject<FlippedView> content(
96       [[FlippedView alloc] initWithFrame:NSZeroRect]);
97   [[self window] setContentView:content];
98   NSPoint origin = NSMakePoint(kFramePadding, kFramePadding);
99
100   // Set the dialog's title.
101   NSString* titleText = l10n_util::GetNSStringF(
102       IDS_DESKTOP_MEDIA_PICKER_TITLE, appName);
103   [[self window] setTitle:titleText];
104
105   // Set the dialog's description.
106   NSString* descriptionText;
107   if (appName == targetName) {
108     descriptionText = l10n_util::GetNSStringF(
109         IDS_DESKTOP_MEDIA_PICKER_TEXT, appName);
110   } else {
111     descriptionText = l10n_util::GetNSStringF(
112         IDS_DESKTOP_MEDIA_PICKER_TEXT_DELEGATED, appName, targetName);
113   }
114   NSTextField* description = [self createTextFieldWithText:descriptionText
115                                                 frameWidth:kPaddedWidth];
116   [description setFrameOrigin:origin];
117   [content addSubview:description];
118   origin.y += NSHeight([description frame]) + kControlSpacing;
119
120   // Create the image browser.
121   sourceBrowser_.reset([[IKImageBrowserView alloc] initWithFrame:NSZeroRect]);
122   NSUInteger cellStyle = IKCellsStyleShadowed | IKCellsStyleTitled;
123   [sourceBrowser_ setDelegate:self];
124   [sourceBrowser_ setDataSource:self];
125   [sourceBrowser_ setCellsStyleMask:cellStyle];
126   [sourceBrowser_ setCellSize:NSMakeSize(kThumbnailWidth, kThumbnailHeight)];
127
128   // Create a scroll view to host the image browser.
129   NSRect imageBrowserScrollFrame = NSMakeRect(
130       origin.x, origin.y, kPaddedWidth, 350);
131   base::scoped_nsobject<NSScrollView> imageBrowserScroll(
132       [[NSScrollView alloc] initWithFrame:imageBrowserScrollFrame]);
133   [imageBrowserScroll setHasVerticalScroller:YES];
134   [imageBrowserScroll setDocumentView:sourceBrowser_];
135   [imageBrowserScroll setBorderType:NSBezelBorder];
136   [imageBrowserScroll setAutoresizingMask:
137       NSViewWidthSizable | NSViewHeightSizable];
138   [content addSubview:imageBrowserScroll];
139   origin.y += NSHeight(imageBrowserScrollFrame) + kControlSpacing;
140
141   // Create the share button.
142   shareButton_ = [self createButtonWithTitle:l10n_util::GetNSString(
143       IDS_DESKTOP_MEDIA_PICKER_SHARE)];
144   origin.x = kInitialContentWidth - kFramePadding -
145       (NSWidth([shareButton_ frame]) - kExcessButtonPadding);
146   [shareButton_ setEnabled:NO];
147   [shareButton_ setFrameOrigin:origin];
148   [shareButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
149   [shareButton_ setTarget:self];
150   [shareButton_ setAction:@selector(sharePressed:)];
151   [content addSubview:shareButton_];
152
153   // Create the cancel button.
154   cancelButton_ =
155       [self createButtonWithTitle:l10n_util::GetNSString(IDS_CANCEL)];
156   origin.x -= kControlSpacing +
157       (NSWidth([cancelButton_ frame]) - (kExcessButtonPadding * 2));
158   [cancelButton_ setFrameOrigin:origin];
159   [cancelButton_ setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
160   [cancelButton_ setTarget:self];
161   [cancelButton_ setAction:@selector(cancelPressed:)];
162   [content addSubview:cancelButton_];
163   origin.y += kFramePadding +
164       (NSHeight([cancelButton_ frame]) - kExcessButtonPadding);
165
166   // Resize window to fit.
167   [[[self window] contentView] setAutoresizesSubviews:NO];
168   [[self window] setContentSize:NSMakeSize(kInitialContentWidth, origin.y)];
169   [[self window] setContentMinSize:
170       NSMakeSize(kMinimumContentWidth, kMinimumContentHeight)];
171   [[[self window] contentView] setAutoresizesSubviews:YES];
172 }
173
174 - (void)showWindow:(id)sender {
175   // Signal the media_list to start sending thumbnails. |bridge_| is used as the
176   // observer, and will forward notifications to this object.
177   media_list_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight));
178   media_list_->StartUpdating(bridge_.get());
179
180   [self.window center];
181   [super showWindow:sender];
182 }
183
184 - (void)reportResult:(content::DesktopMediaID)sourceID {
185   if (doneCallback_.is_null()) {
186     return;
187   }
188
189   // Notify the |callback_| asynchronously because it may release the
190   // controller.
191   content::BrowserThread::PostTask(
192       content::BrowserThread::UI, FROM_HERE,
193       base::Bind(doneCallback_, sourceID));
194   doneCallback_.Reset();
195 }
196
197 - (void)sharePressed:(id)sender {
198   NSIndexSet* indexes = [sourceBrowser_ selectionIndexes];
199   NSUInteger selectedIndex = [indexes firstIndex];
200   DesktopMediaPickerItem* item =
201       [items_ objectAtIndex:selectedIndex];
202   [self reportResult:[item sourceID]];
203   [self close];
204 }
205
206 - (void)cancelPressed:(id)sender {
207   [self reportResult:content::DesktopMediaID()];
208   [self close];
209 }
210
211 - (NSTextField*)createTextFieldWithText:(NSString*)text
212                              frameWidth:(CGFloat)width {
213   NSRect frame = NSMakeRect(0, 0, width, 1);
214   base::scoped_nsobject<NSTextField> textField(
215       [[NSTextField alloc] initWithFrame:frame]);
216   [textField setEditable:NO];
217   [textField setSelectable:YES];
218   [textField setDrawsBackground:NO];
219   [textField setBezeled:NO];
220   [textField setStringValue:text];
221   [textField setFont:[NSFont systemFontOfSize:13]];
222   [textField setAutoresizingMask:NSViewWidthSizable];
223   [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:textField];
224   return textField.autorelease();
225 }
226
227 - (NSButton*)createButtonWithTitle:(NSString*)title {
228   base::scoped_nsobject<NSButton> button(
229       [[NSButton alloc] initWithFrame:NSZeroRect]);
230   [button setButtonType:NSMomentaryPushInButton];
231   [button setBezelStyle:NSRoundedBezelStyle];
232   [button setTitle:title];
233   [GTMUILocalizerAndLayoutTweaker sizeToFitView:button];
234   return button.autorelease();
235 }
236
237 #pragma mark NSWindowDelegate
238
239 - (void)windowWillClose:(NSNotification*)notification {
240   // Report the result if it hasn't been reported yet. |reportResult:| ensures
241   // that the result is only reported once.
242   [self reportResult:content::DesktopMediaID()];
243
244   // Remove self from the parent.
245   NSWindow* window = [self window];
246   [[window parentWindow] removeChildWindow:window];
247 }
248
249 #pragma mark IKImageBrowserDataSource
250
251 - (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView*)browser {
252   return [items_ count];
253 }
254
255 - (id)imageBrowser:(IKImageBrowserView *)browser
256        itemAtIndex:(NSUInteger)index {
257   return [items_ objectAtIndex:index];
258 }
259
260 #pragma mark IKImageBrowserDelegate
261
262 - (void)imageBrowser:(IKImageBrowserView *)browser
263       cellWasDoubleClickedAtIndex:(NSUInteger)index {
264   DesktopMediaPickerItem* item = [items_ objectAtIndex:index];
265   [self reportResult:[item sourceID]];
266   [self close];
267 }
268
269 - (void)imageBrowserSelectionDidChange:(IKImageBrowserView*) aBrowser {
270   // Enable or disable the OK button based on whether we have a selection.
271   [shareButton_ setEnabled:([[sourceBrowser_ selectionIndexes] count] > 0)];
272 }
273
274 #pragma mark DesktopMediaPickerObserver
275
276 - (void)sourceAddedAtIndex:(int)index {
277   const DesktopMediaList::Source& source = media_list_->GetSource(index);
278   NSString* imageTitle = base::SysUTF16ToNSString(source.name);
279   base::scoped_nsobject<DesktopMediaPickerItem> item(
280       [[DesktopMediaPickerItem alloc] initWithSourceId:source.id
281                                               imageUID:++lastImageUID_
282                                             imageTitle:imageTitle]);
283   [items_ insertObject:item atIndex:index];
284   [sourceBrowser_ reloadData];
285 }
286
287 - (void)sourceRemovedAtIndex:(int)index {
288   if ([[sourceBrowser_ selectionIndexes] containsIndex:index]) {
289     // Selected item was removed. Clear selection.
290     [sourceBrowser_ setSelectionIndexes:[NSIndexSet indexSet]
291                       byExtendingSelection:FALSE];
292   }
293   [items_ removeObjectAtIndex:index];
294   [sourceBrowser_ reloadData];
295 }
296
297 - (void)sourceMovedFrom:(int)oldIndex to:(int)newIndex {
298   base::scoped_nsobject<DesktopMediaPickerItem> item(
299       [[items_ objectAtIndex:oldIndex] retain]);
300   [items_ removeObjectAtIndex:oldIndex];
301   [items_ insertObject:item atIndex:newIndex];
302   [sourceBrowser_ reloadData];
303 }
304
305 - (void)sourceNameChangedAtIndex:(int)index {
306   DesktopMediaPickerItem* item = [items_ objectAtIndex:index];
307   const DesktopMediaList::Source& source = media_list_->GetSource(index);
308   [item setImageTitle:base::SysUTF16ToNSString(source.name)];
309   [sourceBrowser_ reloadData];
310 }
311
312 - (void)sourceThumbnailChangedAtIndex:(int)index {
313   const DesktopMediaList::Source& source = media_list_->GetSource(index);
314   NSImage* image = gfx::NSImageFromImageSkia(source.thumbnail);
315
316   DesktopMediaPickerItem* item = [items_ objectAtIndex:index];
317   [item setImageRepresentation:image];
318   [sourceBrowser_ reloadData];
319 }
320
321 @end  // @interface DesktopMediaPickerController