Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
David Tseng | b774097e | 2021-03-24 17:31:10 | [diff] [blame] | 5 | #include "ash/accessibility/ui/accessibility_layer.h" |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 6 | |
| 7 | #include "ui/aura/window.h" |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 8 | #include "ui/compositor/layer.h" |
| 9 | #include "ui/compositor/paint_recorder.h" |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 10 | #include "ui/gfx/canvas.h" |
| 11 | |
| 12 | namespace ui { |
| 13 | class Compositor; |
| 14 | } |
| 15 | |
| 16 | namespace ash { |
| 17 | |
| 18 | AccessibilityLayer::AccessibilityLayer(AccessibilityLayerDelegate* delegate) |
| 19 | : delegate_(delegate) { |
| 20 | DCHECK(delegate); |
| 21 | } |
| 22 | |
David Tseng | 63e7387 | 2021-06-29 20:24:51 | [diff] [blame] | 23 | AccessibilityLayer::~AccessibilityLayer() = default; |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 24 | |
| 25 | void AccessibilityLayer::Set(aura::Window* root_window, |
Joel Riley | 0790b722 | 2020-12-14 20:42:02 | [diff] [blame] | 26 | const gfx::Rect& bounds, |
| 27 | bool stack_at_top) { |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 28 | DCHECK(root_window); |
| 29 | layer_rect_ = bounds; |
| 30 | gfx::Rect layer_bounds = bounds; |
| 31 | int inset = -(GetInset()); |
Xianzhu Wang | 549ddc4 | 2022-04-04 17:05:55 | [diff] [blame] | 32 | layer_bounds.Inset(inset); |
Joel Riley | 0790b722 | 2020-12-14 20:42:02 | [diff] [blame] | 33 | CreateOrUpdateLayer(root_window, "AccessibilityLayer", layer_bounds, |
| 34 | stack_at_top); |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void AccessibilityLayer::SetOpacity(float opacity) { |
Katie D | 6af68c5e | 2018-02-14 23:21:16 | [diff] [blame] | 38 | // Clamp to 0. It's possible for floating-point math to produce opacity |
| 39 | // slightly less than 0. |
| 40 | layer_->SetOpacity(std::max(0.f, opacity)); |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 41 | } |
| 42 | |
Shannon Chen | 9e0cdf7e | 2020-10-29 02:48:48 | [diff] [blame] | 43 | void AccessibilityLayer::SetSubpixelPositionOffset( |
| 44 | const gfx::Vector2dF& offset) { |
| 45 | layer_->SetSubpixelPositionOffset(offset); |
| 46 | } |
| 47 | |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 48 | void AccessibilityLayer::CreateOrUpdateLayer(aura::Window* root_window, |
| 49 | const char* layer_name, |
Joel Riley | 0790b722 | 2020-12-14 20:42:02 | [diff] [blame] | 50 | const gfx::Rect& bounds, |
| 51 | bool stack_at_top) { |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 52 | if (!layer_ || root_window != root_window_) { |
| 53 | root_window_ = root_window; |
| 54 | ui::Layer* root_layer = root_window->layer(); |
| 55 | layer_ = std::make_unique<ui::Layer>(ui::LAYER_TEXTURED); |
yjliu | 0b287621 | 2019-12-19 03:36:20 | [diff] [blame] | 56 | layer_->SetName(layer_name); |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 57 | layer_->SetFillsBoundsOpaquely(false); |
| 58 | root_layer->Add(layer_.get()); |
Evan Stade | bb4f2778 | 2019-07-10 23:42:15 | [diff] [blame] | 59 | // Adding |layer_| to |root_layer| will trigger a DeviceScaleFactorChanged. |
| 60 | // AccessibilityFocusRingControllerImpl doesn't need to react to this |
| 61 | // initial DSF change, so set the delegate after Add(). |
| 62 | layer_->set_delegate(this); |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Keep moving it to the top in case new layers have been added |
| 66 | // since we created this layer. |
Joel Riley | 0790b722 | 2020-12-14 20:42:02 | [diff] [blame] | 67 | if (stack_at_top) { |
| 68 | layer_->parent()->StackAtTop(layer_.get()); |
| 69 | } else { |
| 70 | layer_->parent()->StackAtBottom(layer_.get()); |
| 71 | } |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 72 | |
| 73 | layer_->SetBounds(bounds); |
| 74 | gfx::Rect layer_bounds(0, 0, bounds.width(), bounds.height()); |
| 75 | layer_->SchedulePaint(layer_bounds); |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 76 | } |
| 77 | |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 78 | void AccessibilityLayer::OnDeviceScaleFactorChanged( |
| 79 | float old_device_scale_factor, |
| 80 | float new_device_scale_factor) { |
| 81 | if (delegate_) |
| 82 | delegate_->OnDeviceScaleFactorChanged(); |
| 83 | } |
| 84 | |
Katie Dektar | c0ad2ff | 2017-11-08 04:42:08 | [diff] [blame] | 85 | } // namespace ash |