Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/drag_util.h" | 5 #import "chrome/browser/ui/cocoa/drag_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/i18n/rtl.h" | |
| 10 #include "base/mac/scoped_nsobject.h" | 11 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/cocoa/l10n_util.h" | |
| 13 #include "components/mime_util/mime_util.h" | 15 #include "components/mime_util/mime_util.h" |
| 14 #include "content/public/browser/plugin_service.h" | 16 #include "content/public/browser/plugin_service.h" |
| 15 #include "content/public/common/webplugininfo.h" | 17 #include "content/public/common/webplugininfo.h" |
| 16 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 17 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
| 18 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
| 19 #import "third_party/mozilla/NSPasteboard+Utils.h" | 21 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 20 #import "ui/base/dragdrop/cocoa_dnd_util.h" | 22 #import "ui/base/dragdrop/cocoa_dnd_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 24 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 52 return PluginService::GetInstance()->GetPluginInfo( | 54 return PluginService::GetInstance()->GetPluginInfo( |
| 53 -1, // process ID | 55 -1, // process ID |
| 54 MSG_ROUTING_NONE, // routing ID | 56 MSG_ROUTING_NONE, // routing ID |
| 55 profile->GetResourceContext(), url, url::Origin(), mime_type, | 57 profile->GetResourceContext(), url, url::Origin(), mime_type, |
| 56 allow_wildcard, NULL, &plugin, NULL); | 58 allow_wildcard, NULL, &plugin, NULL); |
| 57 } | 59 } |
| 58 | 60 |
| 59 // Draws string |title| within box |frame|, positioning it at the origin. | 61 // Draws string |title| within box |frame|, positioning it at the origin. |
| 60 // Truncates text with fading if it is too long to fit horizontally. | 62 // Truncates text with fading if it is too long to fit horizontally. |
| 61 // Based on code from GradientButtonCell but simplified where possible. | 63 // Based on code from GradientButtonCell but simplified where possible. |
| 62 void DrawTruncatedTitle(NSAttributedString* title, NSRect frame) { | 64 void DrawTruncatedTitle(const base::string16& title16, |
|
erikchen
2017/02/18 01:56:25
I was a little bit confused about why you needed b
Avi (use Gerrit)
2017/02/18 02:17:00
Done.
| |
| 65 NSAttributedString* title, | |
| 66 NSRect frame) { | |
| 63 NSSize size = [title size]; | 67 NSSize size = [title size]; |
| 64 if (std::floor(size.width) <= NSWidth(frame)) { | 68 if (std::floor(size.width) <= NSWidth(frame)) { |
| 65 [title drawAtPoint:frame.origin]; | 69 [title drawAtPoint:frame.origin]; |
| 66 return; | 70 return; |
| 67 } | 71 } |
| 68 | 72 |
| 69 // Gradient is about twice our line height long. | 73 // The gradient is about twice the line height long. |
| 74 NSRectEdge gradient_edge; | |
| 75 bool is_title_rtl = base::i18n::GetFirstStrongCharacterDirection(title16) == | |
| 76 base::i18n::RIGHT_TO_LEFT; | |
| 77 if (is_title_rtl) | |
| 78 gradient_edge = NSMinXEdge; | |
| 79 else | |
| 80 gradient_edge = NSMaxXEdge; | |
| 81 | |
| 70 CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); | 82 CGFloat gradient_width = std::min(size.height * 2, NSWidth(frame) / 4); |
| 71 NSRect solid_part, gradient_part; | 83 NSRect solid_part, gradient_part; |
| 72 NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, NSMaxXEdge); | 84 NSDivideRect(frame, &gradient_part, &solid_part, gradient_width, |
| 85 gradient_edge); | |
| 73 CGContextRef context = static_cast<CGContextRef>( | 86 CGContextRef context = static_cast<CGContextRef>( |
| 74 [[NSGraphicsContext currentContext] graphicsPort]); | 87 [[NSGraphicsContext currentContext] graphicsPort]); |
| 75 CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); | 88 CGContextBeginTransparencyLayerWithRect(context, NSRectToCGRect(frame), 0); |
| 76 { // Draw text clipped to frame. | 89 { // Draw text clipped to frame. |
| 77 gfx::ScopedNSGraphicsContextSaveGState scoped_state; | 90 gfx::ScopedNSGraphicsContextSaveGState scoped_state; |
| 78 [NSBezierPath clipRect:frame]; | 91 [NSBezierPath clipRect:frame]; |
| 79 [title drawAtPoint:frame.origin]; | 92 [title drawInRect:frame]; |
| 80 } | 93 } |
| 81 | 94 |
| 82 NSColor* color = [NSColor blackColor]; | 95 NSColor* color = [NSColor blackColor]; |
| 83 NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; | 96 NSColor* alpha_color = [color colorWithAlphaComponent:0.0]; |
| 84 base::scoped_nsobject<NSGradient> mask( | 97 base::scoped_nsobject<NSGradient> mask( |
| 85 [[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]); | 98 [[NSGradient alloc] initWithStartingColor:color endingColor:alpha_color]); |
| 86 // Draw the gradient mask. | 99 // Draw the gradient mask. |
| 87 CGContextSetBlendMode(context, kCGBlendModeDestinationIn); | 100 CGContextSetBlendMode(context, kCGBlendModeDestinationIn); |
| 88 [mask drawFromPoint:NSMakePoint(NSMaxX(frame) - gradient_width, | 101 CGFloat gradient_x_start, gradient_x_end; |
| 89 NSMinY(frame)) | 102 if (is_title_rtl) { |
| 90 toPoint:NSMakePoint(NSMaxX(frame), | 103 gradient_x_start = NSMaxX(gradient_part); |
| 91 NSMinY(frame)) | 104 gradient_x_end = NSMinX(gradient_part); |
| 105 } else { | |
| 106 gradient_x_start = NSMinX(gradient_part); | |
| 107 gradient_x_end = NSMaxX(gradient_part); | |
| 108 } | |
| 109 [mask drawFromPoint:NSMakePoint(gradient_x_start, NSMinY(frame)) | |
| 110 toPoint:NSMakePoint(gradient_x_end, NSMinY(frame)) | |
| 92 options:NSGradientDrawsBeforeStartingLocation]; | 111 options:NSGradientDrawsBeforeStartingLocation]; |
| 93 CGContextEndTransparencyLayer(context); | 112 CGContextEndTransparencyLayer(context); |
| 94 } | 113 } |
| 95 | 114 |
| 96 } // namespace | 115 } // namespace |
| 97 | 116 |
| 98 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { | 117 GURL GetFileURLFromDropData(id<NSDraggingInfo> info) { |
| 99 if ([[info draggingPasteboard] containsURLDataConvertingTextToURL:YES]) { | 118 if ([[info draggingPasteboard] containsURLDataConvertingTextToURL:YES]) { |
| 100 GURL url; | 119 GURL url; |
| 101 ui::PopulateURLAndTitleFromPasteboard(&url, | 120 ui::PopulateURLAndTitleFromPasteboard(&url, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 114 if (!url.is_empty()) { | 133 if (!url.is_empty()) { |
| 115 // If dragging a file, only allow dropping supported file types (that the | 134 // If dragging a file, only allow dropping supported file types (that the |
| 116 // web view can display). | 135 // web view can display). |
| 117 return !IsSupportedFileURL(profile, url); | 136 return !IsSupportedFileURL(profile, url); |
| 118 } | 137 } |
| 119 return NO; | 138 return NO; |
| 120 } | 139 } |
| 121 | 140 |
| 122 NSImage* DragImageForBookmark(NSImage* favicon, | 141 NSImage* DragImageForBookmark(NSImage* favicon, |
| 123 const base::string16& title, | 142 const base::string16& title, |
| 124 CGFloat title_width) { | 143 CGFloat drag_image_width) { |
| 125 // If no favicon, use a default. | 144 // If no favicon, use a default. |
| 126 if (!favicon) { | 145 if (!favicon) { |
| 127 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 146 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 128 favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); | 147 favicon = rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).ToNSImage(); |
| 129 } | 148 } |
| 130 | 149 |
| 131 // If no title, just use icon. | 150 // If no title, just use icon. |
| 132 if (title.empty()) | 151 if (title.empty()) |
| 133 return favicon; | 152 return favicon; |
| 134 NSString* ns_title = base::SysUTF16ToNSString(title); | 153 NSString* ns_title = base::SysUTF16ToNSString(title); |
| 135 | 154 |
| 136 // Set the look of the title. | 155 // Set the look of the title. |
| 137 NSDictionary* attrs = | 156 NSDictionary* attrs = |
| 138 [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: | 157 [NSDictionary dictionaryWithObject:[NSFont systemFontOfSize: |
| 139 [NSFont smallSystemFontSize]] | 158 [NSFont smallSystemFontSize]] |
| 140 forKey:NSFontAttributeName]; | 159 forKey:NSFontAttributeName]; |
| 141 base::scoped_nsobject<NSAttributedString> rich_title( | 160 base::scoped_nsobject<NSAttributedString> rich_title( |
| 142 [[NSAttributedString alloc] initWithString:ns_title attributes:attrs]); | 161 [[NSAttributedString alloc] initWithString:ns_title attributes:attrs]); |
| 143 | 162 |
| 144 // Set up sizes and locations for rendering. | 163 // Set up sizes and locations for rendering. |
| 145 const CGFloat kIconMargin = 2.0; // Gap between icon and text. | 164 const CGFloat kIconPadding = 2.0; // Gap between icon and text. |
| 146 CGFloat text_left = [favicon size].width + kIconMargin; | 165 NSRect favicon_rect = {NSZeroPoint, [favicon size]}; |
| 147 NSSize drag_image_size = [favicon size]; | 166 CGFloat icon_plus_padding_width = NSWidth(favicon_rect) + kIconPadding; |
| 148 NSSize text_size = [rich_title size]; | 167 CGFloat full_text_width = [rich_title size].width; |
| 149 CGFloat max_text_width = title_width - text_left; | 168 CGFloat allowed_text_width = drag_image_width - icon_plus_padding_width; |
| 150 text_size.width = std::min(text_size.width, max_text_width); | 169 CGFloat used_text_width = std::min(full_text_width, allowed_text_width); |
| 151 drag_image_size.width = text_left + text_size.width; | 170 NSRect full_drag_image_rect = NSMakeRect( |
| 171 0, 0, icon_plus_padding_width + used_text_width, NSHeight(favicon_rect)); | |
| 172 | |
| 173 NSRectEdge icon_edge; | |
| 174 if (cocoa_l10n_util::ShouldDoExperimentalRTLLayout()) | |
| 175 icon_edge = NSMaxXEdge; | |
| 176 else | |
| 177 icon_edge = NSMinXEdge; | |
| 178 | |
| 179 NSRect icon_rect; | |
| 180 NSRect text_plus_padding_rect; | |
| 181 NSRect padding_rect; | |
| 182 NSRect text_rect; | |
| 183 | |
| 184 // Slice off the icon. | |
| 185 NSDivideRect(full_drag_image_rect, &icon_rect, &text_plus_padding_rect, | |
| 186 NSWidth(favicon_rect), icon_edge); | |
| 187 | |
| 188 // Slice off the padding. | |
| 189 NSDivideRect(text_plus_padding_rect, &padding_rect, &text_rect, kIconPadding, | |
| 190 icon_edge); | |
| 152 | 191 |
| 153 // Render the drag image. | 192 // Render the drag image. |
| 154 NSImage* drag_image = | 193 NSImage* drag_image = |
| 155 [[[NSImage alloc] initWithSize:drag_image_size] autorelease]; | 194 [[[NSImage alloc] initWithSize:full_drag_image_rect.size] autorelease]; |
| 156 [drag_image lockFocus]; | 195 [drag_image lockFocus]; |
| 157 [favicon drawAtPoint:NSZeroPoint | 196 [favicon drawAtPoint:icon_rect.origin |
| 158 fromRect:NSZeroRect | 197 fromRect:NSZeroRect |
| 159 operation:NSCompositeSourceOver | 198 operation:NSCompositeSourceOver |
| 160 fraction:0.7]; | 199 fraction:0.7]; |
| 161 NSRect target_text_rect = NSMakeRect(text_left, 0, | 200 |
| 162 text_size.width, drag_image_size.height); | 201 DrawTruncatedTitle(title, rich_title, text_rect); |
| 163 DrawTruncatedTitle(rich_title, target_text_rect); | |
| 164 [drag_image unlockFocus]; | 202 [drag_image unlockFocus]; |
| 165 | 203 |
| 166 return drag_image; | 204 return drag_image; |
| 167 } | 205 } |
| 168 | 206 |
| 169 } // namespace drag_util | 207 } // namespace drag_util |
| OLD | NEW |