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
{
29 BubbleDelegateView(View
* anchor_view
, BubbleBorder::Arrow arrow
);
30 ~BubbleDelegateView() override
;
32 // Create and initialize the bubble Widget(s) with proper bounds.
33 static Widget
* CreateBubble(BubbleDelegateView
* bubble_delegate
);
35 // WidgetDelegateView overrides:
36 BubbleDelegateView
* AsBubbleDelegate() override
;
37 bool ShouldShowCloseButton() const override
;
38 View
* GetContentsView() override
;
39 NonClientFrameView
* CreateNonClientFrameView(Widget
* widget
) override
;
40 void GetAccessibleState(ui::AXViewState
* state
) override
;
42 // WidgetObserver overrides:
43 void OnWidgetDestroying(Widget
* widget
) override
;
44 void OnWidgetVisibilityChanging(Widget
* widget
, bool visible
) override
;
45 void OnWidgetVisibilityChanged(Widget
* widget
, bool visible
) override
;
46 void OnWidgetActivationChanged(Widget
* widget
, bool active
) override
;
47 void OnWidgetBoundsChanged(Widget
* widget
,
48 const gfx::Rect
& new_bounds
) override
;
50 bool close_on_esc() const { return close_on_esc_
; }
51 void set_close_on_esc(bool close_on_esc
) { close_on_esc_
= close_on_esc
; }
53 bool close_on_deactivate() const { return close_on_deactivate_
; }
54 void set_close_on_deactivate(bool close
) { close_on_deactivate_
= close
; }
56 View
* GetAnchorView() const;
57 Widget
* anchor_widget() const { return anchor_widget_
; }
59 // The anchor rect is used in the absence of an assigned anchor view.
60 const gfx::Rect
& anchor_rect() const { return anchor_rect_
; }
62 BubbleBorder::Arrow
arrow() const { return arrow_
; }
63 void set_arrow(BubbleBorder::Arrow arrow
) { arrow_
= arrow
; }
65 BubbleBorder::Shadow
shadow() const { return shadow_
; }
66 void set_shadow(BubbleBorder::Shadow shadow
) { shadow_
= shadow
; }
68 SkColor
color() const { return color_
; }
69 void set_color(SkColor color
) {
71 color_explicitly_set_
= true;
74 const gfx::Insets
& margins() const { return margins_
; }
75 void set_margins(const gfx::Insets
& margins
) { margins_
= margins
; }
77 const gfx::Insets
& anchor_view_insets() const { return anchor_view_insets_
; }
78 void set_anchor_view_insets(const gfx::Insets
& i
) { anchor_view_insets_
= i
; }
80 gfx::NativeView
parent_window() const { return parent_window_
; }
81 void set_parent_window(gfx::NativeView window
) { parent_window_
= window
; }
83 bool accept_events() const { return accept_events_
; }
84 void set_accept_events(bool accept_events
) { accept_events_
= accept_events
; }
86 bool border_accepts_events() const { return border_accepts_events_
; }
87 void set_border_accepts_events(bool event
) { border_accepts_events_
= event
; }
89 bool adjust_if_offscreen() const { return adjust_if_offscreen_
; }
90 void set_adjust_if_offscreen(bool adjust
) { adjust_if_offscreen_
= adjust
; }
92 // Get the arrow's anchor rect in screen space.
93 virtual gfx::Rect
GetAnchorRect() const;
95 // Allows delegates to provide custom parameters before widget initialization.
96 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams
* params
,
97 Widget
* widget
) const;
99 // Sets the bubble alignment relative to the anchor. This may only be called
100 // after calling CreateBubble.
101 void SetAlignment(BubbleBorder::BubbleAlignment alignment
);
103 // Sets the bubble arrow paint type.
104 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type
);
106 // Call this method when the anchor bounds have changed to reposition the
107 // bubble. The bubble is automatically repositioned when the anchor view
108 // bounds change as a result of the widget's bounds changing.
109 void OnAnchorBoundsChanged();
112 // Get bubble bounds from the anchor rect and client view's preferred size.
113 virtual gfx::Rect
GetBubbleBounds();
115 // Return a FontList to use for the title of the bubble.
116 // (The default is MediumFont).
117 virtual const gfx::FontList
& GetTitleFontList() const;
120 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
;
121 void OnNativeThemeChanged(const ui::NativeTheme
* theme
) override
;
123 // Perform view initialization on the contents for bubble sizing.
126 // Sets the anchor view or rect and repositions the bubble. Note that if a
127 // valid view gets passed, the anchor rect will get ignored. If the view gets
128 // deleted, but no new view gets set, the last known anchor postion will get
130 void SetAnchorView(View
* anchor_view
);
131 void SetAnchorRect(const gfx::Rect
& rect
);
133 // Resize and potentially move the bubble to fit the content's preferred size.
134 void SizeToContents();
136 BubbleFrameView
* GetBubbleFrameView() const;
139 friend class BubbleBorderDelegate
;
140 friend class BubbleWindowTargeter
;
142 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest
, CreateDelegate
);
143 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest
, NonClientHitTest
);
145 // Update the bubble color from |theme|, unless it was explicitly set.
146 void UpdateColorsFromTheme(const ui::NativeTheme
* theme
);
148 // Handles widget visibility changes.
149 void HandleVisibilityChanged(Widget
* widget
, bool visible
);
151 // Flags controlling bubble closure on the escape key and deactivation.
153 bool close_on_deactivate_
;
155 // The view and widget to which this bubble is anchored. Since an anchor view
156 // can be deleted without notice, we store it in the ViewStorage and retrieve
157 // it from there. It will make sure that the view is still valid.
158 const int anchor_view_storage_id_
;
159 Widget
* anchor_widget_
;
161 // The anchor rect used in the absence of an anchor view.
162 mutable gfx::Rect anchor_rect_
;
164 // The arrow's location on the bubble.
165 BubbleBorder::Arrow arrow_
;
167 // Bubble border shadow to use.
168 BubbleBorder::Shadow shadow_
;
170 // The background color of the bubble; and flag for when it's explicitly set.
172 bool color_explicitly_set_
;
174 // The margins between the content and the inside of the border.
175 gfx::Insets margins_
;
177 // Insets applied to the |anchor_view_| bounds.
178 gfx::Insets anchor_view_insets_
;
180 // Specifies whether the bubble (or its border) handles mouse events, etc.
182 bool border_accepts_events_
;
184 // If true (defaults to true), the arrow may be mirrored and moved to fit the
185 // bubble on screen better. It would be a no-op if the bubble has no arrow.
186 bool adjust_if_offscreen_
;
188 // Parent native window of the bubble.
189 gfx::NativeView parent_window_
;
191 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView
);
196 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_