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

Side by Side Diff: ui/touch_selection/touch_handle_drawable_aura.cc

Issue 1046783002: wip: Aura-specific implementation of unified touch selection: touch_selection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving the responsibility for showing the menu into the client. Created 5 years, 8 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 2015 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 "ui/touch_selection/touch_handle_drawable_aura.h"
6
7 #include "ui/aura/window.h"
8 #include "ui/aura/window_targeter.h"
9 #include "ui/base/cursor/cursor.h"
10 #include "ui/base/hit_test.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/events/event.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/geometry/rect_conversions.h"
15 #include "ui/resources/grit/ui_resources.h"
16
17 namespace ui {
18 namespace {
19
20 // The distance by which a handle image is offset from the focal point (i.e.
21 // text baseline) downwards.
22 const int kSelectionHandleVerticalVisualOffset = 2;
23
24 // The padding around the selection handle image can be used to extend the
25 // handle window so that touch events near the selection handle image are
26 // targeted to the selection handle window.
27 const int kSelectionHandlePadding = 0;
28
29 // Epsilon value used to compare float values to zero.
30 const float kEpsilon = 1e-8f;
31
32 // Returns the appropriate handle image based on the handle orientation.
33 gfx::Image* GetHandleImage(TouchHandleOrientation orientation) {
34 int resource_id = 0;
35 switch (orientation) {
36 case TouchHandleOrientation::LEFT:
37 resource_id = IDR_TEXT_SELECTION_HANDLE_LEFT;
38 break;
39 case TouchHandleOrientation::CENTER:
40 resource_id = IDR_TEXT_SELECTION_HANDLE_CENTER;
41 break;
42 case TouchHandleOrientation::RIGHT:
43 resource_id = IDR_TEXT_SELECTION_HANDLE_RIGHT;
44 break;
45 case TouchHandleOrientation::UNDEFINED:
46 NOTREACHED() << "Invalid touch handle bound type.";
47 return nullptr;
48 };
49 return &ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
50 }
51
52 bool IsNearlyZero(float value) {
53 return std::abs(value) < kEpsilon;
54 }
55
56 }
57
58 TouchHandleDrawableAura::TouchHandleDrawableAura(aura::Window* parent)
59 : window_delegate_(new aura_extra::ImageWindowDelegate),
60 window_(new aura::Window(window_delegate_)),
61 enabled_(false),
62 alpha_(0),
63 orientation_(TouchHandleOrientation::UNDEFINED) {
64 window_delegate_->set_image_offset(gfx::Vector2d(kSelectionHandlePadding,
65 kSelectionHandlePadding));
66 window_delegate_->set_background_color(SK_ColorTRANSPARENT);
67 window_->SetTransparent(true);
68 window_->Init(ui::LAYER_TEXTURED);
69 window_->set_owned_by_parent(false);
70 window_->set_ignore_events(true);
71 parent->AddChild(window_.get());
72 }
73
74 TouchHandleDrawableAura::~TouchHandleDrawableAura() {
75 }
76
77 // static
78 gfx::Size TouchHandleDrawableAura::GetMaxHandleImageSize() {
79 gfx::Size max_size(GetHandleImage(TouchHandleOrientation::CENTER)->Size());
80 max_size.SetToMax(GetHandleImage(TouchHandleOrientation::LEFT)->Size());
81 max_size.SetToMax(GetHandleImage(TouchHandleOrientation::RIGHT)->Size());
82 return max_size;
83 }
84
85 void TouchHandleDrawableAura::UpdateBounds() {
86 gfx::RectF new_bounds = relative_bounds_;
87 new_bounds.Offset(focal_position_.x(), focal_position_.y());
88 window_->SetBounds(gfx::ToNearestRect(new_bounds));
89 }
90
91 void TouchHandleDrawableAura::SetEnabled(bool enabled) {
92 if (enabled == enabled_)
93 return;
94
95 enabled_ = enabled;
96 bool visible = !IsNearlyZero(alpha_);
97 if (enabled_ && visible)
98 window_->Show();
99 else
100 window_->Hide();
101 }
102
103 void TouchHandleDrawableAura::SetOrientation(
104 TouchHandleOrientation orientation) {
105 if (orientation_ == orientation)
106 return;
107 orientation_ = orientation;
108 gfx::Image* image = GetHandleImage(orientation);
109 window_delegate_->SetImage(*image);
110
111 // Calculate the relative bounds.
112 gfx::Size image_size = image->Size();
113 int window_width = image_size.width() + 2 * kSelectionHandlePadding;
114 int window_height = image_size.height() + 2 * kSelectionHandlePadding;
115 // Due to the shape of the handle images, the window is aligned differently to
116 // the selection bound depending on the orientation.
117 int window_left = 0;
118 switch (orientation) {
119 case TouchHandleOrientation::LEFT:
120 window_left = -image_size.width() - kSelectionHandlePadding;
121 break;
122 case TouchHandleOrientation::RIGHT:
123 window_left = -kSelectionHandlePadding;
124 break;
125 case TouchHandleOrientation::CENTER:
126 window_left = -window_width / 2;
127 break;
128 case TouchHandleOrientation::UNDEFINED:
129 NOTREACHED() << "Undefined handle orientation.";
130 break;
131 };
132 relative_bounds_ = gfx::RectF(
133 window_left,
134 kSelectionHandleVerticalVisualOffset - kSelectionHandlePadding,
135 window_width,
136 window_height);
137 UpdateBounds();
138 }
139
140 void TouchHandleDrawableAura::SetAlpha(float alpha) {
141 if (alpha == alpha_)
142 return;
143
144 alpha_ = alpha;
145 window_->layer()->SetOpacity(alpha_);
146 bool visible = !IsNearlyZero(alpha_);
147 if (enabled_ && visible)
148 window_->Show();
149 else
150 window_->Hide();
151 }
152
153 void TouchHandleDrawableAura::SetFocus(const gfx::PointF& position) {
154 focal_position_ = position;
155 UpdateBounds();
156 }
157
158 gfx::RectF TouchHandleDrawableAura::GetVisibleBounds() const {
159 gfx::RectF bounds(window_->bounds());
160 bounds.Inset(kSelectionHandlePadding,
161 kSelectionHandlePadding + kSelectionHandleVerticalVisualOffset,
162 kSelectionHandlePadding,
163 kSelectionHandlePadding);
164 return bounds;
165 }
166
167 } // namespace ui
OLDNEW
« no previous file with comments | « ui/touch_selection/touch_handle_drawable_aura.h ('k') | ui/touch_selection/touch_selection_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698