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

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

Issue 2703863004: [Mac RTL] Correctly position icons and fade text for RTL. (Closed)
Patch Set: pass just the bool Created 3 years, 10 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 | « chrome/browser/ui/cocoa/drag_util.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ui/cocoa/drag_util.mm
diff --git a/chrome/browser/ui/cocoa/drag_util.mm b/chrome/browser/ui/cocoa/drag_util.mm
index 4a3e00eaac9900e710418ed2cb3cb3c34bf21450..0ce31db0ca53cc0a05c1ac8c195a9758c2f1ae60 100644
--- a/chrome/browser/ui/cocoa/drag_util.mm
+++ b/chrome/browser/ui/cocoa/drag_util.mm
@@ -7,9 +7,11 @@
#include <cmath>
#include "base/files/file_path.h"
+#include "base/i18n/rtl.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/ui/cocoa/l10n_util.h"
#include "components/mime_util/mime_util.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/common/webplugininfo.h"
@@ -59,24 +61,33 @@ BOOL IsSupportedFileURL(Profile* profile, const GURL& url) {
// Draws string |title| within box |frame|, positioning it at the origin.
// Truncates text with fading if it is too long to fit horizontally.
// Based on code from GradientButtonCell but simplified where possible.
-void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) {
+void DrawTruncatedTitle(NSAttributedString* title,
+ NSRect frame,
+ bool is_title_rtl) {
NSSize size = [title size];
if (std::floor(size.width) <= NSWidth(frame)) {
[title drawAtPoint:frame.origin];
return;
}
- // Gradient is about twice our line height long.
+ // The gradient is about twice the line height long.
+ NSRectEdge gradient_edge;
+ if (is_title_rtl)
+ gradient_edge = NSMinXEdge;
+ else
+ gradient_edge = NSMaxXEdge;
+
CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4);
NSRect solid_part, gradient_part;
- NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge);
+ NSDivideRect(frame, &gradient_part, &solid_part, gradient_width,
+ gradient_edge);
CGContextRef context = static_cast<CGContextRef>(
[[NSGraphicsContext currentContext] graphicsPort]);
CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0);
{ // Draw text clipped to frame.
gfx::ScopedNSGraphicsContextSaveGState scoped_state;
[NSBezierPath clipRect:frame];
- [title drawAtPoint:frame.origin];
+ [title drawInRect:frame];
}
NSColor* color = [NSColor blackColor];
@@ -85,10 +96,16 @@ void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) {
[[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]);
// Draw the gradient mask.
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
- [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width,
- NSMinY(frame))
- toPoint:NSMakePoint(NSMaxX(frame),
- NSMinY(frame))
+ CGFloat gradient_x_start, gradient_x_end;
+ if (is_title_rtl) {
+ gradient_x_start = NSMaxX(gradient_part);
+ gradient_x_end = NSMinX(gradient_part);
+ } else {
+ gradient_x_start = NSMinX(gradient_part);
+ gradient_x_end = NSMaxX(gradient_part);
+ }
+ [mask drawFromPoint:NSMakePoint(gradient_x_start, NSMinY(frame))
+ toPoint:NSMakePoint(gradient_x_end, NSMinY(frame))
options:NSGradientDrawsBeforeStartingLocation];
CGContextEndTransparencyLayer(context);
}
@@ -121,7 +138,7 @@ BOOL IsUnsupportedDropData(Profile* profile, id<NSDraggingInfo> info) {
NSImage* DragImageForBookmark(NSImage* favicon,
const base::string16& title,
- CGFloat title_width) {
+ CGFloat drag_image_width) {
// If no favicon, use a default.
if (!favicon) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
@@ -142,25 +159,46 @@ NSImage* DragImageForBookmark(NSImage* favicon,
[[NSAttributedString alloc] initWithString:ns_title attributes:attrs]);
// Set up sizes and locations for rendering.
- const CGFloat kIconMargin = 2.0; // Gap between icon and text.
- CGFloat text_left = [favicon size].width + kIconMargin;
- NSSize drag_image_size = [favicon size];
- NSSize text_size = [rich_title size];
- CGFloat max_text_width = title_width - text_left;
- text_size.width = std::min(text_size.width, max_text_width);
- drag_image_size.width = text_left + text_size.width;
+ const CGFloat kIconPadding = 2.0; // Gap between icon and text.
+ NSRect favicon_rect = {NSZeroPoint, [favicon size]};
+ CGFloat icon_plus_padding_width = NSWidth(favicon_rect) + kIconPadding;
+ CGFloat full_text_width = [rich_title size].width;
+ CGFloat allowed_text_width = drag_image_width - icon_plus_padding_width;
+ CGFloat used_text_width = std::min(full_text_width, allowed_text_width);
+ NSRect full_drag_image_rect = NSMakeRect(
+ 0, 0, icon_plus_padding_width + used_text_width, NSHeight(favicon_rect));
+
+ NSRectEdge icon_edge;
+ if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout())
+ icon_edge = NSMaxXEdge;
+ else
+ icon_edge = NSMinXEdge;
+
+ NSRect icon_rect;
+ NSRect text_plus_padding_rect;
+ NSRect padding_rect;
+ NSRect text_rect;
+
+ // Slice off the icon.
+ NSDivideRect(full_drag_image_rect, &icon_rect, &text_plus_padding_rect,
+ NSWidth(favicon_rect), icon_edge);
+
+ // Slice off the padding.
+ NSDivideRect(text_plus_padding_rect, &padding_rect, &text_rect, kIconPadding,
+ icon_edge);
// Render the drag image.
NSImage* drag_image =
- [[[NSImage alloc] initWithSize:drag_image_size] autorelease];
+ [[[NSImage alloc] initWithSize:full_drag_image_rect.size] autorelease];
[drag_image lockFocus];
- [favicon drawAtPoint:NSZeroPoint
+ [favicon drawAtPoint:icon_rect.origin
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:0.7];
- NSRect target_text_rect = NSMakeRect(text_left, 0,
- text_size.width, drag_image_size.height);
- DrawTruncatedTitle(rich_title, target_text_rect);
+
+ bool is_title_rtl = base::i18n::GetFirstStrongCharacterDirection(title) ==
+ base::i18n::RIGHT_TO_LEFT;
+ DrawTruncatedTitle(rich_title, text_rect, is_title_rtl);
[drag_image unlockFocus];
return drag_image;
« no previous file with comments | « chrome/browser/ui/cocoa/drag_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698