OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/location_bar/secure_verbose_bubble_decoration.h
" |
| 6 |
| 7 #import "base/logging.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
| 10 #import "chrome/browser/ui/cocoa/location_bar/location_icon_decoration.h" |
| 11 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 12 #include "grit/theme_resources.h" |
| 13 #include "skia/ext/skia_utils_mac.h" |
| 14 #import "ui/base/cocoa/nsview_additions.h" |
| 15 #include "ui/base/material_design/material_design_controller.h" |
| 16 #include "ui/gfx/color_palette.h" |
| 17 #include "ui/gfx/font_list.h" |
| 18 #include "ui/gfx/text_elider.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // This is used to increase the right margin of this decoration. |
| 23 const CGFloat kRightSideMargin = 1.0; |
| 24 |
| 25 // TODO(shess): In general, decorations that don't fit in the |
| 26 // available space are omitted. This one never goes to omitted, it |
| 27 // sticks at 150px, which AFAICT follows the Windows code. Since the |
| 28 // Layout() code doesn't take this into account, it's possible the |
| 29 // control could end up with display artifacts, though things still |
| 30 // work (and don't crash). |
| 31 // http://crbug.com/49822 |
| 32 |
| 33 // Minimum acceptable width for the ev bubble. |
| 34 const CGFloat kMinElidedBubbleWidth = 150.0; |
| 35 |
| 36 // Maximum amount of available space to make the bubble, subject to |
| 37 // |kMinElidedBubbleWidth|. |
| 38 const float kMaxBubbleFraction = 0.5; |
| 39 |
| 40 // The info-bubble point should look like it points to the bottom of the lock |
| 41 // icon. Determined with Pixie.app. |
| 42 const CGFloat kPageInfoBubblePointYOffset = 6.0; |
| 43 |
| 44 // Duration of animation, 0.5 seconds. |
| 45 const NSTimeInterval kAnimationDuration = 0.5; |
| 46 |
| 47 // Interval of the animation timer, 60Hz. |
| 48 const NSTimeInterval kAnimationInterval = 1.0 / 60.0; |
| 49 |
| 50 // TODO(shess): This is ugly, find a better way. Using it right now |
| 51 // so that I can crib from gtk and still be able to see that I'm using |
| 52 // the same values easily. |
| 53 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { |
| 54 DCHECK_LE(rr, 255); |
| 55 DCHECK_LE(bb, 255); |
| 56 DCHECK_LE(gg, 255); |
| 57 return [NSColor colorWithCalibratedRed:static_cast<float>(rr) / 255.0 |
| 58 green:static_cast<float>(gg) / 255.0 |
| 59 blue:static_cast<float>(bb) / 255.0 |
| 60 alpha:1.0]; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 @interface SecureVerboseAnimation : NSObject { |
| 66 @private |
| 67 SecureVerboseBubbleDecoration* owner_; // Weak, owns this. |
| 68 double progress_; // Counter, [0..1], with aninmation progress. |
| 69 NSTimer* timer_; // Animation timer. Owns this, owned by the run loop. |
| 70 } |
| 71 |
| 72 // [0..1], the current progress of the animation. -animationState will return |
| 73 // |kNoAnimation| when progress is <= 0 or >= 1. Useful when state is |
| 74 // |kOpening| or |kClosing| as a multiplier for displaying width. Don't use |
| 75 // to track state transitions, use -animationState instead. |
| 76 @property(readonly, nonatomic) double progress; |
| 77 |
| 78 // Designated initializer. |owner| must not be nil. Animation timer will start |
| 79 // as soon as the object is created. |
| 80 - (id)initWithOwner:(SecureVerboseBubbleDecoration*)owner; |
| 81 |
| 82 // Call when |owner| is going away or the animation needs to be stopped. |
| 83 // Ensures that any dangling references are cleared. Can be called multiple |
| 84 // times. |
| 85 - (void)stopAnimation; |
| 86 |
| 87 - (BOOL)isRunning; |
| 88 |
| 89 @end |
| 90 |
| 91 @implementation SecureVerboseAnimation |
| 92 |
| 93 @synthesize progress = progress_; |
| 94 |
| 95 - (id)initWithOwner:(SecureVerboseBubbleDecoration*)owner { |
| 96 self = [super init]; |
| 97 if (self) { |
| 98 owner_ = owner; |
| 99 timer_ = [NSTimer scheduledTimerWithTimeInterval:kAnimationInterval |
| 100 target:self |
| 101 selector:@selector(timerFired:) |
| 102 userInfo:nil |
| 103 repeats:YES]; |
| 104 } |
| 105 return self; |
| 106 } |
| 107 |
| 108 - (void)dealloc { |
| 109 DCHECK(!timer_); |
| 110 [super dealloc]; |
| 111 } |
| 112 |
| 113 // Clear weak references and stop the timer. |
| 114 - (void)stopAnimation { |
| 115 owner_ = nil; |
| 116 [timer_ invalidate]; |
| 117 timer_ = nil; |
| 118 } |
| 119 |
| 120 - (BOOL)isRunning { |
| 121 return timer_ != nil; |
| 122 } |
| 123 |
| 124 - (void)timerFired:(NSTimer*)timer { |
| 125 // Increment animation progress, normalized to [0..1]. |
| 126 progress_ += kAnimationInterval / kAnimationDuration; |
| 127 progress_ = std::min(progress_, 1.0); |
| 128 owner_->OnAnimationProgressed(); |
| 129 // Stop timer if it has reached the end of its life. |
| 130 if (progress_ >= 1.0) |
| 131 [self stopAnimation]; |
| 132 } |
| 133 |
| 134 @end |
| 135 |
| 136 SecureVerboseBubbleDecoration::SecureVerboseBubbleDecoration( |
| 137 LocationIconDecoration* location_icon, |
| 138 LocationBarViewMac* owner) |
| 139 : location_icon_(location_icon), owner_(owner) { |
| 140 if (ui::MaterialDesignController::IsModeMaterial()) { |
| 141 // On Retina the text label is 1px above the Omnibox textfield's text |
| 142 // baseline. If the Omnibox textfield also drew the label the baselines |
| 143 // would align. |
| 144 SetRetinaBaselineOffset(0.5); |
| 145 } else { |
| 146 // Color tuples stolen from location_bar_view_gtk.cc. |
| 147 SetTextColor(ColorWithRGBBytes(0x07, 0x95, 0x00)); |
| 148 } |
| 149 } |
| 150 |
| 151 SecureVerboseBubbleDecoration::~SecureVerboseBubbleDecoration() {} |
| 152 |
| 153 NSColor* SecureVerboseBubbleDecoration::GetBackgroundBorderColor() { |
| 154 if (label_color_) |
| 155 return label_color_.get(); |
| 156 |
| 157 return skia::SkColorToSRGBNSColor(gfx::kGoogleGreen700); |
| 158 } |
| 159 |
| 160 void SecureVerboseBubbleDecoration::SetFullLabel(NSString* label) { |
| 161 full_label_.reset([label retain]); |
| 162 SetLabel(full_label_); |
| 163 } |
| 164 |
| 165 void SecureVerboseBubbleDecoration::SetLabelColor(NSColor* color) { |
| 166 label_color_.reset([color retain]); |
| 167 } |
| 168 |
| 169 void SecureVerboseBubbleDecoration::OnAnimationProgressed() { |
| 170 owner_->Layout(); |
| 171 } |
| 172 |
| 173 void SecureVerboseBubbleDecoration::StartAnimation() { |
| 174 animation_.reset([[SecureVerboseAnimation alloc] initWithOwner:this]); |
| 175 } |
| 176 |
| 177 void SecureVerboseBubbleDecoration::DrawWithBackgroundInFrame( |
| 178 NSRect background_frame, |
| 179 NSRect frame, |
| 180 NSView* control_view) { |
| 181 if (!ui::MaterialDesignController::IsModeMaterial()) { |
| 182 BubbleDecoration::DrawWithBackgroundInFrame(background_frame, frame, |
| 183 control_view); |
| 184 return; |
| 185 } |
| 186 |
| 187 NSRect rect = NSInsetRect(background_frame, 0, 3); |
| 188 rect.size.width -= kRightSideMargin; |
| 189 |
| 190 CGFloat line_width = [control_view cr_lineWidth]; |
| 191 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; |
| 192 // Only adjust the path rect by 1/2 the line width if it's going to be |
| 193 // stroked (so that the stroke lines fall along pixel lines). |
| 194 if (!in_dark_mode) { |
| 195 rect = NSInsetRect(rect, line_width / 2., line_width / 2.); |
| 196 } |
| 197 |
| 198 DrawInFrame(frame, control_view); |
| 199 } |
| 200 |
| 201 NSPoint SecureVerboseBubbleDecoration::GetBubblePointInFrame(NSRect frame) { |
| 202 NSRect image_rect = GetImageRectInFrame(frame); |
| 203 return NSMakePoint(NSMidX(image_rect), |
| 204 NSMaxY(image_rect) - kPageInfoBubblePointYOffset); |
| 205 } |
| 206 |
| 207 CGFloat SecureVerboseBubbleDecoration::GetWidthForSpace(CGFloat width) { |
| 208 CGFloat progress = (!animation_.get() || ![animation_ isRunning]) |
| 209 ? 1.0 |
| 210 : [animation_ progress]; |
| 211 |
| 212 // Limit with to not take up too much of the available width, but |
| 213 // also don't let it shrink too much. |
| 214 width = std::max(width * kMaxBubbleFraction, kMinElidedBubbleWidth); |
| 215 |
| 216 // Use the full label if it fits. |
| 217 NSImage* image = GetImage(); |
| 218 const CGFloat all_width = GetWidthForImageAndLabel(image, full_label_); |
| 219 if (all_width <= width) { |
| 220 SetLabel(full_label_); |
| 221 return all_width * progress; |
| 222 } |
| 223 |
| 224 // Width left for laying out the label. |
| 225 const CGFloat width_left = width - GetWidthForImageAndLabel(image, @""); |
| 226 |
| 227 // Middle-elide the label to fit |width_left|. This leaves the |
| 228 // prefix and the trailing country code in place. |
| 229 NSString* elided_label = base::SysUTF16ToNSString(gfx::ElideText( |
| 230 base::SysNSStringToUTF16(full_label_), |
| 231 gfx::FontList(gfx::Font(GetFont())), width_left, gfx::ELIDE_MIDDLE)); |
| 232 |
| 233 // Use the elided label. |
| 234 SetLabel(elided_label); |
| 235 return GetWidthForImageAndLabel(image, elided_label) * progress; |
| 236 } |
| 237 |
| 238 // Pass mouse operations through to location icon. |
| 239 bool SecureVerboseBubbleDecoration::IsDraggable() { |
| 240 return location_icon_->IsDraggable(); |
| 241 } |
| 242 |
| 243 NSPasteboard* SecureVerboseBubbleDecoration::GetDragPasteboard() { |
| 244 return location_icon_->GetDragPasteboard(); |
| 245 } |
| 246 |
| 247 NSImage* SecureVerboseBubbleDecoration::GetDragImage() { |
| 248 return location_icon_->GetDragImage(); |
| 249 } |
| 250 |
| 251 NSRect SecureVerboseBubbleDecoration::GetDragImageFrame(NSRect frame) { |
| 252 return GetImageRectInFrame(frame); |
| 253 } |
| 254 |
| 255 bool SecureVerboseBubbleDecoration::OnMousePressed(NSRect frame, |
| 256 NSPoint location) { |
| 257 return location_icon_->OnMousePressed(frame, location); |
| 258 } |
| 259 |
| 260 bool SecureVerboseBubbleDecoration::AcceptsMousePress() { |
| 261 return true; |
| 262 } |
| 263 |
| 264 ui::NinePartImageIds SecureVerboseBubbleDecoration::GetBubbleImageIds() { |
| 265 return IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE); |
| 266 } |
OLD | NEW |