Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/laser/laser_pointer_controller.h" | |
| 6 | |
| 7 #include "ash/common/system/chromeos/palette/palette_utils.h" | |
| 8 #include "ash/laser/laser_pointer_view.h" | |
| 9 #include "ash/shell.h" | |
| 10 #include "ui/aura/window_event_dispatcher.h" | |
| 11 #include "ui/aura/window_tree_host.h" | |
| 12 #include "ui/display/screen.h" | |
| 13 #include "ui/views/widget/widget.h" | |
| 14 | |
| 15 namespace ash { | |
| 16 namespace { | |
| 17 | |
| 18 // A point gets removed from the collection if it is older than | |
| 19 // |kPointLifeDurationMs|. | |
| 20 const int kPointLifeDurationMs = 200; | |
| 21 | |
| 22 // When no move events are being received we add a new point every | |
| 23 // |kAddStationaryPointsDelayMs| so that points older than | |
| 24 // |kPointLifeDurationMs| can get removed. | |
| 25 const int kAddStationaryPointsDelayMs = 5; | |
| 26 | |
| 27 aura::Window* GetCurrentRootWindow() { | |
| 28 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | |
| 29 for (aura::Window* root_window : root_windows) { | |
| 30 if (root_window->ContainsPointInRoot( | |
| 31 root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) | |
| 32 return root_window; | |
| 33 } | |
| 34 return nullptr; | |
| 35 } | |
| 36 } // namespace | |
| 37 | |
| 38 LaserPointerController::LaserPointerController() | |
| 39 : stationary_timer_(new base::Timer( | |
| 40 FROM_HERE, | |
| 41 base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), | |
| 42 base::Bind(&LaserPointerController::AddStationaryPoint, | |
| 43 base::Unretained(this)), | |
| 44 true /* is_repeating */)) {} | |
| 45 | |
| 46 LaserPointerController::~LaserPointerController() { | |
| 47 Shell::GetInstance()->RemovePreTargetHandler(this); | |
| 48 } | |
| 49 | |
| 50 void LaserPointerController::SetEnabled(bool enabled) { | |
| 51 if (enabled == enabled_) | |
| 52 return; | |
| 53 | |
| 54 enabled_ = enabled; | |
| 55 if (enabled_) | |
| 56 Shell::GetInstance()->AddPreTargetHandler(this); | |
| 57 else | |
| 58 Shell::GetInstance()->RemovePreTargetHandler(this); | |
| 59 } | |
| 60 | |
| 61 void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { | |
| 62 if (!enabled_) | |
| 63 return; | |
| 64 | |
| 65 if (event->pointer_details().pointer_type != | |
| 66 ui::EventPointerType::POINTER_TYPE_PEN) | |
| 67 return; | |
| 68 | |
| 69 if (event->type() != ui::ET_MOUSE_DRAGGED && | |
| 70 event->type() != ui::ET_MOUSE_PRESSED && | |
| 71 event->type() != ui::ET_MOUSE_RELEASED) | |
| 72 return; | |
| 73 | |
| 74 // Delete the LaserPointerView instance if mouse is released. | |
| 75 if (event->type() == ui::ET_MOUSE_RELEASED) { | |
| 76 StopStationaryTimer(); | |
| 77 laser_pointer_view_->Stop(); | |
| 78 laser_pointer_view_.reset(); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 // This will handle creating the initial laser pointer view on | |
| 83 // ET_MOUSE_PRESSED events. | |
| 84 SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow()); | |
| 85 | |
| 86 if (laser_pointer_view_) { | |
| 87 // Remap point from where it was captured to the display it is actually on. | |
| 88 gfx::Point event_location = event->root_location(); | |
| 89 aura::Window* target = static_cast<aura::Window*>(event->target()); | |
| 90 aura::Window* event_root = target->GetRootWindow(); | |
| 91 aura::Window::ConvertPointToTarget( | |
| 92 event_root, laser_pointer_view_->GetRootWindow(), &event_location); | |
| 93 | |
| 94 current_mouse_location_ = event_location; | |
| 95 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 96 | |
| 97 stationary_timer_repeat_count_ = 0; | |
| 98 if (event->type() == ui::ET_MOUSE_DRAGGED) { | |
| 99 // Start the timer to add stationary points if dragged. | |
| 100 if (!stationary_timer_->IsRunning()) | |
| 101 stationary_timer_->Reset(); | |
| 102 } | |
| 103 | |
| 104 // If the stylus is over the palette icon or widget, do not consume the | |
| 105 // event. | |
| 106 if (!PaletteContainsPointInScreen(current_mouse_location_)) | |
| 107 event->StopPropagation(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void LaserPointerController::OnWindowDestroying(aura::Window* window) { | |
| 112 SwitchTargetRootWindowIfNeeded(window); | |
| 113 } | |
| 114 | |
| 115 void LaserPointerController::StopStationaryTimer() { | |
|
jdufault
2016/09/16 19:58:41
Remove this function and just call stationary_time
sammiequon
2016/09/16 20:37:40
Done.
| |
| 116 stationary_timer_->Stop(); | |
| 117 } | |
| 118 | |
| 119 void LaserPointerController::SwitchTargetRootWindowIfNeeded( | |
| 120 aura::Window* root_window) { | |
| 121 if (!root_window) { | |
| 122 stationary_timer_->Stop(); | |
| 123 laser_pointer_view_.reset(); | |
| 124 } else if (laser_pointer_view_) { | |
| 125 laser_pointer_view_->ReparentWidget(root_window); | |
| 126 } else if (enabled_) { | |
| 127 laser_pointer_view_.reset(new LaserPointerView( | |
| 128 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window)); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void LaserPointerController::AddStationaryPoint() { | |
| 133 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 134 // We can stop repeating the timer once the mouse has been stationary for | |
| 135 // longer than the life of a point. | |
| 136 if (stationary_timer_repeat_count_++ * kAddStationaryPointsDelayMs >= | |
| 137 kPointLifeDurationMs) { | |
| 138 StopStationaryTimer(); | |
| 139 } | |
| 140 } | |
| 141 } // namespace ash | |
| OLD | NEW |