Chromium Code Reviews| 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..d9b91280cc124dc4669226766a3dd69d10e02d7e |
| --- /dev/null |
| +++ b/ash/laser/laser_pointer_controller.cc |
| @@ -0,0 +1,161 @@ |
| +// 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) |
| +#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 |
| +// |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(); |
| + for (aura::Window* root_window : root_windows) { |
| + if (root_window->ContainsPointInRoot( |
| + root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot())) |
| + return root_window; |
| + } |
| + return nullptr; |
| +} |
| +} // namespace |
|
jdufault
2016/09/12 21:10:11
newline above
sammiequon
2016/09/13 01:09:56
Done.
|
| + |
| +LaserPointerController::LaserPointerController() { |
| + timer_.reset(new base::Timer( |
| + FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), |
| + base::Bind(&LaserPointerController::AddStationaryPoint, |
| + base::Unretained(this)), |
| + true)); |
| +} |
| + |
| +LaserPointerController::~LaserPointerController() { |
| + Shell::GetInstance()->RemovePreTargetHandler(this); |
| +} |
| + |
| +void LaserPointerController::SetEnabled(bool enabled) { |
| + if (enabled == enabled_) |
| + return; |
| + |
| + enabled_ = enabled; |
| + if (enabled_) { |
| + laser_pointer_view_.reset(new LaserPointerView( |
| + base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), |
| + GetCurrentRootWindow())); |
| + Shell::GetInstance()->AddPreTargetHandler(this); |
| + } else { |
| + Shell::GetInstance()->RemovePreTargetHandler(this); |
| + StopTimer(); |
| + laser_pointer_view_.reset(); |
| + } |
| +} |
| + |
| +void LaserPointerController::OnWindowDestroying(aura::Window* window) { |
| + SwitchTargetRootWindowIfNeeded(); |
| +} |
| + |
| +void LaserPointerController::OnMouseEvent(ui::MouseEvent* event) { |
| + OnLocatedEvent(event, event->pointer_details()); |
| +} |
| + |
| +void LaserPointerController::OnTouchEvent(ui::TouchEvent* event) { |
|
jdufault
2016/09/12 21:10:11
We can probably remove the OnTouchEvent override,
sammiequon
2016/09/13 01:09:56
Done.
|
| + OnLocatedEvent(event, event->pointer_details()); |
| +} |
| + |
| +void LaserPointerController::OnLocatedEvent( |
| + ui::LocatedEvent* event, |
| + const ui::PointerDetails& pointer_details) { |
| + if (!enabled_) |
| + return; |
| + |
| + if (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; |
| + |
| + SwitchTargetRootWindowIfNeeded(); |
| + |
| + // 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; |
| + 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(); |
| + } else if (event->type() == ui::ET_MOUSE_RELEASED) { |
| + StopTimer(); |
| + laser_pointer_view_->Stop(); |
| + } |
| + |
| + // Consume the event unless it is over the palette tray or palette shelf |
| + // button. |
| + if (!ShouldSkipEventFiltering(current_mouse_location_)) |
| + event->StopPropagation(); |
| +} |
| + |
| +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 |