Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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 #include "base/mac/bundle_locations.h" | |
| 9 #include "base/mac/scoped_cftyperef.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 11 #include "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_item.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "grit/generated_resources.h" | |
| 14 #include "skia/ext/skia_utils_mac.h" | |
| 15 #include "ui/base/l10n/l10n_util_mac.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 const int kThumbnailWidth = 150; | |
| 20 const int kThumbnailHeight = 150; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 @implementation DesktopMediaPickerController | |
| 25 | |
| 26 - (id)initWithModel:(scoped_ptr<DesktopMediaPickerModel>)model | |
| 27 callback:(const DesktopMediaPicker::DoneCallback&)callback | |
| 28 appName:(const string16&)appName { | |
| 29 NSString* nibpath = | |
| 30 [base::mac::FrameworkBundle() pathForResource:@"DesktopMediaPicker" | |
| 31 ofType:@"nib"]; | |
| 32 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { | |
| 33 model_ = model.Pass(); | |
| 34 done_callback_ = callback; | |
| 35 appName_ = appName; | |
| 36 items_.reset([[NSMutableArray alloc] init]); | |
| 37 bridge_.reset(new DesktopMediaPickerBridge(self)); | |
| 38 } | |
| 39 return self; | |
| 40 } | |
| 41 | |
| 42 - (void)awakeFromNib { | |
| 43 // Format the dialog's title. | |
| 44 NSString* title = l10n_util::GetNSStringF(IDS_DESKTOP_MEDIA_PICKER_TITLE, | |
| 45 appName_); | |
| 46 [[self window] setTitle:title]; | |
| 47 | |
| 48 // Format the dialog's label. | |
| 49 NSString* label = l10n_util::GetNSStringF(IDS_DESKTOP_MEDIA_PICKER_TEXT, | |
| 50 appName_); | |
| 51 [label_ setStringValue:label]; | |
| 52 | |
| 53 // Configure the image browser. | |
| 54 NSUInteger cellStyle = IKCellsStyleShadowed | IKCellsStyleTitled; | |
| 55 [sourceBrowser_ setCellsStyleMask:cellStyle]; | |
| 56 [sourceBrowser_ setCellSize:NSMakeSize(kThumbnailWidth, kThumbnailHeight)]; | |
| 57 } | |
| 58 | |
| 59 - (IBAction)showWindow:(id)sender { | |
| 60 // Signal the model to start sending thumbnails. |bridge_| is used as the | |
| 61 // observer, and will forward notifications to this object. | |
| 62 model_->SetThumbnailSize(gfx::Size(kThumbnailWidth, kThumbnailHeight)); | |
| 63 model_->StartUpdating(bridge_.get()); | |
| 64 | |
| 65 [self.window center]; | |
| 66 [super showWindow:sender]; | |
| 67 } | |
| 68 | |
| 69 - (void)reportResult:(content::DesktopMediaID)sourceId { | |
| 70 if (!done_callback_.is_null()) { | |
|
Robert Sesek
2013/09/04 20:23:19
I'd flip this to an early return.
dcaiafa
2013/09/04 21:39:35
Done.
| |
| 71 // Notify the |callback_| asynchronously because it may release the | |
| 72 // controller. | |
| 73 content::BrowserThread::PostTask( | |
| 74 content::BrowserThread::UI, FROM_HERE, | |
| 75 base::Bind(done_callback_, sourceId)); | |
| 76 done_callback_.Reset(); | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 - (IBAction)okPressed:(id)sender { | |
| 81 NSIndexSet* indexes = [sourceBrowser_ selectionIndexes]; | |
| 82 NSUInteger selectedIndex = [indexes firstIndex]; | |
| 83 DesktopMediaPickerItem* item = | |
| 84 [items_ objectAtIndex:selectedIndex]; | |
| 85 [self reportResult:[item sourceId]]; | |
| 86 [self close]; | |
| 87 } | |
| 88 | |
| 89 - (IBAction)cancelPressed:(id)sender { | |
| 90 [self reportResult:content::DesktopMediaID()]; | |
| 91 [self close]; | |
| 92 } | |
| 93 | |
| 94 - (void)windowWillClose:(NSNotification*)notification { | |
| 95 [self reportResult:content::DesktopMediaID()]; | |
|
Robert Sesek
2013/09/04 20:23:19
This is being done unconditionally, even after -ok
dcaiafa
2013/09/04 21:39:35
|reportResult:| will only report the result once.
| |
| 96 } | |
| 97 | |
| 98 #pragma mark IKImageBrowserDataSource | |
| 99 | |
| 100 - (NSUInteger)numberOfItemsInImageBrowser:(IKImageBrowserView*)browser { | |
| 101 return [items_ count]; | |
| 102 } | |
| 103 | |
| 104 - (id)imageBrowser:(IKImageBrowserView *)browser | |
| 105 itemAtIndex:(NSUInteger)index { | |
| 106 return [items_ objectAtIndex:index]; | |
| 107 } | |
| 108 | |
| 109 #pragma mark IKImageBrowserDelegate | |
| 110 | |
| 111 - (void)imageBrowser:(IKImageBrowserView *)browser | |
| 112 cellWasDoubleClickedAtIndex:(NSUInteger)index { | |
| 113 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | |
| 114 [self reportResult:[item sourceId]]; | |
| 115 [self close]; | |
| 116 } | |
| 117 | |
| 118 - (void)imageBrowserSelectionDidChange:(IKImageBrowserView*) aBrowser { | |
| 119 // Enable or disable the OK button based on whether we have a selection. | |
| 120 if ([[sourceBrowser_ selectionIndexes] count] > 0) { | |
| 121 [okButton_ setEnabled:YES]; | |
|
Robert Sesek
2013/09/04 20:23:19
You can just setEnabled: with the result of the ab
dcaiafa
2013/09/04 21:39:35
Done.
| |
| 122 } else { | |
| 123 [okButton_ setEnabled:NO]; | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 #pragma mark DesktopMediaPickerObserver | |
| 128 | |
| 129 - (void)sourceAddedAtIndex:(int)index { | |
| 130 const DesktopMediaPickerModel::Source& source = model_->source(index); | |
| 131 NSString* imageTitle = base::SysUTF16ToNSString(source.name); | |
| 132 DesktopMediaPickerItem* item = | |
|
Robert Sesek
2013/09/04 20:23:19
Put this in a scoped_nsobject<>
dcaiafa
2013/09/04 21:39:35
Done.
| |
| 133 [[DesktopMediaPickerItem alloc] initWithSourceId:source.id | |
| 134 imageUID:++lastImageUID_ | |
| 135 imageTitle:imageTitle]; | |
| 136 [items_ insertObject:item atIndex:index]; | |
| 137 [item release]; | |
| 138 [sourceBrowser_ reloadData]; | |
| 139 } | |
| 140 | |
| 141 - (void)sourceRemovedAtIndex:(int)index { | |
| 142 if ([[sourceBrowser_ selectionIndexes] containsIndex:index]) { | |
| 143 // Selected item was removed. Clear selection. | |
| 144 [sourceBrowser_ setSelectionIndexes:[NSIndexSet indexSet] | |
| 145 byExtendingSelection:FALSE]; | |
| 146 } | |
| 147 [items_ removeObjectAtIndex:index]; | |
| 148 [sourceBrowser_ reloadData]; | |
| 149 } | |
| 150 | |
| 151 - (void)sourceNameChangedAtIndex:(int)index { | |
| 152 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | |
| 153 const DesktopMediaPickerModel::Source& source = model_->source(index); | |
| 154 [item setImageTitle:base::SysUTF16ToNSString(source.name)]; | |
| 155 [sourceBrowser_ reloadData]; | |
| 156 } | |
| 157 | |
| 158 - (void)sourceThumbnailChangedAtIndex:(int)index { | |
| 159 const DesktopMediaPickerModel::Source& source = model_->source(index); | |
| 160 base::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( | |
| 161 CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); | |
| 162 gfx::ImageSkia thumbnail = source.thumbnail; | |
| 163 NSImage* image = gfx::SkBitmapToNSImageWithColorSpace( | |
|
Robert Sesek
2013/09/04 20:23:19
Use gfx::NSImageFromImageSkia()
dcaiafa
2013/09/04 21:39:35
Awesome! Done.
On 2013/09/04 20:23:19, rsesek wro
| |
| 164 *source.thumbnail.bitmap(), | |
| 165 colorSpace.get()); | |
| 166 | |
| 167 DesktopMediaPickerItem* item = [items_ objectAtIndex:index]; | |
| 168 [item setImageRepresentation:image]; | |
| 169 [sourceBrowser_ reloadData]; | |
| 170 } | |
| 171 | |
| 172 @end // @interface DesktopMediaPickerController | |
| OLD | NEW |