| 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/utility/screenshot_controller.h" | 5 #include "ash/utility/screenshot_controller.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "ash/common/shell_window_ids.h" | 9 #include "ash/common/shell_window_ids.h" |
| 10 #include "ash/display/mouse_cursor_event_filter.h" | 10 #include "ash/display/mouse_cursor_event_filter.h" |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 root_window_(nullptr), | 238 root_window_(nullptr), |
| 239 selected_(nullptr), | 239 selected_(nullptr), |
| 240 screenshot_delegate_(nullptr) { | 240 screenshot_delegate_(nullptr) { |
| 241 // Keep this here and don't move it to StartPartialScreenshotSession(), as it | 241 // Keep this here and don't move it to StartPartialScreenshotSession(), as it |
| 242 // needs to be pre-pended by MouseCursorEventFilter in Shell::Init(). | 242 // needs to be pre-pended by MouseCursorEventFilter in Shell::Init(). |
| 243 Shell::GetInstance()->PrependPreTargetHandler(this); | 243 Shell::GetInstance()->PrependPreTargetHandler(this); |
| 244 } | 244 } |
| 245 | 245 |
| 246 ScreenshotController::~ScreenshotController() { | 246 ScreenshotController::~ScreenshotController() { |
| 247 if (screenshot_delegate_) | 247 if (screenshot_delegate_) |
| 248 Cancel(); | 248 CancelScreenshotSession(); |
| 249 Shell::GetInstance()->RemovePreTargetHandler(this); | 249 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 250 } | 250 } |
| 251 | 251 |
| 252 void ScreenshotController::StartWindowScreenshotSession( | 252 void ScreenshotController::StartWindowScreenshotSession( |
| 253 ScreenshotDelegate* screenshot_delegate) { | 253 ScreenshotDelegate* screenshot_delegate) { |
| 254 if (screenshot_delegate_) { | 254 if (screenshot_delegate_) { |
| 255 DCHECK_EQ(screenshot_delegate_, screenshot_delegate); | 255 DCHECK_EQ(screenshot_delegate_, screenshot_delegate); |
| 256 return; | 256 return; |
| 257 } | 257 } |
| 258 screenshot_delegate_ = screenshot_delegate; | 258 screenshot_delegate_ = screenshot_delegate; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 } | 291 } |
| 292 | 292 |
| 293 if (!pen_events_only_) { | 293 if (!pen_events_only_) { |
| 294 cursor_setter_.reset(new ScopedCursorSetter( | 294 cursor_setter_.reset(new ScopedCursorSetter( |
| 295 Shell::GetInstance()->cursor_manager(), ui::kCursorCross)); | 295 Shell::GetInstance()->cursor_manager(), ui::kCursorCross)); |
| 296 } | 296 } |
| 297 | 297 |
| 298 EnableMouseWarp(false); | 298 EnableMouseWarp(false); |
| 299 } | 299 } |
| 300 | 300 |
| 301 void ScreenshotController::CancelScreenshotSession() { |
| 302 mode_ = NONE; |
| 303 pen_events_only_ = false; |
| 304 root_window_ = nullptr; |
| 305 SetSelectedWindow(nullptr); |
| 306 screenshot_delegate_ = nullptr; |
| 307 display::Screen::GetScreen()->RemoveObserver(this); |
| 308 base::STLDeleteValues(&layers_); |
| 309 cursor_setter_.reset(); |
| 310 EnableMouseWarp(true); |
| 311 } |
| 312 |
| 301 void ScreenshotController::MaybeStart(const ui::LocatedEvent& event) { | 313 void ScreenshotController::MaybeStart(const ui::LocatedEvent& event) { |
| 302 aura::Window* current_root = | 314 aura::Window* current_root = |
| 303 static_cast<aura::Window*>(event.target())->GetRootWindow(); | 315 static_cast<aura::Window*>(event.target())->GetRootWindow(); |
| 304 if (root_window_) { | 316 if (root_window_) { |
| 305 // It's already started. This can happen when the second finger touches | 317 // It's already started. This can happen when the second finger touches |
| 306 // the screen, or combination of the touch and mouse. We should grab the | 318 // the screen, or combination of the touch and mouse. We should grab the |
| 307 // partial screenshot instead of restarting. | 319 // partial screenshot instead of restarting. |
| 308 if (current_root == root_window_) { | 320 if (current_root == root_window_) { |
| 309 Update(event); | 321 Update(event); |
| 310 CompletePartialScreenshot(); | 322 CompletePartialScreenshot(); |
| 311 } | 323 } |
| 312 } else { | 324 } else { |
| 313 root_window_ = current_root; | 325 root_window_ = current_root; |
| 314 start_position_ = event.root_location(); | 326 start_position_ = event.root_location(); |
| 315 if (!pen_events_only_) { | 327 if (!pen_events_only_) { |
| 316 // ScopedCursorSetter must be reset first to make sure that its dtor is | 328 // ScopedCursorSetter must be reset first to make sure that its dtor is |
| 317 // called before ctor is called. | 329 // called before ctor is called. |
| 318 cursor_setter_.reset(); | 330 cursor_setter_.reset(); |
| 319 cursor_setter_.reset(new ScopedCursorSetter( | 331 cursor_setter_.reset(new ScopedCursorSetter( |
| 320 Shell::GetInstance()->cursor_manager(), ui::kCursorNone)); | 332 Shell::GetInstance()->cursor_manager(), ui::kCursorNone)); |
| 321 } | 333 } |
| 322 Update(event); | 334 Update(event); |
| 323 } | 335 } |
| 324 } | 336 } |
| 325 | 337 |
| 326 void ScreenshotController::CompleteWindowScreenshot() { | 338 void ScreenshotController::CompleteWindowScreenshot() { |
| 327 if (selected_) | 339 if (selected_) |
| 328 screenshot_delegate_->HandleTakeWindowScreenshot(selected_); | 340 screenshot_delegate_->HandleTakeWindowScreenshot(selected_); |
| 329 Cancel(); | 341 CancelScreenshotSession(); |
| 330 } | 342 } |
| 331 | 343 |
| 332 void ScreenshotController::CompletePartialScreenshot() { | 344 void ScreenshotController::CompletePartialScreenshot() { |
| 333 if (!root_window_) { | 345 if (!root_window_) { |
| 334 // If we received a released event before we ever got a pressed event | 346 // If we received a released event before we ever got a pressed event |
| 335 // (resulting in setting |root_window_|), we just return without canceling | 347 // (resulting in setting |root_window_|), we just return without canceling |
| 336 // to keep the screenshot session active waiting for the next press. | 348 // to keep the screenshot session active waiting for the next press. |
| 337 // | 349 // |
| 338 // This is to avoid a crash that used to happen when we start the screenshot | 350 // This is to avoid a crash that used to happen when we start the screenshot |
| 339 // session while the mouse is pressed and then release without moving the | 351 // session while the mouse is pressed and then release without moving the |
| 340 // mouse. crbug.com/581432. | 352 // mouse. crbug.com/581432. |
| 341 return; | 353 return; |
| 342 } | 354 } |
| 343 | 355 |
| 344 DCHECK(layers_.count(root_window_)); | 356 DCHECK(layers_.count(root_window_)); |
| 345 const gfx::Rect& region = layers_.at(root_window_)->region(); | 357 const gfx::Rect& region = layers_.at(root_window_)->region(); |
| 346 if (!region.IsEmpty()) { | 358 if (!region.IsEmpty()) { |
| 347 screenshot_delegate_->HandleTakePartialScreenshot( | 359 screenshot_delegate_->HandleTakePartialScreenshot( |
| 348 root_window_, gfx::IntersectRects(root_window_->bounds(), region)); | 360 root_window_, gfx::IntersectRects(root_window_->bounds(), region)); |
| 349 } | 361 } |
| 350 Cancel(); | 362 CancelScreenshotSession(); |
| 351 } | |
| 352 | |
| 353 void ScreenshotController::Cancel() { | |
| 354 mode_ = NONE; | |
| 355 pen_events_only_ = false; | |
| 356 root_window_ = nullptr; | |
| 357 SetSelectedWindow(nullptr); | |
| 358 screenshot_delegate_ = nullptr; | |
| 359 display::Screen::GetScreen()->RemoveObserver(this); | |
| 360 base::STLDeleteValues(&layers_); | |
| 361 cursor_setter_.reset(); | |
| 362 EnableMouseWarp(true); | |
| 363 } | 363 } |
| 364 | 364 |
| 365 void ScreenshotController::Update(const ui::LocatedEvent& event) { | 365 void ScreenshotController::Update(const ui::LocatedEvent& event) { |
| 366 // Update may happen without MaybeStart() if the partial screenshot session | 366 // Update may happen without MaybeStart() if the partial screenshot session |
| 367 // starts when dragging. | 367 // starts when dragging. |
| 368 if (!root_window_) | 368 if (!root_window_) |
| 369 MaybeStart(event); | 369 MaybeStart(event); |
| 370 DCHECK(layers_.find(root_window_) != layers_.end()); | 370 DCHECK(layers_.find(root_window_) != layers_.end()); |
| 371 | 371 |
| 372 ScreenshotLayer* layer = layers_.at(root_window_); | 372 ScreenshotLayer* layer = layers_.at(root_window_); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 416 return !pen_events_only_ || | 416 return !pen_events_only_ || |
| 417 pointer_details.pointer_type == ui::EventPointerType::POINTER_TYPE_PEN; | 417 pointer_details.pointer_type == ui::EventPointerType::POINTER_TYPE_PEN; |
| 418 } | 418 } |
| 419 | 419 |
| 420 void ScreenshotController::OnKeyEvent(ui::KeyEvent* event) { | 420 void ScreenshotController::OnKeyEvent(ui::KeyEvent* event) { |
| 421 if (!screenshot_delegate_) | 421 if (!screenshot_delegate_) |
| 422 return; | 422 return; |
| 423 | 423 |
| 424 if (event->type() == ui::ET_KEY_RELEASED) { | 424 if (event->type() == ui::ET_KEY_RELEASED) { |
| 425 if (event->key_code() == ui::VKEY_ESCAPE) { | 425 if (event->key_code() == ui::VKEY_ESCAPE) { |
| 426 Cancel(); | 426 CancelScreenshotSession(); |
| 427 event->StopPropagation(); | 427 event->StopPropagation(); |
| 428 } else if (event->key_code() == ui::VKEY_RETURN && mode_ == WINDOW) { | 428 } else if (event->key_code() == ui::VKEY_RETURN && mode_ == WINDOW) { |
| 429 CompleteWindowScreenshot(); | 429 CompleteWindowScreenshot(); |
| 430 event->StopPropagation(); | 430 event->StopPropagation(); |
| 431 } | 431 } |
| 432 } | 432 } |
| 433 | 433 |
| 434 // Stop all key events except if the user is using a pointer, in which case | 434 // Stop all key events except if the user is using a pointer, in which case |
| 435 // they should be able to continue manipulating the screen. | 435 // they should be able to continue manipulating the screen. |
| 436 if (!pen_events_only_) | 436 if (!pen_events_only_) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 515 break; | 515 break; |
| 516 } | 516 } |
| 517 break; | 517 break; |
| 518 } | 518 } |
| 519 event->StopPropagation(); | 519 event->StopPropagation(); |
| 520 } | 520 } |
| 521 | 521 |
| 522 void ScreenshotController::OnDisplayAdded(const display::Display& new_display) { | 522 void ScreenshotController::OnDisplayAdded(const display::Display& new_display) { |
| 523 if (!screenshot_delegate_) | 523 if (!screenshot_delegate_) |
| 524 return; | 524 return; |
| 525 Cancel(); | 525 CancelScreenshotSession(); |
| 526 } | 526 } |
| 527 | 527 |
| 528 void ScreenshotController::OnDisplayRemoved( | 528 void ScreenshotController::OnDisplayRemoved( |
| 529 const display::Display& old_display) { | 529 const display::Display& old_display) { |
| 530 if (!screenshot_delegate_) | 530 if (!screenshot_delegate_) |
| 531 return; | 531 return; |
| 532 Cancel(); | 532 CancelScreenshotSession(); |
| 533 } | 533 } |
| 534 | 534 |
| 535 void ScreenshotController::OnDisplayMetricsChanged( | 535 void ScreenshotController::OnDisplayMetricsChanged( |
| 536 const display::Display& display, | 536 const display::Display& display, |
| 537 uint32_t changed_metrics) {} | 537 uint32_t changed_metrics) {} |
| 538 | 538 |
| 539 void ScreenshotController::OnWindowDestroying(aura::Window* window) { | 539 void ScreenshotController::OnWindowDestroying(aura::Window* window) { |
| 540 SetSelectedWindow(nullptr); | 540 SetSelectedWindow(nullptr); |
| 541 } | 541 } |
| 542 | 542 |
| 543 } // namespace ash | 543 } // namespace ash |
| OLD | NEW |