Commit 91433614 authored by mohsen's avatar mohsen Committed by Commit bot

Add Aura handles to be used in unified touch selection

This patch is part of Aura side of unified touch selection. It adds an
implementation of touch handle drawables to be used on Aura. It also
adds some functions to the public interface of
ui::TouchSelectionController needed for unified touch selection.

COLLABORATOR=mfomitchev
BUG=399721

Review URL: https://codereview.chromium.org/996373002

Cr-Commit-Position: refs/heads/master@{#329191}
parent b06ff5fb
......@@ -500,6 +500,9 @@
<structure type="chrome_scaled_image" name="IDR_SCROLLBAR_THUMB_BASE_PRESSED_TOP" file="common/aura_scrollbar_thumb_base_pressed_top.png" />
<structure type="chrome_scaled_image" name="IDR_SCROLLBAR_THUMB_BASE_PRESSED_TOP_LEFT" file="common/aura_scrollbar_thumb_base_pressed_top_left.png" />
<structure type="chrome_scaled_image" name="IDR_SCROLLBAR_THUMB_BASE_PRESSED_TOP_RIGHT" file="common/aura_scrollbar_thumb_base_pressed_top_right.png" />
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_CENTER" file="common/text_selection_handle_center.png" />
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_LEFT" file="common/text_selection_handle_left.png" />
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_RIGHT" file="common/text_selection_handle_right.png" />
</if>
<if expr="toolkit_views">
<structure type="chrome_scaled_image" name="IDR_TEXTBUTTON_HOVER_BOTTOM" file="common/textbutton_hover_bottom.png" />
......@@ -530,11 +533,6 @@
<structure type="chrome_scaled_image" name="IDR_TEXTBUTTON_RAISED_TOP_LEFT" file="common/textbutton_raised_top_left.png" />
<structure type="chrome_scaled_image" name="IDR_TEXTBUTTON_RAISED_TOP_RIGHT" file="common/textbutton_raised_top_right.png" />
</if>
<if expr="toolkit_views">
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_CENTER" file="common/text_selection_handle_center.png" />
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_LEFT" file="common/text_selection_handle_left.png" />
<structure type="chrome_scaled_image" name="IDR_TEXT_SELECTION_HANDLE_RIGHT" file="common/text_selection_handle_right.png" />
</if>
<structure type="chrome_scaled_image" name="IDR_THROBBER" file="throbber.png" />
<if expr="toolkit_views">
<structure type="chrome_scaled_image" name="IDR_TOUCH_DRAG_TIP_COPY" file="common/drag_tip_copy.png" />
......
......@@ -16,6 +16,8 @@ component("touch_selection") {
"selection_event_type.h",
"touch_handle.cc",
"touch_handle.h",
"touch_handle_drawable_aura.cc",
"touch_handle_drawable_aura.h",
"touch_handle_orientation.h",
"touch_selection_controller.cc",
"touch_selection_controller.h",
......@@ -26,11 +28,29 @@ component("touch_selection") {
deps = [
"//base:base",
"//ui/aura:aura",
"//ui/aura_extra:aura_extra",
"//ui/base:base",
"//ui/compositor:compositor",
"//ui/events:events",
"//ui/events:gesture_detection",
"//ui/gfx:gfx",
"//ui/gfx/geometry:geometry",
]
if (!use_aura) {
deps -= [
"//ui/aura:aura",
"//ui/aura_extra:aura_extra",
"//ui/compositor:compositor",
"//ui/gfx:gfx",
]
sources -= [
"touch_handle_drawable_aura.cc",
"touch_handle_drawable_aura.h",
]
}
}
test("ui_touch_selection_unittests") {
......
include_rules = [
"+ui/aura",
"+ui/aura_extra",
"+ui/base",
"+ui/events",
"+ui/gfx/geometry"
"+ui/gfx",
"+ui/resources"
]
// Copyright 2015 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.
#include "ui/touch_selection/touch_handle_drawable_aura.h"
#include "ui/aura/window.h"
#include "ui/aura/window_targeter.h"
#include "ui/aura_extra/image_window_delegate.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/hit_test.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/resources/grit/ui_resources.h"
namespace ui {
namespace {
// The distance by which a handle image is offset from the focal point (i.e.
// text baseline) downwards.
const int kSelectionHandleVerticalVisualOffset = 2;
// The padding around the selection handle image can be used to extend the
// handle window so that touch events near the selection handle image are
// targeted to the selection handle window.
const int kSelectionHandlePadding = 0;
// Epsilon value used to compare float values to zero.
const float kEpsilon = 1e-8f;
// Returns the appropriate handle image based on the handle orientation.
gfx::Image* GetHandleImage(TouchHandleOrientation orientation) {
int resource_id = 0;
switch (orientation) {
case TouchHandleOrientation::LEFT:
resource_id = IDR_TEXT_SELECTION_HANDLE_LEFT;
break;
case TouchHandleOrientation::CENTER:
resource_id = IDR_TEXT_SELECTION_HANDLE_CENTER;
break;
case TouchHandleOrientation::RIGHT:
resource_id = IDR_TEXT_SELECTION_HANDLE_RIGHT;
break;
case TouchHandleOrientation::UNDEFINED:
NOTREACHED() << "Invalid touch handle bound type.";
return nullptr;
};
return &ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
}
bool IsNearlyZero(float value) {
return std::abs(value) < kEpsilon;
}
} // namespace
TouchHandleDrawableAura::TouchHandleDrawableAura(aura::Window* parent)
: window_delegate_(new aura_extra::ImageWindowDelegate),
window_(new aura::Window(window_delegate_)),
enabled_(false),
alpha_(0),
orientation_(TouchHandleOrientation::UNDEFINED) {
window_delegate_->set_image_offset(gfx::Vector2d(kSelectionHandlePadding,
kSelectionHandlePadding));
window_delegate_->set_background_color(SK_ColorTRANSPARENT);
window_->SetTransparent(true);
window_->Init(LAYER_TEXTURED);
window_->set_owned_by_parent(false);
window_->set_ignore_events(true);
parent->AddChild(window_.get());
}
TouchHandleDrawableAura::~TouchHandleDrawableAura() {
}
void TouchHandleDrawableAura::UpdateBounds() {
gfx::RectF new_bounds = relative_bounds_;
new_bounds.Offset(focal_position_.x(), focal_position_.y());
window_->SetBounds(gfx::ToEnclosingRect(new_bounds));
}
bool TouchHandleDrawableAura::IsVisible() const {
return enabled_ && !IsNearlyZero(alpha_);
}
void TouchHandleDrawableAura::SetEnabled(bool enabled) {
if (enabled == enabled_)
return;
enabled_ = enabled;
if (IsVisible())
window_->Show();
else
window_->Hide();
}
void TouchHandleDrawableAura::SetOrientation(
TouchHandleOrientation orientation) {
if (orientation_ == orientation)
return;
orientation_ = orientation;
gfx::Image* image = GetHandleImage(orientation);
window_delegate_->SetImage(*image);
// Calculate the relative bounds.
gfx::Size image_size = image->Size();
int window_width = image_size.width() + 2 * kSelectionHandlePadding;
int window_height = image_size.height() + 2 * kSelectionHandlePadding;
// Due to the shape of the handle images, the window is aligned differently to
// the selection bound depending on the orientation.
int window_left = 0;
switch (orientation) {
case TouchHandleOrientation::LEFT:
window_left = -image_size.width() - kSelectionHandlePadding;
break;
case TouchHandleOrientation::RIGHT:
window_left = -kSelectionHandlePadding;
break;
case TouchHandleOrientation::CENTER:
window_left = -window_width / 2;
break;
case TouchHandleOrientation::UNDEFINED:
NOTREACHED() << "Undefined handle orientation.";
break;
};
relative_bounds_ = gfx::RectF(
window_left,
kSelectionHandleVerticalVisualOffset - kSelectionHandlePadding,
window_width,
window_height);
UpdateBounds();
}
void TouchHandleDrawableAura::SetAlpha(float alpha) {
if (alpha == alpha_)
return;
alpha_ = alpha;
window_->layer()->SetOpacity(alpha_);
if (IsVisible())
window_->Show();
else
window_->Hide();
}
void TouchHandleDrawableAura::SetFocus(const gfx::PointF& position) {
focal_position_ = position;
UpdateBounds();
}
gfx::RectF TouchHandleDrawableAura::GetVisibleBounds() const {
gfx::RectF bounds(window_->bounds());
bounds.Inset(kSelectionHandlePadding,
kSelectionHandlePadding + kSelectionHandleVerticalVisualOffset,
kSelectionHandlePadding,
kSelectionHandlePadding);
return bounds;
}
} // namespace ui
// Copyright 2015 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 UI_TOUCH_SELECTION_TOUCH_HANDLE_DRAWABLE_AURA_H_
#define UI_TOUCH_SELECTION_TOUCH_HANDLE_DRAWABLE_AURA_H_
#include "ui/touch_selection/touch_handle.h"
#include "ui/touch_selection/touch_handle_orientation.h"
#include "ui/touch_selection/ui_touch_selection_export.h"
namespace aura {
class Window;
}
namespace aura_extra {
class ImageWindowDelegate;
}
namespace ui {
class UI_TOUCH_SELECTION_EXPORT TouchHandleDrawableAura
: public TouchHandleDrawable {
public:
explicit TouchHandleDrawableAura(aura::Window* parent);
~TouchHandleDrawableAura() override;
private:
void UpdateBounds();
bool IsVisible() const;
// TouchHandleDrawable:
void SetEnabled(bool enabled) override;
void SetOrientation(TouchHandleOrientation orientation) override;
void SetAlpha(float alpha) override;
void SetFocus(const gfx::PointF& position) override;
gfx::RectF GetVisibleBounds() const override;
aura_extra::ImageWindowDelegate* window_delegate_;
scoped_ptr<aura::Window> window_;
bool enabled_;
float alpha_;
ui::TouchHandleOrientation orientation_;
// Focal position of the handle set via SetFocus (normally located on the
// intersection of the cursor line and the text base line), in coordinate
// space of selection controller client (i.e. handle's parent).
gfx::PointF focal_position_;
// Window bounds relative to the focal position.
gfx::RectF relative_bounds_;
DISALLOW_COPY_AND_ASSIGN(TouchHandleDrawableAura);
};
} // namespace ui
#endif // UI_TOUCH_SELECTION_TOUCH_HANDLE_DRAWABLE_AURA_H_
......@@ -36,6 +36,12 @@ class UI_TOUCH_SELECTION_EXPORT TouchSelectionControllerClient {
class UI_TOUCH_SELECTION_EXPORT TouchSelectionController
: public TouchHandleClient {
public:
enum ActiveStatus {
INACTIVE,
INSERTION_ACTIVE,
SELECTION_ACTIVE,
};
TouchSelectionController(TouchSelectionControllerClient* client,
base::TimeDelta tap_timeout,
float tap_slop,
......@@ -97,6 +103,11 @@ class UI_TOUCH_SELECTION_EXPORT TouchSelectionController
const gfx::PointF& GetStartPosition() const;
const gfx::PointF& GetEndPosition() const;
const SelectionBound& start() const { return start_; }
const SelectionBound& end() const { return end_; }
ActiveStatus active_status() const { return active_status_; }
private:
enum InputEventType { TAP, LONG_PRESS, INPUT_EVENT_TYPE_NONE };
......@@ -146,13 +157,13 @@ class UI_TOUCH_SELECTION_EXPORT TouchSelectionController
TouchHandleOrientation start_orientation_;
TouchHandleOrientation end_orientation_;
ActiveStatus active_status_;
scoped_ptr<TouchHandle> insertion_handle_;
bool is_insertion_active_;
bool activate_insertion_automatically_;
scoped_ptr<TouchHandle> start_selection_handle_;
scoped_ptr<TouchHandle> end_selection_handle_;
bool is_selection_active_;
bool activate_selection_automatically_;
bool selection_empty_;
......
......@@ -36,6 +36,8 @@ class MockTouchHandleDrawable : public TouchHandleDrawable {
private:
bool* intersects_rect_;
DISALLOW_COPY_AND_ASSIGN(MockTouchHandleDrawable);
};
} // namespace
......@@ -205,6 +207,8 @@ class TouchSelectionControllerTest : public testing::Test,
bool animation_enabled_;
bool dragging_enabled_;
scoped_ptr<TouchSelectionController> controller_;
DISALLOW_COPY_AND_ASSIGN(TouchSelectionControllerTest);
};
TEST_F(TouchSelectionControllerTest, InsertionBasic) {
......@@ -433,7 +437,6 @@ TEST_F(TouchSelectionControllerTest, InsertionTapped) {
MockMotionEvent event(MockMotionEvent::ACTION_DOWN, event_time, 0, 0);
EXPECT_TRUE(controller().WillHandleTouchEvent(event));
//TODO(AKV): this test case has to be modified once crbug.com/394093 is fixed.
EXPECT_THAT(GetAndResetEvents(), ElementsAre(INSERTION_DRAG_STARTED));
event = MockMotionEvent(MockMotionEvent::ACTION_UP, event_time, 0, 0);
......
......@@ -12,9 +12,13 @@
'type': '<(component)',
'dependencies': [
'../../base/base.gyp:base',
'../aura/aura.gyp:aura',
'../aura_extra/aura_extra.gyp:aura_extra',
'../base/ui_base.gyp:ui_base',
'../compositor/compositor.gyp:compositor',
'../events/events.gyp:events',
'../events/events.gyp:gesture_detection',
'../gfx/gfx.gyp:gfx',
'../gfx/gfx.gyp:gfx_geometry',
],
'defines': [
......@@ -24,6 +28,8 @@
'selection_event_type.h',
'touch_handle.cc',
'touch_handle.h',
'touch_handle_drawable_aura.cc',
'touch_handle_drawable_aura.h',
'touch_handle_orientation.h',
'touch_selection_controller.cc',
'touch_selection_controller.h',
......@@ -32,6 +38,20 @@
'include_dirs': [
'../..',
],
'conditions': [
['use_aura==0', {
'dependencies!': [
'../aura/aura.gyp:aura',
'../aura_extra/aura_extra.gyp:aura_extra',
'../compositor/compositor.gyp:compositor',
'../gfx/gfx.gyp:gfx',
],
'sources!': [
'touch_handle_drawable_aura.cc',
'touch_handle_drawable_aura.h',
],
}],
],
},
{
'target_name': 'ui_touch_selection_unittests',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment