Chromium Code Reviews| 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/wm_shell.h" | |
| 10 #include "grit/ash_strings.h" | |
| 11 #include "ui/base/l10n/l10n_util.h" | |
| 12 #include "ui/wm/core/coordinate_conversion.h" | |
| 13 #include "ui/wm/core/cursor_manager.h" | |
| 14 | |
| 15 namespace ash { | |
| 16 namespace { | |
| 17 const int kPointLifeDurationMs = 300; | |
| 18 const int kAddStationaryPointsDelayMs = 10; | |
| 19 } // namespace | |
| 20 | |
| 21 LaserPointerMode::LaserPointerMode(Delegate* delegate) | |
| 22 : CommonPaletteTool(delegate) { | |
| 23 laser_pointer_view_.reset(new LaserPointerView( | |
| 24 base::TimeDelta::FromMilliseconds(kPointLifeDurationMs))); | |
| 25 timer_.reset(new base::Timer( | |
| 26 FROM_HERE, base::TimeDelta::FromMilliseconds(kAddStationaryPointsDelayMs), | |
| 27 base::Bind(&LaserPointerMode::AddStationaryPoint, base::Unretained(this)), | |
| 28 false)); | |
|
jdufault
2016/08/16 19:33:55
Can we make the timer repeating (change the false
sammiequon
2016/08/16 23:18:54
Done.
| |
| 29 WmShell::Get()->AddPointerWatcher(this, true); | |
| 30 } | |
| 31 | |
| 32 LaserPointerMode::~LaserPointerMode() { | |
| 33 StopTimer(); | |
| 34 WmShell::Get()->RemovePointerWatcher(this); | |
| 35 } | |
| 36 | |
| 37 PaletteGroup LaserPointerMode::GetGroup() const { | |
| 38 return PaletteGroup::MODE; | |
| 39 } | |
| 40 | |
| 41 PaletteToolId LaserPointerMode::GetToolId() const { | |
| 42 return PaletteToolId::LASER_POINTER; | |
| 43 } | |
| 44 | |
| 45 void LaserPointerMode::OnEnable() { | |
| 46 CommonPaletteTool::OnEnable(); | |
| 47 | |
| 48 if (WmShell::Get()->palette_delegate()) | |
| 49 WmShell::Get()->palette_delegate()->OnLaserPointerEnabled(); | |
| 50 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 51 } | |
| 52 | |
| 53 void LaserPointerMode::OnDisable() { | |
| 54 CommonPaletteTool::OnDisable(); | |
| 55 | |
| 56 if (WmShell::Get()->palette_delegate()) | |
| 57 WmShell::Get()->palette_delegate()->OnLaserPointerDisabled(); | |
| 58 StopTimer(); | |
| 59 laser_pointer_view_->Stop(); | |
| 60 } | |
| 61 | |
| 62 gfx::VectorIconId LaserPointerMode::GetActiveTrayIcon() { | |
| 63 return gfx::VectorIconId::PALETTE_TRAY_ICON_LASER_POINTER; | |
| 64 } | |
| 65 | |
| 66 gfx::VectorIconId LaserPointerMode::GetPaletteIconId() { | |
| 67 return gfx::VectorIconId::PALETTE_MODE_LASER_POINTER; | |
| 68 } | |
| 69 | |
| 70 views::View* LaserPointerMode::CreateView() { | |
| 71 return CreateDefaultView( | |
| 72 l10n_util::GetStringUTF16(IDS_ASH_PALETTE_LASER_POINTER_MODE)); | |
| 73 } | |
| 74 | |
| 75 void LaserPointerMode::StopTimer() { | |
| 76 timer_repeat_count_ = 0; | |
| 77 timer_->Stop(); | |
| 78 } | |
| 79 | |
| 80 void LaserPointerMode::AddStationaryPoint() { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 83 int count = timer_repeat_count_; | |
| 84 // We can stop repeating the timer once the mouse has been stationary for | |
| 85 // longer than the life of a point. | |
| 86 if (count * kAddStationaryPointsDelayMs < kPointLifeDurationMs) { | |
| 87 timer_repeat_count_++; | |
| 88 timer_->Reset(); | |
| 89 } else { | |
| 90 StopTimer(); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void LaserPointerMode::OnPointerEventObserved( | |
| 95 const ui::PointerEvent& event, | |
| 96 const gfx::Point& location_in_screen, | |
| 97 views::Widget* target) { | |
| 98 if (event.type() == ui::ET_POINTER_MOVED && | |
| 99 event.pointer_details().pointer_type == | |
| 100 ui::EventPointerType::POINTER_TYPE_PEN) { | |
| 101 current_mouse_location_ = location_in_screen; | |
| 102 if (enabled()) { | |
| 103 // Add the new point and notify the queue. | |
| 104 laser_pointer_view_->AddNewPoint(current_mouse_location_); | |
| 105 timer_repeat_count_ = 0; | |
| 106 timer_->Reset(); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 } // namespace ash | |
| OLD | NEW |