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

Side by Side Diff: ash/laser/laser_pointer_view.cc

Issue 2311393004: Laser tool blocks events from propagating. (Closed)
Patch Set: Fixed patch set 4 errors. Created 4 years, 3 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
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_view.h" 5 #include "ash/laser/laser_pointer_view.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "ash/common/shell_window_ids.h" 9 #include "ash/common/shell_window_ids.h"
10 #include "ash/common/wm_root_window_controller.h" 10 #include "ash/laser/laser_pointer_points.h"
11 #include "ash/common/wm_shell.h" 11 #include "ash/shell.h"
12 #include "ash/common/wm_window.h" 12 #include "base/timer/timer.h"
13 #include "third_party/skia/include/core/SkColor.h" 13 #include "third_party/skia/include/core/SkColor.h"
14 #include "third_party/skia/include/core/SkPaint.h" 14 #include "third_party/skia/include/core/SkPaint.h"
15 #include "ui/aura/window.h"
16 #include "ui/events/event.h"
15 #include "ui/gfx/canvas.h" 17 #include "ui/gfx/canvas.h"
18 #include "ui/views/widget/widget.h"
16 19
17 namespace ash { 20 namespace ash {
18 namespace { 21 namespace {
19 22
23 // Variables for rendering the laser. Radius in DIP.
20 const double kPointInitialRadius = 5; 24 const double kPointInitialRadius = 5;
21 const double kPointFinalRadius = 0.25; 25 const double kPointFinalRadius = 0.25;
22 const int kPointInitialOpacity = 200; 26 const int kPointInitialOpacity = 200;
23 const int kPointFinalOpacity = 0; 27 const int kPointFinalOpacity = 0;
24 const SkColor kPointColor = SkColorSetRGB(255, 0, 0); 28 const SkColor kPointColor = SkColorSetRGB(255, 0, 0);
25 29
26 float DistanceBetweenPoints(const gfx::Point& point1, 30 float DistanceBetweenPoints(const gfx::Point& point1,
27 const gfx::Point& point2) { 31 const gfx::Point& point2) {
28 return (point1 - point2).Length(); 32 return (point1 - point2).Length();
29 } 33 }
30 34
31 double LinearInterpolate(double initial_value, 35 double LinearInterpolate(double initial_value,
32 double final_value, 36 double final_value,
33 double progress) { 37 double progress) {
34 return initial_value + (final_value - initial_value) * progress; 38 return initial_value + (final_value - initial_value) * progress;
35 } 39 }
36 40
37 } // namespace 41 } // namespace
38 42
39 //////////////////////////////////////////////////////////////////////////////// 43 ////////////////////////////////////////////////////////////////////////////////
40 44
41 // LaserPointerView 45 // LaserPointerView
42 LaserPointerView::LaserPointerView(base::TimeDelta life_duration) 46 LaserPointerView::LaserPointerView(base::TimeDelta life_duration,
47 aura::Window* root_window)
43 : laser_points_(life_duration) { 48 : laser_points_(life_duration) {
44 widget_.reset(new views::Widget); 49 widget_.reset(new views::Widget);
45 views::Widget::InitParams params; 50 views::Widget::InitParams params;
46 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; 51 params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS;
47 params.name = "LaserOverlay"; 52 params.name = "LaserOverlay";
48 params.accept_events = false; 53 params.accept_events = false;
49 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; 54 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
50 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 55 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
51 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 56 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
52 WmShell::Get() 57 params.parent =
53 ->GetRootWindowForNewWindows() 58 Shell::GetContainer(root_window, kShellWindowId_OverlayContainer);
54 ->GetRootWindowController() 59
55 ->ConfigureWidgetInitParamsForContainer(
56 widget_.get(), kShellWindowId_OverlayContainer, &params);
57 widget_->Init(params); 60 widget_->Init(params);
58 widget_->Show(); 61 widget_->Show();
59 widget_->SetContentsView(this); 62 widget_->SetContentsView(this);
60 set_owned_by_client(); 63 set_owned_by_client();
61 } 64 }
62 65
63 LaserPointerView::~LaserPointerView() {} 66 LaserPointerView::~LaserPointerView() {}
64 67
65 void LaserPointerView::Stop() { 68 void LaserPointerView::Stop() {
66 laser_points_.Clear(); 69 laser_points_.Clear();
67 SchedulePaint(); 70 SchedulePaint();
68 } 71 }
69 72
73 aura::Window* LaserPointerView::GetRootWindow() {
74 return widget_->GetNativeView()->GetRootWindow();
75 }
76
77 void LaserPointerView::ReparentWidget(aura::Window* new_root_window) {
78 if (GetRootWindow() != new_root_window) {
79 Stop();
jdufault 2016/09/16 18:43:54 Is Stop the right approach, or should we actually
sammiequon 2016/09/16 19:21:04 I think the points get converted in the paint func
80 views::Widget::ReparentNativeView(
81 widget_->GetNativeView(),
82 Shell::GetContainer(new_root_window, kShellWindowId_OverlayContainer));
83 }
84 }
85
70 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) { 86 void LaserPointerView::AddNewPoint(const gfx::Point& new_point) {
71 laser_points_.AddPoint(new_point); 87 laser_points_.AddPoint(new_point);
88
89 // The bounding box should be relative to the screen.
90 gfx::Point screen_offset =
91 widget_->GetNativeView()->GetRootWindow()->GetBoundsInScreen().origin();
92
72 // Expand the bounding box so that it includes the radius of the points on the 93 // Expand the bounding box so that it includes the radius of the points on the
73 // edges. 94 // edges.
74 gfx::Rect bounding_box; 95 gfx::Rect bounding_box;
75 bounding_box = laser_points_.GetBoundingBox(); 96 bounding_box = laser_points_.GetBoundingBox();
76 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius); 97 bounding_box.Offset(-kPointInitialRadius, -kPointInitialRadius);
98 bounding_box.Offset(screen_offset.x(), screen_offset.y());
77 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2)); 99 bounding_box.set_width(bounding_box.width() + (kPointInitialRadius * 2));
78 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2)); 100 bounding_box.set_height(bounding_box.height() + (kPointInitialRadius * 2));
79 widget_->SetBounds(bounding_box); 101 widget_->SetBounds(bounding_box);
80 SchedulePaint(); 102 SchedulePaint();
81 } 103 }
82 104
83 void LaserPointerView::OnPaint(gfx::Canvas* canvas) { 105 void LaserPointerView::OnPaint(gfx::Canvas* canvas) {
84 if (laser_points_.IsEmpty()) 106 if (laser_points_.IsEmpty())
85 return; 107 return;
86 108
87 SkPaint paint; 109 SkPaint paint;
88 paint.setStyle(SkPaint::kStroke_Style); 110 paint.setStyle(SkPaint::kStroke_Style);
89 paint.setAntiAlias(true); 111 paint.setAntiAlias(true);
90 paint.setStrokeJoin(SkPaint::kBevel_Join); 112 paint.setStrokeJoin(SkPaint::kBevel_Join);
91 113
92 base::Time oldest = laser_points_.GetOldest().creation_time; 114 base::Time oldest = laser_points_.GetOldest().creation_time;
93 base::Time newest = laser_points_.GetNewest().creation_time; 115 base::Time newest = laser_points_.GetNewest().creation_time;
94 gfx::Point previous_point = laser_points_.GetOldest().location; 116 gfx::Point previous_point = laser_points_.GetOldest().location;
95 gfx::Point current_point; 117 gfx::Point current_point;
96 gfx::Rect widget_bounds = widget_->GetWindowBoundsInScreen(); 118
119 // Compute the offset of the current widget.
120 gfx::Point widget_offset =
121 widget_->GetNativeView()->GetBoundsInRootWindow().origin();
97 int num_points_ = laser_points_.GetNumberOfPoints(); 122 int num_points_ = laser_points_.GetNumberOfPoints();
98 int point_count = 0; 123 int point_count = 0;
99 for (const LaserPointerPoints::LaserPoint& point : 124 for (const LaserPointerPoints::LaserPoint& point :
100 laser_points_.laser_points()) { 125 laser_points_.laser_points()) {
101 // relative_time is a value between [0,1] where 0 means the point is about 126 // relative_time is a value between [0,1] where 0 means the point is about
102 // to be removed and 1 means that the point was just added. 127 // to be removed and 1 means that the point was just added.
103 double relative_time = 1.0; 128 double relative_time = 1.0;
104 if (oldest != newest) { 129 if (oldest != newest) {
105 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() / 130 relative_time = 1.0 - ((point.creation_time - oldest).InMillisecondsF() /
106 (newest - oldest).InMillisecondsF()); 131 (newest - oldest).InMillisecondsF());
107 } 132 }
108 133
109 // Set the radius and opacity based on the distance. 134 // Set the radius and opacity based on the distance.
110 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius, 135 double radius = LinearInterpolate(kPointInitialRadius, kPointFinalRadius,
111 relative_time); 136 relative_time);
112 137
113 gfx::Vector2d center = point.location - widget_bounds.origin(); 138 gfx::Vector2d center = point.location - widget_offset;
114 current_point = gfx::Point(center.x(), center.y()); 139 current_point = gfx::Point(center.x(), center.y());
115 140
116 // If we draw laser_points_ that are within a stroke width of each other, 141 // If we draw laser_points_ that are within a stroke width of each other,
117 // the result will be very jagged, unless we are on the last point, then we 142 // the result will be very jagged, unless we are on the last point, then we
118 // draw regardless. 143 // draw regardless.
119 point_count++; 144 point_count++;
120 float distance_threshold = float{radius * 2}; 145 float distance_threshold = float{radius * 2};
121 if (DistanceBetweenPoints(previous_point, current_point) <= 146 if (DistanceBetweenPoints(previous_point, current_point) <=
122 distance_threshold && 147 distance_threshold &&
123 point_count != num_points_) { 148 point_count != num_points_) {
124 continue; 149 continue;
125 } 150 }
126 151
127 int opacity = 152 int opacity =
128 int{LinearInterpolate(double{kPointInitialOpacity}, 153 int{LinearInterpolate(double{kPointInitialOpacity},
129 double{kPointFinalOpacity}, relative_time)}; 154 double{kPointFinalOpacity}, relative_time)};
130 paint.setColor(SkColorSetA(kPointColor, opacity)); 155 paint.setColor(SkColorSetA(kPointColor, opacity));
131 paint.setStrokeWidth(radius * 2); 156 paint.setStrokeWidth(radius * 2);
132 canvas->DrawLine(previous_point, current_point, paint); 157 canvas->DrawLine(previous_point, current_point, paint);
133 previous_point = current_point; 158 previous_point = current_point;
134 } 159 }
160 // Draw the last point as a circle.
135 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity)); 161 paint.setColor(SkColorSetA(kPointColor, kPointInitialOpacity));
136 paint.setStyle(SkPaint::kFill_Style); 162 paint.setStyle(SkPaint::kFill_Style);
137 canvas->DrawCircle(current_point, kPointInitialRadius, paint); 163 canvas->DrawCircle(current_point, kPointInitialRadius, paint);
138 } 164 }
139 } // namespace ash 165 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698