Commit 36c124b2 authored by lgrey's avatar lgrey Committed by Commit bot

[Mac] RTL-aware rendering for BubbleDecoration and SecurityStateBubbleDecoration

This is almost the same exact code, so it might be worthwhile to find a way
to abstract it out, but I think it's probably not in scope for this CL.

BUG=648557

Review-Url: https://codereview.chromium.org/2563593003
Cr-Commit-Position: refs/heads/master@{#437631}
parent 7038e9c5
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/themed_window.h" #import "chrome/browser/ui/cocoa/themed_window.h"
#include "skia/ext/skia_utils_mac.h" #include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/nsview_additions.h" #import "ui/base/cocoa/nsview_additions.h"
...@@ -108,12 +109,20 @@ NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) { ...@@ -108,12 +109,20 @@ NSRect BubbleDecoration::GetBackgroundFrame(NSRect frame) {
void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset); const NSRect decoration_frame = NSInsetRect(frame, 0.0, kImageFrameYInset);
CGFloat text_offset = NSMinX(decoration_frame); CGFloat text_left_offset = NSMinX(decoration_frame);
CGFloat text_right_offset = NSMaxX(decoration_frame);
const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout();
if (image_) { if (image_) {
// Center the image vertically. // Center the image vertically.
const NSSize image_size = [image_ size]; const NSSize image_size = [image_ size];
NSRect image_rect = decoration_frame; NSRect image_rect = decoration_frame;
image_rect.origin.x += kLeftSidePadding; if (is_rtl) {
image_rect.origin.x =
NSMaxX(decoration_frame) - image_size.width - kLeftSidePadding;
} else {
image_rect.origin.x += kLeftSidePadding;
}
image_rect.origin.y += image_rect.origin.y +=
std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0);
image_rect.size = image_size; image_rect.size = image_size;
...@@ -123,16 +132,21 @@ void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { ...@@ -123,16 +132,21 @@ void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
fraction:1.0 fraction:1.0
respectFlipped:YES respectFlipped:YES
hints:nil]; hints:nil];
text_offset = NSMaxX(image_rect) + kIconLabelPadding; if (is_rtl)
text_right_offset = NSMinX(image_rect) - kIconLabelPadding;
else
text_left_offset = NSMaxX(image_rect) + kIconLabelPadding;
} }
// Draw the divider and set the text color. // Draw the divider and set the text color.
NSBezierPath* line = [NSBezierPath bezierPath]; NSBezierPath* line = [NSBezierPath bezierPath];
const CGFloat divider_x_position =
is_rtl ? NSMinX(decoration_frame) + DividerPadding()
: NSMaxX(decoration_frame) - DividerPadding();
[line setLineWidth:1]; [line setLineWidth:1];
[line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), [line moveToPoint:NSMakePoint(divider_x_position, NSMinY(decoration_frame))];
NSMinY(decoration_frame))]; [line lineToPoint:NSMakePoint(divider_x_position, NSMaxY(decoration_frame))];
[line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(),
NSMaxY(decoration_frame))];
bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme];
[GetDividerColor(in_dark_mode) set]; [GetDividerColor(in_dark_mode) set];
...@@ -144,8 +158,8 @@ void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) { ...@@ -144,8 +158,8 @@ void BubbleDecoration::DrawInFrame(NSRect frame, NSView* control_view) {
if (label_) { if (label_) {
NSRect text_rect = frame; NSRect text_rect = frame;
text_rect.origin.x = text_offset; text_rect.origin.x = text_left_offset;
text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); text_rect.size.width = text_right_offset - text_left_offset;
// Transform the coordinate system to adjust the baseline on Retina. This is // Transform the coordinate system to adjust the baseline on Retina. This is
// the only way to get fractional adjustments. // the only way to get fractional adjustments.
gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState; gfx::ScopedNSGraphicsContextSaveGState saveGraphicsState;
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#import "base/mac/mac_util.h" #import "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h"
#import "chrome/browser/ui/cocoa/themed_window.h" #import "chrome/browser/ui/cocoa/themed_window.h"
...@@ -59,7 +60,7 @@ const NSTimeInterval kOutAnimationDuration = 250; ...@@ -59,7 +60,7 @@ const NSTimeInterval kOutAnimationDuration = 250;
// Transformation values at the beginning of the animation. // Transformation values at the beginning of the animation.
const CGFloat kStartScale = 0.25; const CGFloat kStartScale = 0.25;
const CGFloat kStartx_offset = -15.0; const CGFloat kStartx_offset = 15.0;
} // namespace } // namespace
...@@ -150,7 +151,9 @@ CGFloat SecurityStateBubbleDecoration::GetWidthForSpace(CGFloat width) { ...@@ -150,7 +151,9 @@ CGFloat SecurityStateBubbleDecoration::GetWidthForSpace(CGFloat width) {
void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
NSView* control_view) { NSView* control_view) {
const NSRect decoration_frame = NSInsetRect(frame, 0.0, kBackgroundYInset); const NSRect decoration_frame = NSInsetRect(frame, 0.0, kBackgroundYInset);
CGFloat text_offset = NSMinX(decoration_frame); CGFloat text_left_offset = NSMinX(decoration_frame);
CGFloat text_right_offset = NSMaxX(decoration_frame);
const BOOL is_rtl = cocoa_l10n_util::ShouldDoExperimentalRTLLayout();
if (image_) { if (image_) {
// The image should fade in if we're animating in. // The image should fade in if we're animating in.
CGFloat image_alpha = CGFloat image_alpha =
...@@ -161,7 +164,12 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, ...@@ -161,7 +164,12 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
NSRect image_rect = decoration_frame; NSRect image_rect = decoration_frame;
image_rect.origin.y += image_rect.origin.y +=
std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0);
image_rect.origin.x += kLeftSidePadding; if (is_rtl) {
image_rect.origin.x =
NSMaxX(decoration_frame) - image_size.width - kLeftSidePadding;
} else {
image_rect.origin.x += kLeftSidePadding;
}
image_rect.size = image_size; image_rect.size = image_size;
[image_ drawInRect:image_rect [image_ drawInRect:image_rect
fromRect:NSZeroRect // Entire image fromRect:NSZeroRect // Entire image
...@@ -169,7 +177,10 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, ...@@ -169,7 +177,10 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
fraction:image_alpha fraction:image_alpha
respectFlipped:YES respectFlipped:YES
hints:nil]; hints:nil];
text_offset = NSMaxX(image_rect) + kIconLabelPadding; if (is_rtl)
text_right_offset = NSMinX(image_rect) - kIconLabelPadding;
else
text_left_offset = NSMaxX(image_rect) + kIconLabelPadding;
} }
// Set the text color and draw the text. // Set the text color and draw the text.
...@@ -198,9 +209,9 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, ...@@ -198,9 +209,9 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
NSRect text_rect = frame; NSRect text_rect = frame;
CGFloat textHeight = [text size].height; CGFloat textHeight = [text size].height;
text_rect.origin.x = text_offset; text_rect.origin.x = text_left_offset;
text_rect.origin.y = std::round(NSMidY(text_rect) - textHeight / 2.0) - 1; text_rect.origin.y = std::round(NSMidY(text_rect) - textHeight / 2.0) - 1;
text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); text_rect.size.width = text_right_offset - text_left_offset;
text_rect.size.height = textHeight; text_rect.size.height = textHeight;
NSAffineTransform* transform = [NSAffineTransform transform]; NSAffineTransform* transform = [NSAffineTransform transform];
...@@ -215,8 +226,9 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, ...@@ -215,8 +226,9 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
double x_origin_offset = NSMinX(text_rect) * (1 - scale); double x_origin_offset = NSMinX(text_rect) * (1 - scale);
double y_origin_offset = NSMinY(text_rect) * (1 - scale); double y_origin_offset = NSMinY(text_rect) * (1 - scale);
double start_x_offset = is_rtl ? -kStartx_offset : kStartx_offset;
double x_offset = double x_offset =
gfx::Tween::DoubleValueBetween(progress, kStartx_offset, 0); gfx::Tween::DoubleValueBetween(progress, start_x_offset, 0);
double y_offset = NSHeight(text_rect) * (1 - scale) / 2.0; double y_offset = NSHeight(text_rect) * (1 - scale) / 2.0;
[transform translateXBy:x_offset + x_origin_offset [transform translateXBy:x_offset + x_origin_offset
...@@ -229,11 +241,14 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, ...@@ -229,11 +241,14 @@ void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame,
// Draw the divider. // Draw the divider.
if (state() == DecorationMouseState::NONE && !active()) { if (state() == DecorationMouseState::NONE && !active()) {
const CGFloat divider_x_position =
is_rtl ? NSMinX(decoration_frame) + DividerPadding()
: NSMaxX(decoration_frame) - DividerPadding();
NSBezierPath* line = [NSBezierPath bezierPath]; NSBezierPath* line = [NSBezierPath bezierPath];
[line setLineWidth:line_width]; [line setLineWidth:line_width];
[line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), [line moveToPoint:NSMakePoint(divider_x_position,
NSMinY(decoration_frame))]; NSMinY(decoration_frame))];
[line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), [line lineToPoint:NSMakePoint(divider_x_position,
NSMaxY(decoration_frame))]; NSMaxY(decoration_frame))];
NSColor* divider_color = GetDividerColor(in_dark_mode); NSColor* divider_color = GetDividerColor(in_dark_mode);
......
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