Index: chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm |
diff --git a/chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm b/chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm |
index f92de8c0e4af49483756c2a0d11e3dde05255575..5e8b6e3652a1c3096f393985af7b8f4f1d9c0437 100644 |
--- a/chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm |
+++ b/chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm |
@@ -6,7 +6,9 @@ |
#import <Cocoa/Cocoa.h> |
+#include "base/i18n/rtl.h" |
#include "base/logging.h" |
+#import "base/mac/foundation_util.h" |
#include "base/strings/sys_string_conversions.h" |
#import "chrome/browser/chrome_browser_application_mac.h" |
#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" |
@@ -142,8 +144,12 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa( |
dialog_->default_prompt_text())]; |
} |
[alert_ setDelegate:helper_]; |
- [alert_ setInformativeText:base::SysUTF16ToNSString(dialog_->message_text())]; |
- [alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; |
+ NSString* informative_text = |
+ base::SysUTF16ToNSString(dialog_->message_text()); |
+ [alert_ setInformativeText:informative_text]; |
+ NSString* message_text = |
+ base::SysUTF16ToNSString(dialog_->title()); |
+ [alert_ setMessageText:message_text]; |
[alert_ addButtonWithTitle:default_button]; |
if (!one_button) { |
NSButton* other = [alert_ addButtonWithTitle:other_button]; |
@@ -155,6 +161,85 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa( |
IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); |
[[alert_ suppressionButton] setTitle:suppression_title]; |
} |
+ |
+ // Fix RTL dialogs. |
+ // |
+ // Mac OS X will always display NSAlert strings as LTR. A workaround is to |
+ // manually set the text as attributed strings in the implementing |
+ // NSTextFields. This is a basic correctness issue. |
+ // |
+ // In addition, for readability, the overall alignment is set based on the |
+ // directionality of the first strongly-directional character. |
+ // |
+ // If the dialog fields are selectable then they will scramble when clicked. |
+ // Therefore, selectability is disabled. |
+ // |
+ // See http://crbug.com/70806 for more details. |
+ |
+ bool message_has_rtl = |
+ base::i18n::StringContainsStrongRTLChars(dialog_->title()); |
+ bool informative_has_rtl = |
+ base::i18n::StringContainsStrongRTLChars(dialog_->message_text()); |
+ |
+ NSTextField* message_text_field = nil; |
+ NSTextField* informative_text_field = nil; |
+ if (message_has_rtl || informative_has_rtl) { |
+ // Force layout of the dialog. NSAlert leaves its dialog alone once laid |
+ // out; if this is not done then all the modifications that are to come will |
+ // be un-done when the dialog is finally displayed. |
+ [alert_ layout]; |
+ |
+ // Locate the NSTextFields that implement the text display. These are |
+ // actually available as the ivars |_messageField| and |_informationField| |
+ // of the NSAlert, but it is safer (and more forward-compatible) to search |
+ // for them in the subviews. |
+ for (NSView* view in [[[alert_ window] contentView] subviews]) { |
+ NSTextField* text_field = base::mac::ObjCCast<NSTextField>(view); |
+ if ([[text_field stringValue] isEqualTo:message_text]) |
+ message_text_field = text_field; |
+ else if ([[text_field stringValue] isEqualTo:informative_text]) |
+ informative_text_field = text_field; |
+ } |
+ |
+ // This may fail in future OS releases, but it will still work for shipped |
+ // versions of Chromium. |
+ DCHECK(message_text_field); |
+ DCHECK(informative_text_field); |
+ } |
+ |
+ if (message_has_rtl && message_text_field) { |
+ scoped_nsobject<NSMutableParagraphStyle> alignment( |
+ [[NSParagraphStyle defaultParagraphStyle] mutableCopy]); |
+ [alignment setAlignment:NSRightTextAlignment]; |
+ |
+ NSDictionary* alignment_attributes = |
+ @{ NSParagraphStyleAttributeName : alignment }; |
+ scoped_nsobject<NSAttributedString> attr_string( |
+ [[NSAttributedString alloc] initWithString:message_text |
+ attributes:alignment_attributes]); |
+ |
+ [message_text_field setAttributedStringValue:attr_string]; |
+ [message_text_field setSelectable:NO]; |
+ } |
+ |
+ if (informative_has_rtl && informative_text_field) { |
+ base::i18n::TextDirection direction = |
+ base::i18n::GetFirstStrongCharacterDirection(dialog_->message_text()); |
+ scoped_nsobject<NSMutableParagraphStyle> alignment( |
+ [[NSParagraphStyle defaultParagraphStyle] mutableCopy]); |
+ [alignment setAlignment: |
+ (direction == base::i18n::RIGHT_TO_LEFT) ? NSRightTextAlignment |
+ : NSLeftTextAlignment]; |
+ |
+ NSDictionary* alignment_attributes = |
+ @{ NSParagraphStyleAttributeName : alignment }; |
+ scoped_nsobject<NSAttributedString> attr_string( |
+ [[NSAttributedString alloc] initWithString:informative_text |
+ attributes:alignment_attributes]); |
+ |
+ [informative_text_field setAttributedStringValue:attr_string]; |
+ [informative_text_field setSelectable:NO]; |
+ } |
} |
JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() { |