Index: ui/gfx/icon_family.h |
diff --git a/ui/gfx/icon_family.h b/ui/gfx/icon_family.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..37af40fb85c5bbab5c76428a010b9223d563f90c |
--- /dev/null |
+++ b/ui/gfx/icon_family.h |
@@ -0,0 +1,138 @@ |
+// Copyright 2013 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_GFX_ICON_FAMILY_H_ |
+#define UI_GFX_ICON_FAMILY_H_ |
+ |
+#include <iterator> |
+#include <map> |
+#include <utility> |
+ |
+#include "ui/base/ui_export.h" |
+#include "ui/gfx/image/image_skia.h" |
+ |
+namespace gfx { |
+class Image; |
+ |
+// A collection of images, representing icons at different sizes. Each image is |
+// an ImageSkia, which may contain multi-resolution bitmaps. |
+class UI_EXPORT IconFamily { |
+ public: |
+ // Type for iterating over all icons in the family, in order. |
+ // Dereferencing this iterator returns a gfx::ImageSkia. |
+ class const_iterator; |
+ |
+ IconFamily(); |
+ ~IconFamily(); |
+ |
+ // Gets an iterator to the first icon. |
+ inline const_iterator begin() const { return const_iterator(map_.begin()); } |
+ // Gets an iterator to one after the last icon. |
+ inline const_iterator end() const { return const_iterator(map_.end()); } |
+ |
+ // Determines whether the icon family has no icons in it. |
+ inline bool empty() const { return map_.empty(); } |
+ |
+ // Removes all icons from the family. |
+ inline void clear() { return map_.clear(); } |
+ |
+ // Adds an icon to the family. If another icon is already present at the |
+ // same size, it will be overwritten. If |icon| does not contain an image, |
+ // does nothing. |
+ // The icon SHOULD be square; if not, its size will be whichever is smaller |
+ // of the width and height. |
+ void Add(const gfx::Image& icon); |
+ |
+ // Adds an icon to the family. If another icon is already present at the |
+ // same size, it will be overwritten. |
+ // The icon SHOULD be square; if not, its size will be whichever is smaller |
+ // of the width and height. |
+ void Add(const gfx::ImageSkia& icon); |
+ |
+ // Gets the best image to use at |width|x|height|. |
+ // Gets an image at the same aspect ratio as |width|:|height|, if possible, or |
+ // if not, the closest aspect ratio. Among images of that aspect ratio, |
+ // returns the smallest image bigger or equal to the requested size. If none |
+ // exists, returns the largest image of that aspect ratio. |
+ // An image's size is determined by the minimum of its width and height, and |
+ // the desired size is the maximum of |width| and |height|, ensuring that the |
+ // resulting image is larger than or equal to the requested size in both axes. |
+ // If there are no image in the family, returns NULL. |
+ const gfx::ImageSkia* Get(int width, int height) const; |
+ |
+ // An <aspect ratio, size> pair, where size is the minimum of an image's width |
+ // and height. |
+ struct MapKey : std::pair<float, int> { |
+ MapKey(float aspect, int size) |
+ : std::pair<float, int>(aspect, size) {} |
+ |
+ inline float aspect() const { return first; } |
+ |
+ inline float size() const { return second; } |
+ }; |
+ |
+ class const_iterator : |
+ std::iterator<std::bidirectional_iterator_tag, const gfx::ImageSkia> { |
+ public: |
+ inline const_iterator() {} |
+ |
+ inline const_iterator& operator++() { |
+ ++map_iterator_; |
+ return *this; |
+ } |
+ |
+ inline const_iterator operator++(int /*unused*/) { |
+ const_iterator result(*this); |
+ ++(*this); |
+ return result; |
+ } |
+ |
+ inline const_iterator& operator--() { |
+ --map_iterator_; |
+ return *this; |
+ } |
+ |
+ inline const_iterator operator--(int /*unused*/) { |
+ const_iterator result(*this); |
+ --(*this); |
+ return result; |
+ } |
+ |
+ inline bool operator==(const const_iterator& other) const { |
+ return map_iterator_ == other.map_iterator_; |
+ } |
+ |
+ inline bool operator!=(const const_iterator& other) const { |
+ return map_iterator_ != other.map_iterator_; |
+ } |
+ |
+ inline const gfx::ImageSkia& operator*() const { |
+ return map_iterator_->second; |
+ } |
+ |
+ inline const gfx::ImageSkia* operator->() const { |
+ return &**this; |
+ } |
+ |
+ private: |
+ explicit const_iterator( |
+ const std::map<MapKey, gfx::ImageSkia>::const_iterator& other); |
+ |
+ std::map<MapKey, gfx::ImageSkia>::const_iterator map_iterator_; |
+ |
+ friend class IconFamily; |
+ }; |
+ |
+ private: |
+ // Map from base icon size to multi-resolution image. If an image is |
+ // non-square, its size refers to the minimum of its width and height |
+ // (although this is not recommended). |
+ // If an image contains multiple resolution bitmaps, the size refers to the |
+ // width/height at the 100% representation. |
+ std::map<MapKey, gfx::ImageSkia> map_; |
+}; |
+ |
+} // namespace gfx |
+ |
+#endif // UI_GFX_ICON_FAMILY_H_ |