File: cursor_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (150 lines) | stat: -rw-r--r-- 4,572 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/fast_ink/cursor/cursor_view.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/task/single_thread_task_runner.h"
#include "cc/paint/paint_canvas.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/image_skia_rep_default.h"

namespace ash {
namespace {

// Amount of time without cursor movement before entering stationary state.
const int kStationaryDelayMs = 500;

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// CursorView, public:

CursorView::~CursorView() = default;

// static
views::UniqueWidgetPtr CursorView::Create(const gfx::Point& initial_location,
                                          aura::Window* container) {
  CursorView* cursor_view = new CursorView(initial_location);
  auto widget = FastInkView::CreateWidgetWithContents(
      base::WrapUnique(cursor_view), container);

  // Initialize after FaskInkHost is set on `cursor_view` and it is attached to
  // a widget. So that it could get `buffer_to_screen_transform_`.
  cursor_view->Init();

  return widget;
}

void CursorView::SetCursorImages(
    const std::vector<gfx::ImageSkia>& cursor_images,
    const gfx::Size& cursor_size,
    const gfx::Point& cursor_hotspot) {
  cursor_images_.clear();
  for (const gfx::ImageSkia& cursor_image : cursor_images) {
    // Scale cursor images to device scale factor.
    cursor_images_.push_back(gfx::ImageSkia::CreateFrom1xBitmap(
        cursor_image.GetRepresentation(device_scale_factor_).GetBitmap()));
  }
  cursor_size_ = cursor_size;
  cursor_hotspot_ = cursor_hotspot;

  UpdateAnimation();

  UpdateCursor();

  stationary_timer_->Reset();
}

void CursorView::SetLocation(const gfx::Point& location) {
  if (location == cursor_location_) {
    return;
  }
  stationary_timer_->Reset();
  cursor_location_ = location;

  UpdateCursor();
}

////////////////////////////////////////////////////////////////////////////////
// CursorView, private:

CursorView::CursorView(const gfx::Point& initial_location)
    : cursor_location_(initial_location),
      ui_task_runner_(base::SingleThreadTaskRunner::GetCurrentDefault()) {
  stationary_timer_.emplace(
      FROM_HERE, base::Milliseconds(kStationaryDelayMs),
      base::BindRepeating(&CursorView::OnStationary,
                          weak_ptr_factory_.GetWeakPtr()));
}

void CursorView::Init() {
  buffer_to_screen_transform_ =
      host()->window_to_buffer_transform().GetCheckedInverse();
  device_scale_factor_ =
      GetWidget()->GetNativeView()->GetHost()->device_scale_factor();
}

void CursorView::UpdateAnimation() {
  cursor_image_index_ = 0;
  if (cursor_images_.size() <= 1) {
    animated_cursor_timer_.Stop();
  } else if (cursor_images_.size() > 1) {
    animated_cursor_timer_.Start(
        FROM_HERE, base::Milliseconds(16),
        base::BindRepeating(&CursorView::AdvanceFrame,
                            weak_ptr_factory_.GetWeakPtr()));
  }
}

void CursorView::AdvanceFrame() {
  cursor_image_index_ = (cursor_image_index_ + 1) % cursor_images_.size();
  Draw();
}

void CursorView::Draw() {
  {
    std::unique_ptr<FastInkHost::ScopedPaint> paint =
        GetScopedPaint(damage_rect_);
    cc::PaintCanvas* sk_canvas = paint->canvas().sk_canvas();
    sk_canvas->translate(cursor_rect_.x(), cursor_rect_.y());

    // Undo device scale factor on the canvas.
    sk_canvas->scale(SkFloatToScalar(1 / device_scale_factor_));
    if (!cursor_images_.empty()) {
      paint->canvas().DrawImageInt(cursor_images_[cursor_image_index_], 0, 0);
    }
  }

  // TODO(b/360768376): Disable overlay until we know why
  // fast ink cursor glitches.
  UpdateSurface(cursor_rect_, damage_rect_,
                /*auto_refresh=*/false);
}

void CursorView::OnStationary() {
  stationary_timer_->Stop();
  Draw();
}

void CursorView::UpdateCursor() {
  damage_rect_ = cursor_rect_;
  cursor_rect_ = gfx::Rect(cursor_size_);
  cursor_rect_.set_x(SkIntToScalar(cursor_location_.x() - cursor_hotspot_.x()));
  cursor_rect_.set_y(SkIntToScalar(cursor_location_.y() - cursor_hotspot_.y()));
  damage_rect_.Union(cursor_rect_);

  // Grow `damage_rect_` on all sides by 1 pixel for the possible rounding
  // errors after scaling cursor images to device scale factor.
  damage_rect_.Outset(1);

  Draw();
}

}  // namespace ash