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

Side by Side Diff: ash/common/system/chromeos/palette/tools/laser_pointer_view.cc

Issue 2231533004: Pointer watcher modifications to support laser pointer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Created 4 years, 4 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 unified diff | Download patch
OLDNEW
(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_view.h"
6
7 #include <memory>
8
9 #include "ash/aura/wm_window_aura.h"
10 #include "ash/common/shell_window_ids.h"
11 #include "ash/common/wm/root_window_finder.h"
12 #include "ash/root_window_controller.h"
13 #include "ash/shell.h"
14 #include "third_party/skia/include/core/SkColor.h"
15 #include "third_party/skia/include/core/SkPaint.h"
16 #include "third_party/skia/include/core/SkRect.h"
17 #include "ui/aura/client/screen_position_client.h"
18 #include "ui/aura/env.h"
19 #include "ui/aura/window.h"
20 #include "ui/aura/window_event_dispatcher.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/transform.h"
24 #include "ui/wm/core/coordinate_conversion.h"
25
26 namespace ash {
27 namespace {
28
29 const double kMouselaserUiStartRadius = 4;
30 const double kMouselaserUiEndRadius = 0.25;
31 const int kMouselaserUiStartOpacityValue = 200;
32 const int kMouselaserUiEndOpacityValue = 0;
33
34 views::Widget* CreateLaserPointerWidget(aura::Window* root_window) {
35 views::Widget* widget = new views::Widget;
36 views::Widget::InitParams params;
37 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
38 params.accept_events = false;
39 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
40 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
41 params.context = root_window;
42 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
43 params.parent = ash::Shell::GetContainer(
44 root_window, ash::kShellWindowId_OverlayContainer);
45
46 widget->Init(params);
47 widget->SetOpacity(1.f);
48 return widget;
49 }
50
51 float DistanceBetweenPoints(const gfx::Point& point1,
52 const gfx::Point& point2) {
53 float deltaX = point1.x() - point2.x();
54 float deltaY = point1.y() - point2.y();
55 return deltaX * deltaX + deltaY * deltaY;
56 }
57
58 void PaintLaser(gfx::Canvas* canvas,
59 const LaserPointerPoints& points,
60 const gfx::Rect& widget_bounds) {
61 SkPaint paint;
62 paint.setStyle(SkPaint::kStroke_Style);
63 paint.setAntiAlias(true);
64 paint.setStrokeJoin(SkPaint::kBevel_Join);
65 SkColor paintColor;
66
67 if (!points.IsEmpty()) {
68 base::Time oldest = points.GetOldest().creationTime_;
69 base::Time most_recent = points.GetMostRecent().creationTime_;
70 gfx::Point last_point, current_point;
71 bool first = true;
72 int point_count = 0;
73 int num_points = points.GetNumberOfPoints();
74 for (auto it = points.PointsStart(); it != points.PointsEnd(); it++, point_c ount++) {
75 float normalized;
76 // Only one point, the regular normalization will return undefined for
77 // this case.
78 if (oldest == most_recent)
79 normalized = 1.0;
80 else { // Normalize the distance.
81 normalized = float{(it->creationTime_ - oldest).InMillisecondsF()} /
82 float{(most_recent - oldest).InMillisecondsF()};
83 normalized = 1.0 - normalized;
84 }
85
86 float radius = float{kMouselaserUiStartRadius} - normalized *
87 float{kMouselaserUiStartRadius - kMouselaserUiEndRadius};
88 int opacity = kMouselaserUiStartOpacityValue - int{normalized *
89 float{kMouselaserUiStartOpacityValue - kMouselaserUiEndOpacityValue}};
90 gfx::Vector2d center = it->location_ - widget_bounds.origin();
91 current_point = gfx::Point(center.x(),center.y());
92 paintColor = SkColorSetARGB(opacity, 255, 0, 0);
93 paint.setColor(paintColor);
94 paint.setStrokeWidth(radius*2);
95 if (first) {
96 first = false;
97 last_point = current_point;
98 } else if(DistanceBetweenPoints(last_point, current_point) >
99 radius * 4 * radius) {
100 canvas->DrawLine(last_point, current_point, paint);
101 last_point = current_point;
102 } else if (point_count == num_points-1) {
103 canvas->DrawLine(last_point, current_point, paint);
104 }
105 }
106 paintColor = SkColorSetARGB(kMouselaserUiStartOpacityValue, 255, 0, 0);
107 paint.setColor(paintColor);
108 paint.setStyle(SkPaint::kFill_Style);
109 canvas->DrawCircle(current_point, kMouselaserUiStartRadius, paint);
110 }
111 }
112 } // namespace
113
114 ////////////////////////////////////////////////////////////////////////////////
115
116 // LaserPointerView
117 LaserPointerView::LaserPointerView()
118 : views::View(),
119 target_(nullptr),
120 laser_points_(base::TimeDelta()) {
121 }
122
123 LaserPointerView::~LaserPointerView() {
124 Stop();
125 }
126
127 aura::Window* LaserPointerView::GetTargetWindow() {
128 gfx::Point location = gfx::Point(0,0);
129 if (!laser_points_.IsEmpty())
130 location = laser_points_.GetMostRecent().location_;
131
132 aura::Window* target = ash::WmWindowAura::GetAuraWindow(
133 ash::wm::GetRootWindowAt(location));
134 DCHECK(target) << "Root window not found while rendering autoclick circle;";
135 return target;
136 }
137
138 void LaserPointerView::Stop() {
139 laser_points_.Clear();
140 SetTarget(nullptr);
141 SetNewPoints(laser_points_);
142 }
143
144 void LaserPointerView::SetTarget() {
145 aura::Window* target = GetTargetWindow();
146 SetTarget(target);
147 }
148
149 void LaserPointerView::SetTarget(aura::Window* target) {
150 if (target_ == target)
151 return;
152
153 if (target_)
154 target_->RemoveObserver(this);
155 target_ = target;
156 if (target_)
157 target_->AddObserver(this);
158 }
159
160 void LaserPointerView::OnWindowDestroying(aura::Window* window) {
161 DCHECK_EQ(target_, window);
162 Stop();
163 }
164
165 void LaserPointerView::SetNewPoints(const LaserPointerPoints& laser_points) {
166 laser_points_ = laser_points;
167 gfx::Rect oldBoundingBox, newBoundingBox;
168 oldBoundingBox = laser_points_.GetBoundingBox();
169 newBoundingBox.SetRect(oldBoundingBox.x() - 20,
170 oldBoundingBox.y() - 20,
171 oldBoundingBox.width() + 40,
172 oldBoundingBox.height() + 40);
173 widget_.reset(CreateLaserPointerWidget(GetTargetWindow()));
174 widget_->SetBounds(newBoundingBox);
175 widget_->Show();
176 widget_->GetNativeView()->layer()->SetOpacity(1.f);
177 widget_->SetContentsView(this);
178 set_owned_by_client();
179 }
180
181 void LaserPointerView::OnPaint(gfx::Canvas* canvas) {
182 canvas->Save();
183 PaintLaser(canvas, laser_points_, widget_->GetWindowBoundsInScreen());
184 canvas->Restore();
185 }
186 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698