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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_CURSOR_CURSOR_H_
#define UI_BASE_CURSOR_CURSOR_H_
#include <vector>
#include "base/component_export.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/base/cursor/platform_cursor.h"
#include "ui/gfx/geometry/point.h"
namespace gfx {
class Size;
}
namespace ui {
inline constexpr SkColor kDefaultCursorColor = SK_ColorBLACK;
inline constexpr int kDefaultLargeCursorSize = 64;
inline constexpr int kMinLargeCursorSize = 25;
inline constexpr int kMaxLargeCursorSize = 128;
struct COMPONENT_EXPORT(UI_BASE_CURSOR) CursorData {
public:
CursorData();
CursorData(std::vector<SkBitmap> bitmaps,
gfx::Point hotspot,
float scale_factor = 1.0f);
CursorData(const CursorData&);
~CursorData();
// `bitmaps` contains at least 1 element. Animated cursors (e.g.
// `CursorType::kWait`, `CursorType::kProgress`) are represented as a list
// of images, so a bigger number is expected.
std::vector<SkBitmap> bitmaps;
gfx::Point hotspot;
// `scale_factor` cannot be zero, since it will be either the device scale
// factor or the image scale factor for custom cursors. In both cases, the
// code is checked for a minimum value at its origin.
float scale_factor = 1.0f;
};
// Ref-counted cursor that supports both default and custom cursors.
class COMPONENT_EXPORT(UI_BASE_CURSOR) Cursor {
public:
// Creates a custom cursor with the provided parameters. `hotspot` is
// clamped to `bitmap` dimensions. `image_scale_factor` cannot be 0.
static Cursor NewCustom(SkBitmap bitmap,
gfx::Point hotspot,
float image_scale_factor = 1.0f);
Cursor();
Cursor(mojom::CursorType type);
Cursor(const Cursor& cursor);
~Cursor();
void SetPlatformCursor(scoped_refptr<PlatformCursor> platform_cursor);
mojom::CursorType type() const { return type_; }
scoped_refptr<PlatformCursor> platform() const { return platform_cursor_; }
// Methods to access custom cursor data. For any other cursor type, the
// program will abort.
const SkBitmap& custom_bitmap() const;
const gfx::Point& custom_hotspot() const;
float image_scale_factor() const;
// Note: custom cursor comparison may perform expensive pixel equality checks!
bool operator==(const Cursor& cursor) const;
bool operator==(mojom::CursorType type) const { return type_ == type; }
// Limit the size of cursors so that they cannot be used to cover UI
// elements in chrome.
// `size` is the size of the cursor in physical pixels.
static bool AreDimensionsValidForWeb(const gfx::Size& size,
float scale_factor);
private:
// Custom cursor constructor.
Cursor(SkBitmap bitmap, gfx::Point hotspot, float image_scale_factor);
mojom::CursorType type_ = mojom::CursorType::kNull;
scoped_refptr<PlatformCursor> platform_cursor_;
// Only used for custom cursors:
SkBitmap custom_bitmap_;
gfx::Point custom_hotspot_;
// Scale factor of `custom_bitmap_`. When creating the platform cursor, the
// bitmap will be scaled to the device scale factor taking into account this
// value.
float image_scale_factor_ = 1.0f;
};
} // namespace ui
#endif // UI_BASE_CURSOR_CURSOR_H_
|