From 18831714d344d489908705d374cf8c416afa603d Mon Sep 17 00:00:00 2001 From: jbbegue Date: Fri, 7 Aug 2015 03:15:47 -0700 Subject: [PATCH] Upstreaming browser/ui/uikit_ui_util from iOS. Added AddSameCenterXConstraint which add constraints to a parent view to horizontally align two subviews. Added BOOL IsRTLUILayout() which take into account RTL pseudo language. This will be removed once base::i18n::IsRTL() supports it. BUG=None Review URL: https://codereview.chromium.org/1280913003 Cr-Commit-Position: refs/heads/master@{#342328} --- ios/chrome/browser/ui/uikit_ui_util.h | 13 +++++++++++++ ios/chrome/browser/ui/uikit_ui_util.mm | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/ios/chrome/browser/ui/uikit_ui_util.h b/ios/chrome/browser/ui/uikit_ui_util.h index 56bad293000e..5bffe3906000 100644 --- a/ios/chrome/browser/ui/uikit_ui_util.h +++ b/ios/chrome/browser/ui/uikit_ui_util.h @@ -142,6 +142,12 @@ void ApplyVisualConstraintsWithMetrics(NSArray* constraints, // |subview| must be a subview of |parentView|. void AddSameCenterXConstraint(UIView* parentView, UIView* subview); +// Adds a constraint that |subview1| and |subview2| are center aligned +// horizontally on |parentView|. +// |subview1| and |subview2| must be subview of |parentView|. +void AddSameCenterXConstraint(UIView *parentView, UIView *subview1, + UIView *subview2); + // Adds a constraint that |subview| is center aligned vertically in // |parentView|. // |subview| must be a subview of |parentView|. @@ -153,6 +159,13 @@ void AddSameCenterYConstraint(UIView* parentView, UIView* subview); void AddSameCenterYConstraint(UIView* parentView, UIView* subview1, UIView* subview2); +// Whether the UI is configured for right to left layout. +// The implementation will use the local in order to get the UI layout direction +// for version of iOS under 9. +// TODO(jbbegue): Use base::i18n::IsRTL() instead when it will support RTL +// pseudo language. Remove that method once base::i18n::IsRTL() is fixed. +// crbug/514625. +BOOL IsRTLUILayout(); // Returns true if the main application window's rootViewController is a compact // iPad horizontal size class. diff --git a/ios/chrome/browser/ui/uikit_ui_util.mm b/ios/chrome/browser/ui/uikit_ui_util.mm index 3f8d5b70fc8c..9b9d8f5f2291 100644 --- a/ios/chrome/browser/ui/uikit_ui_util.mm +++ b/ios/chrome/browser/ui/uikit_ui_util.mm @@ -457,6 +457,21 @@ void AddSameCenterXConstraint(UIView* parentView, UIView* subview) { constant:0]]; } +void AddSameCenterXConstraint(UIView *parentView, UIView *subview1, + UIView *subview2) { + DCHECK_EQ(parentView, [subview1 superview]); + DCHECK_EQ(parentView, [subview2 superview]); + DCHECK_NE(subview1, subview2); + [parentView addConstraint:[NSLayoutConstraint + constraintWithItem:subview1 + attribute:NSLayoutAttributeCenterX + relatedBy:NSLayoutRelationEqual + toItem:subview2 + attribute:NSLayoutAttributeCenterX + multiplier:1 + constant:0]]; +} + void AddSameCenterYConstraint(UIView* parentView, UIView* subview) { DCHECK_EQ(parentView, [subview superview]); [parentView addConstraint:[NSLayoutConstraint @@ -499,3 +514,22 @@ bool IsCompactTablet() { bool IsCompactTabletSizeClass(UIUserInterfaceSizeClass sizeClass) { return IsIPadIdiom() && sizeClass == UIUserInterfaceSizeClassCompact; } + +BOOL IsRTLUILayout() { + if (base::ios::IsRunningOnIOS9OrLater()) { +#if __IPHONE_9_0 + // Calling this method is better than using the locale on iOS9 since it will + // work with the right to left pseudolanguage. + return [UIView userInterfaceLayoutDirectionForSemanticContentAttribute: + UISemanticContentAttributeUnspecified] == + UIUserInterfaceLayoutDirectionRightToLeft; +#endif + } + // Using NSLocale instead of base::i18n::IsRTL() in order to take into account + // right to left pseudolanguage correctly (which base::i18n::IsRTL() doesn't). + return + [NSLocale + characterDirectionForLanguage: + [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]] == + NSLocaleLanguageDirectionRightToLeft; +} -- 2.11.4.GIT