OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 #include "ash/frame/caption_buttons/maximize_bubble_controller_bubble.h" | |
6 | |
7 #include "ash/frame/caption_buttons/bubble_contents_button_row.h" | |
8 #include "ash/frame/caption_buttons/frame_maximize_button.h" | |
9 #include "ash/frame/caption_buttons/maximize_bubble_controller.h" | |
10 #include "ash/metrics/user_metrics_recorder.h" | |
11 #include "ash/shell.h" | |
12 #include "ash/shell_window_ids.h" | |
13 #include "grit/ash_strings.h" | |
14 #include "ui/base/resource/resource_bundle.h" | |
15 #include "ui/gfx/canvas.h" | |
16 #include "ui/gfx/path.h" | |
17 #include "ui/views/bubble/bubble_frame_view.h" | |
18 #include "ui/views/controls/label.h" | |
19 #include "ui/views/layout/box_layout.h" | |
20 #include "ui/views/mouse_watcher.h" | |
21 #include "ui/wm/core/masked_window_targeter.h" | |
22 | |
23 namespace ash { | |
24 | |
25 // BubbleContentsView --------------------------------------------------------- | |
26 | |
27 // A class which creates the content of the bubble: The buttons, and the label. | |
28 class BubbleContentsView : public views::View { | |
29 public: | |
30 BubbleContentsView(MaximizeBubbleControllerBubble* bubble, | |
31 SnapType initial_snap_type); | |
32 virtual ~BubbleContentsView(); | |
33 | |
34 // Set the label content to reflect the currently selected |snap_type|. | |
35 // This function can be executed through the frame maximize button as well as | |
36 // through hover operations. | |
37 void SetSnapType(SnapType snap_type); | |
38 | |
39 // Added for unit test: Retrieve the button for an action. | |
40 // |state| can be either SNAP_LEFT, SNAP_RIGHT or SNAP_MINIMIZE. | |
41 views::CustomButton* GetButtonForUnitTest(SnapType state); | |
42 | |
43 private: | |
44 // The owning class. | |
45 MaximizeBubbleControllerBubble* bubble_; | |
46 | |
47 // The object which owns all the buttons. | |
48 BubbleContentsButtonRow* buttons_view_; | |
49 | |
50 // The label object which shows the user the selected action. | |
51 views::Label* label_view_; | |
52 | |
53 DISALLOW_COPY_AND_ASSIGN(BubbleContentsView); | |
54 }; | |
55 | |
56 BubbleContentsView::BubbleContentsView( | |
57 MaximizeBubbleControllerBubble* bubble, | |
58 SnapType initial_snap_type) | |
59 : bubble_(bubble), | |
60 buttons_view_(NULL), | |
61 label_view_(NULL) { | |
62 SetLayoutManager(new views::BoxLayout( | |
63 views::BoxLayout::kVertical, 0, 0, | |
64 MaximizeBubbleControllerBubble::kLayoutSpacing)); | |
65 set_background(views::Background::CreateSolidBackground( | |
66 MaximizeBubbleControllerBubble::kBubbleBackgroundColor)); | |
67 | |
68 buttons_view_ = new BubbleContentsButtonRow(bubble); | |
69 AddChildView(buttons_view_); | |
70 | |
71 label_view_ = new views::Label(); | |
72 SetSnapType(initial_snap_type); | |
73 label_view_->SetBackgroundColor( | |
74 MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
75 const SkColor kBubbleTextColor = SK_ColorWHITE; | |
76 label_view_->SetEnabledColor(kBubbleTextColor); | |
77 const int kLabelSpacing = 4; | |
78 label_view_->SetBorder( | |
79 views::Border::CreateEmptyBorder(kLabelSpacing, 0, kLabelSpacing, 0)); | |
80 AddChildView(label_view_); | |
81 } | |
82 | |
83 BubbleContentsView::~BubbleContentsView() { | |
84 } | |
85 | |
86 // Set the label content to reflect the currently selected |snap_type|. | |
87 // This function can be executed through the frame maximize button as well as | |
88 // through hover operations. | |
89 void BubbleContentsView::SetSnapType(SnapType snap_type) { | |
90 if (!bubble_->controller()) | |
91 return; | |
92 | |
93 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
94 int id = 0; | |
95 switch (snap_type) { | |
96 case SNAP_LEFT: | |
97 id = IDS_ASH_SNAP_WINDOW_LEFT; | |
98 break; | |
99 case SNAP_RIGHT: | |
100 id = IDS_ASH_SNAP_WINDOW_RIGHT; | |
101 break; | |
102 case SNAP_MAXIMIZE: | |
103 DCHECK_NE(FRAME_STATE_FULL, bubble_->controller()->maximize_type()); | |
104 id = IDS_ASH_MAXIMIZE_WINDOW; | |
105 break; | |
106 case SNAP_MINIMIZE: | |
107 id = IDS_ASH_MINIMIZE_WINDOW; | |
108 break; | |
109 case SNAP_RESTORE: | |
110 DCHECK_NE(FRAME_STATE_NONE, bubble_->controller()->maximize_type()); | |
111 id = IDS_ASH_RESTORE_WINDOW; | |
112 break; | |
113 default: | |
114 // If nothing is selected, we automatically select the click operation. | |
115 id = bubble_->controller()->maximize_type() == FRAME_STATE_FULL ? | |
116 IDS_ASH_RESTORE_WINDOW : IDS_ASH_MAXIMIZE_WINDOW; | |
117 break; | |
118 } | |
119 label_view_->SetText(rb.GetLocalizedString(id)); | |
120 } | |
121 | |
122 views::CustomButton* BubbleContentsView::GetButtonForUnitTest(SnapType state) { | |
123 return buttons_view_->GetButtonForUnitTest(state); | |
124 } | |
125 | |
126 | |
127 // MaximizeBubbleBorder ------------------------------------------------------- | |
128 | |
129 namespace { | |
130 | |
131 const int kLineWidth = 1; | |
132 const int kArrowHeight = 10; | |
133 const int kArrowWidth = 20; | |
134 | |
135 } // namespace | |
136 | |
137 class MaximizeBubbleBorder : public views::BubbleBorder { | |
138 public: | |
139 MaximizeBubbleBorder(views::View* content_view, views::View* anchor); | |
140 | |
141 virtual ~MaximizeBubbleBorder() {} | |
142 | |
143 // Get the mouse active area of the window. | |
144 void GetMask(gfx::Path* mask); | |
145 | |
146 // views::BubbleBorder: | |
147 virtual gfx::Rect GetBounds(const gfx::Rect& position_relative_to, | |
148 const gfx::Size& contents_size) const OVERRIDE; | |
149 virtual void Paint(const views::View& view, gfx::Canvas* canvas) OVERRIDE; | |
150 virtual gfx::Size GetMinimumSize() const OVERRIDE; | |
151 | |
152 private: | |
153 // Note: Animations can continue after then main window frame was destroyed. | |
154 // To avoid this problem, the owning screen metrics get extracted upon | |
155 // creation. | |
156 gfx::Size anchor_size_; | |
157 gfx::Point anchor_screen_origin_; | |
158 views::View* content_view_; | |
159 | |
160 DISALLOW_COPY_AND_ASSIGN(MaximizeBubbleBorder); | |
161 }; | |
162 | |
163 MaximizeBubbleBorder::MaximizeBubbleBorder(views::View* content_view, | |
164 views::View* anchor) | |
165 : views::BubbleBorder( | |
166 views::BubbleBorder::TOP_RIGHT, views::BubbleBorder::NO_SHADOW, | |
167 MaximizeBubbleControllerBubble::kBubbleBackgroundColor), | |
168 anchor_size_(anchor->size()), | |
169 anchor_screen_origin_(0, 0), | |
170 content_view_(content_view) { | |
171 views::View::ConvertPointToScreen(anchor, &anchor_screen_origin_); | |
172 set_alignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | |
173 } | |
174 | |
175 void MaximizeBubbleBorder::GetMask(gfx::Path* mask) { | |
176 gfx::Insets inset = GetInsets(); | |
177 // Note: Even though the tip could be added as activatable, it is left out | |
178 // since it would not change the action behavior in any way plus it makes | |
179 // more sense to keep the focus on the underlying button for clicks. | |
180 int left = inset.left() - kLineWidth; | |
181 int right = inset.left() + content_view_->width() + kLineWidth; | |
182 int top = inset.top() - kLineWidth; | |
183 int bottom = inset.top() + content_view_->height() + kLineWidth; | |
184 mask->moveTo(left, top); | |
185 mask->lineTo(right, top); | |
186 mask->lineTo(right, bottom); | |
187 mask->lineTo(left, bottom); | |
188 mask->lineTo(left, top); | |
189 mask->close(); | |
190 } | |
191 | |
192 gfx::Rect MaximizeBubbleBorder::GetBounds( | |
193 const gfx::Rect& position_relative_to, | |
194 const gfx::Size& contents_size) const { | |
195 gfx::Size border_size(contents_size); | |
196 gfx::Insets insets = GetInsets(); | |
197 border_size.Enlarge(insets.width(), insets.height()); | |
198 | |
199 // Position the bubble to center the box on the anchor. | |
200 int x = (anchor_size_.width() - border_size.width()) / 2; | |
201 // Position the bubble under the anchor, overlapping the arrow with it. | |
202 int y = anchor_size_.height() - insets.top(); | |
203 | |
204 gfx::Point view_origin(x + anchor_screen_origin_.x(), | |
205 y + anchor_screen_origin_.y()); | |
206 | |
207 return gfx::Rect(view_origin, border_size); | |
208 } | |
209 | |
210 void MaximizeBubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) { | |
211 gfx::Insets inset = GetInsets(); | |
212 | |
213 // Draw the border line around everything. | |
214 int y = inset.top(); | |
215 // Top | |
216 canvas->FillRect(gfx::Rect(inset.left(), | |
217 y - kLineWidth, | |
218 content_view_->width(), | |
219 kLineWidth), | |
220 MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
221 // Bottom | |
222 canvas->FillRect(gfx::Rect(inset.left(), | |
223 y + content_view_->height(), | |
224 content_view_->width(), | |
225 kLineWidth), | |
226 MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
227 // Left | |
228 canvas->FillRect(gfx::Rect(inset.left() - kLineWidth, | |
229 y - kLineWidth, | |
230 kLineWidth, | |
231 content_view_->height() + 2 * kLineWidth), | |
232 MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
233 // Right | |
234 canvas->FillRect(gfx::Rect(inset.left() + content_view_->width(), | |
235 y - kLineWidth, | |
236 kLineWidth, | |
237 content_view_->height() + 2 * kLineWidth), | |
238 MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
239 | |
240 // Draw the arrow afterwards covering the border. | |
241 SkPath path; | |
242 path.incReserve(4); | |
243 // The center of the tip should be in the middle of the button. | |
244 int tip_x = inset.left() + content_view_->width() / 2; | |
245 int left_base_x = tip_x - kArrowWidth / 2; | |
246 int left_base_y = y; | |
247 int tip_y = left_base_y - kArrowHeight; | |
248 path.moveTo(SkIntToScalar(left_base_x), SkIntToScalar(left_base_y)); | |
249 path.lineTo(SkIntToScalar(tip_x), SkIntToScalar(tip_y)); | |
250 path.lineTo(SkIntToScalar(left_base_x + kArrowWidth), | |
251 SkIntToScalar(left_base_y)); | |
252 | |
253 SkPaint paint; | |
254 paint.setStyle(SkPaint::kFill_Style); | |
255 paint.setColor(MaximizeBubbleControllerBubble::kBubbleBackgroundColor); | |
256 canvas->DrawPath(path, paint); | |
257 } | |
258 | |
259 gfx::Size MaximizeBubbleBorder::GetMinimumSize() const { | |
260 return gfx::Size(kLineWidth * 2 + kArrowWidth, | |
261 std::max(kLineWidth, kArrowHeight) + kLineWidth); | |
262 } | |
263 | |
264 namespace { | |
265 | |
266 // MaximizebubbleTargeter ----------------------------------------------------- | |
267 | |
268 // Window targeter used for the bubble. | |
269 class MaximizeBubbleTargeter : public ::wm::MaskedWindowTargeter { | |
270 public: | |
271 MaximizeBubbleTargeter(aura::Window* window, | |
272 MaximizeBubbleBorder* border) | |
273 : ::wm::MaskedWindowTargeter(window), | |
274 border_(border) { | |
275 } | |
276 | |
277 virtual ~MaximizeBubbleTargeter() {} | |
278 | |
279 private: | |
280 // ::wm::MaskedWindowTargeter: | |
281 virtual bool GetHitTestMask(aura::Window* window, | |
282 gfx::Path* mask) const OVERRIDE { | |
283 border_->GetMask(mask); | |
284 return true; | |
285 } | |
286 | |
287 MaximizeBubbleBorder* border_; | |
288 | |
289 DISALLOW_COPY_AND_ASSIGN(MaximizeBubbleTargeter); | |
290 }; | |
291 | |
292 } // namespace | |
293 | |
294 | |
295 // BubbleMouseWatcherHost ----------------------------------------------------- | |
296 | |
297 // The mouse watcher host which makes sure that the bubble does not get closed | |
298 // while the mouse cursor is over the maximize button or the balloon content. | |
299 // Note: This object gets destroyed when the MouseWatcher gets destroyed. | |
300 class BubbleMouseWatcherHost: public views::MouseWatcherHost { | |
301 public: | |
302 explicit BubbleMouseWatcherHost(MaximizeBubbleControllerBubble* bubble); | |
303 virtual ~BubbleMouseWatcherHost(); | |
304 | |
305 // views::MouseWatcherHost: | |
306 virtual bool Contains(const gfx::Point& screen_point, | |
307 views::MouseWatcherHost::MouseEventType type) OVERRIDE; | |
308 private: | |
309 MaximizeBubbleControllerBubble* bubble_; | |
310 | |
311 DISALLOW_COPY_AND_ASSIGN(BubbleMouseWatcherHost); | |
312 }; | |
313 | |
314 BubbleMouseWatcherHost::BubbleMouseWatcherHost( | |
315 MaximizeBubbleControllerBubble* bubble) | |
316 : bubble_(bubble) { | |
317 } | |
318 | |
319 BubbleMouseWatcherHost::~BubbleMouseWatcherHost() { | |
320 } | |
321 | |
322 bool BubbleMouseWatcherHost::Contains( | |
323 const gfx::Point& screen_point, | |
324 views::MouseWatcherHost::MouseEventType type) { | |
325 return bubble_->Contains(screen_point, type); | |
326 } | |
327 | |
328 | |
329 // MaximizeBubbleControllerBubble --------------------------------------------- | |
330 | |
331 // static | |
332 const SkColor MaximizeBubbleControllerBubble::kBubbleBackgroundColor = | |
333 0xFF141414; | |
334 const int MaximizeBubbleControllerBubble::kLayoutSpacing = -1; | |
335 | |
336 MaximizeBubbleControllerBubble::MaximizeBubbleControllerBubble( | |
337 MaximizeBubbleController* owner, | |
338 int appearance_delay_ms, | |
339 SnapType initial_snap_type) | |
340 : views::BubbleDelegateView(owner->frame_maximize_button(), | |
341 views::BubbleBorder::TOP_RIGHT), | |
342 shutting_down_(false), | |
343 owner_(owner), | |
344 contents_view_(NULL), | |
345 bubble_border_(NULL), | |
346 appearance_delay_ms_(appearance_delay_ms) { | |
347 set_margins(gfx::Insets()); | |
348 | |
349 // The window needs to be owned by the root so that the phantom window does | |
350 // not cover it upon animation. | |
351 aura::Window* parent = Shell::GetContainer(Shell::GetTargetRootWindow(), | |
352 kShellWindowId_ShelfContainer); | |
353 set_parent_window(parent); | |
354 | |
355 set_notify_enter_exit_on_child(true); | |
356 set_adjust_if_offscreen(false); | |
357 SetPaintToLayer(true); | |
358 set_color(kBubbleBackgroundColor); | |
359 set_close_on_deactivate(false); | |
360 set_background( | |
361 views::Background::CreateSolidBackground(kBubbleBackgroundColor)); | |
362 | |
363 SetLayoutManager(new views::BoxLayout( | |
364 views::BoxLayout::kVertical, 0, 0, kLayoutSpacing)); | |
365 | |
366 contents_view_ = new BubbleContentsView(this, initial_snap_type); | |
367 AddChildView(contents_view_); | |
368 | |
369 // Note that the returned widget has an observer which points to our | |
370 // functions. | |
371 views::Widget* bubble_widget = views::BubbleDelegateView::CreateBubble(this); | |
372 bubble_widget->set_focus_on_creation(false); | |
373 | |
374 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE); | |
375 bubble_widget->non_client_view()->frame_view()->set_background(NULL); | |
376 | |
377 bubble_border_ = new MaximizeBubbleBorder(this, GetAnchorView()); | |
378 GetBubbleFrameView()->SetBubbleBorder( | |
379 scoped_ptr<views::BubbleBorder>(bubble_border_)); | |
380 GetBubbleFrameView()->set_background(NULL); | |
381 | |
382 // Recalculate size with new border. | |
383 SizeToContents(); | |
384 | |
385 GetWidget()->Show(); | |
386 | |
387 aura::Window* window = bubble_widget->GetNativeWindow(); | |
388 window->SetEventTargeter(scoped_ptr<ui::EventTargeter>( | |
389 new MaximizeBubbleTargeter(window, bubble_border_))); | |
390 | |
391 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction( | |
392 ash::UMA_WINDOW_MAXIMIZE_BUTTON_SHOW_BUBBLE); | |
393 | |
394 mouse_watcher_.reset(new views::MouseWatcher( | |
395 new BubbleMouseWatcherHost(this), | |
396 this)); | |
397 mouse_watcher_->Start(); | |
398 } | |
399 | |
400 MaximizeBubbleControllerBubble::~MaximizeBubbleControllerBubble() { | |
401 } | |
402 | |
403 aura::Window* MaximizeBubbleControllerBubble::GetBubbleWindow() { | |
404 return GetWidget()->GetNativeWindow(); | |
405 } | |
406 | |
407 gfx::Rect MaximizeBubbleControllerBubble::GetAnchorRect() { | |
408 if (!owner_) | |
409 return gfx::Rect(); | |
410 | |
411 gfx::Rect anchor_rect = | |
412 owner_->frame_maximize_button()->GetBoundsInScreen(); | |
413 return anchor_rect; | |
414 } | |
415 | |
416 bool MaximizeBubbleControllerBubble::CanActivate() const { | |
417 return false; | |
418 } | |
419 | |
420 bool MaximizeBubbleControllerBubble::WidgetHasHitTestMask() const { | |
421 return bubble_border_ != NULL; | |
422 } | |
423 | |
424 void MaximizeBubbleControllerBubble::GetWidgetHitTestMask( | |
425 gfx::Path* mask) const { | |
426 DCHECK(mask); | |
427 DCHECK(bubble_border_); | |
428 bubble_border_->GetMask(mask); | |
429 } | |
430 | |
431 void MaximizeBubbleControllerBubble::MouseMovedOutOfHost() { | |
432 if (!owner_ || shutting_down_) | |
433 return; | |
434 // When we leave the bubble, we might be still be in gesture mode or over | |
435 // the maximize button. So only close if none of the other cases apply. | |
436 if (!owner_->frame_maximize_button()->is_snap_enabled()) { | |
437 gfx::Point screen_location = Shell::GetScreen()->GetCursorScreenPoint(); | |
438 if (!owner_->frame_maximize_button()->GetBoundsInScreen().Contains( | |
439 screen_location)) { | |
440 owner_->RequestDestructionThroughOwner(); | |
441 } | |
442 } | |
443 } | |
444 | |
445 bool MaximizeBubbleControllerBubble::Contains( | |
446 const gfx::Point& screen_point, | |
447 views::MouseWatcherHost::MouseEventType type) { | |
448 if (!owner_ || shutting_down_) | |
449 return false; | |
450 bool inside_button = | |
451 owner_->frame_maximize_button()->GetBoundsInScreen().Contains( | |
452 screen_point); | |
453 if (!owner_->frame_maximize_button()->is_snap_enabled() && inside_button) { | |
454 SetSnapType(controller()->maximize_type() == FRAME_STATE_FULL ? | |
455 SNAP_RESTORE : SNAP_MAXIMIZE); | |
456 return true; | |
457 } | |
458 // Check if either a gesture is taking place (=> bubble stays no matter what | |
459 // the mouse does) or the mouse is over the maximize button or the bubble | |
460 // content. | |
461 return (owner_->frame_maximize_button()->is_snap_enabled() || | |
462 inside_button || | |
463 contents_view_->GetBoundsInScreen().Contains(screen_point)); | |
464 } | |
465 | |
466 gfx::Size MaximizeBubbleControllerBubble::GetPreferredSize() { | |
467 return contents_view_->GetPreferredSize(); | |
468 } | |
469 | |
470 void MaximizeBubbleControllerBubble::OnWidgetDestroying(views::Widget* widget) { | |
471 if (GetWidget() == widget) { | |
472 mouse_watcher_->Stop(); | |
473 | |
474 if (owner_) { | |
475 // If the bubble destruction was triggered by some other external | |
476 // influence then ourselves, the owner needs to be informed that the menu | |
477 // is gone. | |
478 shutting_down_ = true; | |
479 owner_->RequestDestructionThroughOwner(); | |
480 owner_ = NULL; | |
481 } | |
482 } | |
483 BubbleDelegateView::OnWidgetDestroying(widget); | |
484 } | |
485 | |
486 void MaximizeBubbleControllerBubble::ControllerRequestsCloseAndDelete() { | |
487 // This only gets called from the owning base class once it is deleted. | |
488 if (shutting_down_) | |
489 return; | |
490 shutting_down_ = true; | |
491 owner_ = NULL; | |
492 | |
493 // Close the widget asynchronously after the hide animation is finished. | |
494 if (!appearance_delay_ms_) | |
495 GetWidget()->CloseNow(); | |
496 else | |
497 GetWidget()->Close(); | |
498 } | |
499 | |
500 void MaximizeBubbleControllerBubble::SetSnapType(SnapType snap_type) { | |
501 if (contents_view_) | |
502 contents_view_->SetSnapType(snap_type); | |
503 } | |
504 | |
505 views::CustomButton* MaximizeBubbleControllerBubble::GetButtonForUnitTest( | |
506 SnapType state) { | |
507 return contents_view_->GetButtonForUnitTest(state); | |
508 } | |
509 | |
510 } // namespace ash | |
OLD | NEW |