| 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/bubble_contents_button_row.h" | |
| 6 | |
| 7 #include "ash/frame/caption_buttons/maximize_bubble_controller.h" | |
| 8 #include "ash/frame/caption_buttons/maximize_bubble_controller_bubble.h" | |
| 9 #include "grit/ash_resources.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/views/controls/button/image_button.h" | |
| 12 #include "ui/views/layout/box_layout.h" | |
| 13 | |
| 14 | |
| 15 namespace ash { | |
| 16 | |
| 17 // BubbleDialogButton --------------------------------------------------------- | |
| 18 | |
| 19 // The image button gets overridden to be able to capture mouse hover events. | |
| 20 // The constructor also assigns all button states and adds |this| as a child of | |
| 21 // |button_row|. | |
| 22 class BubbleDialogButton : public views::ImageButton { | |
| 23 public: | |
| 24 explicit BubbleDialogButton(BubbleContentsButtonRow* button_row, | |
| 25 int normal_image, | |
| 26 int hovered_image, | |
| 27 int pressed_image); | |
| 28 virtual ~BubbleDialogButton(); | |
| 29 | |
| 30 // views::ImageButton: | |
| 31 virtual void OnMouseCaptureLost() OVERRIDE; | |
| 32 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; | |
| 33 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; | |
| 34 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; | |
| 35 | |
| 36 private: | |
| 37 // The creating class which needs to get notified in case of a hover event. | |
| 38 BubbleContentsButtonRow* button_row_; | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(BubbleDialogButton); | |
| 41 }; | |
| 42 | |
| 43 BubbleDialogButton::BubbleDialogButton( | |
| 44 BubbleContentsButtonRow* button_row, | |
| 45 int normal_image, | |
| 46 int hovered_image, | |
| 47 int pressed_image) | |
| 48 : views::ImageButton(button_row), | |
| 49 button_row_(button_row) { | |
| 50 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 51 SetImage(views::CustomButton::STATE_NORMAL, | |
| 52 rb.GetImageSkiaNamed(normal_image)); | |
| 53 SetImage(views::CustomButton::STATE_HOVERED, | |
| 54 rb.GetImageSkiaNamed(hovered_image)); | |
| 55 SetImage(views::CustomButton::STATE_PRESSED, | |
| 56 rb.GetImageSkiaNamed(pressed_image)); | |
| 57 button_row->AddChildView(this); | |
| 58 } | |
| 59 | |
| 60 BubbleDialogButton::~BubbleDialogButton() { | |
| 61 } | |
| 62 | |
| 63 void BubbleDialogButton::OnMouseCaptureLost() { | |
| 64 button_row_->ButtonHovered(NULL); | |
| 65 views::ImageButton::OnMouseCaptureLost(); | |
| 66 } | |
| 67 | |
| 68 void BubbleDialogButton::OnMouseEntered(const ui::MouseEvent& event) { | |
| 69 button_row_->ButtonHovered(this); | |
| 70 views::ImageButton::OnMouseEntered(event); | |
| 71 } | |
| 72 | |
| 73 void BubbleDialogButton::OnMouseExited(const ui::MouseEvent& event) { | |
| 74 button_row_->ButtonHovered(NULL); | |
| 75 views::ImageButton::OnMouseExited(event); | |
| 76 } | |
| 77 | |
| 78 bool BubbleDialogButton::OnMouseDragged(const ui::MouseEvent& event) { | |
| 79 if (!button_row_->bubble()->controller()) | |
| 80 return false; | |
| 81 | |
| 82 // Remove the phantom window when we leave the button. | |
| 83 gfx::Point screen_location(event.location()); | |
| 84 View::ConvertPointToScreen(this, &screen_location); | |
| 85 if (!GetBoundsInScreen().Contains(screen_location)) | |
| 86 button_row_->ButtonHovered(NULL); | |
| 87 else | |
| 88 button_row_->ButtonHovered(this); | |
| 89 | |
| 90 // Pass the event on to the normal handler. | |
| 91 return views::ImageButton::OnMouseDragged(event); | |
| 92 } | |
| 93 | |
| 94 | |
| 95 // BubbleContentsButtonRow ---------------------------------------------------- | |
| 96 | |
| 97 BubbleContentsButtonRow::BubbleContentsButtonRow( | |
| 98 MaximizeBubbleControllerBubble* bubble) | |
| 99 : bubble_(bubble), | |
| 100 left_button_(NULL), | |
| 101 minimize_button_(NULL), | |
| 102 right_button_(NULL) { | |
| 103 SetLayoutManager(new views::BoxLayout( | |
| 104 views::BoxLayout::kHorizontal, 0, 0, | |
| 105 MaximizeBubbleControllerBubble::kLayoutSpacing)); | |
| 106 set_background(views::Background::CreateSolidBackground( | |
| 107 MaximizeBubbleControllerBubble::kBubbleBackgroundColor)); | |
| 108 | |
| 109 if (base::i18n::IsRTL()) { | |
| 110 AddMaximizeRightButton(); | |
| 111 AddMinimizeButton(); | |
| 112 AddMaximizeLeftButton(); | |
| 113 } else { | |
| 114 AddMaximizeLeftButton(); | |
| 115 AddMinimizeButton(); | |
| 116 AddMaximizeRightButton(); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 BubbleContentsButtonRow::~BubbleContentsButtonRow() { | |
| 121 } | |
| 122 | |
| 123 void BubbleContentsButtonRow::ButtonPressed(views::Button* sender, | |
| 124 const ui::Event& event) { | |
| 125 // While shutting down, the connection to the owner might already be broken. | |
| 126 if (!bubble_->controller()) | |
| 127 return; | |
| 128 if (sender == left_button_) { | |
| 129 bubble_->controller()->OnButtonClicked( | |
| 130 (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_LEFT) ? | |
| 131 SNAP_RESTORE : SNAP_LEFT); | |
| 132 } else if (sender == minimize_button_) { | |
| 133 bubble_->controller()->OnButtonClicked(SNAP_MINIMIZE); | |
| 134 } else { | |
| 135 DCHECK(sender == right_button_); | |
| 136 bubble_->controller()->OnButtonClicked( | |
| 137 (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_RIGHT) ? | |
| 138 SNAP_RESTORE : SNAP_RIGHT); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void BubbleContentsButtonRow::ButtonHovered(BubbleDialogButton* sender) { | |
| 143 // While shutting down, the connection to the owner might already be broken. | |
| 144 if (!bubble_->controller()) | |
| 145 return; | |
| 146 if (sender == left_button_) { | |
| 147 bubble_->controller()->OnButtonHover( | |
| 148 (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_LEFT) ? | |
| 149 SNAP_RESTORE : SNAP_LEFT); | |
| 150 } else if (sender == minimize_button_) { | |
| 151 bubble_->controller()->OnButtonHover(SNAP_MINIMIZE); | |
| 152 } else if (sender == right_button_) { | |
| 153 bubble_->controller()->OnButtonHover( | |
| 154 (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_RIGHT) ? | |
| 155 SNAP_RESTORE : SNAP_RIGHT); | |
| 156 } else { | |
| 157 bubble_->controller()->OnButtonHover(SNAP_NONE); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 views::CustomButton* BubbleContentsButtonRow::GetButtonForUnitTest( | |
| 162 SnapType state) { | |
| 163 switch (state) { | |
| 164 case SNAP_LEFT: | |
| 165 return left_button_; | |
| 166 case SNAP_MINIMIZE: | |
| 167 return minimize_button_; | |
| 168 case SNAP_RIGHT: | |
| 169 return right_button_; | |
| 170 default: | |
| 171 NOTREACHED(); | |
| 172 return NULL; | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void BubbleContentsButtonRow::AddMaximizeLeftButton() { | |
| 177 if (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_LEFT) { | |
| 178 left_button_ = new BubbleDialogButton( | |
| 179 this, | |
| 180 IDR_AURA_WINDOW_POSITION_LEFT_RESTORE, | |
| 181 IDR_AURA_WINDOW_POSITION_LEFT_RESTORE_H, | |
| 182 IDR_AURA_WINDOW_POSITION_LEFT_RESTORE_P); | |
| 183 } else { | |
| 184 left_button_ = new BubbleDialogButton( | |
| 185 this, | |
| 186 IDR_AURA_WINDOW_POSITION_LEFT, | |
| 187 IDR_AURA_WINDOW_POSITION_LEFT_H, | |
| 188 IDR_AURA_WINDOW_POSITION_LEFT_P); | |
| 189 } | |
| 190 } | |
| 191 | |
| 192 void BubbleContentsButtonRow::AddMaximizeRightButton() { | |
| 193 if (bubble_->controller()->maximize_type() == FRAME_STATE_SNAP_RIGHT) { | |
| 194 right_button_ = new BubbleDialogButton( | |
| 195 this, | |
| 196 IDR_AURA_WINDOW_POSITION_RIGHT_RESTORE, | |
| 197 IDR_AURA_WINDOW_POSITION_RIGHT_RESTORE_H, | |
| 198 IDR_AURA_WINDOW_POSITION_RIGHT_RESTORE_P); | |
| 199 } else { | |
| 200 right_button_ = new BubbleDialogButton( | |
| 201 this, | |
| 202 IDR_AURA_WINDOW_POSITION_RIGHT, | |
| 203 IDR_AURA_WINDOW_POSITION_RIGHT_H, | |
| 204 IDR_AURA_WINDOW_POSITION_RIGHT_P); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 void BubbleContentsButtonRow::AddMinimizeButton() { | |
| 209 minimize_button_ = new BubbleDialogButton( | |
| 210 this, | |
| 211 IDR_AURA_WINDOW_POSITION_MIDDLE, | |
| 212 IDR_AURA_WINDOW_POSITION_MIDDLE_H, | |
| 213 IDR_AURA_WINDOW_POSITION_MIDDLE_P); | |
| 214 } | |
| 215 | |
| 216 } // namespace ash | |
| OLD | NEW |