Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Unified Diff: chrome/browser/ui/cocoa/javascript_app_modal_dialog_cocoa.mm

Issue 15197003: Fix RTL issues in javascript alerts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleaner Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0e8965c8d4263dee1d56245499da615ad30de8f8 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,7 +144,9 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa(
dialog_->default_prompt_text())];
}
[alert_ setDelegate:helper_];
- [alert_ setInformativeText:base::SysUTF16ToNSString(dialog_->message_text())];
+ NSString* informative_text =
+ base::SysUTF16ToNSString(dialog_->message_text());
+ [alert_ setInformativeText:informative_text];
[alert_ setMessageText:base::SysUTF16ToNSString(dialog_->title())];
[alert_ addButtonWithTitle:default_button];
if (!one_button) {
@@ -155,6 +159,57 @@ JavaScriptAppModalDialogCocoa::JavaScriptAppModalDialogCocoa(
IDS_JAVASCRIPT_MESSAGEBOX_SUPPRESS_OPTION);
[[alert_ suppressionButton] setTitle:suppression_title];
}
+
+ // Fix RTL dialogs.
+ //
+ // Mac OS X will always display an NSAlert's informative text as LTR. A
+ // workaround is to manually set the informative text as an attributed string
+ // in the implementing NSTextField. 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.
+ //
+ // See http://crbug.com/70806 for more details.
+
+ if (base::i18n::StringContainsStrongRTLChars(dialog_->message_text())) {
+ // 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];
Mark Mentovai 2013/05/17 01:32:57 The indentation is wrong on this line.
Avi (use Gerrit) 2013/05/20 21:55:29 Done.
+
+ // Locate the NSTextField that implements the informative text. This is
+ // actually available as the ivar |_informationField| of the NSAlert, but it
+ // is safer (and more forward-compatible) to search for it in the subviews.
+ NSTextField* informative_text_field = nil;
+ for (NSView* view in [[[alert_ window] contentView] subviews]) {
+ NSTextField* text_field = base::mac::ObjCCast<NSTextField>(view);
+ if ([[text_field stringValue] isEqualTo:informative_text]) {
+ informative_text_field = text_field;
+ break;
+ }
+ }
+ // This may fail in future OS releases, but it will still work for shipped
+ // versions of Chromium.
+ DCHECK(informative_text_field);
+
+ if (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];
jeremy 2013/05/19 10:51:50 Do you also need to manually set the directionalit
Avi (use Gerrit) 2013/05/20 21:55:29 It turns out that I don't need to. As soon as you
+
+ NSDictionary* alignment_attributes =
+ @{ NSParagraphStyleAttributeName : alignment };
+ scoped_nsobject<NSAttributedString> attr_string(
+ [[NSAttributedString alloc] initWithString:informative_text
+ attributes:alignment_attributes]);
+
+ [informative_text_field setAttributedStringValue:attr_string];
+ }
+ }
}
JavaScriptAppModalDialogCocoa::~JavaScriptAppModalDialogCocoa() {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698