summaryrefslogtreecommitdiff
path: root/chromium/content/common/cursors/webcursor.cc
blob: 0ce6024377a1f7e9e091a9b666fb9e7c3c823885 (plain)
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/cursors/webcursor.h"

#include <algorithm>

#include "build/build_config.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"

namespace content {

WebCursor::WebCursor() = default;

WebCursor::~WebCursor() {
  CleanupPlatformData();
}

WebCursor::WebCursor(const ui::Cursor& cursor) {
  SetCursor(cursor);
}

WebCursor::WebCursor(const WebCursor& other) {
  CopyAllData(other);
}

WebCursor& WebCursor::operator=(const WebCursor& other) {
  CleanupPlatformData();
  CopyAllData(other);
  return *this;
}

bool WebCursor::SetCursor(const ui::Cursor& cursor) {
  // This value matches kMaximumCursorSize from Blink's EventHandler.
  static constexpr int kMaximumCursorSize = 128;
  if (cursor.image_scale_factor() < 0.01f ||
      cursor.image_scale_factor() > 100.f ||
      (cursor.type() == ui::mojom::CursorType::kCustom &&
       (cursor.custom_bitmap().width() > kMaximumCursorSize ||
        cursor.custom_bitmap().height() > kMaximumCursorSize ||
        cursor.custom_bitmap().width() / cursor.image_scale_factor() >
            kMaximumCursorSize ||
        cursor.custom_bitmap().height() / cursor.image_scale_factor() >
            kMaximumCursorSize))) {
    return false;
  }

  CleanupPlatformData();
  cursor_ = cursor;

  // Clamp the hotspot to the custom image's dimensions.
  if (cursor_.type() == ui::mojom::CursorType::kCustom) {
    cursor_.set_custom_hotspot(
        gfx::Point(std::max(0, std::min(cursor_.custom_bitmap().width() - 1,
                                        cursor_.custom_hotspot().x())),
                   std::max(0, std::min(cursor_.custom_bitmap().height() - 1,
                                        cursor_.custom_hotspot().y()))));
  }

  return true;
}

bool WebCursor::operator==(const WebCursor& other) const {
  return
#if defined(USE_AURA) || defined(USE_OZONE)
      rotation_ == other.rotation_ &&
#endif
      cursor_ == other.cursor_;
}

bool WebCursor::operator!=(const WebCursor& other) const {
  return !(*this == other);
}

void WebCursor::CopyAllData(const WebCursor& other) {
  SetCursor(other.cursor_);
  CopyPlatformData(other);
}

}  // namespace content