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/palette_delegate.h" |
| 8 #include "ash/common/system/chromeos/palette/palette_ids.h" |
| 9 #include "ash/common/system/chromeos/palette/tools/laser_pointer_view.h" |
| 10 #include "ash/common/wm_shell.h" |
| 11 #include "grit/ash_strings.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/wm/core/coordinate_conversion.h" |
| 14 #include "ui/wm/core/cursor_manager.h" |
| 15 |
| 16 namespace ash { |
| 17 namespace { |
| 18 |
| 19 // A point gets removed from the collection if it is older than |
| 20 // |kPointLifeDurationMs|. |
| 21 const int kPointLifeDurationMs = 200; |
| 22 |
| 23 // When no move events are being recieved we add a new point every |
| 24 // |kAddStationaryPointsDelayMs| so that points older than |
| 25 // |kPointLifeDurationMs| can get removed. |
| 26 const int kAddStationaryPointsDelayMs = 5; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 LaserPointerMode::LaserPointerMode(Delegate* delegate) |
| 31 : CommonPaletteTool(delegate) { |
| 32 laser_pointer_view_.reset(new LaserPointerView( |
| 33 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs))); |
| 34 timer_.reset(new base::Timer( |
| 35 FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), |
| 36 base::Bind(&LaserPointerMode::AddStationaryPoint, base::Unretained(this)), |
| 37 true)); |
| 38 WmShell::Get()->AddPointerWatcher(this, true); |
| 39 } |
| 40 |
| 41 LaserPointerMode::~LaserPointerMode() { |
| 42 OnDisable(); |
| 43 WmShell::Get()->RemovePointerWatcher(this); |
| 44 } |
| 45 |
| 46 PaletteGroup LaserPointerMode::GetGroup() const { |
| 47 return PaletteGroup::MODE; |
| 48 } |
| 49 |
| 50 PaletteToolId LaserPointerMode::GetToolId() const { |
| 51 return PaletteToolId::LASER_POINTER; |
| 52 } |
| 53 |
| 54 void LaserPointerMode::OnEnable() { |
| 55 CommonPaletteTool::OnEnable(); |
| 56 |
| 57 WmShell::Get()->palette_delegate()->OnLaserPointerEnabled(); |
| 58 laser_pointer_view_->AddNewPoint(current_mouse_location_); |
| 59 } |
| 60 |
| 61 void LaserPointerMode::OnDisable() { |
| 62 CommonPaletteTool::OnDisable(); |
| 63 |
| 64 WmShell::Get()->palette_delegate()->OnLaserPointerDisabled(); |
| 65 StopTimer(); |
| 66 laser_pointer_view_->Stop(); |
| 67 } |
| 68 |
| 69 gfx::VectorIconId LaserPointerMode::GetActiveTrayIcon() { |
| 70 return gfx::VectorIconId::PALETTE_TRAY_ICON_LASER_POINTER; |
| 71 } |
| 72 |
| 73 gfx::VectorIconId LaserPointerMode::GetPaletteIconId() { |
| 74 return gfx::VectorIconId::PALETTE_MODE_LASER_POINTER; |
| 75 } |
| 76 |
| 77 views::View* LaserPointerMode::CreateView() { |
| 78 return CreateDefaultView( |
| 79 l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_LASER_POINTER_MODE)); |
| 80 } |
| 81 |
| 82 void LaserPointerMode::StopTimer() { |
| 83 timer_repeat_count_ = 0; |
| 84 timer_->Stop(); |
| 85 } |
| 86 |
| 87 void LaserPointerMode::AddStationaryPoint() { |
| 88 laser_pointer_view_->AddNewPoint(current_mouse_location_); |
| 89 // We can stop repeating the timer once the mouse has been stationary for |
| 90 // longer than the life of a point. |
| 91 if (timer_repeat_count_++ * kAddStationaryPointsDelayMs >= |
| 92 kPointLifeDurationMs) { |
| 93 StopTimer(); |
| 94 } |
| 95 } |
| 96 |
| 97 void LaserPointerMode::OnPointerEventObserved( |
| 98 const ui::PointerEvent& event, |
| 99 const gfx::Point& location_in_screen, |
| 100 views::Widget* target) { |
| 101 // TODO(sammiequon): Add support for pointer drags. See crbug.com/640410. |
| 102 if (event.type() == ui::ET_POINTER_MOVED && |
| 103 event.pointer_details().pointer_type == |
| 104 ui::EventPointerType::POINTER_TYPE_PEN) { |
| 105 current_mouse_location_ = location_in_screen; |
| 106 if (enabled()) { |
| 107 laser_pointer_view_->AddNewPoint(current_mouse_location_); |
| 108 timer_repeat_count_ = 0; |
| 109 if (!timer_->IsRunning()) |
| 110 timer_->Reset(); |
| 111 } |
| 112 } |
| 113 } |
| 114 } // namespace ash |
OLD | NEW |