dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 1 | // 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 Cook | 108e612 | 2017-09-20 19:04:29 | [diff] [blame] | 5 | #include "ash/accessibility/accessibility_focus_ring.h" |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 6 | |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dmazzoni | cac4d247 | 2014-09-25 19:36:19 | [diff] [blame] | 9 | #include "base/logging.h" |
Zach Helfinstein | 9c98a1b3 | 2018-08-29 00:00:32 | [diff] [blame] | 10 | #include "ui/display/display.h" |
| 11 | #include "ui/display/screen.h" |
dmazzoni | cac4d247 | 2014-09-25 19:36:19 | [diff] [blame] | 12 | |
James Cook | 108e612 | 2017-09-20 19:04:29 | [diff] [blame] | 13 | namespace ash { |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 14 | namespace { |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 15 | |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 16 | bool 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; |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 29 | } |
| 30 | |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 31 | AccessibilityFocusRing CreateFromThreeRects(const gfx::Rect& top, |
| 32 | const gfx::Rect& middle, |
| 33 | const gfx::Rect& bottom, |
| 34 | int margin) { |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 35 | 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 Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 96 | constexpr int kScreenPaddingDip = 2; |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 97 | } // namespace |
| 98 | |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 99 | int AccessibilityFocusRing::GetScreenPaddingForTesting() { |
| 100 | return kScreenPaddingDip; |
| 101 | } |
| 102 | |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 103 | // static |
| 104 | gfx::Rect AccessibilityFocusRing::screen_bounds_for_testing_; |
| 105 | |
| 106 | // static |
| 107 | AccessibilityFocusRing 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 |
| 123 | AccessibilityFocusRing 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 |
| 137 | void 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 |
| 163 | AccessibilityFocusRing 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 Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 171 | |
| 172 | gfx::Rect screen_bounds = GetScreenBoundsForRect(middle); |
| 173 | screen_bounds.Inset(kScreenPaddingDip, kScreenPaddingDip); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 174 | |
| 175 | // Don't force a focus ring that is entirely offscreen to display. |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 176 | if (IsFocusRingOffscreen(top, middle, bottom, screen_bounds)) |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 177 | return CreateFromThreeRects(top, middle, bottom, margin); |
| 178 | |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 179 | if (top.Intersects(screen_bounds)) |
| 180 | top.Intersect(screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 181 | else |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 182 | ClipToBounds(&top, screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 183 | |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 184 | if (middle.Intersects(screen_bounds)) |
| 185 | middle.Intersect(screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 186 | else |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 187 | ClipToBounds(&middle, screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 188 | |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 189 | if (bottom.Intersects(screen_bounds)) |
| 190 | bottom.Intersect(screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 191 | else |
Anastasia Helfinstein | b3d030a | 2019-04-26 19:11:58 | [diff] [blame] | 192 | ClipToBounds(&bottom, screen_bounds); |
Anastasia Helfinstein | 242aefe | 2019-04-25 18:59:31 | [diff] [blame] | 193 | |
| 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 | |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 218 | gfx::Rect AccessibilityFocusRing::GetBounds() const { |
| 219 | gfx::Point top_left = points[0]; |
| 220 | gfx::Point bottom_right = points[0]; |
dmazzoni | cac4d247 | 2014-09-25 19:36:19 | [diff] [blame] | 221 | for (size_t i = 1; i < 36; ++i) { |
dmazzoni | 65ea5c0 | 2014-09-12 00:51:43 | [diff] [blame] | 222 | 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 Helfinstein | 9c98a1b3 | 2018-08-29 00:00:32 | [diff] [blame] | 229 | // static |
| 230 | gfx::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 Cook | 108e612 | 2017-09-20 19:04:29 | [diff] [blame] | 239 | } // namespace ash |