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/gfx/animation/animation_delegate.h"
10 #include "ui/views/bubble/bubble_border.h"
11 #include "ui/views/widget/widget.h"
12 #include "ui/views/widget/widget_delegate.h"
13 #include "ui/views/widget/widget_observer.h"
22 class BubbleBorderDelegate;
23 class BubbleFrameView;
25 // BubbleDelegateView creates frame and client views for bubble Widgets.
26 // BubbleDelegateView itself is the client's contents view.
28 ///////////////////////////////////////////////////////////////////////////////
29 class VIEWS_EXPORT BubbleDelegateView : public WidgetDelegateView,
30 public gfx::AnimationDelegate,
31 public WidgetObserver {
34 BubbleDelegateView(View* anchor_view, BubbleBorder::Arrow arrow);
35 virtual ~BubbleDelegateView();
37 // Create and initialize the bubble Widget(s) with proper bounds.
38 static Widget* CreateBubble(BubbleDelegateView* bubble_delegate);
40 // WidgetDelegate overrides:
41 virtual BubbleDelegateView* AsBubbleDelegate() OVERRIDE;
42 virtual bool CanActivate() const OVERRIDE;
43 virtual bool ShouldShowCloseButton() const OVERRIDE;
44 virtual View* GetContentsView() OVERRIDE;
45 virtual NonClientFrameView* CreateNonClientFrameView(Widget* widget) OVERRIDE;
47 // WidgetObserver overrides:
48 virtual void OnWidgetDestroying(Widget* widget) OVERRIDE;
49 virtual void OnWidgetVisibilityChanging(Widget* widget, bool visible)
51 virtual void OnWidgetVisibilityChanged(Widget* widget, bool visible)
53 virtual void OnWidgetActivationChanged(Widget* widget, bool active) OVERRIDE;
54 virtual void OnWidgetBoundsChanged(Widget* widget,
55 const gfx::Rect& new_bounds) OVERRIDE;
57 bool close_on_esc() const { return close_on_esc_; }
58 void set_close_on_esc(bool close_on_esc) { close_on_esc_ = close_on_esc; }
60 bool close_on_deactivate() const { return close_on_deactivate_; }
61 void set_close_on_deactivate(bool close) { close_on_deactivate_ = close; }
63 View* GetAnchorView() const;
64 Widget* anchor_widget() const { return anchor_widget_; }
66 // The anchor rect is used in the absence of an assigned anchor view.
67 const gfx::Rect& anchor_rect() const { return anchor_rect_; }
69 BubbleBorder::Arrow arrow() const { return arrow_; }
70 void set_arrow(BubbleBorder::Arrow arrow) { arrow_ = arrow; }
72 BubbleBorder::Shadow shadow() const { return shadow_; }
73 void set_shadow(BubbleBorder::Shadow shadow) { shadow_ = shadow; }
75 SkColor color() const { return color_; }
76 void set_color(SkColor color) {
78 color_explicitly_set_ = true;
81 const gfx::Insets& margins() const { return margins_; }
82 void set_margins(const gfx::Insets& margins) { margins_ = margins; }
84 const gfx::Insets& anchor_view_insets() const { return anchor_view_insets_; }
85 void set_anchor_view_insets(const gfx::Insets& i) { anchor_view_insets_ = i; }
87 gfx::NativeView parent_window() const { return parent_window_; }
88 void set_parent_window(gfx::NativeView window) { parent_window_ = window; }
90 bool use_focusless() const { return use_focusless_; }
91 void set_use_focusless(bool focusless) { use_focusless_ = focusless; }
93 bool accept_events() const { return accept_events_; }
94 void set_accept_events(bool accept_events) { accept_events_ = accept_events; }
96 bool border_accepts_events() const { return border_accepts_events_; }
97 void set_border_accepts_events(bool event) { border_accepts_events_ = event; }
99 bool adjust_if_offscreen() const { return adjust_if_offscreen_; }
100 void set_adjust_if_offscreen(bool adjust) { adjust_if_offscreen_ = adjust; }
102 bool move_with_anchor() const { return move_with_anchor_; }
103 void set_move_with_anchor(bool move) { move_with_anchor_ = move; }
105 // Get the arrow's anchor rect in screen space.
106 virtual gfx::Rect GetAnchorRect();
108 // Allows delegates to provide custom parameters before widget initialization.
109 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams* params,
110 Widget* widget) const;
112 // Fade the bubble in or out by animation Widget transparency.
113 // Fade-in calls Widget::Show; fade-out calls Widget::Close upon completion.
114 void StartFade(bool fade_in);
116 // Restores bubble opacity to its value before StartFade() was called.
119 // Sets the bubble alignment relative to the anchor. This may only be called
120 // after calling CreateBubble.
121 void SetAlignment(BubbleBorder::BubbleAlignment alignment);
123 // Sets the bubble arrow paint type.
124 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type);
126 // Call this method when the anchor view bounds have changed to reposition
127 // the bubble. The bubble is automatically repositioned when the anchor view
128 // bounds change as a result of the widget's bounds changing.
129 void OnAnchorViewBoundsChanged();
132 // Get bubble bounds from the anchor rect and client view's preferred size.
133 virtual gfx::Rect GetBubbleBounds();
135 // Returns the duration in milliseconds for the fade animation.
136 virtual int GetFadeDuration();
139 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE;
140 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE;
142 // gfx::AnimationDelegate overrides:
143 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE;
144 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE;
146 // Perform view initialization on the contents for bubble sizing.
149 // Whether |arrow()| should automatically flip while in RTL.
150 virtual bool ShouldFlipArrowForRtl() const;
152 // Set the anchor view or rect; set these before CreateBubble or Show. Note
153 // that if a valid view gets passed, the anchor rect will get ignored. If the
154 // view gets deleted, but no new view gets set, the last known anchor postion
155 // will get returned.
156 void SetAnchorView(View* anchor_view);
157 void set_anchor_rect(const gfx::Rect& rect) { anchor_rect_ = rect; }
159 // Resize and potentially move the bubble to fit the content's preferred size.
160 void SizeToContents();
162 BubbleFrameView* GetBubbleFrameView() const;
165 friend class BubbleBorderDelegate;
167 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, CreateDelegate);
168 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, NonClientHitTest);
170 // Update the bubble color from |theme|, unless it was explicitly set.
171 void UpdateColorsFromTheme(const ui::NativeTheme* theme);
173 #if defined(OS_WIN) && !defined(USE_AURA)
174 // Get bounds for the Windows-only widget that hosts the bubble's contents.
175 gfx::Rect GetBubbleClientBounds() const;
178 // Handles widget visibility changes.
179 void HandleVisibilityChanged(Widget* widget, bool visible);
181 // Fade animation for bubble.
182 scoped_ptr<gfx::SlideAnimation> fade_animation_;
184 // Flags controlling bubble closure on the escape key and deactivation.
186 bool close_on_deactivate_;
188 // The view and widget to which this bubble is anchored. Since an anchor view
189 // can be deleted without notice, we store it in the ViewStorage and retrieve
190 // it from there. It will make sure that the view is still valid.
191 const int anchor_view_storage_id_;
192 Widget* anchor_widget_;
194 // The anchor rect used in the absence of an anchor view.
195 gfx::Rect anchor_rect_;
197 // If true, the bubble will re-anchor (and may resize) with |anchor_widget_|.
198 bool move_with_anchor_;
200 // The arrow's location on the bubble.
201 BubbleBorder::Arrow arrow_;
203 // Bubble border shadow to use.
204 BubbleBorder::Shadow shadow_;
206 // The background color of the bubble; and flag for when it's explicitly set.
208 bool color_explicitly_set_;
210 // The margins between the content and the inside of the border.
211 gfx::Insets margins_;
213 // Insets applied to the |anchor_view_| bounds.
214 gfx::Insets anchor_view_insets_;
216 // Original opacity of the bubble.
217 int original_opacity_;
219 // The widget hosting the border for this bubble (non-Aura Windows only).
220 Widget* border_widget_;
222 // If true, the bubble does not take focus on display; default is false.
225 // Specifies whether the bubble (or its border) handles mouse events, etc.
227 bool border_accepts_events_;
229 // If true (defaults to true), the arrow may be mirrored and moved to fit the
230 // bubble on screen better. It would be a no-op if the bubble has no arrow.
231 bool adjust_if_offscreen_;
233 // Parent native window of the bubble.
234 gfx::NativeView parent_window_;
236 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView);
241 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_