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/views/bubble/bubble_border.h"
10 #include "ui/views/widget/widget.h"
11 #include "ui/views/widget/widget_delegate.h"
12 #include "ui/views/widget/widget_observer.h"
21 class BubbleFrameView
;
23 // BubbleDelegateView creates frame and client views for bubble Widgets.
24 // BubbleDelegateView itself is the client's contents view.
25 class VIEWS_EXPORT BubbleDelegateView
: public WidgetDelegateView
,
26 public WidgetObserver
{
28 // Internal class name.
29 static const char kViewClassName
[];
32 BubbleDelegateView(View
* anchor_view
, BubbleBorder::Arrow arrow
);
33 ~BubbleDelegateView() override
;
35 // Create and initialize the bubble Widget(s) with proper bounds.
36 static Widget
* CreateBubble(BubbleDelegateView
* bubble_delegate
);
38 // WidgetDelegateView overrides:
39 BubbleDelegateView
* AsBubbleDelegate() override
;
40 bool ShouldShowCloseButton() const override
;
41 View
* GetContentsView() override
;
42 NonClientFrameView
* CreateNonClientFrameView(Widget
* widget
) override
;
43 void GetAccessibleState(ui::AXViewState
* state
) override
;
44 const char* GetClassName() const override
;
46 // WidgetObserver overrides:
47 void OnWidgetDestroying(Widget
* widget
) override
;
48 void OnWidgetVisibilityChanging(Widget
* widget
, bool visible
) override
;
49 void OnWidgetVisibilityChanged(Widget
* widget
, bool visible
) override
;
50 void OnWidgetActivationChanged(Widget
* widget
, bool active
) override
;
51 void OnWidgetBoundsChanged(Widget
* widget
,
52 const gfx::Rect
& new_bounds
) override
;
54 bool close_on_esc() const { return close_on_esc_
; }
55 void set_close_on_esc(bool close_on_esc
) { close_on_esc_
= close_on_esc
; }
57 bool close_on_deactivate() const { return close_on_deactivate_
; }
58 void set_close_on_deactivate(bool close
) { close_on_deactivate_
= close
; }
60 View
* GetAnchorView() const;
61 Widget
* anchor_widget() const { return anchor_widget_
; }
63 // The anchor rect is used in the absence of an assigned anchor view.
64 const gfx::Rect
& anchor_rect() const { return anchor_rect_
; }
66 BubbleBorder::Arrow
arrow() const { return arrow_
; }
67 void set_arrow(BubbleBorder::Arrow arrow
) { arrow_
= arrow
; }
69 BubbleBorder::Shadow
shadow() const { return shadow_
; }
70 void set_shadow(BubbleBorder::Shadow shadow
) { shadow_
= shadow
; }
72 SkColor
color() const { return color_
; }
73 void set_color(SkColor color
) {
75 color_explicitly_set_
= true;
78 const gfx::Insets
& margins() const { return margins_
; }
79 void set_margins(const gfx::Insets
& margins
) { margins_
= margins
; }
81 const gfx::Insets
& anchor_view_insets() const { return anchor_view_insets_
; }
82 void set_anchor_view_insets(const gfx::Insets
& i
) { anchor_view_insets_
= i
; }
84 gfx::NativeView
parent_window() const { return parent_window_
; }
85 void set_parent_window(gfx::NativeView window
) { parent_window_
= window
; }
87 bool accept_events() const { return accept_events_
; }
88 void set_accept_events(bool accept_events
) { accept_events_
= accept_events
; }
90 bool border_accepts_events() const { return border_accepts_events_
; }
91 void set_border_accepts_events(bool event
) { border_accepts_events_
= event
; }
93 bool adjust_if_offscreen() const { return adjust_if_offscreen_
; }
94 void set_adjust_if_offscreen(bool adjust
) { adjust_if_offscreen_
= adjust
; }
96 // Get the arrow's anchor rect in screen space.
97 virtual gfx::Rect
GetAnchorRect() const;
99 // Allows delegates to provide custom parameters before widget initialization.
100 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams
* params
,
101 Widget
* widget
) const;
103 // Sets the bubble alignment relative to the anchor. This may only be called
104 // after calling CreateBubble.
105 void SetAlignment(BubbleBorder::BubbleAlignment alignment
);
107 // Sets the bubble arrow paint type.
108 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type
);
110 // Call this method when the anchor bounds have changed to reposition the
111 // bubble. The bubble is automatically repositioned when the anchor view
112 // bounds change as a result of the widget's bounds changing.
113 void OnAnchorBoundsChanged();
116 // Get bubble bounds from the anchor rect and client view's preferred size.
117 virtual gfx::Rect
GetBubbleBounds();
119 // Return a FontList to use for the title of the bubble.
120 // (The default is MediumFont).
121 virtual const gfx::FontList
& GetTitleFontList() const;
124 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
125 void OnNativeThemeChanged(const ui::NativeTheme
* theme
) override
;
127 // Perform view initialization on the contents for bubble sizing.
130 // Sets the anchor view or rect and repositions the bubble. Note that if a
131 // valid view gets passed, the anchor rect will get ignored. If the view gets
132 // deleted, but no new view gets set, the last known anchor postion will get
134 void SetAnchorView(View
* anchor_view
);
135 void SetAnchorRect(const gfx::Rect
& rect
);
137 // Resize and potentially move the bubble to fit the content's preferred size.
138 void SizeToContents();
140 BubbleFrameView
* GetBubbleFrameView() const;
143 friend class BubbleBorderDelegate
;
144 friend class BubbleWindowTargeter
;
146 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest
, CreateDelegate
);
147 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest
, NonClientHitTest
);
149 // Update the bubble color from |theme|, unless it was explicitly set.
150 void UpdateColorsFromTheme(const ui::NativeTheme
* theme
);
152 // Handles widget visibility changes.
153 void HandleVisibilityChanged(Widget
* widget
, bool visible
);
155 // Flags controlling bubble closure on the escape key and deactivation.
157 bool close_on_deactivate_
;
159 // The view and widget to which this bubble is anchored. Since an anchor view
160 // can be deleted without notice, we store it in the ViewStorage and retrieve
161 // it from there. It will make sure that the view is still valid.
162 const int anchor_view_storage_id_
;
163 Widget
* anchor_widget_
;
165 // The anchor rect used in the absence of an anchor view.
166 mutable gfx::Rect anchor_rect_
;
168 // The arrow's location on the bubble.
169 BubbleBorder::Arrow arrow_
;
171 // Bubble border shadow to use.
172 BubbleBorder::Shadow shadow_
;
174 // The background color of the bubble; and flag for when it's explicitly set.
176 bool color_explicitly_set_
;
178 // The margins between the content and the inside of the border.
179 gfx::Insets margins_
;
181 // Insets applied to the |anchor_view_| bounds.
182 gfx::Insets anchor_view_insets_
;
184 // Specifies whether the bubble (or its border) handles mouse events, etc.
186 bool border_accepts_events_
;
188 // If true (defaults to true), the arrow may be mirrored and moved to fit the
189 // bubble on screen better. It would be a no-op if the bubble has no arrow.
190 bool adjust_if_offscreen_
;
192 // Parent native window of the bubble.
193 gfx::NativeView parent_window_
;
195 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView
);
200 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_