OLD | NEW |
| (Empty) |
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 "base/macros.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 FontList; | |
17 class Rect; | |
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 // TODO(estade): remove this in favor of BubbleDialogDelegateView. | |
27 class VIEWS_EXPORT BubbleDelegateView : public WidgetDelegateView, | |
28 public WidgetObserver { | |
29 public: | |
30 // Internal class name. | |
31 static const char kViewClassName[]; | |
32 | |
33 enum class CloseReason { | |
34 DEACTIVATION, | |
35 ESCAPE, | |
36 CLOSE_BUTTON, | |
37 UNKNOWN, | |
38 }; | |
39 | |
40 BubbleDelegateView(); | |
41 BubbleDelegateView(View* anchor_view, BubbleBorder::Arrow arrow); | |
42 ~BubbleDelegateView() override; | |
43 | |
44 // Create and initialize the bubble Widget(s) with proper bounds. | |
45 static Widget* CreateBubble(BubbleDelegateView* bubble_delegate); | |
46 | |
47 // WidgetDelegateView overrides: | |
48 BubbleDelegateView* AsBubbleDelegate() override; | |
49 bool ShouldShowCloseButton() const override; | |
50 View* GetContentsView() override; | |
51 NonClientFrameView* CreateNonClientFrameView(Widget* widget) override; | |
52 void GetAccessibleState(ui::AXViewState* state) override; | |
53 const char* GetClassName() const override; | |
54 | |
55 // WidgetObserver overrides: | |
56 void OnWidgetClosing(Widget* widget) override; | |
57 void OnWidgetDestroying(Widget* widget) override; | |
58 void OnWidgetVisibilityChanging(Widget* widget, bool visible) override; | |
59 void OnWidgetVisibilityChanged(Widget* widget, bool visible) override; | |
60 void OnWidgetActivationChanged(Widget* widget, bool active) override; | |
61 void OnWidgetBoundsChanged(Widget* widget, | |
62 const gfx::Rect& new_bounds) override; | |
63 | |
64 bool close_on_esc() const { return close_on_esc_; } | |
65 void set_close_on_esc(bool close_on_esc) { close_on_esc_ = close_on_esc; } | |
66 | |
67 bool close_on_deactivate() const { return close_on_deactivate_; } | |
68 void set_close_on_deactivate(bool close) { close_on_deactivate_ = close; } | |
69 | |
70 View* GetAnchorView() const; | |
71 Widget* anchor_widget() const { return anchor_widget_; } | |
72 | |
73 // The anchor rect is used in the absence of an assigned anchor view. | |
74 const gfx::Rect& anchor_rect() const { return anchor_rect_; } | |
75 | |
76 BubbleBorder::Arrow arrow() const { return arrow_; } | |
77 void set_arrow(BubbleBorder::Arrow arrow) { arrow_ = arrow; } | |
78 | |
79 BubbleBorder::Shadow shadow() const { return shadow_; } | |
80 void set_shadow(BubbleBorder::Shadow shadow) { shadow_ = shadow; } | |
81 | |
82 SkColor color() const { return color_; } | |
83 void set_color(SkColor color) { | |
84 color_ = color; | |
85 color_explicitly_set_ = true; | |
86 } | |
87 | |
88 const gfx::Insets& margins() const { return margins_; } | |
89 void set_margins(const gfx::Insets& margins) { margins_ = margins; } | |
90 | |
91 const gfx::Insets& anchor_view_insets() const { return anchor_view_insets_; } | |
92 void set_anchor_view_insets(const gfx::Insets& i) { anchor_view_insets_ = i; } | |
93 | |
94 gfx::NativeView parent_window() const { return parent_window_; } | |
95 void set_parent_window(gfx::NativeView window) { parent_window_ = window; } | |
96 | |
97 bool accept_events() const { return accept_events_; } | |
98 void set_accept_events(bool accept_events) { accept_events_ = accept_events; } | |
99 | |
100 bool border_accepts_events() const { return border_accepts_events_; } | |
101 void set_border_accepts_events(bool event) { border_accepts_events_ = event; } | |
102 | |
103 bool adjust_if_offscreen() const { return adjust_if_offscreen_; } | |
104 void set_adjust_if_offscreen(bool adjust) { adjust_if_offscreen_ = adjust; } | |
105 | |
106 CloseReason close_reason() const { return close_reason_; } | |
107 | |
108 // Get the arrow's anchor rect in screen space. | |
109 virtual gfx::Rect GetAnchorRect() const; | |
110 | |
111 // Allows delegates to provide custom parameters before widget initialization. | |
112 virtual void OnBeforeBubbleWidgetInit(Widget::InitParams* params, | |
113 Widget* widget) const; | |
114 | |
115 // Creates and returns a view to be displayed at the bottom of the bubble. | |
116 virtual View* CreateFootnoteView(); | |
117 | |
118 // Sets |margins_| to a default picked for smaller bubbles. | |
119 void UseCompactMargins(); | |
120 | |
121 // Sets the bubble alignment relative to the anchor. This may only be called | |
122 // after calling CreateBubble. | |
123 void SetAlignment(BubbleBorder::BubbleAlignment alignment); | |
124 | |
125 // Sets the bubble arrow paint type. | |
126 void SetArrowPaintType(BubbleBorder::ArrowPaintType paint_type); | |
127 | |
128 // Call this method when the anchor bounds have changed to reposition the | |
129 // bubble. The bubble is automatically repositioned when the anchor view | |
130 // bounds change as a result of the widget's bounds changing. | |
131 void OnAnchorBoundsChanged(); | |
132 | |
133 protected: | |
134 // Get bubble bounds from the anchor rect and client view's preferred size. | |
135 virtual gfx::Rect GetBubbleBounds(); | |
136 | |
137 // Return a FontList to use for the title of the bubble. | |
138 // (The default is MediumFont). | |
139 virtual const gfx::FontList& GetTitleFontList() const; | |
140 | |
141 // View overrides: | |
142 bool AcceleratorPressed(const ui::Accelerator& accelerator) override; | |
143 void OnNativeThemeChanged(const ui::NativeTheme* theme) override; | |
144 | |
145 // Perform view initialization on the contents for bubble sizing. | |
146 virtual void Init(); | |
147 | |
148 // Sets the anchor view or rect and repositions the bubble. Note that if a | |
149 // valid view gets passed, the anchor rect will get ignored. If the view gets | |
150 // deleted, but no new view gets set, the last known anchor postion will get | |
151 // returned. | |
152 void SetAnchorView(View* anchor_view); | |
153 void SetAnchorRect(const gfx::Rect& rect); | |
154 | |
155 // Resize and potentially move the bubble to fit the content's preferred size. | |
156 void SizeToContents(); | |
157 | |
158 BubbleFrameView* GetBubbleFrameView() const; | |
159 | |
160 private: | |
161 friend class BubbleBorderDelegate; | |
162 friend class BubbleWindowTargeter; | |
163 | |
164 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, CreateDelegate); | |
165 FRIEND_TEST_ALL_PREFIXES(BubbleDelegateTest, NonClientHitTest); | |
166 | |
167 // Update the bubble color from |theme|, unless it was explicitly set. | |
168 void UpdateColorsFromTheme(const ui::NativeTheme* theme); | |
169 | |
170 // Handles widget visibility changes. | |
171 void HandleVisibilityChanged(Widget* widget, bool visible); | |
172 | |
173 // Flags controlling bubble closure on the escape key and deactivation. | |
174 bool close_on_esc_; | |
175 bool close_on_deactivate_; | |
176 | |
177 // The view and widget to which this bubble is anchored. Since an anchor view | |
178 // can be deleted without notice, we store it in the ViewStorage and retrieve | |
179 // it from there. It will make sure that the view is still valid. | |
180 const int anchor_view_storage_id_; | |
181 Widget* anchor_widget_; | |
182 | |
183 // The anchor rect used in the absence of an anchor view. | |
184 mutable gfx::Rect anchor_rect_; | |
185 | |
186 // The arrow's location on the bubble. | |
187 BubbleBorder::Arrow arrow_; | |
188 | |
189 // Bubble border shadow to use. | |
190 BubbleBorder::Shadow shadow_; | |
191 | |
192 // The background color of the bubble; and flag for when it's explicitly set. | |
193 SkColor color_; | |
194 bool color_explicitly_set_; | |
195 | |
196 // The margins between the content and the inside of the border. | |
197 gfx::Insets margins_; | |
198 | |
199 // Insets applied to the |anchor_view_| bounds. | |
200 gfx::Insets anchor_view_insets_; | |
201 | |
202 // Specifies whether the bubble (or its border) handles mouse events, etc. | |
203 bool accept_events_; | |
204 bool border_accepts_events_; | |
205 | |
206 // If true (defaults to true), the arrow may be mirrored and moved to fit the | |
207 // bubble on screen better. It would be a no-op if the bubble has no arrow. | |
208 bool adjust_if_offscreen_; | |
209 | |
210 // Parent native window of the bubble. | |
211 gfx::NativeView parent_window_; | |
212 | |
213 CloseReason close_reason_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(BubbleDelegateView); | |
216 }; | |
217 | |
218 } // namespace views | |
219 | |
220 #endif // UI_VIEWS_BUBBLE_BUBBLE_DELEGATE_H_ | |
OLD | NEW |