Skip to content

Commit

Permalink
ash: Refactor touch hud drawing to ash/touch_hud.
Browse files Browse the repository at this point in the history
This is part of the touch hud app for mustash (https://codereview.chromium.org/2092343002/).

BUG=588311
TEST=ash_unittests

Review-Url: https://codereview.chromium.org/2118223004
Cr-Commit-Position: refs/heads/master@{#405296}
  • Loading branch information
riaj authored and Commit bot committed Jul 13, 2016
1 parent 9184130 commit 98caac0
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 144 deletions.
2 changes: 2 additions & 0 deletions ash/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ component("ash") {
"//ash/strings",
]
deps = [
"//ash/touch_hud",
"//base",
"//base:i18n",
"//base/third_party/dynamic_annotations",
Expand Down Expand Up @@ -349,6 +350,7 @@ test("ash_unittests") {
":test_support_with_content",
"//ash/resources",
"//ash/strings",
"//ash/touch_hud",
"//base",
"//base/test:test_support",
"//components/signin/core/account_id",
Expand Down
2 changes: 2 additions & 0 deletions ash/ash.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,7 @@
'../url/url.gyp:url_lib',
'ash_resources.gyp:ash_resources',
'ash_strings.gyp:ash_strings',
'ash_touch_hud.gyp:ash_touch_hud',
],
'defines': [
'ASH_IMPLEMENTATION',
Expand Down Expand Up @@ -1278,6 +1279,7 @@
'ash_strings.gyp:ash_strings',
'ash_strings.gyp:ash_test_strings',
'ash_test_support',
'ash_touch_hud.gyp:ash_touch_hud',
'ash_with_content',
],
'sources': [
Expand Down
27 changes: 27 additions & 0 deletions ash/ash_touch_hud.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

{
'targets': [
{
# GN version: //ash/touch_hud
'target_name': 'ash_touch_hud',
'type': '<(component)',
'dependencies': [
'../base/base.gyp:base',
'../skia/skia.gyp:skia',
'../ui/events/events.gyp:events',
'../ui/gfx/gfx.gyp:gfx',
'../ui/views/views.gyp:views',
],
'defines': [
'ASH_TOUCH_HUD_IMPLEMENTATION',
],
'sources': [
'touch_hud/ash_touch_hud_export.h',
'touch_hud/touch_hud_renderer.cc',
'touch_hud/touch_hud_renderer.h',
],
},
}
147 changes: 6 additions & 141 deletions ash/touch/touch_hud_projection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,160 +6,25 @@

#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ash/touch_hud/touch_hud_renderer.h"
#include "base/memory/ptr_util.h"
#include "ui/events/event.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/widget/widget.h"

namespace ash {

const int kPointRadius = 20;
const SkColor kProjectionFillColor = SkColorSetRGB(0xF5, 0xF5, 0xDC);
const SkColor kProjectionStrokeColor = SK_ColorGRAY;
const int kProjectionAlpha = 0xB0;
const int kFadeoutDurationInMs = 250;
const int kFadeoutFrameRate = 60;

// TouchPointView draws a single touch point. This object manages its own
// lifetime and deletes itself upon fade-out completion or whenever |Remove()|
// is explicitly called.
class TouchPointView : public views::View,
public gfx::AnimationDelegate,
public views::WidgetObserver {
public:
explicit TouchPointView(views::Widget* parent_widget)
: circle_center_(kPointRadius + 1, kPointRadius + 1),
gradient_center_(SkPoint::Make(kPointRadius + 1, kPointRadius + 1)) {
SetPaintToLayer(true);
layer()->SetFillsBoundsOpaquely(false);

SetSize(gfx::Size(2 * kPointRadius + 2, 2 * kPointRadius + 2));

stroke_paint_.setStyle(SkPaint::kStroke_Style);
stroke_paint_.setColor(kProjectionStrokeColor);

gradient_colors_[0] = kProjectionFillColor;
gradient_colors_[1] = kProjectionStrokeColor;

gradient_pos_[0] = SkFloatToScalar(0.9f);
gradient_pos_[1] = SkFloatToScalar(1.0f);

parent_widget->GetContentsView()->AddChildView(this);

parent_widget->AddObserver(this);
}

void UpdateTouch(const ui::TouchEvent& touch) {
if (touch.type() == ui::ET_TOUCH_RELEASED ||
touch.type() == ui::ET_TOUCH_CANCELLED) {
fadeout_.reset(new gfx::LinearAnimation(kFadeoutDurationInMs,
kFadeoutFrameRate, this));
fadeout_->Start();
} else {
SetX(parent()->GetMirroredXInView(touch.root_location().x()) -
kPointRadius - 1);
SetY(touch.root_location().y() - kPointRadius - 1);
}
}

void Remove() { delete this; }

private:
~TouchPointView() override {
GetWidget()->RemoveObserver(this);
parent()->RemoveChildView(this);
}

// Overridden from views::View.
void OnPaint(gfx::Canvas* canvas) override {
int alpha = kProjectionAlpha;
if (fadeout_)
alpha = static_cast<int>(fadeout_->CurrentValueBetween(alpha, 0));
fill_paint_.setAlpha(alpha);
stroke_paint_.setAlpha(alpha);
fill_paint_.setShader(SkGradientShader::MakeRadial(
gradient_center_, SkIntToScalar(kPointRadius), gradient_colors_,
gradient_pos_, arraysize(gradient_colors_),
SkShader::kMirror_TileMode));
canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius),
fill_paint_);
canvas->DrawCircle(circle_center_, SkIntToScalar(kPointRadius),
stroke_paint_);
}

// Overridden from gfx::AnimationDelegate.
void AnimationEnded(const gfx::Animation* animation) override {
DCHECK_EQ(fadeout_.get(), animation);
delete this;
}

void AnimationProgressed(const gfx::Animation* animation) override {
DCHECK_EQ(fadeout_.get(), animation);
SchedulePaint();
}

void AnimationCanceled(const gfx::Animation* animation) override {
AnimationEnded(animation);
}

// Overridden from views::WidgetObserver.
void OnWidgetDestroying(views::Widget* widget) override {
if (fadeout_)
fadeout_->Stop();
else
Remove();
}

const gfx::Point circle_center_;
const SkPoint gradient_center_;

SkPaint fill_paint_;
SkPaint stroke_paint_;
SkColor gradient_colors_[2];
SkScalar gradient_pos_[2];

std::unique_ptr<gfx::Animation> fadeout_;

DISALLOW_COPY_AND_ASSIGN(TouchPointView);
};

TouchHudProjection::TouchHudProjection(aura::Window* initial_root)
: TouchObserverHUD(initial_root) {}
: TouchObserverHUD(initial_root),
touch_hud_renderer_(new TouchHudRenderer(widget())) {}

TouchHudProjection::~TouchHudProjection() {}

void TouchHudProjection::Clear() {
for (std::map<int, TouchPointView*>::iterator iter = points_.begin();
iter != points_.end(); iter++)
iter->second->Remove();
points_.clear();
touch_hud_renderer_->Clear();
}

void TouchHudProjection::OnTouchEvent(ui::TouchEvent* event) {
if (event->type() == ui::ET_TOUCH_PRESSED) {
TouchPointView* point = new TouchPointView(widget());
point->UpdateTouch(*event);
std::pair<std::map<int, TouchPointView*>::iterator, bool> result =
points_.insert(std::make_pair(event->touch_id(), point));
// If a |TouchPointView| is already mapped to the touch id, remove it and
// replace it with the new one.
if (!result.second) {
result.first->second->Remove();
result.first->second = point;
}
} else {
std::map<int, TouchPointView*>::iterator iter =
points_.find(event->touch_id());
if (iter != points_.end()) {
iter->second->UpdateTouch(*event);
if (event->type() == ui::ET_TOUCH_RELEASED ||
event->type() == ui::ET_TOUCH_CANCELLED)
points_.erase(iter);
}
}
touch_hud_renderer_->HandleTouchEvent(*event);
}

void TouchHudProjection::SetHudForRootWindowController(
Expand Down
5 changes: 3 additions & 2 deletions ash/touch/touch_hud_projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "base/macros.h"

namespace ash {
class TouchPointView;
class TouchHudRenderer;

// A heads-up display to show active touch points on the screen. As a derivative
// of TouchObserverHUD, objects of this class manage their own lifetime.
Expand All @@ -33,7 +33,8 @@ class TouchHudProjection : public TouchObserverHUD {
void UnsetHudForRootWindowController(
RootWindowController* controller) override;

std::map<int, TouchPointView*> points_;
// TouchHudRenderer draws out the touch points.
std::unique_ptr<TouchHudRenderer> touch_hud_renderer_;

DISALLOW_COPY_AND_ASSIGN(TouchHudProjection);
};
Expand Down
3 changes: 2 additions & 1 deletion ash/touch/touch_observer_hud_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ash/test/display_manager_test_api.h"
#include "ash/touch/touch_hud_debug.h"
#include "ash/touch/touch_hud_projection.h"
#include "ash/touch_hud/touch_hud_renderer.h"
#include "base/command_line.h"
#include "base/format_macros.h"
#include "base/strings/stringprintf.h"
Expand Down Expand Up @@ -283,7 +284,7 @@ class TouchHudProjectionTest : public TouchHudTestBase {
}

int GetInternalTouchPointsCount() {
return GetInternalTouchHudProjection()->points_.size();
return GetInternalTouchHudProjection()->touch_hud_renderer_->points_.size();
}

void SendTouchEventToInternalHud(ui::EventType type,
Expand Down
23 changes: 23 additions & 0 deletions ash/touch_hud/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//build/config/ui.gni")

component("touch_hud") {
sources = [
"ash_touch_hud_export.h",
"touch_hud_renderer.cc",
"touch_hud_renderer.h",
]

defines = [ "ASH_TOUCH_HUD_IMPLEMENTATION" ]

deps = [
"//base",
"//skia",
"//ui/events",
"//ui/gfx",
"//ui/views",
]
}
29 changes: 29 additions & 0 deletions ash/touch_hud/ash_touch_hud_export.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_TOUCH_HUD_ASH_TOUCH_HUD_EXPORT_H_
#define ASH_TOUCH_HUD_ASH_TOUCH_HUD_EXPORT_H_

#if defined(COMPONENT_BUILD)
#if defined(WIN32)

#if defined(ASH_TOUCH_HUD_IMPLEMENTATION)
#define ASH_TOUCH_HUD_EXPORT __declspec(dllexport)
#else
#define ASH_TOUCH_HUD_EXPORT __declspec(dllimport)
#endif // defined(ASH_TOUCH_HUD_IMPLEMENTATION)

#else // defined(WIN32)
#if defined(ASH_TOUCH_HUD_IMPLEMENTATION)
#define ASH_TOUCH_HUD_EXPORT __attribute__((visibility("default")))
#else
#define ASH_TOUCH_HUD_EXPORT
#endif
#endif

#else // defined(COMPONENT_BUILD)
#define ASH_TOUCH_HUD_EXPORT
#endif

#endif // ASH_TOUCH_HUD_ASH_TOUCH_HUD_EXPORT_H_
Loading

0 comments on commit 98caac0

Please sign in to comment.