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 #include "chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
8 | 8 |
9 #include "base/i18n/rtl.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/mac/foundation_util.h" | |
Mark Mentovai
2013/05/16 22:34:27
#import me.
| |
10 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
11 #import "chrome/browser/chrome_browser_application_mac.h" | 13 #import "chrome/browser/chrome_browser_application_mac.h" |
12 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" | 14 #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
13 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
14 #include "grit/ui_strings.h" | 16 #include "grit/ui_strings.h" |
15 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
16 #include "ui/base/ui_base_types.h" | 18 #include "ui/base/ui_base_types.h" |
17 | 19 |
18 // Helper object that receives the notification that the dialog/sheet is | 20 // Helper object that receives the notification that the dialog/sheet is |
19 // going away. Is responsible for cleaning itself up. | 21 // going away. Is responsible for cleaning itself up. |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 | 137 |
136 // Show the modal dialog. | 138 // Show the modal dialog. |
137 alert_ = [helper_ alert]; | 139 alert_ = [helper_ alert]; |
138 NSTextField* field = nil; | 140 NSTextField* field = nil; |
139 if (text_field) { | 141 if (text_field) { |
140 field = [helper_ textField]; | 142 field = [helper_ textField]; |
141 [field setStringValue:base::SysUTF16ToNSString( | 143 [field setStringValue:base::SysUTF16ToNSString( |
142 dialog_->default_prompt_text())]; | 144 dialog_->default_prompt_text())]; |
143 } | 145 } |
144 [alert_ setDelegate:helper_]; | 146 [alert_ setDelegate:helper_]; |
145 [alert_ setInformativeText:base::SysUTF16ToNSString(dialog_->message_text())]; | 147 NSString* informativeText = base::SysUTF16ToNSString(dialog_->message_text()); |
Mark Mentovai
2013/05/16 22:34:27
Outside of @interface, @implementation, etc., name
| |
148 [alert_ setInformativeText:informativeText]; | |
146 [alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; | 149 [alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; |
147 [alert_ addButtonWithTitle:default_button]; | 150 [alert_ addButtonWithTitle:default_button]; |
148 if (!one_button) { | 151 if (!one_button) { |
149 NSButton* other = [alert_ addButtonWithTitle:other_button]; | 152 NSButton* other = [alert_ addButtonWithTitle:other_button]; |
150 [other setKeyEquivalent:@"\e"]; | 153 [other setKeyEquivalent:@"\e"]; |
151 } | 154 } |
152 if (dialog_->display_suppress_checkbox()) { | 155 if (dialog_->display_suppress_checkbox()) { |
153 [alert_ setShowsSuppressionButton:YES]; | 156 [alert_ setShowsSuppressionButton:YES]; |
154 NSString* suppression_title = l10n_util::GetNSStringWithFixup( | 157 NSString* suppression_title = l10n_util::GetNSStringWithFixup( |
155 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); | 158 IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
156 [[alert_ suppressionButton] setTitle:suppression_title]; | 159 [[alert_ suppressionButton] setTitle:suppression_title]; |
157 } | 160 } |
161 | |
162 // Fix RTL dialogs. | |
Mark Mentovai
2013/05/16 22:34:27
Can you avoid doing this if it’s not needed? Gate
| |
163 // | |
164 // Mac OS X will always display an NSAlert's informative text as LTR. A | |
165 // workaround is to manually set the informative text as an attributed string | |
166 // in the implementing NSTextField. This is a basic correctness issue. | |
167 // | |
168 // In addition, for readability, the overall alignment is set based on the | |
169 // directionality of the first strongly-directional character. | |
170 // | |
171 // See http://crbug.com/70806 for more details. | |
172 | |
173 // Force layout of the dialog. NSAlert leaves its dialog alone once laid out; | |
174 // if this is not done then all the modifications that are to come will be | |
175 // un-done when the dialog is finally displayed. | |
176 [alert_ layout]; | |
177 | |
178 // Locate the NSTextField that implements the informative text. This is | |
179 // actually available as the ivar |_informationField| of the NSAlert, but it | |
180 // is safer (and more forward-compatible) to search for it in the subviews. | |
181 NSTextField* informativeTextField = nil; | |
182 for (NSView* view in [[[alert_ window] contentView] subviews]) { | |
183 NSTextField* textField = base::mac::ObjCCast<NSTextField>(view); | |
184 if (!textField) | |
Mark Mentovai
2013/05/16 22:34:27
This check is unnecessary.
| |
185 continue; | |
186 | |
187 if (![[textField stringValue] isEqualTo:informativeText]) | |
188 continue; | |
189 | |
190 informativeTextField = textField; | |
Mark Mentovai
2013/05/16 22:34:27
break here?
Since you’ve removed one of the “cont
| |
191 } | |
192 | |
193 if (informativeTextField) { | |
Mark Mentovai
2013/05/16 22:34:27
DCHECK(informativeTextField) ?
| |
194 base::i18n::TextDirection direction = | |
195 base::i18n::GetFirstStrongCharacterDirection(dialog_->message_text()); | |
196 scoped_nsobject<NSMutableParagraphStyle> alignment( | |
197 [[NSParagraphStyle defaultParagraphStyle] mutableCopy]); | |
Mark Mentovai
2013/05/16 22:34:27
Nit: continuation line indent gets another 2 space
| |
198 if (direction == base::i18n::RIGHT_TO_LEFT) | |
199 [alignment setAlignment:NSRightTextAlignment]; | |
Mark Mentovai
2013/05/16 22:34:27
Ideas: Ternary? Or avoid having an alignment attri
Avi (use Gerrit)
2013/05/16 23:16:20
Ternary. At this point I'm a little paranoid and j
| |
200 else | |
201 [alignment setAlignment:NSLeftTextAlignment]; | |
202 | |
203 NSDictionary* alignmentAttributes = | |
204 @{ NSParagraphStyleAttributeName : alignment }; | |
205 scoped_nsobject<NSAttributedString> attrString( | |
206 [[NSAttributedString alloc] initWithString:informativeText | |
207 attributes:alignmentAttributes]); | |
208 | |
209 [informativeTextField setAttributedStringValue:attrString]; | |
210 } | |
158 } | 211 } |
159 | 212 |
160 JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() { | 213 JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() { |
161 } | 214 } |
162 | 215 |
163 //////////////////////////////////////////////////////////////////////////////// | 216 //////////////////////////////////////////////////////////////////////////////// |
164 // JavaScriptAppModalDialogCocoa, NativeAppModalDialog implementation: | 217 // JavaScriptAppModalDialogCocoa, NativeAppModalDialog implementation: |
165 | 218 |
166 int JavaScriptAppModalDialogCocoa::GetAppModalDialogButtons() const { | 219 int JavaScriptAppModalDialogCocoa::GetAppModalDialogButtons() const { |
167 // From the above, it is the case that if there is 1 button, it is always the | 220 // From the above, it is the case that if there is 1 button, it is always the |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 266 |
214 //////////////////////////////////////////////////////////////////////////////// | 267 //////////////////////////////////////////////////////////////////////////////// |
215 // NativeAppModalDialog, public: | 268 // NativeAppModalDialog, public: |
216 | 269 |
217 // static | 270 // static |
218 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( | 271 NativeAppModalDialog* NativeAppModalDialog::CreateNativeJavaScriptPrompt( |
219 JavaScriptAppModalDialog* dialog, | 272 JavaScriptAppModalDialog* dialog, |
220 gfx::NativeWindow parent_window) { | 273 gfx::NativeWindow parent_window) { |
221 return new JavaScriptAppModalDialogCocoa(dialog); | 274 return new JavaScriptAppModalDialogCocoa(dialog); |
222 } | 275 } |
OLD | NEW |