OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef UI_GFX_ICON_FAMILY_H_ |
| 6 #define UI_GFX_ICON_FAMILY_H_ |
| 7 |
| 8 #include <iterator> |
| 9 #include <map> |
| 10 #include <utility> |
| 11 |
| 12 #include "ui/base/ui_export.h" |
| 13 #include "ui/gfx/image/image_skia.h" |
| 14 |
| 15 namespace gfx { |
| 16 class Image; |
| 17 |
| 18 // A collection of images, representing icons at different sizes. Each image is |
| 19 // an ImageSkia, which may contain multi-resolution bitmaps. |
| 20 class UI_EXPORT IconFamily { |
| 21 public: |
| 22 // Type for iterating over all icons in the family, in order. |
| 23 // Dereferencing this iterator returns a gfx::ImageSkia. |
| 24 class const_iterator; |
| 25 |
| 26 IconFamily(); |
| 27 ~IconFamily(); |
| 28 |
| 29 // Gets an iterator to the first icon. |
| 30 inline const_iterator begin() const { return const_iterator(map_.begin()); } |
| 31 // Gets an iterator to one after the last icon. |
| 32 inline const_iterator end() const { return const_iterator(map_.end()); } |
| 33 |
| 34 // Determines whether the icon family has no icons in it. |
| 35 inline bool empty() const { return map_.empty(); } |
| 36 |
| 37 // Removes all icons from the family. |
| 38 inline void clear() { return map_.clear(); } |
| 39 |
| 40 // Adds an icon to the family. If another icon is already present at the |
| 41 // same size, it will be overwritten. If |icon| does not contain an image, |
| 42 // does nothing. |
| 43 // The icon SHOULD be square; if not, its size will be whichever is smaller |
| 44 // of the width and height. |
| 45 void Add(const gfx::Image& icon); |
| 46 |
| 47 // Adds an icon to the family. If another icon is already present at the |
| 48 // same size, it will be overwritten. |
| 49 // The icon SHOULD be square; if not, its size will be whichever is smaller |
| 50 // of the width and height. |
| 51 void Add(const gfx::ImageSkia& icon); |
| 52 |
| 53 // Gets the best image to use at |width|x|height|. |
| 54 // Gets an image at the same aspect ratio as |width|:|height|, if possible, or |
| 55 // if not, the closest aspect ratio. Among images of that aspect ratio, |
| 56 // returns the smallest image bigger or equal to the requested size. If none |
| 57 // exists, returns the largest image of that aspect ratio. |
| 58 // An image's size is determined by the minimum of its width and height, and |
| 59 // the desired size is the maximum of |width| and |height|, ensuring that the |
| 60 // resulting image is larger than or equal to the requested size in both axes. |
| 61 // If there are no image in the family, returns NULL. |
| 62 const gfx::ImageSkia* Get(int width, int height) const; |
| 63 |
| 64 // An <aspect ratio, size> pair, where size is the minimum of an image's width |
| 65 // and height. |
| 66 struct MapKey : std::pair<float, int> { |
| 67 MapKey(float aspect, int size) |
| 68 : std::pair<float, int>(aspect, size) {} |
| 69 |
| 70 inline float aspect() const { return first; } |
| 71 |
| 72 inline float size() const { return second; } |
| 73 }; |
| 74 |
| 75 class const_iterator : |
| 76 std::iterator<std::bidirectional_iterator_tag, const gfx::ImageSkia> { |
| 77 public: |
| 78 inline const_iterator() {} |
| 79 |
| 80 inline const_iterator& operator++() { |
| 81 ++map_iterator_; |
| 82 return *this; |
| 83 } |
| 84 |
| 85 inline const_iterator operator++(int /*unused*/) { |
| 86 const_iterator result(*this); |
| 87 ++(*this); |
| 88 return result; |
| 89 } |
| 90 |
| 91 inline const_iterator& operator--() { |
| 92 --map_iterator_; |
| 93 return *this; |
| 94 } |
| 95 |
| 96 inline const_iterator operator--(int /*unused*/) { |
| 97 const_iterator result(*this); |
| 98 --(*this); |
| 99 return result; |
| 100 } |
| 101 |
| 102 inline bool operator==(const const_iterator& other) const { |
| 103 return map_iterator_ == other.map_iterator_; |
| 104 } |
| 105 |
| 106 inline bool operator!=(const const_iterator& other) const { |
| 107 return map_iterator_ != other.map_iterator_; |
| 108 } |
| 109 |
| 110 inline const gfx::ImageSkia& operator*() const { |
| 111 return map_iterator_->second; |
| 112 } |
| 113 |
| 114 inline const gfx::ImageSkia* operator->() const { |
| 115 return &**this; |
| 116 } |
| 117 |
| 118 private: |
| 119 explicit const_iterator( |
| 120 const std::map<MapKey, gfx::ImageSkia>::const_iterator& other); |
| 121 |
| 122 std::map<MapKey, gfx::ImageSkia>::const_iterator map_iterator_; |
| 123 |
| 124 friend class IconFamily; |
| 125 }; |
| 126 |
| 127 private: |
| 128 // Map from base icon size to multi-resolution image. If an image is |
| 129 // non-square, its size refers to the minimum of its width and height |
| 130 // (although this is not recommended). |
| 131 // If an image contains multiple resolution bitmaps, the size refers to the |
| 132 // width/height at the 100% representation. |
| 133 std::map<MapKey, gfx::ImageSkia> map_; |
| 134 }; |
| 135 |
| 136 } // namespace gfx |
| 137 |
| 138 #endif // UI_GFX_ICON_FAMILY_H_ |
OLD | NEW |