Index: ash/laser/laser_pointer_controller.cc |
diff --git a/ash/laser/laser_pointer_controller.cc b/ash/laser/laser_pointer_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..06c5af43950ce90724d0f2200aeddfa52490aea5 |
--- /dev/null |
+++ b/ash/laser/laser_pointer_controller.cc |
@@ -0,0 +1,157 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/laser/laser_pointer_controller.h" |
+ |
+#include "ash/laser/laser_pointer_view.h" |
+#include "ash/shell.h" |
+#include "ui/aura/window_event_dispatcher.h" |
+#include "ui/aura/window_tree_host.h" |
+#include "ui/views/widget/widget.h" |
+ |
+#if defined(OS_CHROMEOS) |
James Cook
2016/09/14 17:52:26
Since this feature only works on chromeos, how abo
sammiequon
2016/09/14 21:44:08
Done.
|
+#include "ash/common/system/chromeos/palette/palette_utils.h" |
+#endif |
+ |
+namespace ash { |
+namespace { |
+ |
+// A point gets removed from the collection if it is older than |
+// |kPointLifeDurationMs|. |
+const int kPointLifeDurationMs = 200; |
+ |
+// When no move events are being recieved we add a new point every |
James Cook
2016/09/14 17:52:26
nit: received
sammiequon
2016/09/14 21:44:08
Done.
|
+// |kAddStationaryPointsDelayMs| so that points older than |
+// |kPointLifeDurationMs| can get removed. |
+const int kAddStationaryPointsDelayMs = 5; |
+ |
+// Returns true if the event should be processed normally, ie, the stylus is |
+// over the palette icon or widget. |
+bool ShouldSkipEventFiltering(const gfx::Point& point) { |
+#if defined(OS_CHROMEOS) |
+ return PaletteContainsPointInScreen(point); |
+#else |
+ return false; |
+#endif |
+} |
+ |
+aura::Window* GetCurrentRootWindow() { |
+ aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); |
James Cook
2016/09/14 17:52:25
nit: no ash
sammiequon
2016/09/14 21:44:09
Done.
|
+ for (aura::Window* root_window : root_windows) { |
+ if (root_window->ContainsPointInRoot( |
+ root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) |
+ return root_window; |
+ } |
+ return nullptr; |
+} |
+} // namespace |
+ |
+LaserPointerController::LaserPointerController() { |
+ timer_.reset(new base::Timer( |
James Cook
2016/09/14 17:52:26
do this in constructor initializer list : timer_(n
sammiequon
2016/09/14 21:44:09
Done.
|
+ FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), |
+ base::Bind(&LaserPointerController::AddStationaryPoint, |
James Cook
2016/09/14 17:52:25
So if I instantiate a LaserPointerController, but
sammiequon
2016/09/14 21:44:08
The timer currently only runs once a drag is detec
|
+ base::Unretained(this)), |
+ true)); |
James Cook
2016/09/14 17:52:26
nit: document /* is_repeating */ or put in const b
sammiequon
2016/09/14 21:44:09
Done.
|
+} |
+ |
+LaserPointerController::~LaserPointerController() { |
+ Shell::GetInstance()->RemovePreTargetHandler(this); |
+} |
+ |
+void LaserPointerController::SetEnabled(bool enabled) { |
+ if (enabled == enabled_) |
+ return; |
+ |
+ enabled_ = enabled; |
+ if (enabled_) |
+ Shell::GetInstance()->AddPreTargetHandler(this); |
+ else |
+ Shell::GetInstance()->RemovePreTargetHandler(this); |
+} |
+ |
+void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { |
+ if (!enabled_) |
+ return; |
+ |
+ if (event->pointer_details().pointer_type != |
+ ui::EventPointerType::POINTER_TYPE_PEN) |
+ return; |
+ |
+ if (event->type() != ui::ET_MOUSE_DRAGGED && |
+ event->type() != ui::ET_MOUSE_PRESSED && |
+ event->type() != ui::ET_MOUSE_RELEASED) |
+ return; |
+ |
+ // Delete the LaserPointerView instance if mouse is released. |
+ if (event->type() == ui::ET_MOUSE_RELEASED) { |
+ StopTimer(); |
+ laser_pointer_view_->Stop(); |
+ laser_pointer_view_.reset(); |
+ return; |
+ } |
+ |
+ if (!laser_pointer_view_) { |
+ laser_pointer_view_.reset(new LaserPointerView( |
+ base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), |
+ GetCurrentRootWindow())); |
+ } |
+ |
+ SwitchTargetRootWindowIfNeeded(); |
James Cook
2016/09/14 17:52:26
please document what this is doing, either in the
sammiequon
2016/09/14 21:44:08
Done.
|
+ |
+ // Remap point from where it was captured to the display it is actually on. |
+ gfx::Point event_location = event->root_location(); |
+ aura::Window* target = static_cast<aura::Window*>(event->target()); |
+ aura::Window* event_root = target->GetRootWindow(); |
+ aura::Window::ConvertPointToTarget( |
+ event_root, laser_pointer_view_->GetRootWindow(), &event_location); |
+ |
+ current_mouse_location_ = event_location; |
James Cook
2016/09/14 17:52:26
Could you use ui/display/screen.h Screen::GetCurso
sammiequon
2016/09/14 21:44:09
We would need to compute the display origin for wh
James Cook
2016/09/14 22:53:37
This is fine.
|
+ laser_pointer_view_->AddNewPoint(current_mouse_location_); |
+ |
+ timer_repeat_count_ = 0; |
+ if (event->type() == ui::ET_MOUSE_DRAGGED) { |
+ // Start the timer to add stationary points if dragged. |
+ if (!timer_->IsRunning()) |
+ timer_->Reset(); |
+ } |
+ |
+ // Consume the event unless it is over the palette tray or palette shelf |
+ // button. |
+ if (!ShouldSkipEventFiltering(current_mouse_location_)) |
+ event->StopPropagation(); |
+} |
+ |
+void LaserPointerController::OnWindowDestroying(aura::Window* window) { |
+ SwitchTargetRootWindowIfNeeded(); |
+} |
+ |
+void LaserPointerController::StopTimer() { |
+ timer_repeat_count_ = 0; |
+ timer_->Stop(); |
+} |
+ |
+void LaserPointerController::SwitchTargetRootWindowIfNeeded() { |
+ if (enabled_) { |
+ // If the current root window has changed, remake the laser pointer view |
+ // with the new root window. |
+ if (laser_pointer_view_->GetRootWindow() != GetCurrentRootWindow()) { |
+ laser_pointer_view_.reset(new LaserPointerView( |
+ base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), |
+ GetCurrentRootWindow())); |
+ } |
+ } else { |
+ laser_pointer_view_.reset(); |
+ } |
+} |
+ |
+void LaserPointerController::AddStationaryPoint() { |
+ laser_pointer_view_->AddNewPoint(current_mouse_location_); |
+ // We can stop repeating the timer once the mouse has been stationary for |
+ // longer than the life of a point. |
+ if (timer_repeat_count_++ * kAddStationaryPointsDelayMs >= |
+ kPointLifeDurationMs) { |
+ StopTimer(); |
+ } |
+} |
+} // namespace ash |