Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / ash / frame / caption_buttons / maximize_bubble_controller_bubble.cc
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(
352       Shell::GetTargetRootWindow(),
353       internal::kShellWindowId_ShelfContainer);
354   set_parent_window(parent);
355
356   set_notify_enter_exit_on_child(true);
357   set_adjust_if_offscreen(false);
358   SetPaintToLayer(true);
359   set_color(kBubbleBackgroundColor);
360   set_close_on_deactivate(false);
361   set_background(
362       views::Background::CreateSolidBackground(kBubbleBackgroundColor));
363
364   SetLayoutManager(new views::BoxLayout(
365       views::BoxLayout::kVertical, 0, 0, kLayoutSpacing));
366
367   contents_view_ = new BubbleContentsView(this, initial_snap_type);
368   AddChildView(contents_view_);
369
370   // Note that the returned widget has an observer which points to our
371   // functions.
372   views::Widget* bubble_widget = views::BubbleDelegateView::CreateBubble(this);
373   bubble_widget->set_focus_on_creation(false);
374
375   SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE);
376   bubble_widget->non_client_view()->frame_view()->set_background(NULL);
377
378   bubble_border_ = new MaximizeBubbleBorder(this, GetAnchorView());
379   GetBubbleFrameView()->SetBubbleBorder(
380       scoped_ptr<views::BubbleBorder>(bubble_border_));
381   GetBubbleFrameView()->set_background(NULL);
382
383   // Recalculate size with new border.
384   SizeToContents();
385
386   GetWidget()->Show();
387
388   aura::Window* window = bubble_widget->GetNativeWindow();
389   window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
390       new MaximizeBubbleTargeter(window, bubble_border_)));
391
392   ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(
393       ash::UMA_WINDOW_MAXIMIZE_BUTTON_SHOW_BUBBLE);
394
395   mouse_watcher_.reset(new views::MouseWatcher(
396       new BubbleMouseWatcherHost(this),
397       this));
398   mouse_watcher_->Start();
399 }
400
401 MaximizeBubbleControllerBubble::~MaximizeBubbleControllerBubble() {
402 }
403
404 aura::Window* MaximizeBubbleControllerBubble::GetBubbleWindow() {
405   return GetWidget()->GetNativeWindow();
406 }
407
408 gfx::Rect MaximizeBubbleControllerBubble::GetAnchorRect() {
409   if (!owner_)
410     return gfx::Rect();
411
412   gfx::Rect anchor_rect =
413       owner_->frame_maximize_button()->GetBoundsInScreen();
414   return anchor_rect;
415 }
416
417 bool MaximizeBubbleControllerBubble::CanActivate() const {
418   return false;
419 }
420
421 bool MaximizeBubbleControllerBubble::WidgetHasHitTestMask() const {
422   return bubble_border_ != NULL;
423 }
424
425 void MaximizeBubbleControllerBubble::GetWidgetHitTestMask(
426     gfx::Path* mask) const {
427   DCHECK(mask);
428   DCHECK(bubble_border_);
429   bubble_border_->GetMask(mask);
430 }
431
432 void MaximizeBubbleControllerBubble::MouseMovedOutOfHost() {
433   if (!owner_ || shutting_down_)
434     return;
435   // When we leave the bubble, we might be still be in gesture mode or over
436   // the maximize button. So only close if none of the other cases apply.
437   if (!owner_->frame_maximize_button()->is_snap_enabled()) {
438     gfx::Point screen_location = Shell::GetScreen()->GetCursorScreenPoint();
439     if (!owner_->frame_maximize_button()->GetBoundsInScreen().Contains(
440         screen_location)) {
441       owner_->RequestDestructionThroughOwner();
442     }
443   }
444 }
445
446 bool MaximizeBubbleControllerBubble::Contains(
447     const gfx::Point& screen_point,
448     views::MouseWatcherHost::MouseEventType type) {
449   if (!owner_ || shutting_down_)
450     return false;
451   bool inside_button =
452       owner_->frame_maximize_button()->GetBoundsInScreen().Contains(
453           screen_point);
454   if (!owner_->frame_maximize_button()->is_snap_enabled() && inside_button) {
455     SetSnapType(controller()->maximize_type() == FRAME_STATE_FULL ?
456         SNAP_RESTORE : SNAP_MAXIMIZE);
457     return true;
458   }
459   // Check if either a gesture is taking place (=> bubble stays no matter what
460   // the mouse does) or the mouse is over the maximize button or the bubble
461   // content.
462   return (owner_->frame_maximize_button()->is_snap_enabled() ||
463           inside_button ||
464           contents_view_->GetBoundsInScreen().Contains(screen_point));
465 }
466
467 gfx::Size MaximizeBubbleControllerBubble::GetPreferredSize() {
468   return contents_view_->GetPreferredSize();
469 }
470
471 void MaximizeBubbleControllerBubble::OnWidgetDestroying(views::Widget* widget) {
472   if (GetWidget() == widget) {
473     mouse_watcher_->Stop();
474
475     if (owner_) {
476       // If the bubble destruction was triggered by some other external
477       // influence then ourselves, the owner needs to be informed that the menu
478       // is gone.
479       shutting_down_ = true;
480       owner_->RequestDestructionThroughOwner();
481       owner_ = NULL;
482     }
483   }
484   BubbleDelegateView::OnWidgetDestroying(widget);
485 }
486
487 void MaximizeBubbleControllerBubble::ControllerRequestsCloseAndDelete() {
488   // This only gets called from the owning base class once it is deleted.
489   if (shutting_down_)
490     return;
491   shutting_down_ = true;
492   owner_ = NULL;
493
494   // Close the widget asynchronously after the hide animation is finished.
495   if (!appearance_delay_ms_)
496     GetWidget()->CloseNow();
497   else
498     GetWidget()->Close();
499 }
500
501 void MaximizeBubbleControllerBubble::SetSnapType(SnapType snap_type) {
502   if (contents_view_)
503     contents_view_->SetSnapType(snap_type);
504 }
505
506 views::CustomButton* MaximizeBubbleControllerBubble::GetButtonForUnitTest(
507     SnapType state) {
508   return contents_view_->GetButtonForUnitTest(state);
509 }
510
511 }  // namespace ash