summaryrefslogtreecommitdiff
path: root/chromium/ui/base/models/image_model.h
blob: 69a0f7786773631b82ebb854def3b0dc20990ff2 (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
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
// Copyright 2020 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.

#ifndef UI_BASE_MODELS_IMAGE_MODEL_H_
#define UI_BASE_MODELS_IMAGE_MODEL_H_

#include "base/callback.h"
#include "base/component_export.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"

namespace gfx {
struct VectorIcon;
}  // namespace gfx

namespace ui {

// 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_; }

  bool operator==(const VectorIconModel& other) const;
  bool operator!=(const VectorIconModel& other) const;

 private:
  friend class ThemedVectorIcon;
  friend class ImageModel;

  VectorIconModel(const gfx::VectorIcon& vector_icon,
                  int color_id,
                  int icon_size);
  // TODO (kylixrd): This should be eventually removed once all instances of
  // hard-coded SkColor constants are removed in favor of using a color id.
  VectorIconModel(const gfx::VectorIcon& vector_icon,
                  SkColor color,
                  int icon_size);

  const gfx::VectorIcon* vector_icon() const { return vector_icon_; }
  int icon_size() const { return icon_size_; }
  int color_id() const { return absl::get<int>(color_); }
  SkColor color() const { return absl::get<SkColor>(color_); }
  bool has_color() const { return absl::holds_alternative<SkColor>(color_); }

  const gfx::VectorIcon* vector_icon_ = nullptr;
  int icon_size_ = 0;
  absl::variant<int, SkColor> color_ = gfx::kPlaceholderColor;
};

// ImageModel encapsulates either a gfx::Image or a VectorIconModel. Only one
// of the two may be specified at a given time. This class is instantiated via
// the FromXXXX static factory functions.

class COMPONENT_EXPORT(UI_BASE) ImageModel {
 public:
  ImageModel();
  ImageModel(const ImageModel&);
  ImageModel& operator=(const ImageModel&);
  ImageModel(ImageModel&&);
  ImageModel& operator=(ImageModel&&);
  ~ImageModel();

  static ImageModel FromVectorIcon(const gfx::VectorIcon& vector_icon,
                                   int color_id = -1,
                                   int icon_size = 0);
  static ImageModel FromVectorIcon(const gfx::VectorIcon& vector_icon,
                                   SkColor color,
                                   int icon_size = 0);
  static ImageModel FromImage(const gfx::Image& image);
  static ImageModel FromImageSkia(const gfx::ImageSkia& image_skia);
  static ImageModel FromResourceId(int resource_id);

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

  // Checks if both model yield equal images.
  bool operator==(const ImageModel& other) const;
  bool operator!=(const ImageModel& other) const;

 private:
  ImageModel(const gfx::Image& image);
  ImageModel(const gfx::ImageSkia& image_skia);
  ImageModel(const VectorIconModel& vector_icon_model);

  absl::variant<VectorIconModel, gfx::Image> icon_;
};

}  // namespace ui

#endif  // UI_BASE_MODELS_IMAGE_MODEL_H_