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/ev_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_icon_decoration.h" | |
10 #import "chrome/browser/ui/cocoa/themed_window.h" | |
11 #include "chrome/grit/theme_resources.h" | |
12 #include "skia/ext/skia_utils_mac.h" | |
13 #import "ui/base/cocoa/nsview_additions.h" | |
14 #include "ui/base/material_design/material_design_controller.h" | |
15 #include "ui/gfx/color_palette.h" | |
16 #include "ui/gfx/font_list.h" | |
17 #include "ui/gfx/text_elider.h" | |
18 | |
19 namespace { | |
20 | |
21 // This is used to increase the right margin of this decoration. | |
22 const CGFloat kRightSideMargin = 1.0; | |
23 | |
24 // TODO(shess): In general, decorations that don't fit in the | |
25 // available space are omitted. This one never goes to omitted, it | |
26 // sticks at 150px, which AFAICT follows the Windows code. Since the | |
27 // Layout() code doesn't take this into account, it's possible the | |
28 // control could end up with display artifacts, though things still | |
29 // work (and don't crash). | |
30 // http://crbug.com/49822 | |
31 | |
32 // Minimum acceptable width for the ev bubble. | |
33 const CGFloat kMinElidedBubbleWidth = 150.0; | |
34 | |
35 // Maximum amount of available space to make the bubble, subject to | |
36 // |kMinElidedBubbleWidth|. | |
37 const float kMaxBubbleFraction = 0.5; | |
38 | |
39 // The info-bubble point should look like it points to the bottom of the lock | |
40 // icon. Determined with Pixie.app. | |
41 const CGFloat kPageInfoBubblePointYOffset = 6.0; | |
42 | |
43 // TODO(shess): This is ugly, find a better way. Using it right now | |
44 // so that I can crib from gtk and still be able to see that I'm using | |
45 // the same values easily. | |
46 NSColor* ColorWithRGBBytes(int rr, int gg, int bb) { | |
47 DCHECK_LE(rr, 255); | |
48 DCHECK_LE(bb, 255); | |
49 DCHECK_LE(gg, 255); | |
50 return [NSColor colorWithCalibratedRed:static_cast<float>(rr)/255.0 | |
51 green:static_cast<float>(gg)/255.0 | |
52 blue:static_cast<float>(bb)/255.0 | |
53 alpha:1.0]; | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 EVBubbleDecoration::EVBubbleDecoration(LocationIconDecoration* location_icon) | |
59 : location_icon_(location_icon) { | |
60 if (ui::MaterialDesignController::IsModeMaterial()) { | |
61 // On Retina the text label is 1px above the Omnibox textfield's text | |
62 // baseline. If the Omnibox textfield also drew the label the baselines | |
63 // would align. | |
64 SetRetinaBaselineOffset(0.5); | |
65 } else { | |
66 // Color tuples stolen from location_bar_view_gtk.cc. | |
67 SetTextColor(ColorWithRGBBytes(0x07, 0x95, 0x00)); | |
68 } | |
69 } | |
70 | |
71 EVBubbleDecoration::~EVBubbleDecoration() {} | |
72 | |
73 NSColor* EVBubbleDecoration::GetBackgroundBorderColor() { | |
74 return skia::SkColorToSRGBNSColor(gfx::kGoogleGreen700); | |
75 } | |
76 | |
77 void EVBubbleDecoration::SetFullLabel(NSString* label) { | |
78 full_label_.reset([label retain]); | |
79 SetLabel(full_label_); | |
80 } | |
81 | |
82 void EVBubbleDecoration::DrawWithBackgroundInFrame(NSRect background_frame, | |
83 NSRect frame, | |
84 NSView* control_view) { | |
85 if (!ui::MaterialDesignController::IsModeMaterial()) { | |
86 BubbleDecoration::DrawWithBackgroundInFrame(background_frame, frame, | |
87 control_view); | |
88 return; | |
89 } | |
90 | |
91 NSRect rect = NSInsetRect(background_frame, 0, 3); | |
92 rect.size.width -= kRightSideMargin; | |
93 | |
94 CGFloat line_width = [control_view cr_lineWidth]; | |
95 bool in_dark_mode = [[control_view window] inIncognitoModeWithSystemTheme]; | |
96 // Only adjust the path rect by 1/2 the line width if it's going to be | |
97 // stroked (so that the stroke lines fall along pixel lines). | |
98 if (!in_dark_mode) { | |
99 rect = NSInsetRect(rect, line_width / 2., line_width / 2.); | |
100 } | |
101 | |
102 DrawInFrame(frame, control_view); | |
103 } | |
104 | |
105 NSPoint EVBubbleDecoration::GetBubblePointInFrame(NSRect frame) { | |
106 NSRect image_rect = GetImageRectInFrame(frame); | |
107 return NSMakePoint(NSMidX(image_rect), | |
108 NSMaxY(image_rect) - kPageInfoBubblePointYOffset); | |
109 } | |
110 | |
111 CGFloat EVBubbleDecoration::GetWidthForSpace(CGFloat width) { | |
112 // Limit with to not take up too much of the available width, but | |
113 // also don't let it shrink too much. | |
114 width = std::max(width * kMaxBubbleFraction, kMinElidedBubbleWidth); | |
115 | |
116 // Use the full label if it fits. | |
117 NSImage* image = GetImage(); | |
118 const CGFloat all_width = GetWidthForImageAndLabel(image, full_label_); | |
119 if (all_width <= width) { | |
120 SetLabel(full_label_); | |
121 return all_width; | |
122 } | |
123 | |
124 // Width left for laying out the label. | |
125 const CGFloat width_left = width - GetWidthForImageAndLabel(image, @""); | |
126 | |
127 // Middle-elide the label to fit |width_left|. This leaves the | |
128 // prefix and the trailing country code in place. | |
129 NSString* elided_label = base::SysUTF16ToNSString( | |
130 gfx::ElideText(base::SysNSStringToUTF16(full_label_), | |
131 gfx::FontList(gfx::Font(GetFont())), | |
132 width_left, gfx::ELIDE_MIDDLE)); | |
133 | |
134 // Use the elided label. | |
135 SetLabel(elided_label); | |
136 return GetWidthForImageAndLabel(image, elided_label); | |
137 } | |
138 | |
139 // Pass mouse operations through to location icon. | |
140 bool EVBubbleDecoration::IsDraggable() { | |
141 return location_icon_->IsDraggable(); | |
142 } | |
143 | |
144 NSPasteboard* EVBubbleDecoration::GetDragPasteboard() { | |
145 return location_icon_->GetDragPasteboard(); | |
146 } | |
147 | |
148 NSImage* EVBubbleDecoration::GetDragImage() { | |
149 return location_icon_->GetDragImage(); | |
150 } | |
151 | |
152 NSRect EVBubbleDecoration::GetDragImageFrame(NSRect frame) { | |
153 return GetImageRectInFrame(frame); | |
154 } | |
155 | |
156 bool EVBubbleDecoration::OnMousePressed(NSRect frame, NSPoint location) { | |
157 return location_icon_->OnMousePressed(frame, location); | |
158 } | |
159 | |
160 bool EVBubbleDecoration::AcceptsMousePress() { | |
161 return true; | |
162 } | |
163 | |
164 ui::NinePartImageIds EVBubbleDecoration::GetBubbleImageIds() { | |
165 return IMAGE_GRID(IDR_OMNIBOX_EV_BUBBLE); | |
166 } | |
167 | |
168 NSColor* EVBubbleDecoration::GetDarkModeTextColor() { | |
169 return [NSColor whiteColor]; | |
170 } | |
OLD | NEW |