1 // Copyright (c) 2012 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.
5 #ifndef UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_
6 #define UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/views/background.h"
13 #include "ui/views/border.h"
24 // A helper that combines each border image-set painter with arrows and metrics.
26 BorderImages(const int border_image_ids
[],
27 const int arrow_image_ids
[],
28 int border_interior_thickness
,
29 int arrow_interior_thickness
,
31 virtual ~BorderImages();
33 scoped_ptr
<Painter
> border_painter
;
34 gfx::ImageSkia left_arrow
;
35 gfx::ImageSkia top_arrow
;
36 gfx::ImageSkia right_arrow
;
37 gfx::ImageSkia bottom_arrow
;
39 // The thickness of border and arrow images and their interior areas.
40 // Thickness is the width of left/right and the height of top/bottom images.
41 // The interior is measured without including stroke or shadow pixels. The tip
42 // of the arrow is |arrow_interior_thickness| from the border and the base is
43 // always twice that; drawn in the background color.
45 int border_interior_thickness
;
47 int arrow_interior_thickness
;
49 // Width of an arrow (on the horizontal), including any shadows. Defaults to
50 // the width of the |top_arrow| asset.
53 // The corner radius of the bubble's rounded-rect interior area.
57 } // namespace internal
59 // Renders a border, with optional arrow, and a custom dropshadow.
60 // This can be used to produce floating "bubble" objects with rounded corners.
61 class VIEWS_EXPORT BubbleBorder
: public Border
{
63 // Possible locations for the (optional) arrow.
64 // 0 bit specifies left or right.
65 // 1 bit specifies top or bottom.
66 // 2 bit specifies horizontal or vertical.
67 // 3 bit specifies whether the arrow at the center of its residing edge.
79 BOTTOM_RIGHT
= BOTTOM
| RIGHT
,
81 RIGHT_TOP
= VERTICAL
| RIGHT
,
82 LEFT_BOTTOM
= VERTICAL
| BOTTOM
,
83 RIGHT_BOTTOM
= VERTICAL
| BOTTOM
| RIGHT
,
85 BOTTOM_CENTER
= CENTER
| BOTTOM
,
86 LEFT_CENTER
= CENTER
| VERTICAL
,
87 RIGHT_CENTER
= CENTER
| VERTICAL
| RIGHT
,
88 NONE
= 16, // No arrow. Positioned under the supplied rect.
89 FLOAT
= 17, // No arrow. Centered over the supplied rect.
94 NO_SHADOW_OPAQUE_BORDER
,
101 // The position of the bubble in relation to the anchor.
102 enum BubbleAlignment
{
103 // The tip of the arrow points to the middle of the anchor.
104 ALIGN_ARROW_TO_MID_ANCHOR
,
105 // The edge nearest to the arrow is lined up with the edge of the anchor.
106 ALIGN_EDGE_TO_ANCHOR_EDGE
,
109 // The way the arrow should be painted.
110 enum ArrowPaintType
{
111 // Fully render the arrow.
113 // Leave space for the arrow, but do not paint it.
115 // Neither paint nor leave space for the arrow.
119 BubbleBorder(Arrow arrow
, Shadow shadow
, SkColor color
);
120 ~BubbleBorder() override
;
122 // Returns the radius of the corner of the border.
123 // TODO(xiyuan): Get rid of this since it's part of BorderImages now?
124 static int GetCornerRadius() {
125 // We can't safely calculate a border radius by comparing the sizes of the
126 // side and corner images, because either may have been extended in various
127 // directions in order to do more subtle dropshadow fading or other effects.
128 // So we hardcode the most accurate value.
132 static bool has_arrow(Arrow a
) { return a
< NONE
; }
134 static bool is_arrow_on_left(Arrow a
) {
135 return has_arrow(a
) && (a
== LEFT_CENTER
|| !(a
& (RIGHT
| CENTER
)));
138 static bool is_arrow_on_top(Arrow a
) {
139 return has_arrow(a
) && (a
== TOP_CENTER
|| !(a
& (BOTTOM
| CENTER
)));
142 static bool is_arrow_on_horizontal(Arrow a
) {
143 return a
>= NONE
? false : !(a
& VERTICAL
);
146 static bool is_arrow_at_center(Arrow a
) {
147 return has_arrow(a
) && !!(a
& CENTER
);
150 static Arrow
horizontal_mirror(Arrow a
) {
151 return (a
== TOP_CENTER
|| a
== BOTTOM_CENTER
|| a
>= NONE
) ?
152 a
: static_cast<Arrow
>(a
^ RIGHT
);
155 static Arrow
vertical_mirror(Arrow a
) {
156 return (a
== LEFT_CENTER
|| a
== RIGHT_CENTER
|| a
>= NONE
) ?
157 a
: static_cast<Arrow
>(a
^ BOTTOM
);
160 // Get or set the arrow type.
161 void set_arrow(Arrow arrow
) { arrow_
= arrow
; }
162 Arrow
arrow() const { return arrow_
; }
164 // Get or set the bubble alignment.
165 void set_alignment(BubbleAlignment alignment
) { alignment_
= alignment
; }
166 BubbleAlignment
alignment() const { return alignment_
; }
168 // Get the shadow type.
169 Shadow
shadow() const { return shadow_
; }
171 // Get or set the background color for the bubble and arrow body.
172 void set_background_color(SkColor color
) { background_color_
= color
; }
173 SkColor
background_color() const { return background_color_
; }
175 // If true, the background color should be determined by the host's
177 void set_use_theme_background_color(bool use_theme_background_color
) {
178 use_theme_background_color_
= use_theme_background_color
;
180 bool use_theme_background_color() { return use_theme_background_color_
; }
182 // Sets a desired pixel distance between the arrow tip and the outside edge of
183 // the neighboring border image. For example: |----offset----|
184 // '(' represents shadow around the '{' edge: ((({ ^ })))
185 // The arrow will still anchor to the same location but the bubble will shift
186 // location to place the arrow |offset| pixels from the perpendicular edge.
187 void set_arrow_offset(int offset
) { arrow_offset_
= offset
; }
189 // Sets the way the arrow is actually painted. Default is PAINT_NORMAL.
190 void set_paint_arrow(ArrowPaintType value
) { arrow_paint_type_
= value
; }
192 // Get the desired widget bounds (in screen coordinates) given the anchor rect
193 // and bubble content size; calculated from shadow and arrow image dimensions.
194 virtual gfx::Rect
GetBounds(const gfx::Rect
& anchor_rect
,
195 const gfx::Size
& contents_size
) const;
197 // Get the border exterior thickness, including stroke and shadow, in pixels.
198 int GetBorderThickness() const;
200 // Returns the corner radius of the current image set.
201 int GetBorderCornerRadius() const;
203 // Gets the arrow offset to use.
204 int GetArrowOffset(const gfx::Size
& border_size
) const;
206 // Overridden from Border:
207 void Paint(const View
& view
, gfx::Canvas
* canvas
) override
;
208 gfx::Insets
GetInsets() const override
;
209 gfx::Size
GetMinimumSize() const override
;
212 FRIEND_TEST_ALL_PREFIXES(BubbleBorderTest
, GetSizeForContentsSizeTest
);
213 FRIEND_TEST_ALL_PREFIXES(BubbleBorderTest
, GetBoundsOriginTest
);
214 FRIEND_TEST_ALL_PREFIXES(BubbleBorderTest
, ShadowTypes
);
216 // The border and arrow stroke size used in image assets, in pixels.
217 static const int kStroke
;
219 gfx::Size
GetSizeForContentsSize(const gfx::Size
& contents_size
) const;
220 gfx::ImageSkia
* GetArrowImage() const;
221 gfx::Rect
GetArrowRect(const gfx::Rect
& bounds
) const;
222 void DrawArrow(gfx::Canvas
* canvas
, const gfx::Rect
& arrow_bounds
) const;
224 internal::BorderImages
* GetImagesForTest() const;
228 ArrowPaintType arrow_paint_type_
;
229 BubbleAlignment alignment_
;
231 internal::BorderImages
* images_
;
232 SkColor background_color_
;
233 bool use_theme_background_color_
;
235 DISALLOW_COPY_AND_ASSIGN(BubbleBorder
);
238 // A Background that clips itself to the specified BubbleBorder and uses
239 // the background color of the BubbleBorder.
240 class VIEWS_EXPORT BubbleBackground
: public Background
{
242 explicit BubbleBackground(BubbleBorder
* border
) : border_(border
) {}
244 // Overridden from Background:
245 void Paint(gfx::Canvas
* canvas
, View
* view
) const override
;
248 BubbleBorder
* border_
;
250 DISALLOW_COPY_AND_ASSIGN(BubbleBackground
);
255 #endif // UI_VIEWS_BUBBLE_BUBBLE_BORDER_H_