Commit 237207bc authored by avi@chromium.org's avatar avi@chromium.org

Fix RTL issues in javascript alerts.

BUG=70806
TEST=as in bug

Review URL: https://chromiumcodereview.appspot.com/15197003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@202471 0039d316-1c4b-4281-b951-d872f2087c98
parent db9f2142
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "base/i18n/rtl.h"
#include "base/logging.h" #include "base/logging.h"
#import "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "chrome/browser/chrome_browser_application_mac.h" #import "chrome/browser/chrome_browser_application_mac.h"
#include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h" #include "chrome/browser/ui/app_modal_dialogs/javascript_app_modal_dialog.h"
...@@ -142,8 +144,12 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa( ...@@ -142,8 +144,12 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa(
dialog_->default_prompt_text())]; dialog_->default_prompt_text())];
} }
[alert_ setDelegate:helper_]; [alert_ setDelegate:helper_];
[alert_ setInformativeText:base::SysUTF16ToNSString(dialog_->message_text())]; NSString* informative_text =
[alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())]; base::SysUTF16ToNSString(dialog_->message_text());
[alert_ setInformativeText:informative_text];
NSString* message_text =
base::SysUTF16ToNSString(dialog_->title());
[alert_ setMessageText:message_text];
[alert_ addButtonWithTitle:default_button]; [alert_ addButtonWithTitle:default_button];
if (!one_button) { if (!one_button) {
NSButton* other = [alert_ addButtonWithTitle:other_button]; NSButton* other = [alert_ addButtonWithTitle:other_button];
...@@ -155,6 +161,85 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa( ...@@ -155,6 +161,85 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa(
IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION); IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION);
[[alert_ suppressionButton] setTitle:suppression_title]; [[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() { JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment