OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/location_bar/security_state_bubble_decoration.h
" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #import "base/mac/mac_util.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 12 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" |
| 13 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 14 #include "chrome/grit/theme_resources.h" |
| 15 #include "skia/ext/skia_utils_mac.h" |
| 16 #import "ui/base/cocoa/nsview_additions.h" |
| 17 #include "ui/base/material_design/material_design_controller.h" |
| 18 #include "ui/gfx/animation/tween.h" |
| 19 #include "ui/gfx/color_palette.h" |
| 20 #include "ui/gfx/font_list.h" |
| 21 #include "ui/gfx/image/image_skia_util_mac.h" |
| 22 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 23 #include "ui/gfx/text_elider.h" |
| 24 |
| 25 // TODO(spqchan): Decorations that don't fit in the available space are |
| 26 // omitted. See crbug.com/638427. |
| 27 |
| 28 namespace { |
| 29 |
| 30 // This is used to increase the right margin of this decoration. |
| 31 const CGFloat kRightSideMargin = 1.0; |
| 32 |
| 33 // Padding between the icon and label. |
| 34 CGFloat kIconLabelPadding = 4.0; |
| 35 |
| 36 // Inset for the background. |
| 37 const CGFloat kBackgroundYInset = 4.0; |
| 38 |
| 39 // The offset of the text's baseline on a retina screen. |
| 40 const CGFloat kRetinaBaselineOffset = 0.5; |
| 41 |
| 42 // The info-bubble point should look like it points to the bottom of the lock |
| 43 // icon. Determined with Pixie.app. |
| 44 const CGFloat kPageInfoBubblePointYOffset = 6.0; |
| 45 |
| 46 // Minimum acceptable width for the ev bubble. |
| 47 const CGFloat kMinElidedBubbleWidth = 150.0; |
| 48 |
| 49 // Maximum amount of available space to make the bubble, subject to |
| 50 // |kMinElidedBubbleWidth|. |
| 51 const float kMaxBubbleFraction = 0.5; |
| 52 |
| 53 // The base text color used for non-MD. The color tuples are stolen from |
| 54 // location_bar_view_gtk.cc. |
| 55 const SkColor kBaseTextColor = SkColorSetRGB(0x07, 0x95, 0x00); |
| 56 |
| 57 // Duration of animation in ms. |
| 58 const NSTimeInterval kInAnimationDuration = 330; |
| 59 const NSTimeInterval kOutAnimationDuration = 250; |
| 60 |
| 61 // Transformation values at the beginning of the animation. |
| 62 const CGFloat kStartScale = 0.25; |
| 63 const CGFloat kStartx_offset = -15.0; |
| 64 |
| 65 } // namespace |
| 66 |
| 67 ////////////////////////////////////////////////////////////////// |
| 68 // SecurityStateBubbleDecoration, public: |
| 69 |
| 70 SecurityStateBubbleDecoration::SecurityStateBubbleDecoration( |
| 71 LocationIconDecoration* location_icon, |
| 72 LocationBarViewMac* owner) |
| 73 : location_icon_(location_icon), |
| 74 label_color_(gfx::kGoogleGreen700), |
| 75 image_fade_(true), |
| 76 animation_(this), |
| 77 owner_(owner), |
| 78 disable_animations_during_testing_(false) { |
| 79 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 80 // On Retina the text label is 1px above the Omnibox textfield's text |
| 81 // baseline. If the Omnibox textfield also drew the label the baselines |
| 82 // would align. |
| 83 SetRetinaBaselineOffset(kRetinaBaselineOffset); |
| 84 |
| 85 base::scoped_nsobject<NSMutableParagraphStyle> style( |
| 86 [[NSMutableParagraphStyle alloc] init]); |
| 87 [style setLineBreakMode:NSLineBreakByClipping]; |
| 88 [attributes_ setObject:style forKey:NSParagraphStyleAttributeName]; |
| 89 animation_.SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN); |
| 90 } else { |
| 91 SetTextColor(skia::SkColorToSRGBNSColor(kBaseTextColor)); |
| 92 } |
| 93 } |
| 94 |
| 95 SecurityStateBubbleDecoration::~SecurityStateBubbleDecoration() { |
| 96 // Just in case the timer is still holding onto the animation object, force |
| 97 // cleanup so it can't get back to |this|. |
| 98 } |
| 99 |
| 100 void SecurityStateBubbleDecoration::SetFullLabel(NSString* label) { |
| 101 full_label_.reset([label copy]); |
| 102 SetLabel(full_label_); |
| 103 } |
| 104 |
| 105 void SecurityStateBubbleDecoration::SetLabelColor(SkColor color) { |
| 106 label_color_ = color; |
| 107 } |
| 108 |
| 109 void SecurityStateBubbleDecoration::AnimateIn(bool image_fade) { |
| 110 image_fade_ = image_fade; |
| 111 if (HasAnimatedIn()) |
| 112 animation_.Reset(); |
| 113 |
| 114 animation_.SetSlideDuration(kInAnimationDuration); |
| 115 animation_.Show(); |
| 116 } |
| 117 |
| 118 void SecurityStateBubbleDecoration::AnimateOut() { |
| 119 if (!HasAnimatedIn()) |
| 120 return; |
| 121 |
| 122 animation_.SetSlideDuration(kOutAnimationDuration); |
| 123 animation_.Hide(); |
| 124 } |
| 125 |
| 126 bool SecurityStateBubbleDecoration::HasAnimatedIn() const { |
| 127 return animation_.IsShowing() && animation_.GetCurrentValue() == 1.0; |
| 128 } |
| 129 |
| 130 bool SecurityStateBubbleDecoration::HasAnimatedOut() const { |
| 131 return !animation_.IsShowing() && animation_.GetCurrentValue() == 0.0; |
| 132 } |
| 133 |
| 134 bool SecurityStateBubbleDecoration::AnimatingOut() const { |
| 135 return !animation_.IsShowing() && animation_.GetCurrentValue() != 0.0; |
| 136 } |
| 137 |
| 138 ////////////////////////////////////////////////////////////////// |
| 139 // SecurityStateBubbleDecoration::LocationBarDecoration: |
| 140 |
| 141 CGFloat SecurityStateBubbleDecoration::GetWidthForSpace(CGFloat width) { |
| 142 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 143 return GetWidthForText(width); |
| 144 |
| 145 CGFloat location_icon_width = location_icon_->GetWidthForSpace(width); |
| 146 CGFloat text_width = GetWidthForText(width) - location_icon_width; |
| 147 return (text_width * GetAnimationProgress()) + location_icon_width; |
| 148 } |
| 149 |
| 150 void SecurityStateBubbleDecoration::DrawInFrame(NSRect frame, |
| 151 NSView* control_view) { |
| 152 const NSRect decoration_frame = NSInsetRect(frame, 0.0, kBackgroundYInset); |
| 153 CGFloat text_offset = NSMinX(decoration_frame); |
| 154 if (image_) { |
| 155 // The image should fade in if we're animating in. |
| 156 CGFloat image_alpha = |
| 157 image_fade_ && animation_.IsShowing() ? GetAnimationProgress() : 1.0; |
| 158 |
| 159 // Center the image vertically. |
| 160 const NSSize image_size = [image_ size]; |
| 161 NSRect image_rect = decoration_frame; |
| 162 image_rect.origin.y += |
| 163 std::floor((NSHeight(decoration_frame) - image_size.height) / 2.0); |
| 164 image_rect.size = image_size; |
| 165 [image_ drawInRect:image_rect |
| 166 fromRect:NSZeroRect // Entire image |
| 167 operation:NSCompositeSourceOver |
| 168 fraction:image_alpha |
| 169 respectFlipped:YES |
| 170 hints:nil]; |
| 171 text_offset = NSMaxX(image_rect) + kIconLabelPadding; |
| 172 } |
| 173 |
| 174 // Set the text color and draw the text. |
| 175 if (label_) { |
| 176 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 177 NSColor* text_color = |
| 178 in_dark_mode ? skia::SkColorToSRGBNSColor(kMaterialDarkModeTextColor) |
| 179 : GetBackgroundBorderColor(); |
| 180 SetTextColor(text_color); |
| 181 |
| 182 // Transform the coordinate system to adjust the baseline on Retina. |
| 183 // This is the only way to get fractional adjustments. |
| 184 // gfx::ScopedNSGraphicsContextSaveGState save_graphics_state; |
| 185 CGFloat line_width = [control_view cr_lineWidth]; |
| 186 if (line_width < 1) { |
| 187 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 188 [transform translateXBy:0 yBy:kRetinaBaselineOffset]; |
| 189 [transform concat]; |
| 190 } |
| 191 |
| 192 base::scoped_nsobject<NSAttributedString> text([[NSAttributedString alloc] |
| 193 initWithString:label_ |
| 194 attributes:attributes_]); |
| 195 |
| 196 // Calculate the text frame based on the text height and offsets. |
| 197 NSRect text_rect = frame; |
| 198 CGFloat textHeight = [text size].height; |
| 199 |
| 200 text_rect.origin.x = text_offset; |
| 201 text_rect.origin.y = std::round(NSMidY(text_rect) - textHeight / 2.0) - 1; |
| 202 text_rect.size.width = NSMaxX(decoration_frame) - NSMinX(text_rect); |
| 203 text_rect.size.height = textHeight; |
| 204 |
| 205 NSAffineTransform* transform = [NSAffineTransform transform]; |
| 206 CGFloat progress = GetAnimationProgress(); |
| 207 |
| 208 // Apply transformations so that the text animation: |
| 209 // - Scales from 0.75 to 1. |
| 210 // - Translates the X position to its origin after it got scaled, and |
| 211 // before moving in a position from from -15 to 0 |
| 212 // - Translates the Y position so that the text is centered vertically. |
| 213 double scale = gfx::Tween::DoubleValueBetween(progress, kStartScale, 1.0); |
| 214 |
| 215 double x_origin_offset = NSMinX(text_rect) * (1 - scale); |
| 216 double y_origin_offset = NSMinY(text_rect) * (1 - scale); |
| 217 double x_offset = |
| 218 gfx::Tween::DoubleValueBetween(progress, kStartx_offset, 0); |
| 219 double y_offset = NSHeight(text_rect) * (1 - scale) / 2.0; |
| 220 |
| 221 [transform translateXBy:x_offset + x_origin_offset |
| 222 yBy:y_offset + y_origin_offset]; |
| 223 [transform scaleBy:scale]; |
| 224 [transform concat]; |
| 225 |
| 226 // Draw the label. |
| 227 [text drawInRect:text_rect]; |
| 228 |
| 229 // Draw the divider. |
| 230 NSBezierPath* line = [NSBezierPath bezierPath]; |
| 231 [line setLineWidth:line_width]; |
| 232 [line moveToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 233 NSMinY(decoration_frame))]; |
| 234 [line lineToPoint:NSMakePoint(NSMaxX(decoration_frame) - DividerPadding(), |
| 235 NSMaxY(decoration_frame))]; |
| 236 |
| 237 NSColor* divider_color = GetDividerColor(in_dark_mode); |
| 238 CGFloat divider_alpha = |
| 239 [divider_color alphaComponent] * GetAnimationProgress(); |
| 240 divider_color = [divider_color colorWithAlphaComponent:divider_alpha]; |
| 241 [divider_color set]; |
| 242 [line stroke]; |
| 243 |
| 244 // Revert the transformation. |
| 245 [transform invert]; |
| 246 [transform concat]; |
| 247 } |
| 248 } |
| 249 |
| 250 void SecurityStateBubbleDecoration::DrawWithBackgroundInFrame( |
| 251 NSRect background_frame, |
| 252 NSRect frame, |
| 253 NSView* control_view) { |
| 254 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 255 BubbleDecoration::DrawWithBackgroundInFrame(background_frame, frame, |
| 256 control_view); |
| 257 return; |
| 258 } |
| 259 |
| 260 NSRect rect = NSInsetRect(background_frame, 0, 3); |
| 261 rect.size.width -= kRightSideMargin; |
| 262 |
| 263 CGFloat line_width = [control_view cr_lineWidth]; |
| 264 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 265 // Only adjust the path rect by 1/2 the line width if it's going to be |
| 266 // stroked (so that the stroke lines fall along pixel lines). |
| 267 if (!in_dark_mode) { |
| 268 rect = NSInsetRect(rect, line_width / 2., line_width / 2.); |
| 269 } |
| 270 |
| 271 DrawInFrame(frame, control_view); |
| 272 } |
| 273 |
| 274 // Pass mouse operations through to location icon. |
| 275 bool SecurityStateBubbleDecoration::IsDraggable() { |
| 276 return location_icon_->IsDraggable(); |
| 277 } |
| 278 |
| 279 NSPasteboard* SecurityStateBubbleDecoration::GetDragPasteboard() { |
| 280 return location_icon_->GetDragPasteboard(); |
| 281 } |
| 282 |
| 283 NSImage* SecurityStateBubbleDecoration::GetDragImage() { |
| 284 return location_icon_->GetDragImage(); |
| 285 } |
| 286 |
| 287 NSRect SecurityStateBubbleDecoration::GetDragImageFrame(NSRect frame) { |
| 288 return GetImageRectInFrame(frame); |
| 289 } |
| 290 |
| 291 bool SecurityStateBubbleDecoration::OnMousePressed(NSRect frame, |
| 292 NSPoint location) { |
| 293 return location_icon_->OnMousePressed(frame, location); |
| 294 } |
| 295 |
| 296 bool SecurityStateBubbleDecoration::AcceptsMousePress() { |
| 297 return true; |
| 298 } |
| 299 |
| 300 NSPoint SecurityStateBubbleDecoration::GetBubblePointInFrame(NSRect frame) { |
| 301 NSRect image_rect = GetImageRectInFrame(frame); |
| 302 return NSMakePoint(NSMidX(image_rect), |
| 303 NSMaxY(image_rect) - kPageInfoBubblePointYOffset); |
| 304 } |
| 305 |
| 306 ////////////////////////////////////////////////////////////////// |
| 307 // SecurityStateBubbleDecoration::BubbleDecoration: |
| 308 |
| 309 NSColor* SecurityStateBubbleDecoration::GetBackgroundBorderColor() { |
| 310 return skia::SkColorToSRGBNSColor( |
| 311 SkColorSetA(label_color_, 255.0 * GetAnimationProgress())); |
| 312 } |
| 313 |
| 314 ui::NinePartImageIds SecurityStateBubbleDecoration::GetBubbleImageIds() { |
| 315 return IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE); |
| 316 } |
| 317 |
| 318 NSColor* SecurityStateBubbleDecoration::GetDarkModeTextColor() { |
| 319 return [NSColor whiteColor]; |
| 320 } |
| 321 |
| 322 ////////////////////////////////////////////////////////////////// |
| 323 // SecurityStateBubbleDecoration::AnimationDelegate: |
| 324 |
| 325 void SecurityStateBubbleDecoration::AnimationProgressed( |
| 326 const gfx::Animation* animation) { |
| 327 owner_->Layout(); |
| 328 } |
| 329 |
| 330 ////////////////////////////////////////////////////////////////// |
| 331 // SecurityStateBubbleDecoration, private: |
| 332 |
| 333 CGFloat SecurityStateBubbleDecoration::GetAnimationProgress() const { |
| 334 if (!ui::MaterialDesignController::IsModeMaterial()) |
| 335 return 1.0; |
| 336 |
| 337 if (disable_animations_during_testing_) |
| 338 return 1.0; |
| 339 |
| 340 return animation_.GetCurrentValue(); |
| 341 } |
| 342 |
| 343 CGFloat SecurityStateBubbleDecoration::GetWidthForText(CGFloat width) { |
| 344 // Limit with to not take up too much of the available width, but |
| 345 // also don't let it shrink too much. |
| 346 width = std::max(width * kMaxBubbleFraction, kMinElidedBubbleWidth); |
| 347 |
| 348 // Use the full label if it fits. |
| 349 NSImage* image = GetImage(); |
| 350 const CGFloat all_width = GetWidthForImageAndLabel(image, full_label_); |
| 351 if (all_width <= width) { |
| 352 SetLabel(full_label_); |
| 353 return all_width; |
| 354 } |
| 355 |
| 356 // Width left for laying out the label. |
| 357 const CGFloat width_left = width - GetWidthForImageAndLabel(image, @""); |
| 358 |
| 359 // Middle-elide the label to fit |width_left|. This leaves the |
| 360 // prefix and the trailing country code in place. |
| 361 NSString* elided_label = base::SysUTF16ToNSString(gfx::ElideText( |
| 362 base::SysNSStringToUTF16(full_label_), |
| 363 gfx::FontList(gfx::Font(GetFont())), width_left, gfx::ELIDE_MIDDLE)); |
| 364 |
| 365 // Use the elided label. |
| 366 SetLabel(elided_label); |
| 367 return GetWidthForImageAndLabel(image, elided_label); |
| 368 } |
OLD | NEW |