File: image_model.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,194 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 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_MODELS_IMAGE_MODEL_H_
#define UI_BASE_MODELS_IMAGE_MODEL_H_

#include <variant>

#include "base/component_export.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/color/color_id.h"
#include "ui/color/color_variant.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"

namespace gfx {
struct VectorIcon;
}  // namespace gfx

namespace ui {

class ColorProvider;

// The following classes encapsulate the various ways that a model may provide
// or otherwise specify an icon or image. Most notably, these are used by the
// MenuModel and SimpleMenuModel for building actual menus.
//
// The VectorIconModel represents the combination of the icon path and its
// optional color id. The optional color is provided by the color id which is
// eventually resolved by the ColorProvider from the correct context. This class
// is only used internal to ImageModel and should never be instantiated except
// by ImageModel.
class COMPONENT_EXPORT(UI_BASE) VectorIconModel {
 public:
  VectorIconModel();

  VectorIconModel(const VectorIconModel&);
  VectorIconModel& operator=(const VectorIconModel&);

  VectorIconModel(VectorIconModel&&);
  VectorIconModel& operator=(VectorIconModel&&);

  ~VectorIconModel();

  bool is_empty() const { return !vector_icon_; }

  friend bool operator==(const VectorIconModel&,
                         const VectorIconModel&) = default;

  const gfx::VectorIcon* vector_icon() const { return vector_icon_; }
  int icon_size() const { return icon_size_; }
  ui::ColorVariant color() const { return color_; }
  const gfx::VectorIcon* badge_icon() const { return badge_icon_; }

 private:
  friend class ImageModel;

  VectorIconModel(const gfx::VectorIcon& vector_icon,
                  ui::ColorVariant color,
                  int icon_size,
                  const gfx::VectorIcon* badge_icon);

  raw_ptr<const gfx::VectorIcon> vector_icon_ = nullptr;
  int icon_size_ = 0;
  ui::ColorVariant color_;
  raw_ptr<const gfx::VectorIcon> badge_icon_ = nullptr;
};

// ImageModel encapsulates one of several image representations. See FromXXXX
// static-factory functions for supported formats.
class COMPONENT_EXPORT(UI_BASE) ImageModel {
 public:
  using ImageGenerator =
      base::RepeatingCallback<gfx::ImageSkia(const ui::ColorProvider*)>;

  ImageModel();
  ImageModel(const ImageModel&);
  ImageModel& operator=(const ImageModel&);
  ImageModel(ImageModel&&);
  ImageModel& operator=(ImageModel&&);
  ~ImageModel();

  // TODO(pkasting): Remove the default `color_id` or replace with kColorIcon.
  static ImageModel FromVectorIcon(const gfx::VectorIcon& vector_icon,
                                   ui::ColorVariant color = kColorMenuIcon,
                                   int icon_size = 0,
                                   const gfx::VectorIcon* badge_icon = nullptr);
  static ImageModel FromImage(const gfx::Image& image);
  static ImageModel FromImageSkia(const gfx::ImageSkia& image_skia);
  // |FromResourceId| does not support color theming. To create an |ImageModel|
  // with color theming, use |ResourceBundle::GetThemedLottieImageNamed|.
  static ImageModel FromResourceId(int resource_id);
  // `size` must be the size of the image the `generator` returns.
  // NOTE: If this proves onerous, we could allow autodetection, at the cost of
  // requiring `generator` to be runnable with a null ColorProvider*.
  static ImageModel FromImageGenerator(ImageGenerator generator,
                                       gfx::Size size);

  bool IsEmpty() const;
  bool IsVectorIcon() const;
  bool IsImage() const;
  bool IsImageGenerator() const;
  gfx::Size Size() const;
  // Only valid if IsVectorIcon() or IsImage() return true, respectively.
  VectorIconModel GetVectorIcon() const;
  gfx::Image GetImage() const;
  ImageGenerator GetImageGenerator() const;

  // Checks if both models yield equal images.
  friend bool operator==(const ImageModel&, const ImageModel&) = default;

  // Rasterizes if necessary.
  gfx::ImageSkia Rasterize(const ui::ColorProvider* color_provider) const;

 private:
  struct ImageGeneratorAndSize {
    ImageGeneratorAndSize(ImageGenerator generator, gfx::Size size);
    ImageGeneratorAndSize(const ImageGeneratorAndSize&);
    ImageGeneratorAndSize& operator=(const ImageGeneratorAndSize&);
    ~ImageGeneratorAndSize();

    friend bool operator==(const ImageGeneratorAndSize&,
                           const ImageGeneratorAndSize&) = default;

    ImageGenerator generator;
    gfx::Size size;
  };

  explicit ImageModel(const VectorIconModel& vector_icon_model);
  explicit ImageModel(const gfx::Image& image);
  explicit ImageModel(const gfx::ImageSkia& image_skia);
  explicit ImageModel(ImageGeneratorAndSize image_generator);

  std::variant<VectorIconModel, gfx::Image, ImageGeneratorAndSize> icon_;
};

}  // namespace ui

#endif  // UI_BASE_MODELS_IMAGE_MODEL_H_