OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/infobars/media_stream_infobar_controller.h" | 5 #import "chrome/browser/ui/cocoa/infobars/media_stream_infobar_controller.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #import "base/sys_string_conversions.h" | 10 #import "base/sys_string_conversions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/media/media_stream_devices_menu_model.h" | 12 #include "chrome/browser/media/media_stream_devices_menu_model.h" |
13 #include "chrome/browser/ui/cocoa/hover_close_button.h" | 13 #include "chrome/browser/ui/cocoa/hover_close_button.h" |
14 #include "chrome/browser/ui/cocoa/infobars/infobar.h" | 14 #include "chrome/browser/ui/cocoa/infobars/infobar.h" |
15 #import "chrome/browser/ui/cocoa/infobars/infobar_utilities.h" | |
15 #include "chrome/browser/ui/media_stream_infobar_delegate.h" | 16 #include "chrome/browser/ui/media_stream_infobar_delegate.h" |
16 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
17 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 18 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
18 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
19 | 20 |
21 using InfoBarUtilities::CreateLabel; | |
22 using InfoBarUtilities::MoveControl; | |
23 using InfoBarUtilities::VerifyControlOrderAndSpacing; | |
20 using l10n_util::GetNSStringWithFixup; | 24 using l10n_util::GetNSStringWithFixup; |
21 | 25 |
22 static const CGFloat kSpaceBetweenControls = 8; | |
23 | |
24 InfoBar* MediaStreamInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* owner) { | 26 InfoBar* MediaStreamInfoBarDelegate::CreateInfoBar(InfoBarTabHelper* owner) { |
25 MediaStreamInfoBarController* infobar_controller = | 27 MediaStreamInfoBarController* infobar_controller = |
26 [[MediaStreamInfoBarController alloc] initWithDelegate:this | 28 [[MediaStreamInfoBarController alloc] initWithDelegate:this |
27 owner:owner]; | 29 owner:owner]; |
28 return new InfoBar(infobar_controller, this); | 30 return new InfoBar(infobar_controller, this); |
29 } | 31 } |
30 | 32 |
31 namespace { | |
32 | |
33 // Puts |toMove| to the right or left of |anchor|. | |
34 void SizeAndPlaceControl(NSView* toModify, NSView* anchor, bool after) { | |
35 [GTMUILocalizerAndLayoutTweaker sizeToFitView:toModify]; | |
36 NSRect toModifyFrame = [toModify frame]; | |
37 NSRect anchorFrame = [anchor frame]; | |
38 if (after) { | |
39 toModifyFrame.origin.x = NSMaxX(anchorFrame) + kSpaceBetweenControls; | |
40 } else { | |
41 toModifyFrame.origin.x = NSMinX(anchorFrame) - kSpaceBetweenControls - | |
42 NSWidth(toModifyFrame); | |
43 } | |
44 [toModify setFrame:toModifyFrame]; | |
45 } | |
46 | |
47 } // namespace | |
48 | |
49 | 33 |
50 @interface MediaStreamInfoBarController(Private) | 34 @interface MediaStreamInfoBarController(Private) |
51 | 35 |
52 // Adds text for all buttons and text fields. | 36 // Adds text for all buttons and text fields. |
53 - (void)setLabelTexts; | 37 - (void)setLabelTexts; |
54 | 38 |
55 // Populates the device menu with device id:s. | 39 // Populates the device menu with device id:s. |
56 - (void)rebuildDeviceMenu; | 40 - (void)rebuildDeviceMenu; |
57 | 41 |
58 // Arranges all buttons and pup-up menu relative each other. | 42 // Arranges all buttons and pup-up menu relative each other. |
59 - (void)arrangeInfobarLayout; | 43 - (void)arrangeInfobarLayout; |
60 | 44 |
45 // Create all the components for the infobar. | |
46 - (void)constructView; | |
47 | |
48 - (void)showDeviceMenuTitle:(BOOL)showTitle; | |
61 @end | 49 @end |
62 | 50 |
63 | 51 |
64 @implementation MediaStreamInfoBarController | 52 @implementation MediaStreamInfoBarController |
65 | 53 |
66 - (id)initWithDelegate:(MediaStreamInfoBarDelegate*)delegate | 54 - (id)initWithDelegate:(MediaStreamInfoBarDelegate*)delegate |
67 owner:(InfoBarTabHelper*)owner { | 55 owner:(InfoBarTabHelper*)owner { |
68 if (self = [super initWithDelegate:delegate owner:owner]) { | 56 if (self = [super initWithDelegate:delegate owner:owner]) { |
69 deviceMenuModel_.reset(new MediaStreamDevicesMenuModel(delegate)); | 57 deviceMenuModel_.reset(new MediaStreamDevicesMenuModel(delegate)); |
70 | |
71 label1_.reset([[NSTextField alloc] init]); | |
72 [label1_ setEditable:NO]; | |
73 [label1_ setDrawsBackground:NO]; | |
74 [label1_ setBordered:NO]; | |
75 | |
76 [okButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
77 [cancelButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
78 | |
79 deviceMenu_.reset([[NSPopUpButton alloc] init]); | |
80 [deviceMenu_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
81 [deviceMenu_ setAutoresizingMask:NSViewMaxXMargin]; | |
82 [[deviceMenu_ cell] setArrowPosition:NSPopUpArrowAtBottom]; | |
83 NSMenu* menu = [deviceMenu_ menu]; | |
84 [menu setDelegate:nil]; | |
85 [menu setAutoenablesItems:NO]; | |
86 } | 58 } |
87 return self; | 59 return self; |
88 } | 60 } |
89 | 61 |
90 - (void)dealloc { | 62 - (void)dealloc { |
91 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 63 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
92 [deviceMenu_ removeAllItems]; | 64 [deviceMenu_ removeAllItems]; |
93 [super dealloc]; | 65 [super dealloc]; |
94 } | 66 } |
95 | 67 |
(...skipping 21 matching lines...) Expand all Loading... | |
117 [super removeSelf]; | 89 [super removeSelf]; |
118 } | 90 } |
119 | 91 |
120 - (IBAction)deviceMenuChanged:(id)item { | 92 - (IBAction)deviceMenuChanged:(id)item { |
121 // Notify the menu model about the change and rebuild the menu. | 93 // Notify the menu model about the change and rebuild the menu. |
122 deviceMenuModel_->ExecuteCommand([item tag]); | 94 deviceMenuModel_->ExecuteCommand([item tag]); |
123 [self rebuildDeviceMenu]; | 95 [self rebuildDeviceMenu]; |
124 } | 96 } |
125 | 97 |
126 - (void)addAdditionalControls { | 98 - (void)addAdditionalControls { |
127 // Get a frame to use as inital frame for all buttons. | 99 // Create all the components for the infobar. |
128 NSRect initFrame = [okButton_ frame]; | 100 [self constructView]; |
129 | 101 |
130 // Setup up the text label and add the device menu to the infobar. | 102 // Get layout information from the NIB. |
131 // TODO(mflodman) Use |label_| instead. | 103 NSRect cancelButtonFrame = [cancelButton_ frame]; |
132 [label1_ setFrame:[label_ frame]]; | 104 NSRect okButtonFrame = [okButton_ frame]; |
133 [[label_ superview] replaceSubview:label_ with:label1_.get()]; | |
134 label_.reset(); | |
135 | 105 |
136 [deviceMenu_ setFrame:initFrame]; | 106 // Verify Cancel button is on the left of OK button. |
137 [infoBarView_ addSubview:deviceMenu_]; | 107 DCHECK(NSMaxX(cancelButtonFrame) < NSMinX(okButtonFrame)); |
no longer working on chromium
2012/07/27 10:09:56
We don't allow overlap between the buttons. So it
Scott Hess - ex-Googler
2012/07/27 16:35:20
The nib file controls this, the OS doesn't care.
no longer working on chromium
2012/07/30 11:36:31
Done.
| |
138 | 108 spaceBetweenControls_ = NSMinX(okButtonFrame) - NSMaxX(cancelButtonFrame); |
139 // Add text to all buttons and the text field. | |
140 [self setLabelTexts]; | |
141 | 109 |
142 // Arrange the initial layout. | 110 // Arrange the initial layout. |
143 [self arrangeInfobarLayout]; | 111 [self arrangeInfobarLayout]; |
144 | 112 |
145 // Build the device popup menu. | |
146 [self rebuildDeviceMenu]; | |
147 | |
148 // Make sure we get notified of infobar view size changes. | 113 // Make sure we get notified of infobar view size changes. |
149 // TODO(mflodman) Find if there is a way to use 'setAutorezingMask' instead. | 114 // TODO(mflodman) Find if there is a way to use 'setAutorezingMask' instead. |
150 [infoBarView_ setPostsFrameChangedNotifications:YES]; | 115 [infoBarView_ setPostsFrameChangedNotifications:YES]; |
151 [[NSNotificationCenter defaultCenter] | 116 [[NSNotificationCenter defaultCenter] |
152 addObserver:self | 117 addObserver:self |
153 selector:@selector(didChangeFrame:) | 118 selector:@selector(didChangeFrame:) |
154 name:NSViewFrameDidChangeNotification | 119 name:NSViewFrameDidChangeNotification |
155 object:infoBarView_]; | 120 object:infoBarView_]; |
156 } | 121 } |
157 | 122 |
(...skipping 23 matching lines...) Expand all Loading... | |
181 [okButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_ALLOW)]; | 146 [okButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_ALLOW)]; |
182 [cancelButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_DENY)]; | 147 [cancelButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_DENY)]; |
183 | 148 |
184 // Populating |deviceMenu_| is handled separately. | 149 // Populating |deviceMenu_| is handled separately. |
185 } | 150 } |
186 | 151 |
187 - (void)arrangeInfobarLayout { | 152 - (void)arrangeInfobarLayout { |
188 // Set correct size for text frame. | 153 // Set correct size for text frame. |
189 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label1_]; | 154 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label1_]; |
190 | 155 |
191 // Place |okButton_| to the right of the text field. | 156 // From left to right: label_, cancelButton_ and okButton_. |
192 SizeAndPlaceControl(okButton_, label1_, true); | 157 MoveControl(label1_, cancelButton_, spaceBetweenControls_, true); |
158 MoveControl(cancelButton_, okButton_, spaceBetweenControls_, true); | |
193 | 159 |
194 // Place |cancelButton| to the right of [okButton_|. | 160 // Build the device option popup menu. |
195 SizeAndPlaceControl(cancelButton_, okButton_, true); | 161 [deviceMenu_ setHidden:NO]; |
162 [self showDeviceMenuTitle:YES]; | |
163 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | |
164 MoveControl(closeButton_, deviceMenu_, spaceBetweenControls_, false); | |
196 | 165 |
197 // |deviceMenu_| is floating to the right, besides |closeButton_|. | 166 // Hide the title of deviceMenu_ or even the whole menu when it overlaps |
198 SizeAndPlaceControl(deviceMenu_, closeButton_, false); | 167 // with the okButton_. |
168 if (!VerifyControlOrderAndSpacing(okButton_, deviceMenu_)) { | |
169 [self showDeviceMenuTitle:NO]; | |
170 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | |
Scott Hess - ex-Googler
2012/07/27 16:35:20
Maybe move this line (and the occurance above) rig
no longer working on chromium
2012/07/30 11:36:31
Done.
| |
171 MoveControl(closeButton_, deviceMenu_, spaceBetweenControls_, false); | |
172 if (!VerifyControlOrderAndSpacing(okButton_, deviceMenu_)) { | |
173 [deviceMenu_ setHidden:YES]; | |
174 } | |
175 } | |
176 } | |
177 | |
178 - (void)constructView { | |
179 // Use the ok button frame as inital frame for all components. | |
180 NSRect initFrame = [okButton_ frame]; | |
181 | |
182 // Setup the text label and add the device menu to the infobar. | |
183 // TODO(mflodman) Use |label_| instead. | |
184 label1_.reset([[NSTextField alloc] init]); | |
185 [label1_ setEditable:NO]; | |
186 [label1_ setDrawsBackground:NO]; | |
187 [label1_ setBordered:NO]; | |
188 [label1_ setFrame:[label_ frame]]; | |
189 [[label_ superview] replaceSubview:label_ with:label1_]; | |
190 label_.reset(); | |
191 | |
192 [okButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
193 [cancelButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
194 | |
195 // Add text to all buttons and the text field. | |
196 [self setLabelTexts]; | |
197 | |
198 // Setup the device options menu. | |
199 deviceMenu_.reset([[NSPopUpButton alloc] initWithFrame:initFrame | |
200 pullsDown:YES]); | |
201 [self rebuildDeviceMenu]; | |
202 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | |
Scott Hess - ex-Googler
2012/07/27 16:35:20
I would put the setBezelStyle: and setAutoresizing
no longer working on chromium
2012/07/30 11:36:31
Done.
| |
203 [deviceMenu_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
204 [deviceMenu_ setAutoresizingMask:NSViewMinXMargin]; | |
205 NSMenu* menu = [deviceMenu_ menu]; | |
206 [menu setDelegate:nil]; | |
207 [menu setAutoenablesItems:NO]; | |
208 | |
209 // Add "options" popup z-ordered below all other controls so when we | |
210 // resize the toolbar it doesn't hide them. | |
211 [infoBarView_ addSubview:deviceMenu_ | |
212 positioned:NSWindowBelow | |
213 relativeTo:nil]; | |
199 } | 214 } |
200 | 215 |
201 - (void)rebuildDeviceMenu { | 216 - (void)rebuildDeviceMenu { |
202 // Remove all old menu items and rebuild from scratch. | 217 // Remove all old menu items and rebuild from scratch. |
203 [deviceMenu_ removeAllItems]; | 218 [deviceMenu_ removeAllItems]; |
204 NSMenu* menu = [deviceMenu_ menu]; | 219 NSMenu* menu = [deviceMenu_ menu]; |
205 | 220 |
206 // Add title item. | 221 // Add title item. |
207 NSString* menuTitle = GetNSStringWithFixup( | 222 NSString* menuTitle = GetNSStringWithFixup( |
208 IDS_MEDIA_CAPTURE_DEVICES_MENU_TITLE); | 223 IDS_MEDIA_CAPTURE_DEVICES_MENU_TITLE); |
(...skipping 16 matching lines...) Expand all Loading... | |
225 action:@selector(deviceMenuChanged:) | 240 action:@selector(deviceMenuChanged:) |
226 keyEquivalent:@""]; | 241 keyEquivalent:@""]; |
227 [item setTarget:self]; | 242 [item setTarget:self]; |
228 [item setTag:deviceMenuModel_->GetCommandIdAt(i)]; | 243 [item setTag:deviceMenuModel_->GetCommandIdAt(i)]; |
229 if (deviceMenuModel_->IsItemCheckedAt(i)) { | 244 if (deviceMenuModel_->IsItemCheckedAt(i)) { |
230 [item setState:NSOnState]; | 245 [item setState:NSOnState]; |
231 } | 246 } |
232 [item setEnabled:deviceMenuModel_->IsEnabledAt(i)]; | 247 [item setEnabled:deviceMenuModel_->IsEnabledAt(i)]; |
233 } | 248 } |
234 } | 249 } |
235 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | 250 } |
251 | |
252 - (void)showDeviceMenuTitle:(BOOL)showTitle { | |
253 if (showTitle) { | |
254 NSString* menuTitle = GetNSStringWithFixup( | |
255 IDS_MEDIA_CAPTURE_DEVICES_MENU_TITLE); | |
256 [[[deviceMenu_ menu] itemAtIndex:0] setTitle:menuTitle]; | |
257 [[deviceMenu_ cell] setArrowPosition:NSPopUpArrowAtBottom]; | |
258 } else { | |
259 [[[deviceMenu_ menu] itemAtIndex:0] setTitle:@""]; | |
260 [[deviceMenu_ cell] setArrowPosition:NSPopUpArrowAtCenter]; | |
261 } | |
236 } | 262 } |
237 | 263 |
238 - (void)didChangeFrame:(NSNotification*)notification { | 264 - (void)didChangeFrame:(NSNotification*)notification { |
239 [self arrangeInfobarLayout]; | 265 [self arrangeInfobarLayout]; |
240 } | 266 } |
241 | 267 |
242 @end // implementation MediaStreamInfoBarController | 268 @end // implementation MediaStreamInfoBarController |
OLD | NEW |