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.
5 #include "ash/frame/caption_buttons/maximize_bubble_controller_bubble.h"
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"
25 // BubbleContentsView ---------------------------------------------------------
27 // A class which creates the content of the bubble: The buttons, and the label.
28 class BubbleContentsView : public views::View {
30 BubbleContentsView(MaximizeBubbleControllerBubble* bubble,
31 SnapType initial_snap_type);
32 virtual ~BubbleContentsView();
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);
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);
45 MaximizeBubbleControllerBubble* bubble_;
47 // The object which owns all the buttons.
48 BubbleContentsButtonRow* buttons_view_;
50 // The label object which shows the user the selected action.
51 views::Label* label_view_;
53 DISALLOW_COPY_AND_ASSIGN(BubbleContentsView);
56 BubbleContentsView::BubbleContentsView(
57 MaximizeBubbleControllerBubble* bubble,
58 SnapType initial_snap_type)
62 SetLayoutManager(new views::BoxLayout(
63 views::BoxLayout::kVertical, 0, 0,
64 MaximizeBubbleControllerBubble::kLayoutSpacing));
65 set_background(views::Background::CreateSolidBackground(
66 MaximizeBubbleControllerBubble::kBubbleBackgroundColor));
68 buttons_view_ = new BubbleContentsButtonRow(bubble);
69 AddChildView(buttons_view_);
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_);
83 BubbleContentsView::~BubbleContentsView() {
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())
93 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
97 id = IDS_ASH_SNAP_WINDOW_LEFT;
100 id = IDS_ASH_SNAP_WINDOW_RIGHT;
103 DCHECK_NE(FRAME_STATE_FULL, bubble_->controller()->maximize_type());
104 id = IDS_ASH_MAXIMIZE_WINDOW;
107 id = IDS_ASH_MINIMIZE_WINDOW;
110 DCHECK_NE(FRAME_STATE_NONE, bubble_->controller()->maximize_type());
111 id = IDS_ASH_RESTORE_WINDOW;
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;
119 label_view_->SetText(rb.GetLocalizedString(id));
122 views::CustomButton* BubbleContentsView::GetButtonForUnitTest(SnapType state) {
123 return buttons_view_->GetButtonForUnitTest(state);
127 // MaximizeBubbleBorder -------------------------------------------------------
131 const int kLineWidth = 1;
132 const int kArrowHeight = 10;
133 const int kArrowWidth = 20;
137 class MaximizeBubbleBorder : public views::BubbleBorder {
139 MaximizeBubbleBorder(views::View* content_view, views::View* anchor);
141 virtual ~MaximizeBubbleBorder() {}
143 // Get the mouse active area of the window.
144 void GetMask(gfx::Path* mask);
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;
153 // Note: Animations can continue after then main window frame was destroyed.
154 // To avoid this problem, the owning screen metrics get extracted upon
156 gfx::Size anchor_size_;
157 gfx::Point anchor_screen_origin_;
158 views::View* content_view_;
160 DISALLOW_COPY_AND_ASSIGN(MaximizeBubbleBorder);
163 MaximizeBubbleBorder::MaximizeBubbleBorder(views::View* content_view,
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);
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);
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());
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();
204 gfx::Point view_origin(x + anchor_screen_origin_.x(),
205 y + anchor_screen_origin_.y());
207 return gfx::Rect(view_origin, border_size);
210 void MaximizeBubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) {
211 gfx::Insets inset = GetInsets();
213 // Draw the border line around everything.
216 canvas->FillRect(gfx::Rect(inset.left(),
218 content_view_->width(),
220 MaximizeBubbleControllerBubble::kBubbleBackgroundColor);
222 canvas->FillRect(gfx::Rect(inset.left(),
223 y + content_view_->height(),
224 content_view_->width(),
226 MaximizeBubbleControllerBubble::kBubbleBackgroundColor);
228 canvas->FillRect(gfx::Rect(inset.left() - kLineWidth,
231 content_view_->height() + 2 * kLineWidth),
232 MaximizeBubbleControllerBubble::kBubbleBackgroundColor);
234 canvas->FillRect(gfx::Rect(inset.left() + content_view_->width(),
237 content_view_->height() + 2 * kLineWidth),
238 MaximizeBubbleControllerBubble::kBubbleBackgroundColor);
240 // Draw the arrow afterwards covering the border.
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;
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));
254 paint.setStyle(SkPaint::kFill_Style);
255 paint.setColor(MaximizeBubbleControllerBubble::kBubbleBackgroundColor);
256 canvas->DrawPath(path, paint);
259 gfx::Size MaximizeBubbleBorder::GetMinimumSize() const {
260 return gfx::Size(kLineWidth * 2 + kArrowWidth,
261 std::max(kLineWidth, kArrowHeight) + kLineWidth);
266 // MaximizebubbleTargeter -----------------------------------------------------
268 // Window targeter used for the bubble.
269 class MaximizeBubbleTargeter : public ::wm::MaskedWindowTargeter {
271 MaximizeBubbleTargeter(aura::Window* window,
272 MaximizeBubbleBorder* border)
273 : ::wm::MaskedWindowTargeter(window),
277 virtual ~MaximizeBubbleTargeter() {}
280 // ::wm::MaskedWindowTargeter:
281 virtual bool GetHitTestMask(aura::Window* window,
282 gfx::Path* mask) const OVERRIDE {
283 border_->GetMask(mask);
287 MaximizeBubbleBorder* border_;
289 DISALLOW_COPY_AND_ASSIGN(MaximizeBubbleTargeter);
295 // BubbleMouseWatcherHost -----------------------------------------------------
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 {
302 explicit BubbleMouseWatcherHost(MaximizeBubbleControllerBubble* bubble);
303 virtual ~BubbleMouseWatcherHost();
305 // views::MouseWatcherHost:
306 virtual bool Contains(const gfx::Point& screen_point,
307 views::MouseWatcherHost::MouseEventType type) OVERRIDE;
309 MaximizeBubbleControllerBubble* bubble_;
311 DISALLOW_COPY_AND_ASSIGN(BubbleMouseWatcherHost);
314 BubbleMouseWatcherHost::BubbleMouseWatcherHost(
315 MaximizeBubbleControllerBubble* bubble)
319 BubbleMouseWatcherHost::~BubbleMouseWatcherHost() {
322 bool BubbleMouseWatcherHost::Contains(
323 const gfx::Point& screen_point,
324 views::MouseWatcherHost::MouseEventType type) {
325 return bubble_->Contains(screen_point, type);
329 // MaximizeBubbleControllerBubble ---------------------------------------------
332 const SkColor MaximizeBubbleControllerBubble::kBubbleBackgroundColor =
334 const int MaximizeBubbleControllerBubble::kLayoutSpacing = -1;
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),
344 contents_view_(NULL),
345 bubble_border_(NULL),
346 appearance_delay_ms_(appearance_delay_ms) {
347 set_margins(gfx::Insets());
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);
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);
362 views::Background::CreateSolidBackground(kBubbleBackgroundColor));
364 SetLayoutManager(new views::BoxLayout(
365 views::BoxLayout::kVertical, 0, 0, kLayoutSpacing));
367 contents_view_ = new BubbleContentsView(this, initial_snap_type);
368 AddChildView(contents_view_);
370 // Note that the returned widget has an observer which points to our
372 views::Widget* bubble_widget = views::BubbleDelegateView::CreateBubble(this);
373 bubble_widget->set_focus_on_creation(false);
375 SetAlignment(views::BubbleBorder::ALIGN_EDGE_TO_ANCHOR_EDGE);
376 bubble_widget->non_client_view()->frame_view()->set_background(NULL);
378 bubble_border_ = new MaximizeBubbleBorder(this, GetAnchorView());
379 GetBubbleFrameView()->SetBubbleBorder(
380 scoped_ptr<views::BubbleBorder>(bubble_border_));
381 GetBubbleFrameView()->set_background(NULL);
383 // Recalculate size with new border.
388 aura::Window* window = bubble_widget->GetNativeWindow();
389 window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
390 new MaximizeBubbleTargeter(window, bubble_border_)));
392 ash::Shell::GetInstance()->metrics()->RecordUserMetricsAction(
393 ash::UMA_WINDOW_MAXIMIZE_BUTTON_SHOW_BUBBLE);
395 mouse_watcher_.reset(new views::MouseWatcher(
396 new BubbleMouseWatcherHost(this),
398 mouse_watcher_->Start();
401 MaximizeBubbleControllerBubble::~MaximizeBubbleControllerBubble() {
404 aura::Window* MaximizeBubbleControllerBubble::GetBubbleWindow() {
405 return GetWidget()->GetNativeWindow();
408 gfx::Rect MaximizeBubbleControllerBubble::GetAnchorRect() {
412 gfx::Rect anchor_rect =
413 owner_->frame_maximize_button()->GetBoundsInScreen();
417 bool MaximizeBubbleControllerBubble::CanActivate() const {
421 bool MaximizeBubbleControllerBubble::WidgetHasHitTestMask() const {
422 return bubble_border_ != NULL;
425 void MaximizeBubbleControllerBubble::GetWidgetHitTestMask(
426 gfx::Path* mask) const {
428 DCHECK(bubble_border_);
429 bubble_border_->GetMask(mask);
432 void MaximizeBubbleControllerBubble::MouseMovedOutOfHost() {
433 if (!owner_ || shutting_down_)
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(
441 owner_->RequestDestructionThroughOwner();
446 bool MaximizeBubbleControllerBubble::Contains(
447 const gfx::Point& screen_point,
448 views::MouseWatcherHost::MouseEventType type) {
449 if (!owner_ || shutting_down_)
452 owner_->frame_maximize_button()->GetBoundsInScreen().Contains(
454 if (!owner_->frame_maximize_button()->is_snap_enabled() && inside_button) {
455 SetSnapType(controller()->maximize_type() == FRAME_STATE_FULL ?
456 SNAP_RESTORE : SNAP_MAXIMIZE);
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
462 return (owner_->frame_maximize_button()->is_snap_enabled() ||
464 contents_view_->GetBoundsInScreen().Contains(screen_point));
467 gfx::Size MaximizeBubbleControllerBubble::GetPreferredSize() {
468 return contents_view_->GetPreferredSize();
471 void MaximizeBubbleControllerBubble::OnWidgetDestroying(views::Widget* widget) {
472 if (GetWidget() == widget) {
473 mouse_watcher_->Stop();
476 // If the bubble destruction was triggered by some other external
477 // influence then ourselves, the owner needs to be informed that the menu
479 shutting_down_ = true;
480 owner_->RequestDestructionThroughOwner();
484 BubbleDelegateView::OnWidgetDestroying(widget);
487 void MaximizeBubbleControllerBubble::ControllerRequestsCloseAndDelete() {
488 // This only gets called from the owning base class once it is deleted.
491 shutting_down_ = true;
494 // Close the widget asynchronously after the hide animation is finished.
495 if (!appearance_delay_ms_)
496 GetWidget()->CloseNow();
498 GetWidget()->Close();
501 void MaximizeBubbleControllerBubble::SetSnapType(SnapType snap_type) {
503 contents_view_->SetSnapType(snap_type);
506 views::CustomButton* MaximizeBubbleControllerBubble::GetButtonForUnitTest(
508 return contents_view_->GetButtonForUnitTest(state);