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_DELEGATE_H_
6 #define UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_
8 #include "base/gtest_prod_util.h"
9 #include "ui/base/animation/animation_delegate.h"
10 #include "ui/views/bubble/bubble_border.h"
11 #include "ui/views/widget/widget_delegate.h"
12 #include "ui/views/widget/widget_observer.h"
24 class BubbleFrameView
;
26 // BubbleDelegateView creates frame and client views for bubble Widgets.
27 // BubbleDelegateView itself is the client's contents view.
29 ///////////////////////////////////////////////////////////////////////////////
30 class VIEWS_EXPORT BubbleDelegateView
: public WidgetDelegateView
,
31 public ui::AnimationDelegate
,
32 public WidgetObserver
{
35 BubbleDelegateView(View
* anchor_view
,
36 BubbleBorder::ArrowLocation arrow_location
);
37 virtual ~BubbleDelegateView();
39 // Create and initialize the bubble Widget(s) with proper bounds.
40 static Widget
* CreateBubble(BubbleDelegateView
* bubble_delegate
);
42 // WidgetDelegate overrides:
43 virtual BubbleDelegateView
* AsBubbleDelegate() OVERRIDE
;
44 virtual bool CanActivate() const OVERRIDE
;
45 virtual View
* GetContentsView() OVERRIDE
;
46 virtual NonClientFrameView
* CreateNonClientFrameView(Widget
* widget
) OVERRIDE
;
48 // WidgetObserver overrides:
49 virtual void OnWidgetDestroying(Widget
* widget
) OVERRIDE
;
50 virtual void OnWidgetVisibilityChanged(Widget
* widget
, bool visible
) OVERRIDE
;
51 virtual void OnWidgetActivationChanged(Widget
* widget
, bool active
) OVERRIDE
;
52 virtual void OnWidgetBoundsChanged(Widget
* widget
,
53 const gfx::Rect
& new_bounds
) OVERRIDE
;
55 bool close_on_esc() const { return close_on_esc_
; }
56 void set_close_on_esc(bool close_on_esc
) { close_on_esc_
= close_on_esc
; }
58 bool close_on_deactivate() const { return close_on_deactivate_
; }
59 void set_close_on_deactivate(bool close_on_deactivate
) {
60 close_on_deactivate_
= close_on_deactivate
;
63 View
* anchor_view() const { return anchor_view_
; }
64 Widget
* anchor_widget() const { return anchor_widget_
; }
66 // The anchor point is used in the absence of an anchor view.
67 const gfx::Point
& anchor_point() const { return anchor_point_
; }
69 BubbleBorder::ArrowLocation
arrow_location() const { return arrow_location_
; }
70 void set_arrow_location(BubbleBorder::ArrowLocation arrow_location
) {
71 arrow_location_
= arrow_location
;
74 BubbleBorder::Shadow
shadow() const { return shadow_
; }
75 void set_shadow(BubbleBorder::Shadow shadow
) { shadow_
= shadow
; }
77 SkColor
color() const { return color_
; }
78 void set_color(SkColor color
) {
80 color_explicitly_set_
= true;
83 const gfx::Insets
& margins() const { return margins_
; }
84 void set_margins(const gfx::Insets
& margins
) { margins_
= margins
; }
86 void set_anchor_insets(const gfx::Insets
& insets
) { anchor_insets_
= insets
; }
87 const gfx::Insets
& anchor_insets() const { return anchor_insets_
; }
89 gfx::NativeView
parent_window() const { return parent_window_
; }
90 void set_parent_window(gfx::NativeView window
) { parent_window_
= window
; }
92 bool use_focusless() const { return use_focusless_
; }
93 void set_use_focusless(bool use_focusless
) {
94 use_focusless_
= use_focusless
;
97 bool accept_events() const { return accept_events_
; }
98 void set_accept_events(bool accept_events
) { accept_events_
= accept_events
; }
100 bool adjust_if_offscreen() const { return adjust_if_offscreen_
; }
101 void set_adjust_if_offscreen(bool adjust
) { adjust_if_offscreen_
= adjust
; }
103 // Get the arrow's anchor rect in screen space.
104 virtual gfx::Rect
GetAnchorRect();
106 // Show the bubble's widget (and |border_widget_| on Windows).
109 // Fade the bubble in or out via Widget transparency.
110 // Fade in calls Widget::Show; fade out calls Widget::Close upon completion.
111 void StartFade(bool fade_in
);
113 // Reset fade and opacity of bubble. Restore the opacity of the
114 // bubble to the setting before StartFade() was called.
117 // Sets the bubble alignment relative to the anchor.
118 void SetAlignment(BubbleBorder::BubbleAlignment alignment
);
121 // Get bubble bounds from the anchor point and client view's preferred size.
122 virtual gfx::Rect
GetBubbleBounds();
125 virtual bool AcceleratorPressed(const ui::Accelerator
& accelerator
) OVERRIDE
;
126 virtual void OnNativeThemeChanged(const ui::NativeTheme
* theme
) OVERRIDE
;
128 // ui::AnimationDelegate overrides:
129 virtual void AnimationEnded(const ui::Animation
* animation
) OVERRIDE
;
130 virtual void AnimationProgressed(const ui::Animation
* animation
) OVERRIDE
;
132 // Perform view initialization on the contents for bubble sizing.
135 // Set the anchor view, this (or set_anchor_point) must be done before
136 // calling CreateBubble or Show.
137 void set_anchor_view(View
* anchor_view
) { anchor_view_
= anchor_view
; }
139 // The anchor point or anchor view must be set before calling CreateBubble or
141 void set_anchor_point(gfx::Point anchor_point
) {
142 anchor_point_
= anchor_point
;
145 bool move_with_anchor() const { return move_with_anchor_
; }
146 void set_move_with_anchor(bool move_with_anchor
) {
147 move_with_anchor_
= move_with_anchor
;
150 // Resizes and potentially moves the Bubble to best accommodate the
151 // contents preferred size.
152 void SizeToContents();
154 BubbleFrameView
* GetBubbleFrameView() const;
157 FRIEND_TEST_ALL_PREFIXES(BubbleFrameViewTest
, NonClientHitTest
);
158 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest
, CreateDelegate
);
160 // Update the bubble color from |theme|, unless it was explicitly set.
161 void UpdateColorsFromTheme(const ui::NativeTheme
* theme
);
163 #if defined(OS_WIN) && !defined(USE_AURA)
164 // Get bounds for the Windows-only widget that hosts the bubble's contents.
165 gfx::Rect
GetBubbleClientBounds() const;
168 // Fade animation for bubble.
169 scoped_ptr
<ui::SlideAnimation
> fade_animation_
;
171 // Flags controlling bubble closure on the escape key and deactivation.
173 bool close_on_deactivate_
;
175 // The view and widget to which this bubble is anchored.
177 Widget
* anchor_widget_
;
179 // The anchor point used in the absence of an anchor view.
180 gfx::Point anchor_point_
;
182 // If true, the bubble will re-anchor (and may resize) with |anchor_widget_|.
183 bool move_with_anchor_
;
185 // The arrow's location on the bubble.
186 BubbleBorder::ArrowLocation arrow_location_
;
188 // Bubble border shadow to use.
189 BubbleBorder::Shadow shadow_
;
191 // The background color of the bubble; and flag for when it's explicitly set.
193 bool color_explicitly_set_
;
195 // The margins between the content and the inside of the border.
196 gfx::Insets margins_
;
198 // Insets applied to the |anchor_view_| bounds.
199 gfx::Insets anchor_insets_
;
201 // Original opacity of the bubble.
202 int original_opacity_
;
204 // The widget hosting the border for this bubble (non-Aura Windows only).
205 Widget
* border_widget_
;
207 // Create a popup window for focusless bubbles on Linux/ChromeOS.
208 // These bubbles are not interactive and should not gain focus.
211 // Specifies whether the popup accepts events or lets them pass through.
214 // If true (defaults to true), the arrow may be mirrored and moved to fit the
215 // bubble on screen better. It would be a no-op if the bubble has no arrow.
216 bool adjust_if_offscreen_
;
218 // Parent native window of the bubble.
219 gfx::NativeView parent_window_
;
221 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView
);
226 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_