OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/frame/caption_buttons/frame_size_button.h" | 5 #include "ash/frame/caption_buttons/frame_size_button.h" |
6 | 6 |
7 #include "ash/aura/wm_window_aura.h" | 7 #include "ash/aura/wm_window_aura.h" |
8 #include "ash/common/wm/window_positioning_utils.h" | 8 #include "ash/common/wm/window_positioning_utils.h" |
9 #include "ash/common/wm/window_state.h" | 9 #include "ash/common/wm/window_state.h" |
10 #include "ash/common/wm/wm_event.h" | 10 #include "ash/common/wm/wm_event.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 gfx::Rect expanded_bounds_in_screen = button->GetBoundsInScreen(); | 39 gfx::Rect expanded_bounds_in_screen = button->GetBoundsInScreen(); |
40 if (button->state() == views::Button::STATE_HOVERED || | 40 if (button->state() == views::Button::STATE_HOVERED || |
41 button->state() == views::Button::STATE_PRESSED) { | 41 button->state() == views::Button::STATE_PRESSED) { |
42 expanded_bounds_in_screen.Inset(-kMaxOvershootX, -kMaxOvershootY); | 42 expanded_bounds_in_screen.Inset(-kMaxOvershootX, -kMaxOvershootY); |
43 } | 43 } |
44 return expanded_bounds_in_screen.Contains(location_in_screen); | 44 return expanded_bounds_in_screen.Contains(location_in_screen); |
45 } | 45 } |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 FrameSizeButton::FrameSizeButton( | 49 FrameSizeButton::FrameSizeButton(views::ButtonListener* listener, |
50 views::ButtonListener* listener, | 50 views::Widget* frame, |
51 views::Widget* frame, | 51 FrameSizeButtonDelegate* delegate) |
52 FrameSizeButtonDelegate* delegate) | |
53 : FrameCaptionButton(listener, CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE), | 52 : FrameCaptionButton(listener, CAPTION_BUTTON_ICON_MAXIMIZE_RESTORE), |
54 frame_(frame), | 53 frame_(frame), |
55 delegate_(delegate), | 54 delegate_(delegate), |
56 set_buttons_to_snap_mode_delay_ms_(kSetButtonsToSnapModeDelayMs), | 55 set_buttons_to_snap_mode_delay_ms_(kSetButtonsToSnapModeDelayMs), |
57 in_snap_mode_(false), | 56 in_snap_mode_(false), |
58 snap_type_(SNAP_NONE) { | 57 snap_type_(SNAP_NONE) {} |
59 } | |
60 | 58 |
61 FrameSizeButton::~FrameSizeButton() { | 59 FrameSizeButton::~FrameSizeButton() {} |
62 } | |
63 | 60 |
64 bool FrameSizeButton::OnMousePressed(const ui::MouseEvent& event) { | 61 bool FrameSizeButton::OnMousePressed(const ui::MouseEvent& event) { |
65 // The minimize and close buttons are set to snap left and right when snapping | 62 // The minimize and close buttons are set to snap left and right when snapping |
66 // is enabled. Do not enable snapping if the minimize button is not visible. | 63 // is enabled. Do not enable snapping if the minimize button is not visible. |
67 // The close button is always visible. | 64 // The close button is always visible. |
68 if (IsTriggerableEvent(event) && | 65 if (IsTriggerableEvent(event) && !in_snap_mode_ && |
69 !in_snap_mode_ && | |
70 delegate_->IsMinimizeButtonVisible()) { | 66 delegate_->IsMinimizeButtonVisible()) { |
71 StartSetButtonsToSnapModeTimer(event); | 67 StartSetButtonsToSnapModeTimer(event); |
72 } | 68 } |
73 FrameCaptionButton::OnMousePressed(event); | 69 FrameCaptionButton::OnMousePressed(event); |
74 return true; | 70 return true; |
75 } | 71 } |
76 | 72 |
77 bool FrameSizeButton::OnMouseDragged(const ui::MouseEvent& event) { | 73 bool FrameSizeButton::OnMouseDragged(const ui::MouseEvent& event) { |
78 UpdateSnapType(event); | 74 UpdateSnapType(event); |
79 // By default a FrameCaptionButton reverts to STATE_NORMAL once the mouse | 75 // By default a FrameCaptionButton reverts to STATE_NORMAL once the mouse |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 131 |
136 void FrameSizeButton::StartSetButtonsToSnapModeTimer( | 132 void FrameSizeButton::StartSetButtonsToSnapModeTimer( |
137 const ui::LocatedEvent& event) { | 133 const ui::LocatedEvent& event) { |
138 set_buttons_to_snap_mode_timer_event_location_ = event.location(); | 134 set_buttons_to_snap_mode_timer_event_location_ = event.location(); |
139 if (set_buttons_to_snap_mode_delay_ms_ == 0) { | 135 if (set_buttons_to_snap_mode_delay_ms_ == 0) { |
140 AnimateButtonsToSnapMode(); | 136 AnimateButtonsToSnapMode(); |
141 } else { | 137 } else { |
142 set_buttons_to_snap_mode_timer_.Start( | 138 set_buttons_to_snap_mode_timer_.Start( |
143 FROM_HERE, | 139 FROM_HERE, |
144 base::TimeDelta::FromMilliseconds(set_buttons_to_snap_mode_delay_ms_), | 140 base::TimeDelta::FromMilliseconds(set_buttons_to_snap_mode_delay_ms_), |
145 this, | 141 this, &FrameSizeButton::AnimateButtonsToSnapMode); |
146 &FrameSizeButton::AnimateButtonsToSnapMode); | |
147 } | 142 } |
148 } | 143 } |
149 | 144 |
150 void FrameSizeButton::AnimateButtonsToSnapMode() { | 145 void FrameSizeButton::AnimateButtonsToSnapMode() { |
151 SetButtonsToSnapMode(FrameSizeButtonDelegate::ANIMATE_YES); | 146 SetButtonsToSnapMode(FrameSizeButtonDelegate::ANIMATE_YES); |
152 } | 147 } |
153 | 148 |
154 void FrameSizeButton::SetButtonsToSnapMode( | 149 void FrameSizeButton::SetButtonsToSnapMode( |
155 FrameSizeButtonDelegate::Animate animate) { | 150 FrameSizeButtonDelegate::Animate animate) { |
156 in_snap_mode_ = true; | 151 in_snap_mode_ = true; |
157 | 152 |
158 // When using a right-to-left layout the close button is left of the size | 153 // When using a right-to-left layout the close button is left of the size |
159 // button and the minimize button is right of the size button. | 154 // button and the minimize button is right of the size button. |
160 if (base::i18n::IsRTL()) { | 155 if (base::i18n::IsRTL()) { |
161 delegate_->SetButtonIcons(CAPTION_BUTTON_ICON_RIGHT_SNAPPED, | 156 delegate_->SetButtonIcons(CAPTION_BUTTON_ICON_RIGHT_SNAPPED, |
162 CAPTION_BUTTON_ICON_LEFT_SNAPPED, | 157 CAPTION_BUTTON_ICON_LEFT_SNAPPED, animate); |
163 animate); | |
164 } else { | 158 } else { |
165 delegate_->SetButtonIcons(CAPTION_BUTTON_ICON_LEFT_SNAPPED, | 159 delegate_->SetButtonIcons(CAPTION_BUTTON_ICON_LEFT_SNAPPED, |
166 CAPTION_BUTTON_ICON_RIGHT_SNAPPED, | 160 CAPTION_BUTTON_ICON_RIGHT_SNAPPED, animate); |
167 animate); | |
168 } | 161 } |
169 } | 162 } |
170 | 163 |
171 void FrameSizeButton::UpdateSnapType(const ui::LocatedEvent& event) { | 164 void FrameSizeButton::UpdateSnapType(const ui::LocatedEvent& event) { |
172 if (!in_snap_mode_) { | 165 if (!in_snap_mode_) { |
173 // Set the buttons adjacent to the size button to snap left and right early | 166 // Set the buttons adjacent to the size button to snap left and right early |
174 // if the user drags past the drag threshold. | 167 // if the user drags past the drag threshold. |
175 // |set_buttons_to_snap_mode_timer_| is checked to avoid entering the snap | 168 // |set_buttons_to_snap_mode_timer_| is checked to avoid entering the snap |
176 // mode as a result of an unsupported drag type (e.g. only the right mouse | 169 // mode as a result of an unsupported drag type (e.g. only the right mouse |
177 // button is pressed). | 170 // button is pressed). |
178 gfx::Vector2d delta( | 171 gfx::Vector2d delta(event.location() - |
179 event.location() - set_buttons_to_snap_mode_timer_event_location_); | 172 set_buttons_to_snap_mode_timer_event_location_); |
180 if (!set_buttons_to_snap_mode_timer_.IsRunning() || | 173 if (!set_buttons_to_snap_mode_timer_.IsRunning() || |
181 !views::View::ExceededDragThreshold(delta)) { | 174 !views::View::ExceededDragThreshold(delta)) { |
182 return; | 175 return; |
183 } | 176 } |
184 AnimateButtonsToSnapMode(); | 177 AnimateButtonsToSnapMode(); |
185 } | 178 } |
186 | 179 |
187 gfx::Point event_location_in_screen(event.location()); | 180 gfx::Point event_location_in_screen(event.location()); |
188 views::View::ConvertPointToScreen(this, &event_location_in_screen); | 181 views::View::ConvertPointToScreen(this, &event_location_in_screen); |
189 const FrameCaptionButton* to_hover = | 182 const FrameCaptionButton* to_hover = |
190 GetButtonToHover(event_location_in_screen); | 183 GetButtonToHover(event_location_in_screen); |
191 bool press_size_button = | 184 bool press_size_button = |
192 to_hover || HitTestButton(this, event_location_in_screen); | 185 to_hover || HitTestButton(this, event_location_in_screen); |
193 | 186 |
194 if (to_hover) { | 187 if (to_hover) { |
195 // Progress the minimize and close icon morph animations to the end if they | 188 // Progress the minimize and close icon morph animations to the end if they |
196 // are in progress. | 189 // are in progress. |
197 SetButtonsToSnapMode(FrameSizeButtonDelegate::ANIMATE_NO); | 190 SetButtonsToSnapMode(FrameSizeButtonDelegate::ANIMATE_NO); |
198 } | 191 } |
199 | 192 |
200 delegate_->SetHoveredAndPressedButtons( | 193 delegate_->SetHoveredAndPressedButtons(to_hover, |
201 to_hover, press_size_button ? this : NULL); | 194 press_size_button ? this : NULL); |
202 | 195 |
203 snap_type_ = SNAP_NONE; | 196 snap_type_ = SNAP_NONE; |
204 if (to_hover) { | 197 if (to_hover) { |
205 switch (to_hover->icon()) { | 198 switch (to_hover->icon()) { |
206 case CAPTION_BUTTON_ICON_LEFT_SNAPPED: | 199 case CAPTION_BUTTON_ICON_LEFT_SNAPPED: |
207 snap_type_ = SNAP_LEFT; | 200 snap_type_ = SNAP_LEFT; |
208 break; | 201 break; |
209 case CAPTION_BUTTON_ICON_RIGHT_SNAPPED: | 202 case CAPTION_BUTTON_ICON_RIGHT_SNAPPED: |
210 snap_type_ = SNAP_RIGHT; | 203 snap_type_ = SNAP_RIGHT; |
211 break; | 204 break; |
(...skipping 22 matching lines...) Expand all Loading... |
234 WmWindowAura::Get(window)); | 227 WmWindowAura::Get(window)); |
235 phantom_window_controller_->Show(ScreenUtil::ConvertRectToScreen( | 228 phantom_window_controller_->Show(ScreenUtil::ConvertRectToScreen( |
236 window->parent(), phantom_bounds_in_parent)); | 229 window->parent(), phantom_bounds_in_parent)); |
237 } else { | 230 } else { |
238 phantom_window_controller_.reset(); | 231 phantom_window_controller_.reset(); |
239 } | 232 } |
240 } | 233 } |
241 | 234 |
242 const FrameCaptionButton* FrameSizeButton::GetButtonToHover( | 235 const FrameCaptionButton* FrameSizeButton::GetButtonToHover( |
243 const gfx::Point& event_location_in_screen) const { | 236 const gfx::Point& event_location_in_screen) const { |
244 const FrameCaptionButton* closest_button = delegate_->GetButtonClosestTo( | 237 const FrameCaptionButton* closest_button = |
245 event_location_in_screen); | 238 delegate_->GetButtonClosestTo(event_location_in_screen); |
246 if ((closest_button->icon() == CAPTION_BUTTON_ICON_LEFT_SNAPPED || | 239 if ((closest_button->icon() == CAPTION_BUTTON_ICON_LEFT_SNAPPED || |
247 closest_button->icon() == CAPTION_BUTTON_ICON_RIGHT_SNAPPED) && | 240 closest_button->icon() == CAPTION_BUTTON_ICON_RIGHT_SNAPPED) && |
248 HitTestButton(closest_button, event_location_in_screen)) { | 241 HitTestButton(closest_button, event_location_in_screen)) { |
249 return closest_button; | 242 return closest_button; |
250 } | 243 } |
251 return NULL; | 244 return NULL; |
252 } | 245 } |
253 | 246 |
254 bool FrameSizeButton::CommitSnap(const ui::LocatedEvent& event) { | 247 bool FrameSizeButton::CommitSnap(const ui::LocatedEvent& event) { |
255 // The position of |event| may be different than the position of the previous | 248 // The position of |event| may be different than the position of the previous |
256 // event. | 249 // event. |
257 UpdateSnapType(event); | 250 UpdateSnapType(event); |
258 | 251 |
259 if (in_snap_mode_ && | 252 if (in_snap_mode_ && (snap_type_ == SNAP_LEFT || snap_type_ == SNAP_RIGHT)) { |
260 (snap_type_ == SNAP_LEFT || snap_type_ == SNAP_RIGHT)) { | |
261 wm::WindowState* window_state = | 253 wm::WindowState* window_state = |
262 wm::GetWindowState(frame_->GetNativeWindow()); | 254 wm::GetWindowState(frame_->GetNativeWindow()); |
263 const wm::WMEvent snap_event( | 255 const wm::WMEvent snap_event(snap_type_ == SNAP_LEFT |
264 snap_type_ == SNAP_LEFT ? | 256 ? wm::WM_EVENT_SNAP_LEFT |
265 wm::WM_EVENT_SNAP_LEFT : wm::WM_EVENT_SNAP_RIGHT); | 257 : wm::WM_EVENT_SNAP_RIGHT); |
266 window_state->OnWMEvent(&snap_event); | 258 window_state->OnWMEvent(&snap_event); |
267 WmShell::Get()->RecordUserMetricsAction( | 259 WmShell::Get()->RecordUserMetricsAction( |
268 snap_type_ == SNAP_LEFT ? | 260 snap_type_ == SNAP_LEFT ? UMA_WINDOW_MAXIMIZE_BUTTON_MAXIMIZE_LEFT |
269 UMA_WINDOW_MAXIMIZE_BUTTON_MAXIMIZE_LEFT : | 261 : UMA_WINDOW_MAXIMIZE_BUTTON_MAXIMIZE_RIGHT); |
270 UMA_WINDOW_MAXIMIZE_BUTTON_MAXIMIZE_RIGHT); | |
271 SetButtonsToNormalMode(FrameSizeButtonDelegate::ANIMATE_NO); | 262 SetButtonsToNormalMode(FrameSizeButtonDelegate::ANIMATE_NO); |
272 return true; | 263 return true; |
273 } | 264 } |
274 SetButtonsToNormalMode(FrameSizeButtonDelegate::ANIMATE_YES); | 265 SetButtonsToNormalMode(FrameSizeButtonDelegate::ANIMATE_YES); |
275 return false; | 266 return false; |
276 } | 267 } |
277 | 268 |
278 void FrameSizeButton::SetButtonsToNormalMode( | 269 void FrameSizeButton::SetButtonsToNormalMode( |
279 FrameSizeButtonDelegate::Animate animate) { | 270 FrameSizeButtonDelegate::Animate animate) { |
280 in_snap_mode_ = false; | 271 in_snap_mode_ = false; |
281 snap_type_ = SNAP_NONE; | 272 snap_type_ = SNAP_NONE; |
282 set_buttons_to_snap_mode_timer_.Stop(); | 273 set_buttons_to_snap_mode_timer_.Stop(); |
283 delegate_->SetButtonsToNormal(animate); | 274 delegate_->SetButtonsToNormal(animate); |
284 phantom_window_controller_.reset(); | 275 phantom_window_controller_.reset(); |
285 } | 276 } |
286 | 277 |
287 } // namespace ash | 278 } // namespace ash |
OLD | NEW |