| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode.h" | 5 #include "ash/common/system/chromeos/palette/tools/laser_pointer_mode.h" |
| 6 | 6 |
| 7 #include "ash/common/palette_delegate.h" | 7 #include "ash/common/palette_delegate.h" |
| 8 #include "ash/common/system/chromeos/palette/palette_ids.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" | 9 #include "ash/common/wm_shell.h" |
| 11 #include "grit/ash_strings.h" | 10 #include "grit/ash_strings.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/wm/core/coordinate_conversion.h" | |
| 14 #include "ui/wm/core/cursor_manager.h" | |
| 15 | 12 |
| 16 namespace ash { | 13 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 | 14 |
| 30 LaserPointerMode::LaserPointerMode(Delegate* delegate) | 15 LaserPointerMode::LaserPointerMode(Delegate* delegate) |
| 31 : CommonPaletteTool(delegate) { | 16 : 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 } | 17 } |
| 40 | 18 |
| 41 LaserPointerMode::~LaserPointerMode() { | 19 LaserPointerMode::~LaserPointerMode() {} |
| 42 OnDisable(); | |
| 43 WmShell::Get()->RemovePointerWatcher(this); | |
| 44 } | |
| 45 | 20 |
| 46 PaletteGroup LaserPointerMode::GetGroup() const { | 21 PaletteGroup LaserPointerMode::GetGroup() const { |
| 47 return PaletteGroup::MODE; | 22 return PaletteGroup::MODE; |
| 48 } | 23 } |
| 49 | 24 |
| 50 PaletteToolId LaserPointerMode::GetToolId() const { | 25 PaletteToolId LaserPointerMode::GetToolId() const { |
| 51 return PaletteToolId::LASER_POINTER; | 26 return PaletteToolId::LASER_POINTER; |
| 52 } | 27 } |
| 53 | 28 |
| 54 void LaserPointerMode::OnEnable() { | 29 void LaserPointerMode::OnEnable() { |
| 55 CommonPaletteTool::OnEnable(); | 30 CommonPaletteTool::OnEnable(); |
| 56 | 31 |
| 57 WmShell::Get()->palette_delegate()->OnLaserPointerEnabled(); | 32 WmShell::Get()->SetLaserPointerEnabled(true); |
| 58 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 59 } | 33 } |
| 60 | 34 |
| 61 void LaserPointerMode::OnDisable() { | 35 void LaserPointerMode::OnDisable() { |
| 62 CommonPaletteTool::OnDisable(); | 36 CommonPaletteTool::OnDisable(); |
| 63 | 37 |
| 64 WmShell::Get()->palette_delegate()->OnLaserPointerDisabled(); | 38 WmShell::Get()->SetLaserPointerEnabled(false); |
| 65 StopTimer(); | |
| 66 laser_pointer_view_->Stop(); | |
| 67 } | 39 } |
| 68 | 40 |
| 69 gfx::VectorIconId LaserPointerMode::GetActiveTrayIcon() { | 41 gfx::VectorIconId LaserPointerMode::GetActiveTrayIcon() { |
| 70 return gfx::VectorIconId::PALETTE_TRAY_ICON_LASER_POINTER; | 42 return gfx::VectorIconId::PALETTE_TRAY_ICON_LASER_POINTER; |
| 71 } | 43 } |
| 72 | 44 |
| 73 gfx::VectorIconId LaserPointerMode::GetPaletteIconId() { | 45 gfx::VectorIconId LaserPointerMode::GetPaletteIconId() { |
| 74 return gfx::VectorIconId::PALETTE_MODE_LASER_POINTER; | 46 return gfx::VectorIconId::PALETTE_MODE_LASER_POINTER; |
| 75 } | 47 } |
| 76 | 48 |
| 77 views::View* LaserPointerMode::CreateView() { | 49 views::View* LaserPointerMode::CreateView() { |
| 78 return CreateDefaultView( | 50 return CreateDefaultView( |
| 79 l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_LASER_POINTER_MODE)); | 51 l10n_util::GetStringUTF16(IDS_ASH_STYLUS_TOOLS_LASER_POINTER_MODE)); |
| 80 } | 52 } |
| 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 | 53 } // namespace ash |
| OLD | NEW |