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. 4 5 #ifndef UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 6 #define UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 7 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" 14 15 namespace gfx { 16 class Rect; 17 class SlideAnimation; 18 } 19 20 namespace views { 21 22 class BubbleFrameView; 23 24 // BubbleDelegateView creates frame and client views for bubble Widgets. 25 // BubbleDelegateView itself is the client's contents view. 26 class VIEWS_EXPORT BubbleDelegateView : public WidgetDelegateView, 27 public gfx::AnimationDelegate, 28 public WidgetObserver { 29 public: 30 BubbleDelegateView(); 31 BubbleDelegateView(View* anchor_view, BubbleBorder::Arrow arrow); 32 virtual ~BubbleDelegateView(); 33 34 // Create and initialize the bubble Widget(s) with proper bounds. 35 static Widget* CreateBubble(BubbleDelegateView* bubble_delegate); 36 37 // WidgetDelegateView overrides: 38 virtual BubbleDelegateView* AsBubbleDelegate() OVERRIDE; 39 virtual bool CanActivate() const OVERRIDE; 40 virtual bool ShouldShowCloseButton() const OVERRIDE; 41 virtual View* GetContentsView() OVERRIDE; 42 virtual NonClientFrameView* CreateNonClientFrameView(Widget* widget) OVERRIDE; 43 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 44 45 // WidgetObserver overrides: 46 virtual void OnWidgetDestroying(Widget* widget) OVERRIDE; 47 virtual void OnWidgetVisibilityChanging(Widget* widget, bool visible) 48 OVERRIDE; 49 virtual void OnWidgetVisibilityChanged(Widget* widget, bool visible) 50 OVERRIDE; 51 virtual void OnWidgetActivationChanged(Widget* widget, bool active) OVERRIDE; 52 virtual void OnWidgetBoundsChanged(Widget* widget, 53 const gfx::Rect& new_bounds) OVERRIDE; 54 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; } 57 58 bool close_on_deactivate() const { return close_on_deactivate_; } 59 void set_close_on_deactivate(bool close) { close_on_deactivate_ = close; } 60 61 View* GetAnchorView() const; 62 Widget* anchor_widget() const { return anchor_widget_; } 63 64 // The anchor rect is used in the absence of an assigned anchor view. 65 const gfx::Rect& anchor_rect() const { return anchor_rect_; } 66 67 BubbleBorder::Arrow arrow() const { return arrow_; } 68 void set_arrow(BubbleBorder::Arrow arrow) { arrow_ = arrow; } 69 70 BubbleBorder::Shadow shadow() const { return shadow_; } 71 void set_shadow(BubbleBorder::Shadow shadow) { shadow_ = shadow; } 72 73 SkColor color() const { return color_; } 74 void set_color(SkColor color) { 75 color_ = color; 76 color_explicitly_set_ = true; 77 } 78 79 const gfx::Insets& margins() const { return margins_; } 80 void set_margins(const gfx::Insets& margins) { margins_ = margins; } 81 82 const gfx::Insets& anchor_view_insets() const { return anchor_view_insets_; } 83 void set_anchor_view_insets(const gfx::Insets& i) { anchor_view_insets_ = i; } 84 85 gfx::NativeView parent_window() const { return parent_window_; } 86 void set_parent_window(gfx::NativeView window) { parent_window_ = window; } 87 88 bool use_focusless() const { return use_focusless_; } 89 void set_use_focusless(bool focusless) { use_focusless_ = focusless; } 90 91 bool accept_events() const { return accept_events_; } 92 void set_accept_events(bool accept_events) { accept_events_ = accept_events; } 93 94 bool border_accepts_events() const { return border_accepts_events_; } 95 void set_border_accepts_events(bool event) { border_accepts_events_ = event; } 96 97 bool adjust_if_offscreen() const { return adjust_if_offscreen_; } 98 void set_adjust_if_offscreen(bool adjust) { adjust_if_offscreen_ = adjust; } 99 100 bool move_with_anchor() const { return move_with_anchor_; } 101 void set_move_with_anchor(bool move) { move_with_anchor_ = move; } 102 103 // Get the arrow's anchor rect in screen space. 104 virtual gfx::Rect GetAnchorRect(); 105 106 // Allows delegates to provide custom parameters before widget initialization. 107 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams* params, 108 Widget* widget) const; 109 110 // Fade the bubble in or out by animation Widget transparency. 111 // Fade-in calls Widget::Show; fade-out calls Widget::Close upon completion. 112 void StartFade(bool fade_in); 113 114 // Restores bubble opacity to its value before StartFade() was called. 115 void ResetFade(); 116 117 // Sets the bubble alignment relative to the anchor. This may only be called 118 // after calling CreateBubble. 119 void SetAlignment(BubbleBorder::BubbleAlignment alignment); 120 121 // Sets the bubble arrow paint type. 122 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type); 123 124 // Call this method when the anchor bounds have changed to reposition the 125 // bubble. The bubble is automatically repositioned when the anchor view 126 // bounds change as a result of the widget's bounds changing. 127 void OnAnchorBoundsChanged(); 128 129 protected: 130 // Get bubble bounds from the anchor rect and client view's preferred size. 131 virtual gfx::Rect GetBubbleBounds(); 132 133 // Returns the duration in milliseconds for the fade animation. 134 virtual int GetFadeDuration(); 135 136 // View overrides: 137 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; 138 virtual void OnNativeThemeChanged(const ui::NativeTheme* theme) OVERRIDE; 139 140 // gfx::AnimationDelegate overrides: 141 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE; 142 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE; 143 144 // Perform view initialization on the contents for bubble sizing. 145 virtual void Init(); 146 147 // Sets the anchor view or rect and repositions the bubble. Note that if a 148 // valid view gets passed, the anchor rect will get ignored. If the view gets 149 // deleted, but no new view gets set, the last known anchor postion will get 150 // returned. 151 void SetAnchorView(View* anchor_view); 152 void SetAnchorRect(const gfx::Rect& rect); 153 154 // Resize and potentially move the bubble to fit the content's preferred size. 155 void SizeToContents(); 156 157 BubbleFrameView* GetBubbleFrameView() const; 158 159 private: 160 friend class BubbleBorderDelegate; 161 162 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, CreateDelegate); 163 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, NonClientHitTest); 164 165 // Update the bubble color from |theme|, unless it was explicitly set. 166 void UpdateColorsFromTheme(const ui::NativeTheme* theme); 167 168 // Handles widget visibility changes. 169 void HandleVisibilityChanged(Widget* widget, bool visible); 170 171 // Fade animation for bubble. 172 scoped_ptr<gfx::SlideAnimation> fade_animation_; 173 174 // Flags controlling bubble closure on the escape key and deactivation. 175 bool close_on_esc_; 176 bool close_on_deactivate_; 177 178 // The view and widget to which this bubble is anchored. Since an anchor view 179 // can be deleted without notice, we store it in the ViewStorage and retrieve 180 // it from there. It will make sure that the view is still valid. 181 const int anchor_view_storage_id_; 182 Widget* anchor_widget_; 183 184 // The anchor rect used in the absence of an anchor view. 185 gfx::Rect anchor_rect_; 186 187 // If true, the bubble will re-anchor (and may resize) with |anchor_widget_|. 188 bool move_with_anchor_; 189 190 // The arrow's location on the bubble. 191 BubbleBorder::Arrow arrow_; 192 193 // Bubble border shadow to use. 194 BubbleBorder::Shadow shadow_; 195 196 // The background color of the bubble; and flag for when it's explicitly set. 197 SkColor color_; 198 bool color_explicitly_set_; 199 200 // The margins between the content and the inside of the border. 201 gfx::Insets margins_; 202 203 // Insets applied to the |anchor_view_| bounds. 204 gfx::Insets anchor_view_insets_; 205 206 // Original opacity of the bubble. 207 int original_opacity_; 208 209 // The widget hosting the border for this bubble (non-Aura Windows only). 210 Widget* border_widget_; 211 212 // If true, the bubble does not take focus on display; default is false. 213 bool use_focusless_; 214 215 // Specifies whether the bubble (or its border) handles mouse events, etc. 216 bool accept_events_; 217 bool border_accepts_events_; 218 219 // If true (defaults to true), the arrow may be mirrored and moved to fit the 220 // bubble on screen better. It would be a no-op if the bubble has no arrow. 221 bool adjust_if_offscreen_; 222 223 // Parent native window of the bubble. 224 gfx::NativeView parent_window_; 225 226 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView); 227 }; 228 229 } // namespace views 230 231 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ 232