blob: 509d8044dcef73b7f7d1e0b750dcd7fcd4416c3a [file] [log] [blame]
dmazzoni65ea5c02014-09-12 00:51:431// Copyright 2014 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
James Cook108e6122017-09-20 19:04:295#include "ash/accessibility/accessibility_focus_ring.h"
dmazzoni65ea5c02014-09-12 00:51:436
avi8a07d53892015-12-24 22:13:537#include <stddef.h>
8
dmazzonicac4d2472014-09-25 19:36:199#include "base/logging.h"
Zach Helfinstein9c98a1b32018-08-29 00:00:3210#include "ui/display/display.h"
11#include "ui/display/screen.h"
dmazzonicac4d2472014-09-25 19:36:1912
James Cook108e6122017-09-20 19:04:2913namespace ash {
Anastasia Helfinstein242aefe2019-04-25 18:59:3114namespace {
dmazzoni65ea5c02014-09-12 00:51:4315
Anastasia Helfinstein242aefe2019-04-25 18:59:3116bool IsFocusRingOffscreen(const gfx::Rect& top,
17 const gfx::Rect& middle,
18 const gfx::Rect& bottom,
19 const gfx::Rect& screen_bounds) {
20 if (top.y() > screen_bounds.bottom())
21 return true;
22 if (middle.right() < screen_bounds.x())
23 return true;
24 if (middle.x() > screen_bounds.right())
25 return true;
26 if (bottom.bottom() < screen_bounds.y())
27 return true;
28 return false;
dmazzoni65ea5c02014-09-12 00:51:4329}
30
Anastasia Helfinstein242aefe2019-04-25 18:59:3131AccessibilityFocusRing CreateFromThreeRects(const gfx::Rect& top,
32 const gfx::Rect& middle,
33 const gfx::Rect& bottom,
34 int margin) {
dmazzoni65ea5c02014-09-12 00:51:4335 AccessibilityFocusRing ring;
36 ring.points[0] = gfx::Point(top.x(), top.bottom() - margin);
37 ring.points[1] = gfx::Point(top.x(), top.y() + margin);
38 ring.points[2] = gfx::Point(top.x(), top.y());
39 ring.points[3] = gfx::Point(top.x() + margin, top.y());
40 ring.points[4] = gfx::Point(top.right() - margin, top.y());
41 ring.points[5] = gfx::Point(top.right(), top.y());
42 ring.points[6] = gfx::Point(top.right(), top.y() + margin);
43 ring.points[7] = gfx::Point(top.right(), top.bottom() - margin);
44 ring.points[8] = gfx::Point(top.right(), top.bottom());
45 if (top.right() < middle.right()) {
46 ring.points[9] = gfx::Point(top.right() + margin, middle.y());
47 ring.points[10] = gfx::Point(middle.right() - margin, middle.y());
48 } else {
49 ring.points[9] = gfx::Point(top.right(), middle.y());
50 ring.points[10] = gfx::Point(middle.right(), middle.y());
51 }
52 ring.points[11] = gfx::Point(middle.right(), middle.y());
53 ring.points[12] = gfx::Point(middle.right(), middle.y() + margin);
54 ring.points[13] = gfx::Point(middle.right(), middle.bottom() - margin);
55 ring.points[14] = gfx::Point(middle.right(), middle.bottom());
56 if (bottom.right() < middle.right()) {
57 ring.points[15] = gfx::Point(middle.right() - margin, bottom.y());
58 ring.points[16] = gfx::Point(bottom.right() + margin, bottom.y());
59 } else {
60 ring.points[15] = gfx::Point(middle.right(), bottom.y());
61 ring.points[16] = gfx::Point(bottom.right(), bottom.y());
62 }
63 ring.points[17] = gfx::Point(bottom.right(), bottom.y());
64 ring.points[18] = gfx::Point(bottom.right(), bottom.y() + margin);
65 ring.points[19] = gfx::Point(bottom.right(), bottom.bottom() - margin);
66 ring.points[20] = gfx::Point(bottom.right(), bottom.bottom());
67 ring.points[21] = gfx::Point(bottom.right() - margin, bottom.bottom());
68 ring.points[22] = gfx::Point(bottom.x() + margin, bottom.bottom());
69 ring.points[23] = gfx::Point(bottom.x(), bottom.bottom());
70 ring.points[24] = gfx::Point(bottom.x(), bottom.bottom() - margin);
71 ring.points[25] = gfx::Point(bottom.x(), bottom.y() + margin);
72 ring.points[26] = gfx::Point(bottom.x(), bottom.y());
73 if (bottom.x() > middle.x()) {
74 ring.points[27] = gfx::Point(bottom.x() - margin, bottom.y());
75 ring.points[28] = gfx::Point(middle.x() + margin, middle.bottom());
76 } else {
77 ring.points[27] = gfx::Point(bottom.x(), bottom.y());
78 ring.points[28] = gfx::Point(middle.x(), middle.bottom());
79 }
80 ring.points[29] = gfx::Point(middle.x(), middle.bottom());
81 ring.points[30] = gfx::Point(middle.x(), middle.bottom() - margin);
82 ring.points[31] = gfx::Point(middle.x(), middle.y() + margin);
83 ring.points[32] = gfx::Point(middle.x(), middle.y());
84 if (top.x() > middle.x()) {
85 ring.points[33] = gfx::Point(middle.x() + margin, middle.y());
86 ring.points[34] = gfx::Point(top.x() - margin, top.bottom());
87 } else {
88 ring.points[33] = gfx::Point(middle.x(), middle.y());
89 ring.points[34] = gfx::Point(top.x(), top.bottom());
90 }
91 ring.points[35] = gfx::Point(top.x(), top.bottom());
92
93 return ring;
94}
95
Anastasia Helfinsteinb3d030a2019-04-26 19:11:5896constexpr int kScreenPaddingDip = 2;
Anastasia Helfinstein242aefe2019-04-25 18:59:3197} // namespace
98
Anastasia Helfinsteinb3d030a2019-04-26 19:11:5899int AccessibilityFocusRing::GetScreenPaddingForTesting() {
100 return kScreenPaddingDip;
101}
102
Anastasia Helfinstein242aefe2019-04-25 18:59:31103// static
104gfx::Rect AccessibilityFocusRing::screen_bounds_for_testing_;
105
106// static
107AccessibilityFocusRing AccessibilityFocusRing::CreateWithRect(
108 const gfx::Rect& bounds,
109 int margin) {
110 // Compute the height of the top and bottom cap.
111 int cap_height = std::min(bounds.height() / 2, margin * 2);
112
113 gfx::Rect top(bounds.x(), bounds.y(), bounds.width(), cap_height);
114 gfx::Rect bottom(bounds.x(), bounds.bottom() - cap_height, bounds.width(),
115 cap_height);
116 gfx::Rect body(bounds.x(), top.bottom(), bounds.width(),
117 bottom.y() - top.bottom());
118
119 return CreateWithParagraphShape(top, body, bottom, margin);
120}
121
122// static
123AccessibilityFocusRing AccessibilityFocusRing::Interpolate(
124 const AccessibilityFocusRing& r1,
125 const AccessibilityFocusRing& r2,
126 double fraction) {
127 AccessibilityFocusRing dst;
128 for (int i = 0; i < 36; ++i) {
129 dst.points[i] = gfx::Point(
130 r1.points[i].x() * (1 - fraction) + r2.points[i].x() * fraction,
131 r1.points[i].y() * (1 - fraction) + r2.points[i].y() * fraction);
132 }
133 return dst;
134}
135
136// static
137void AccessibilityFocusRing::ClipToBounds(gfx::Rect* rect,
138 const gfx::Rect& bounds) {
139 if (rect->x() < bounds.x()) {
140 rect->set_width(rect->width() + rect->x() - bounds.x());
141 rect->set_x(bounds.x());
142 }
143 if (rect->x() > bounds.right()) {
144 rect->set_width(0);
145 rect->set_x(bounds.right());
146 } else if (rect->right() > bounds.right()) {
147 rect->set_width(rect->width() - rect->right() + bounds.right());
148 }
149
150 if (rect->y() < bounds.y()) {
151 rect->set_height(rect->height() + rect->y() - bounds.y());
152 rect->set_y(bounds.y());
153 }
154 if (rect->y() > bounds.bottom()) {
155 rect->set_height(0);
156 rect->set_y(bounds.bottom());
157 } else if (rect->bottom() > bounds.bottom()) {
158 rect->set_height(rect->height() - rect->bottom() + bounds.bottom());
159 }
160}
161
162// static
163AccessibilityFocusRing AccessibilityFocusRing::CreateWithParagraphShape(
164 const gfx::Rect& orig_top_line,
165 const gfx::Rect& orig_body,
166 const gfx::Rect& orig_bottom_line,
167 int margin) {
168 gfx::Rect top = orig_top_line;
169 gfx::Rect middle = orig_body;
170 gfx::Rect bottom = orig_bottom_line;
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58171
172 gfx::Rect screen_bounds = GetScreenBoundsForRect(middle);
173 screen_bounds.Inset(kScreenPaddingDip, kScreenPaddingDip);
Anastasia Helfinstein242aefe2019-04-25 18:59:31174
175 // Don't force a focus ring that is entirely offscreen to display.
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58176 if (IsFocusRingOffscreen(top, middle, bottom, screen_bounds))
Anastasia Helfinstein242aefe2019-04-25 18:59:31177 return CreateFromThreeRects(top, middle, bottom, margin);
178
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58179 if (top.Intersects(screen_bounds))
180 top.Intersect(screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31181 else
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58182 ClipToBounds(&top, screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31183
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58184 if (middle.Intersects(screen_bounds))
185 middle.Intersect(screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31186 else
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58187 ClipToBounds(&middle, screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31188
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58189 if (bottom.Intersects(screen_bounds))
190 bottom.Intersect(screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31191 else
Anastasia Helfinsteinb3d030a2019-04-26 19:11:58192 ClipToBounds(&bottom, screen_bounds);
Anastasia Helfinstein242aefe2019-04-25 18:59:31193
194 int min_height = std::min(top.height(), bottom.height());
195 margin = std::min(margin, min_height / 2);
196
197 // Add space around top/bottom, if necessary, to allow focus ring to draw
198 // properly.
199 if (top.x() <= middle.x() + 2 * margin) {
200 top.set_width(top.width() + top.x() - middle.x());
201 top.set_x(middle.x());
202 }
203 if (top.right() >= middle.right() - 2 * margin) {
204 top.set_width(middle.right() - top.x());
205 }
206
207 if (bottom.x() <= middle.x() + 2 * margin) {
208 bottom.set_width(bottom.width() + bottom.x() - middle.x());
209 bottom.set_x(middle.x());
210 }
211 if (bottom.right() >= middle.right() - 2 * margin) {
212 bottom.set_width(middle.right() - bottom.x());
213 }
214
215 return CreateFromThreeRects(top, middle, bottom, margin);
216}
217
dmazzoni65ea5c02014-09-12 00:51:43218gfx::Rect AccessibilityFocusRing::GetBounds() const {
219 gfx::Point top_left = points[0];
220 gfx::Point bottom_right = points[0];
dmazzonicac4d2472014-09-25 19:36:19221 for (size_t i = 1; i < 36; ++i) {
dmazzoni65ea5c02014-09-12 00:51:43222 top_left.SetToMin(points[i]);
223 bottom_right.SetToMax(points[i]);
224 }
225 return gfx::Rect(top_left, gfx::Size(bottom_right.x() - top_left.x(),
226 bottom_right.y() - top_left.y()));
227}
228
Zach Helfinstein9c98a1b32018-08-29 00:00:32229// static
230gfx::Rect AccessibilityFocusRing::GetScreenBoundsForRect(
231 const gfx::Rect& rect) {
232 if (!screen_bounds_for_testing_.IsEmpty())
233 return screen_bounds_for_testing_;
234
235 DCHECK(display::Screen::GetScreen());
236 return display::Screen::GetScreen()->GetDisplayMatching(rect).bounds();
237}
238
James Cook108e6122017-09-20 19:04:29239} // namespace ash