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/common/system/chromeos/palette/tools/laser_pointer_mode.h" |
| 6 |
| 7 #include "ash/common/system/chromeos/palette/palette_ids.h" |
| 8 #include "ash/common/wm_shell.h" |
| 9 #include "ash/shell.h" |
| 10 #include "ui/wm/core/coordinate_conversion.h" |
| 11 #include "ui/wm/core/cursor_manager.h" |
| 12 |
| 13 namespace ash { |
| 14 |
| 15 const int LaserPointerMode::kPointLifeDurationMs = 300; |
| 16 const int LaserPointerMode::kAddStationaryPointsDelayMs = 10; |
| 17 |
| 18 LaserPointerMode::LaserPointerMode(Delegate* delegate) |
| 19 : CommonPaletteTool(delegate), |
| 20 timer_repeat_count_(0), |
| 21 laser_points_( |
| 22 base::TimeDelta::FromMilliseconds(int{kPointLifeDurationMs})) { |
| 23 laser_pointer_view_ = new LaserPointerView(); |
| 24 InitializeTimer(); |
| 25 Shell::GetInstance()->AddPreTargetHandler(this); |
| 26 } |
| 27 |
| 28 LaserPointerMode::~LaserPointerMode() { |
| 29 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 30 } |
| 31 |
| 32 PaletteGroup LaserPointerMode::GetGroup() const { |
| 33 return PaletteGroup::MODE; |
| 34 } |
| 35 |
| 36 PaletteToolId LaserPointerMode::GetToolId() const { |
| 37 return PaletteToolId::LASER_POINTER; |
| 38 } |
| 39 |
| 40 void LaserPointerMode::OnEnable() { |
| 41 CommonPaletteTool::OnEnable(); |
| 42 //WmShell::Get()->cursor_manager()->HideCursor(); |
| 43 // We lock the cursor after we hide it because it is always being updated by |
| 44 // compound_event_filter.cc to be shown on every mouse movement. |
| 45 //WmShell::Get()->cursor_manager()->LockCursor(); |
| 46 laser_points_.AddPoint(current_mouse_location_); |
| 47 laser_pointer_view_->SetNewPoints(laser_points_); |
| 48 } |
| 49 |
| 50 void LaserPointerMode::OnDisable() { |
| 51 CommonPaletteTool::OnDisable(); |
| 52 //WmShell::Get()->cursor_manager()->UnlockCursor(); |
| 53 //WmShell::Get()->cursor_manager()->ShowCursor(); |
| 54 StopTimer(); |
| 55 // Clear the points and then notify the view that there is nothing to be |
| 56 // rendered. |
| 57 laser_points_.Clear(); |
| 58 laser_pointer_view_->Stop(); |
| 59 } |
| 60 |
| 61 gfx::VectorIconId LaserPointerMode::GetActiveTrayIcon() { |
| 62 return gfx::VectorIconId::PALETTE_TRAY_ICON_LASER_POINTER; |
| 63 } |
| 64 |
| 65 gfx::VectorIconId LaserPointerMode::GetPaletteIconId() { |
| 66 return gfx::VectorIconId::PALETTE_MODE_LASER_POINTER; |
| 67 } |
| 68 |
| 69 void LaserPointerMode::InitializeTimer() { |
| 70 timer_.reset(new base::Timer( |
| 71 FROM_HERE, |
| 72 base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), |
| 73 base::Bind(&LaserPointerMode::AddStationaryPoint, |
| 74 base::Unretained(this)), |
| 75 false)); |
| 76 } |
| 77 |
| 78 void LaserPointerMode::StartTimer() { |
| 79 timer_->Reset(); |
| 80 } |
| 81 |
| 82 void LaserPointerMode::StopTimer() { |
| 83 points_lock_.Acquire(); |
| 84 timer_repeat_count_ = 0; |
| 85 points_lock_.Release(); |
| 86 timer_->Stop(); |
| 87 } |
| 88 |
| 89 void LaserPointerMode::AddStationaryPoint() { |
| 90 points_lock_.Acquire(); |
| 91 laser_points_.AddPoint(current_mouse_location_); |
| 92 laser_pointer_view_->SetNewPoints(laser_points_); |
| 93 int count = timer_repeat_count_; |
| 94 points_lock_.Release(); |
| 95 // We can stop repeating the timer once the mouse has been stationary for |
| 96 // longer than the life of a point. |
| 97 if (count * kAddStationaryPointsDelayMs < kPointLifeDurationMs) { |
| 98 timer_repeat_count_++; |
| 99 timer_->Reset(); |
| 100 } else { |
| 101 StopTimer(); |
| 102 } |
| 103 } |
| 104 |
| 105 void LaserPointerMode::OnMouseEvent(ui::MouseEvent* event) { |
| 106 if (event->type() == ui::ET_MOUSE_MOVED && |
| 107 !(event->flags() & ui::EF_IS_SYNTHESIZED)) { |
| 108 |
| 109 gfx::Point mouse_location = event->location(); |
| 110 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), |
| 111 &mouse_location); |
| 112 current_mouse_location_ = mouse_location; |
| 113 if (enabled()) { |
| 114 points_lock_.Acquire(); |
| 115 // Add the new point and notify the queue. |
| 116 laser_points_.AddPoint(mouse_location); |
| 117 laser_pointer_view_->SetNewPoints(laser_points_); |
| 118 timer_repeat_count_ = 0; |
| 119 points_lock_.Release(); |
| 120 StartTimer(); |
| 121 } |
| 122 } |
| 123 } |
| 124 |
| 125 void LaserPointerMode::OnKeyEvent(ui::KeyEvent* event) { |
| 126 } |
| 127 |
| 128 void LaserPointerMode::OnTouchEvent(ui::TouchEvent* event) { |
| 129 } |
| 130 |
| 131 void LaserPointerMode::OnGestureEvent(ui::GestureEvent* event) { |
| 132 } |
| 133 |
| 134 void LaserPointerMode::OnScrollEvent(ui::ScrollEvent* event) { |
| 135 } |
| 136 |
| 137 } // namespace ash |
OLD | NEW |