Index: chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.mm |
diff --git a/chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.mm b/chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f94896a068bb2281665caaef4f200f1fbbd43aa1 |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.mm |
@@ -0,0 +1,165 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h" |
+ |
+#include "base/bind.h" |
+#import "base/mac/bundle_locations.h" |
+#include "base/strings/sys_string_conversions.h" |
+#include "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_item.h" |
Robert Sesek
2013/09/05 15:33:19
#import
dcaiafa
2013/09/06 19:15:44
Done.
|
+#include "content/public/browser/browser_thread.h" |
+#include "grit/generated_resources.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
+#include "ui/gfx/image/image_skia_util_mac.h" |
+ |
+namespace { |
+ |
+const int kThumbnailWidth = 150; |
+const int kThumbnailHeight = 150; |
+ |
+} // namespace |
+ |
+@implementation DesktopMediaPickerController |
+ |
+- (id)initWithModel:(scoped_ptr<DesktopMediaPickerModel>)model |
+ callback:(const DesktopMediaPicker::DoneCallback&)callback |
+ appName:(const string16&)appName { |
+ NSString* nibpath = |
+ [base::mac::FrameworkBundle() pathForResource:@"DesktopMediaPicker" |
+ ofType:@"nib"]; |
+ if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
+ model_ = model.Pass(); |
+ doneCallback_ = callback; |
+ appName_ = appName; |
+ items_.reset([[NSMutableArray alloc] init]); |
+ bridge_.reset(new DesktopMediaPickerBridge(self)); |
+ } |
+ return self; |
+} |
+ |
+- (void)awakeFromNib { |
+ // Format the dialog's title. |
+ NSString* title = l10n_util::GetNSStringF(IDS_DESKTOP_MEDIA_PICKER_TITLE, |
+ appName_); |
+ [[self window] setTitle:title]; |
+ |
+ // Format the dialog's label. |
+ NSString* label = l10n_util::GetNSStringF(IDS_DESKTOP_MEDIA_PICKER_TEXT, |
+ appName_); |
+ [label_ setStringValue:label]; |
+ |
+ // Configure the image browser. |
+ NSUInteger cellStyle = IKCellsStyleShadowed | IKCellsStyleTitled; |
+ [sourceBrowser_ setCellsStyleMask:cellStyle]; |
+ [sourceBrowser_ setCellSize:NSMakeSize(kThumbnailWidth, kThumbnailHeight)]; |
+} |
+ |
+- (IBAction)showWindow:(id)sender { |
+ // Signal the model to start sending thumbnails. |bridge_| is used as the |
+ // observer, and will forward notifications to this object. |
+ model_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); |
+ model_->StartUpdating(bridge_.get()); |
+ |
+ [self.window center]; |
+ [super showWindow:sender]; |
+} |
+ |
+- (void)reportResult:(content::DesktopMediaID)sourceID { |
+ if (doneCallback_.is_null()) { |
+ return; |
+ } |
+ |
+ // Notify the |callback_| asynchronously because it may release the |
+ // controller. |
+ content::BrowserThread::PostTask( |
+ content::BrowserThread::UI, FROM_HERE, |
+ base::Bind(doneCallback_, sourceID)); |
+ doneCallback_.Reset(); |
+} |
+ |
+- (IBAction)okPressed:(id)sender { |
+ NSIndexSet* indexes = [sourceBrowser_ selectionIndexes]; |
+ NSUInteger selectedIndex = [indexes firstIndex]; |
+ DesktopMediaPickerItem* item = |
+ [items_ objectAtIndex:selectedIndex]; |
+ [self reportResult:[item sourceID]]; |
+ [self close]; |
+} |
+ |
+- (IBAction)cancelPressed:(id)sender { |
+ [self reportResult:content::DesktopMediaID()]; |
+ [self close]; |
+} |
+ |
+- (void)windowWillClose:(NSNotification*)notification { |
+ // Report the result if it hasn't been reported yet. |reportResult:| ensures |
+ // that the result is only reported once. |
+ [self reportResult:content::DesktopMediaID()]; |
+} |
+ |
+#pragma mark IKImageBrowserDataSource |
+ |
+- (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView*)browser { |
+ return [items_ count]; |
+} |
+ |
+- (id)imageBrowser:(IKImageBrowserView *)browser |
+ itemAtIndex:(NSUInteger)index { |
+ return [items_ objectAtIndex:index]; |
+} |
+ |
+#pragma mark IKImageBrowserDelegate |
+ |
+- (void)imageBrowser:(IKImageBrowserView *)browser |
+ cellWasDoubleClickedAtIndex:(NSUInteger)index { |
+ DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; |
+ [self reportResult:[item sourceID]]; |
+ [self close]; |
+} |
+ |
+- (void)imageBrowserSelectionDidChange:(IKImageBrowserView*) aBrowser { |
+ // Enable or disable the OK button based on whether we have a selection. |
+ [okButton_ setEnabled:([[sourceBrowser_ selectionIndexes] count] > 0)]; |
+} |
+ |
+#pragma mark DesktopMediaPickerObserver |
+ |
+- (void)sourceAddedAtIndex:(int)index { |
+ const DesktopMediaPickerModel::Source& source = model_->source(index); |
+ NSString* imageTitle = base::SysUTF16ToNSString(source.name); |
+ base::scoped_nsobject<DesktopMediaPickerItem> item( |
+ [[DesktopMediaPickerItem alloc] initWithSourceId:source.id |
+ imageUID:++lastImageUID_ |
+ imageTitle:imageTitle]); |
+ [items_ insertObject:item atIndex:index]; |
+ [sourceBrowser_ reloadData]; |
+} |
+ |
+- (void)sourceRemovedAtIndex:(int)index { |
+ if ([[sourceBrowser_ selectionIndexes] containsIndex:index]) { |
+ // Selected item was removed. Clear selection. |
+ [sourceBrowser_ setSelectionIndexes:[NSIndexSet indexSet] |
+ byExtendingSelection:FALSE]; |
+ } |
+ [items_ removeObjectAtIndex:index]; |
+ [sourceBrowser_ reloadData]; |
+} |
+ |
+- (void)sourceNameChangedAtIndex:(int)index { |
+ DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; |
+ const DesktopMediaPickerModel::Source& source = model_->source(index); |
+ [item setImageTitle:base::SysUTF16ToNSString(source.name)]; |
+ [sourceBrowser_ reloadData]; |
+} |
+ |
+- (void)sourceThumbnailChangedAtIndex:(int)index { |
+ const DesktopMediaPickerModel::Source& source = model_->source(index); |
+ NSImage* image = gfx::NSImageFromImageSkia(source.thumbnail); |
+ |
+ DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; |
+ [item setImageRepresentation:image]; |
+ [sourceBrowser_ reloadData]; |
+} |
+ |
+@end // @interface DesktopMediaPickerController |