Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2330)

Unified Diff: ash/laser/laser_pointer_controller.cc

Issue 2311393004: Laser tool blocks events from propagating. (Closed)
Patch Set: Fixed patch set 4 errors. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a9ccd6c54293408cd5d15f769a50dcbd10023df7
--- /dev/null
+++ b/ash/laser/laser_pointer_controller.cc
@@ -0,0 +1,140 @@
+// 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/common/system/chromeos/palette/palette_utils.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/display/screen.h"
+#include "ui/views/widget/widget.h"
+
+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 received we add a new point every
+// |kAddStationaryPointsDelayMs| so that points older than
+// |kPointLifeDurationMs| can get removed.
+const int kAddStationaryPointsDelayMs = 5;
+
+aura::Window* GetCurrentRootWindow() {
+ aura::Window::Windows root_windows = Shell::GetAllRootWindows();
+ for (aura::Window* root_window : root_windows) {
+ if (root_window->ContainsPointInRoot(
+ root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot()))
+ return root_window;
+ }
+ return nullptr;
+}
+} // namespace
+
+LaserPointerController::LaserPointerController()
+ : stationary_timer_(new base::Timer(
+ FROM_HERE,
+ base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs),
+ base::Bind(&LaserPointerController::AddStationaryPoint,
+ base::Unretained(this)),
+ true /* is_repeating */)) {}
+
+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;
+ }
+
+ gfx::Point event_location = event->root_location();
James Cook 2016/09/16 18:13:31 Can this go inside the if() block below?
sammiequon 2016/09/16 19:21:03 Done.
+ aura::Window* target = static_cast<aura::Window*>(event->target());
+ aura::Window* event_root = target->GetRootWindow();
+
+ SwitchTargetRootWindowIfNeeded(GetCurrentRootWindow());
jdufault 2016/09/16 18:43:53 Add a comment in line 85 that this will function c
sammiequon 2016/09/16 19:21:03 Done.
+
+ if (laser_pointer_view_) {
+ // Remap point from where it was captured to the display it is actually on.
+ aura::Window::ConvertPointToTarget(
+ event_root, laser_pointer_view_->GetRootWindow(), &event_location);
+
+ current_mouse_location_ = event_location;
+ laser_pointer_view_->AddNewPoint(current_mouse_location_);
+
+ stationary_timer_repeat_count_ = 0;
James Cook 2016/09/16 18:13:31 Do you ever read this variable? If not, remove it.
jdufault 2016/09/16 18:43:53 It is used inside of AddStationaryPoint
sammiequon 2016/09/16 19:21:03 This is used at line 135 to determine when to stop
+ if (event->type() == ui::ET_MOUSE_DRAGGED) {
+ // Start the timer to add stationary points if dragged.
+ if (!stationary_timer_->IsRunning())
+ stationary_timer_->Reset();
+ }
+ }
+
+ // If the stylus is over the palette icon or widget, do not consume the event.
+ if (!PaletteContainsPointInScreen(current_mouse_location_))
+ event->StopPropagation();
+}
+
+void LaserPointerController::OnWindowDestroying(aura::Window* window) {
+ SwitchTargetRootWindowIfNeeded(window);
+}
+
+void LaserPointerController::StopTimer() {
+ stationary_timer_repeat_count_ = 0;
jdufault 2016/09/16 18:43:53 This value gets reset every time the timer is rese
sammiequon 2016/09/16 19:21:03 Done.
+ stationary_timer_->Stop();
+}
+
+void LaserPointerController::SwitchTargetRootWindowIfNeeded(
+ aura::Window* root_window) {
+ if (root_window == nullptr) {
James Cook 2016/09/16 18:13:31 nit: if (!root_window) is more idiomatic in chrome
sammiequon 2016/09/16 19:21:03 Done.
+ stationary_timer_->Stop();
+ laser_pointer_view_.reset();
+ } else if (laser_pointer_view_) {
+ laser_pointer_view_->ReparentWidget(root_window);
+ } else if (enabled_) {
+ laser_pointer_view_.reset(new LaserPointerView(
+ base::TimeDelta::FromMilliseconds(kPointLifeDurationMs), root_window));
+ }
+}
+
+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 (stationary_timer_repeat_count_++ * kAddStationaryPointsDelayMs >=
+ kPointLifeDurationMs) {
+ StopTimer();
+ }
+}
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698