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