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