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 | |
61 @end | 48 @end |
62 | 49 |
63 | 50 |
64 @implementation MediaStreamInfoBarController | 51 @implementation MediaStreamInfoBarController |
65 | 52 |
66 - (id)initWithDelegate:(MediaStreamInfoBarDelegate*)delegate | 53 - (id)initWithDelegate:(MediaStreamInfoBarDelegate*)delegate |
67 owner:(InfoBarTabHelper*)owner { | 54 owner:(InfoBarTabHelper*)owner { |
68 if (self = [super initWithDelegate:delegate owner:owner]) { | 55 if (self = [super initWithDelegate:delegate owner:owner]) { |
69 deviceMenuModel_.reset(new MediaStreamDevicesMenuModel(delegate)); | 56 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 } | 57 } |
87 return self; | 58 return self; |
88 } | 59 } |
89 | 60 |
90 - (void)dealloc { | 61 - (void)dealloc { |
91 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 62 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
92 [deviceMenu_ removeAllItems]; | 63 [deviceMenu_ removeAllItems]; |
93 [super dealloc]; | 64 [super dealloc]; |
94 } | 65 } |
95 | 66 |
(...skipping 21 matching lines...) Expand all Loading... | |
117 [super removeSelf]; | 88 [super removeSelf]; |
118 } | 89 } |
119 | 90 |
120 - (IBAction)deviceMenuChanged:(id)item { | 91 - (IBAction)deviceMenuChanged:(id)item { |
121 // Notify the menu model about the change and rebuild the menu. | 92 // Notify the menu model about the change and rebuild the menu. |
122 deviceMenuModel_->ExecuteCommand([item tag]); | 93 deviceMenuModel_->ExecuteCommand([item tag]); |
123 [self rebuildDeviceMenu]; | 94 [self rebuildDeviceMenu]; |
124 } | 95 } |
125 | 96 |
126 - (void)addAdditionalControls { | 97 - (void)addAdditionalControls { |
127 // Get a frame to use as inital frame for all buttons. | 98 // Create all the components for the infobar. |
128 NSRect initFrame = [okButton_ frame]; | 99 [self constructView]; |
129 | 100 |
130 // Setup up the text label and add the device menu to the infobar. | 101 // Get layout information from the NIB. |
131 // TODO(mflodman) Use |label_| instead. | 102 NSRect cancelButtonFrame = [cancelButton_ frame]; |
132 [label1_ setFrame:[label_ frame]]; | 103 NSRect okButtonFrame = [okButton_ frame]; |
133 [[label_ superview] replaceSubview:label_ with:label1_.get()]; | |
134 label_.reset(); | |
135 | 104 |
136 [deviceMenu_ setFrame:initFrame]; | 105 spaceBetweenControls_ = NSMaxX(cancelButtonFrame) < NSMinX(okButtonFrame) ? |
137 [infoBarView_ addSubview:deviceMenu_]; | 106 NSMinX(okButtonFrame) - NSMaxX(cancelButtonFrame) : |
138 | 107 NSMinX(cancelButtonFrame) - NSMaxX(okButtonFrame); |
139 // Add text to all buttons and the text field. | |
140 [self setLabelTexts]; | |
141 | 108 |
142 // Arrange the initial layout. | 109 // Arrange the initial layout. |
143 [self arrangeInfobarLayout]; | 110 [self arrangeInfobarLayout]; |
144 | 111 |
145 // Build the device popup menu. | |
146 [self rebuildDeviceMenu]; | |
147 | |
148 // Make sure we get notified of infobar view size changes. | 112 // Make sure we get notified of infobar view size changes. |
149 // TODO(mflodman) Find if there is a way to use 'setAutorezingMask' instead. | 113 // TODO(mflodman) Find if there is a way to use 'setAutorezingMask' instead. |
150 [infoBarView_ setPostsFrameChangedNotifications:YES]; | 114 [infoBarView_ setPostsFrameChangedNotifications:YES]; |
151 [[NSNotificationCenter defaultCenter] | 115 [[NSNotificationCenter defaultCenter] |
152 addObserver:self | 116 addObserver:self |
153 selector:@selector(didChangeFrame:) | 117 selector:@selector(didChangeFrame:) |
154 name:NSViewFrameDidChangeNotification | 118 name:NSViewFrameDidChangeNotification |
155 object:infoBarView_]; | 119 object:infoBarView_]; |
156 } | 120 } |
157 | 121 |
(...skipping 23 matching lines...) Expand all Loading... | |
181 [okButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_ALLOW)]; | 145 [okButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_ALLOW)]; |
182 [cancelButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_DENY)]; | 146 [cancelButton_ setTitle:GetNSStringWithFixup(IDS_MEDIA_CAPTURE_DENY)]; |
183 | 147 |
184 // Populating |deviceMenu_| is handled separately. | 148 // Populating |deviceMenu_| is handled separately. |
185 } | 149 } |
186 | 150 |
187 - (void)arrangeInfobarLayout { | 151 - (void)arrangeInfobarLayout { |
188 // Set correct size for text frame. | 152 // Set correct size for text frame. |
189 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label1_]; | 153 [GTMUILocalizerAndLayoutTweaker sizeToFitView:label1_]; |
190 | 154 |
191 // Place |okButton_| to the right of the text field. | 155 // From left to right: label_, cancelButton_ and okButton_. |
192 SizeAndPlaceControl(okButton_, label1_, true); | 156 MoveControl(label1_, cancelButton_, spaceBetweenControls_, true); |
157 MoveControl(cancelButton_, okButton_, spaceBetweenControls_, true); | |
193 | 158 |
194 // Place |cancelButton| to the right of [okButton_|. | 159 // Build the device option popup menu. |
195 SizeAndPlaceControl(cancelButton_, okButton_, true); | 160 [deviceMenu_ setHidden:NO]; |
161 [self rebuildDeviceMenu]; | |
162 [[deviceMenu_ cell] setArrowPosition:NSPopUpArrowAtBottom]; | |
163 [deviceMenu_ sizeToFit]; | |
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 [[[deviceMenu_ menu] itemAtIndex:0] setTitle:@""]; | |
170 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | |
171 [[deviceMenu_ cell] setArrowPosition:NSPopUpArrowAtCenter]; | |
172 MoveControl(closeButton_, deviceMenu_, spaceBetweenControls_, false); | |
173 if (!VerifyControlOrderAndSpacing(okButton_, deviceMenu_)) { | |
174 [deviceMenu_ setHidden:YES]; | |
175 } | |
176 } | |
177 } | |
178 | |
179 - (void)constructView { | |
180 // Use the ok button frame as inital frame for all components. | |
181 NSRect initFrame = [okButton_ frame]; | |
182 | |
183 // Setup the text label and add the device menu to the infobar. | |
184 // TODO(mflodman) Use |label_| instead. | |
185 label1_.reset([[NSTextField alloc] init]); | |
186 [label1_ setEditable:NO]; | |
187 [label1_ setDrawsBackground:NO]; | |
188 [label1_ setBordered:NO]; | |
189 [label1_ setFrame:[label_ frame]]; | |
190 [[label_ superview] replaceSubview:label_ with:label1_.get()]; | |
Scott Hess - ex-Googler
2012/07/26 19:39:41
Does this really need the get() on label1_? It sh
no longer working on chromium
2012/07/27 10:09:55
Old code, but strangely all the replaceSubview API
Scott Hess - ex-Googler
2012/07/27 16:35:20
I think the rule of thumb is to use the non-get()
| |
191 label_.reset(); | |
192 | |
193 [okButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
194 [cancelButton_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
195 | |
196 // Add text to all buttons and the text field. | |
197 [self setLabelTexts]; | |
198 | |
199 // Setup the device options menu. | |
200 deviceMenu_.reset([[NSPopUpButton alloc] initWithFrame:initFrame | |
201 pullsDown:YES]); | |
202 [deviceMenu_ setBezelStyle:NSTexturedRoundedBezelStyle]; | |
203 NSMenu* menu = [deviceMenu_ menu]; | |
204 [menu setDelegate:nil]; | |
205 [menu setAutoenablesItems:NO]; | |
206 | |
207 [deviceMenu_ setAutoresizingMask:NSViewMinXMargin]; | |
Scott Hess - ex-Googler
2012/07/26 19:39:41
Move this above the NSMenu line so the deviceMenu_
no longer working on chromium
2012/07/27 10:09:55
Done.
| |
208 // Add "options" popup z-ordered below all other controls so when we | |
209 // resize the toolbar it doesn't hide them. | |
210 [infoBarView_ addSubview:deviceMenu_ | |
211 positioned:NSWindowBelow | |
212 relativeTo:nil]; | |
199 } | 213 } |
200 | 214 |
201 - (void)rebuildDeviceMenu { | 215 - (void)rebuildDeviceMenu { |
202 // Remove all old menu items and rebuild from scratch. | 216 // Remove all old menu items and rebuild from scratch. |
203 [deviceMenu_ removeAllItems]; | 217 [deviceMenu_ removeAllItems]; |
204 NSMenu* menu = [deviceMenu_ menu]; | 218 NSMenu* menu = [deviceMenu_ menu]; |
205 | 219 |
206 // Add title item. | 220 // Add title item. |
207 NSString* menuTitle = GetNSStringWithFixup( | 221 NSString* menuTitle = GetNSStringWithFixup( |
208 IDS_MEDIA_CAPTURE_DEVICES_MENU_TITLE); | 222 IDS_MEDIA_CAPTURE_DEVICES_MENU_TITLE); |
(...skipping 16 matching lines...) Expand all Loading... | |
225 action:@selector(deviceMenuChanged:) | 239 action:@selector(deviceMenuChanged:) |
226 keyEquivalent:@""]; | 240 keyEquivalent:@""]; |
227 [item setTarget:self]; | 241 [item setTarget:self]; |
228 [item setTag:deviceMenuModel_->GetCommandIdAt(i)]; | 242 [item setTag:deviceMenuModel_->GetCommandIdAt(i)]; |
229 if (deviceMenuModel_->IsItemCheckedAt(i)) { | 243 if (deviceMenuModel_->IsItemCheckedAt(i)) { |
230 [item setState:NSOnState]; | 244 [item setState:NSOnState]; |
231 } | 245 } |
232 [item setEnabled:deviceMenuModel_->IsEnabledAt(i)]; | 246 [item setEnabled:deviceMenuModel_->IsEnabledAt(i)]; |
233 } | 247 } |
234 } | 248 } |
235 [GTMUILocalizerAndLayoutTweaker sizeToFitView:deviceMenu_]; | |
Scott Hess - ex-Googler
2012/07/26 19:39:41
It looks to me like the GTM version of sizeToFit h
no longer working on chromium
2012/07/27 10:09:55
I guess you what meant is deviceMenu_ sizeToFit];
Scott Hess - ex-Googler
2012/07/27 16:35:20
Basically, I think you probably need to use the GT
no longer working on chromium
2012/07/30 11:36:31
Thanks for pointing out. I just found out that it
| |
236 } | 249 } |
237 | 250 |
238 - (void)didChangeFrame:(NSNotification*)notification { | 251 - (void)didChangeFrame:(NSNotification*)notification { |
239 [self arrangeInfobarLayout]; | 252 [self arrangeInfobarLayout]; |
240 } | 253 } |
241 | 254 |
242 @end // implementation MediaStreamInfoBarController | 255 @end // implementation MediaStreamInfoBarController |
OLD | NEW |