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

Side by Side Diff: ash/touch_hud/touch_hud_renderer.cc

Issue 2118223004: ash: Refactor touch hud drawing to ash/touch_hud as a separate component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add dependency Created 4 years, 5 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
« no previous file with comments | « ash/touch_hud/touch_hud_renderer.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/touch/touch_hud_projection.h" 5 #include "ash/touch_hud/touch_hud_renderer.h"
6 6
7 #include "ash/root_window_controller.h"
8 #include "ash/shell.h"
9 #include "third_party/skia/include/effects/SkGradientShader.h" 7 #include "third_party/skia/include/effects/SkGradientShader.h"
8 #include "ui/compositor/layer.h"
9 #include "ui/compositor/layer_owner.h"
10 #include "ui/events/event.h" 10 #include "ui/events/event.h"
11 #include "ui/gfx/animation/animation_delegate.h" 11 #include "ui/gfx/animation/animation_delegate.h"
12 #include "ui/gfx/animation/linear_animation.h" 12 #include "ui/gfx/animation/linear_animation.h"
13 #include "ui/gfx/canvas.h" 13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/geometry/size.h" 14 #include "ui/gfx/geometry/size.h"
15 #include "ui/views/view.h"
15 #include "ui/views/widget/widget.h" 16 #include "ui/views/widget/widget.h"
17 #include "ui/views/widget/widget_observer.h"
16 18
17 namespace ash { 19 namespace ash {
18 20
19 const int kPointRadius = 20; 21 const int kPointRadius = 20;
20 const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC); 22 const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC);
21 const SkColor kProjectionStrokeColor = SK_ColorGRAY; 23 const SkColor kProjectionStrokeColor = SK_ColorGRAY;
22 const int kProjectionAlpha = 0xB0; 24 const int kProjectionAlpha = 0xB0;
23 const int kFadeoutDurationInMs = 250; 25 const int kFadeoutDurationInMs = 250;
24 const int kFadeoutFrameRate = 60; 26 const int kFadeoutFrameRate = 60;
25 27
26 // TouchPointView draws a single touch point. This object manages its own 28 // TouchPointView draws a single touch point. This object manages its own
27 // lifetime and deletes itself upon fade-out completion or whenever |Remove()| 29 // lifetime and deletes itself upon fade-out completion or whenever |Destroy()|
28 // is explicitly called. 30 // is explicitly called.
29 class TouchPointView : public views::View, 31 class TouchPointView : public views::View,
30 public gfx::AnimationDelegate, 32 public gfx::AnimationDelegate,
31 public views::WidgetObserver { 33 public views::WidgetObserver {
32 public: 34 public:
33 explicit TouchPointView(views::Widget* parent_widget) 35 explicit TouchPointView(views::Widget* parent_widget)
34 : circle_center_(kPointRadius + 1, kPointRadius + 1), 36 : circle_center_(kPointRadius + 1, kPointRadius + 1),
35 gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) { 37 gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) {
36 SetPaintToLayer(true); 38 SetPaintToLayer(true);
37 layer()->SetFillsBoundsOpaquely(false); 39 layer()->SetFillsBoundsOpaquely(false);
38 40
39 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2)); 41 SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2));
40 42
41 stroke_paint_.setStyle(SkPaint::kStroke_Style); 43 stroke_paint_.setStyle(SkPaint::kStroke_Style);
42 stroke_paint_.setColor(kProjectionStrokeColor); 44 stroke_paint_.setColor(kProjectionStrokeColor);
43 45
44 gradient_colors_[0] = kProjectionFillColor; 46 gradient_colors_[0] = kProjectionFillColor;
45 gradient_colors_[1] = kProjectionStrokeColor; 47 gradient_colors_[1] = kProjectionStrokeColor;
46 48
47 gradient_pos_[0] = SkFloatToScalar(0.9f); 49 gradient_pos_[0] = SkFloatToScalar(0.9f);
48 gradient_pos_[1] = SkFloatToScalar(1.0f); 50 gradient_pos_[1] = SkFloatToScalar(1.0f);
49 51
50 parent_widget->GetContentsView()->AddChildView(this); 52 parent_widget->GetContentsView()->AddChildView(this);
51 53
52 parent_widget->AddObserver(this); 54 parent_widget->AddObserver(this);
53 } 55 }
54 56
55 void UpdateTouch(const ui::TouchEvent& touch) { 57 void UpdateTouch(const ui::LocatedEvent& touch) {
56 if (touch.type() == ui::ET_TOUCH_RELEASED || 58 if (touch.type() == ui::ET_TOUCH_RELEASED ||
57 touch.type() == ui::ET_TOUCH_CANCELLED) { 59 touch.type() == ui::ET_TOUCH_CANCELLED ||
60 touch.type() == ui::ET_POINTER_UP ||
61 touch.type() == ui::ET_POINTER_CANCELLED) {
58 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs, 62 fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs,
59 kFadeoutFrameRate, this)); 63 kFadeoutFrameRate, this));
60 fadeout_->Start(); 64 fadeout_->Start();
61 } else { 65 } else {
62 SetX(parent()->GetMirroredXInView(touch.root_location().x()) - 66 SetX(parent()->GetMirroredXInView(touch.root_location().x()) -
63 kPointRadius - 1); 67 kPointRadius - 1);
64 SetY(touch.root_location().y() - kPointRadius - 1); 68 SetY(touch.root_location().y() - kPointRadius - 1);
65 } 69 }
66 } 70 }
67 71
68 void Remove() { delete this; } 72 void Destroy() { delete this; }
69 73
70 private: 74 private:
71 ~TouchPointView() override { 75 ~TouchPointView() override {
72 GetWidget()->RemoveObserver(this); 76 GetWidget()->RemoveObserver(this);
73 parent()->RemoveChildView(this); 77 parent()->RemoveChildView(this);
74 } 78 }
75 79
76 // Overridden from views::View. 80 // Overridden from views::View.
77 void OnPaint(gfx::Canvas* canvas) override { 81 void OnPaint(gfx::Canvas* canvas) override {
78 int alpha = kProjectionAlpha; 82 int alpha = kProjectionAlpha;
(...skipping 24 matching lines...) Expand all
103 107
104 void AnimationCanceled(const gfx::Animation* animation) override { 108 void AnimationCanceled(const gfx::Animation* animation) override {
105 AnimationEnded(animation); 109 AnimationEnded(animation);
106 } 110 }
107 111
108 // Overridden from views::WidgetObserver. 112 // Overridden from views::WidgetObserver.
109 void OnWidgetDestroying(views::Widget* widget) override { 113 void OnWidgetDestroying(views::Widget* widget) override {
110 if (fadeout_) 114 if (fadeout_)
111 fadeout_->Stop(); 115 fadeout_->Stop();
112 else 116 else
113 Remove(); 117 Destroy();
114 } 118 }
115 119
116 const gfx::Point circle_center_; 120 const gfx::Point circle_center_;
117 const SkPoint gradient_center_; 121 const SkPoint gradient_center_;
118 122
119 SkPaint fill_paint_; 123 SkPaint fill_paint_;
120 SkPaint stroke_paint_; 124 SkPaint stroke_paint_;
121 SkColor gradient_colors_[2]; 125 SkColor gradient_colors_[2];
122 SkScalar gradient_pos_[2]; 126 SkScalar gradient_pos_[2];
123 127
124 std::unique_ptr<gfx::Animation> fadeout_; 128 std::unique_ptr<gfx::Animation> fadeout_;
125 129
126 DISALLOW_COPY_AND_ASSIGN(TouchPointView); 130 DISALLOW_COPY_AND_ASSIGN(TouchPointView);
127 }; 131 };
128 132
129 TouchHudProjection::TouchHudProjection(aura::Window* initial_root) 133 TouchHudRenderer::TouchHudRenderer(views::Widget* parent_widget)
130 : TouchObserverHUD(initial_root) {} 134 : parent_widget_(parent_widget) {}
131 135
132 TouchHudProjection::~TouchHudProjection() {} 136 TouchHudRenderer::~TouchHudRenderer() {}
133 137
134 void TouchHudProjection::Clear() { 138 void TouchHudRenderer::Clear() {
135 for (std::map<int, TouchPointView*>::iterator iter = points_.begin(); 139 for (std::map<int, TouchPointView*>::iterator iter = points_.begin();
136 iter != points_.end(); iter++) 140 iter != points_.end(); iter++)
137 iter->second->Remove(); 141 iter->second->Destroy();
138 points_.clear(); 142 points_.clear();
139 } 143 }
140 144
141 void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) { 145 void TouchHudRenderer::HandleTouchEvent(const ui::LocatedEvent& event) {
142 if (event->type() == ui::ET_TOUCH_PRESSED) { 146 int id = 0;
143 TouchPointView* point = new TouchPointView(widget()); 147 if (event.IsTouchEvent()) {
144 point->UpdateTouch(*event); 148 id = event.AsTouchEvent()->touch_id();
149 } else {
150 DCHECK(event.IsPointerEvent());
151 DCHECK(event.AsPointerEvent()->pointer_details().pointer_type ==
152 ui::EventPointerType::POINTER_TYPE_TOUCH);
153 id = event.AsPointerEvent()->pointer_id();
154 }
155 if (event.type() == ui::ET_TOUCH_PRESSED ||
156 event.type() == ui::ET_POINTER_DOWN) {
157 TouchPointView* point = new TouchPointView(parent_widget_);
158 point->UpdateTouch(event);
145 std::pair<std::map<int, TouchPointView*>::iterator, bool> result = 159 std::pair<std::map<int, TouchPointView*>::iterator, bool> result =
146 points_.insert(std::make_pair(event->touch_id(), point)); 160 points_.insert(std::make_pair(id, point));
147 // If a |TouchPointView| is already mapped to the touch id, remove it and 161 // If a |TouchPointView| is already mapped to the touch id, destroy it and
148 // replace it with the new one. 162 // replace it with the new one.
149 if (!result.second) { 163 if (!result.second) {
150 result.first->second->Remove(); 164 result.first->second->Destroy();
151 result.first->second = point; 165 result.first->second = point;
152 } 166 }
153 } else { 167 } else {
154 std::map<int, TouchPointView*>::iterator iter = 168 std::map<int, TouchPointView*>::iterator iter = points_.find(id);
155 points_.find(event->touch_id());
156 if (iter != points_.end()) { 169 if (iter != points_.end()) {
157 iter->second->UpdateTouch(*event); 170 iter->second->UpdateTouch(event);
158 if (event->type() == ui::ET_TOUCH_RELEASED || 171 if (event.type() == ui::ET_TOUCH_RELEASED ||
159 event->type() == ui::ET_TOUCH_CANCELLED) 172 event.type() == ui::ET_TOUCH_CANCELLED ||
173 event.type() == ui::ET_POINTER_UP ||
174 event.type() == ui::ET_POINTER_CANCELLED)
160 points_.erase(iter); 175 points_.erase(iter);
161 } 176 }
162 } 177 }
163 } 178 }
164 179
165 void TouchHudProjection::SetHudForRootWindowController(
166 RootWindowController* controller) {
167 controller->set_touch_hud_projection(this);
168 }
169
170 void TouchHudProjection::UnsetHudForRootWindowController(
171 RootWindowController* controller) {
172 controller->set_touch_hud_projection(NULL);
173 }
174
175 } // namespace ash 180 } // namespace ash
OLDNEW
« no previous file with comments | « ash/touch_hud/touch_hud_renderer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698