[go: nahoru, domu]

blob: bed9e3872ae7bbec01648a4f3213ea4dc86d7e4b [file] [log] [blame]
Avi Drissman3a215d1e2022-09-07 19:43:091// Copyright 2017 The Chromium Authors
Katie Dektarc0ad2ff2017-11-08 04:42:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
David Tsengb774097e2021-03-24 17:31:105#include "ash/accessibility/ui/accessibility_layer.h"
Katie Dektarc0ad2ff2017-11-08 04:42:086
7#include "ui/aura/window.h"
Katie Dektarc0ad2ff2017-11-08 04:42:088#include "ui/compositor/layer.h"
9#include "ui/compositor/paint_recorder.h"
Katie Dektarc0ad2ff2017-11-08 04:42:0810#include "ui/gfx/canvas.h"
11
12namespace ui {
13class Compositor;
14}
15
16namespace ash {
17
18AccessibilityLayer::AccessibilityLayer(AccessibilityLayerDelegate* delegate)
19 : delegate_(delegate) {
20 DCHECK(delegate);
21}
22
David Tseng63e73872021-06-29 20:24:5123AccessibilityLayer::~AccessibilityLayer() = default;
Katie Dektarc0ad2ff2017-11-08 04:42:0824
25void AccessibilityLayer::Set(aura::Window* root_window,
Joel Riley0790b7222020-12-14 20:42:0226 const gfx::Rect& bounds,
27 bool stack_at_top) {
Katie Dektarc0ad2ff2017-11-08 04:42:0828 DCHECK(root_window);
29 layer_rect_ = bounds;
30 gfx::Rect layer_bounds = bounds;
31 int inset = -(GetInset());
Xianzhu Wang549ddc42022-04-04 17:05:5532 layer_bounds.Inset(inset);
Joel Riley0790b7222020-12-14 20:42:0233 CreateOrUpdateLayer(root_window, "AccessibilityLayer", layer_bounds,
34 stack_at_top);
Katie Dektarc0ad2ff2017-11-08 04:42:0835}
36
37void AccessibilityLayer::SetOpacity(float opacity) {
Katie D6af68c5e2018-02-14 23:21:1638 // 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 Dektarc0ad2ff2017-11-08 04:42:0841}
42
Shannon Chen9e0cdf7e2020-10-29 02:48:4843void AccessibilityLayer::SetSubpixelPositionOffset(
44 const gfx::Vector2dF& offset) {
45 layer_->SetSubpixelPositionOffset(offset);
46}
47
Katie Dektarc0ad2ff2017-11-08 04:42:0848void AccessibilityLayer::CreateOrUpdateLayer(aura::Window* root_window,
49 const char* layer_name,
Joel Riley0790b7222020-12-14 20:42:0250 const gfx::Rect& bounds,
51 bool stack_at_top) {
Katie Dektarc0ad2ff2017-11-08 04:42:0852 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);
yjliu0b2876212019-12-19 03:36:2056 layer_->SetName(layer_name);
Katie Dektarc0ad2ff2017-11-08 04:42:0857 layer_->SetFillsBoundsOpaquely(false);
58 root_layer->Add(layer_.get());
Evan Stadebb4f27782019-07-10 23:42:1559 // 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 Dektarc0ad2ff2017-11-08 04:42:0863 }
64
65 // Keep moving it to the top in case new layers have been added
66 // since we created this layer.
Joel Riley0790b7222020-12-14 20:42:0267 if (stack_at_top) {
68 layer_->parent()->StackAtTop(layer_.get());
69 } else {
70 layer_->parent()->StackAtBottom(layer_.get());
71 }
Katie Dektarc0ad2ff2017-11-08 04:42:0872
73 layer_->SetBounds(bounds);
74 gfx::Rect layer_bounds(0, 0, bounds.width(), bounds.height());
75 layer_->SchedulePaint(layer_bounds);
Katie Dektarc0ad2ff2017-11-08 04:42:0876}
77
Katie Dektarc0ad2ff2017-11-08 04:42:0878void AccessibilityLayer::OnDeviceScaleFactorChanged(
79 float old_device_scale_factor,
80 float new_device_scale_factor) {
81 if (delegate_)
82 delegate_->OnDeviceScaleFactorChanged();
83}
84
Katie Dektarc0ad2ff2017-11-08 04:42:0885} // namespace ash