Chromium Code Reviews| 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_IMAGE_IMAGE_FAMILY_H_ | |
| 6 #define UI_GFX_IMAGE_IMAGE_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.h" | |
| 14 | |
| 15 namespace gfx { | |
| 16 class ImageSkia; | |
| 17 | |
| 18 // A collection of images at different sizes. The images should be different | |
| 19 // representations of the same basic concept (for example, an icon) at various | |
| 20 // sizes and (optionally) aspect ratios. A method is provided for finding the | |
| 21 // most appropriate image to fit in a given rectangle. | |
| 22 class UI_EXPORT ImageFamily { | |
| 23 private: | |
| 24 // Forward declaration. | |
| 25 struct MapKey; | |
| 26 | |
| 27 public: | |
| 28 // Type for iterating over all images in the family, in order. | |
| 29 // Dereferencing this iterator returns a gfx::Image. | |
| 30 class const_iterator : | |
| 31 std::iterator<std::bidirectional_iterator_tag, const gfx::Image> { | |
| 32 public: | |
| 33 const_iterator() {} | |
| 34 | |
| 35 const_iterator& operator++() { | |
| 36 ++map_iterator_; | |
| 37 return *this; | |
| 38 } | |
| 39 | |
| 40 const_iterator operator++(int /*unused*/) { | |
| 41 const_iterator result(*this); | |
| 42 ++(*this); | |
| 43 return result; | |
| 44 } | |
| 45 | |
| 46 const_iterator& operator--() { | |
| 47 --map_iterator_; | |
| 48 return *this; | |
| 49 } | |
| 50 | |
| 51 const_iterator operator--(int /*unused*/) { | |
| 52 const_iterator result(*this); | |
| 53 --(*this); | |
| 54 return result; | |
| 55 } | |
| 56 | |
| 57 bool operator==(const const_iterator& other) const { | |
| 58 return map_iterator_ == other.map_iterator_; | |
| 59 } | |
| 60 | |
| 61 bool operator!=(const const_iterator& other) const { | |
| 62 return map_iterator_ != other.map_iterator_; | |
| 63 } | |
| 64 | |
| 65 const gfx::Image& operator*() const { | |
| 66 return map_iterator_->second; | |
| 67 } | |
| 68 | |
| 69 const gfx::Image* operator->() const { | |
| 70 return &**this; | |
| 71 } | |
| 72 | |
| 73 private: | |
| 74 friend class ImageFamily; | |
| 75 | |
| 76 explicit const_iterator( | |
| 77 const std::map<MapKey, gfx::Image>::const_iterator& other); | |
| 78 | |
| 79 std::map<MapKey, gfx::Image>::const_iterator map_iterator_; | |
| 80 }; | |
| 81 | |
| 82 ImageFamily(); | |
| 83 ~ImageFamily(); | |
| 84 | |
| 85 // Gets an iterator to the first image. | |
| 86 const_iterator begin() const { return const_iterator(map_.begin()); } | |
| 87 // Gets an iterator to one after the last image. | |
| 88 const_iterator end() const { return const_iterator(map_.end()); } | |
| 89 | |
| 90 // Determines whether the image family has no images in it. | |
| 91 bool empty() const { return map_.empty(); } | |
| 92 | |
| 93 // Removes all images from the family. | |
| 94 void clear() { return map_.clear(); } | |
| 95 | |
| 96 // Adds an image to the family. If another image is already present at the | |
| 97 // same size, it will be overwritten. | |
| 98 void Add(const gfx::Image& image); | |
| 99 | |
| 100 // Adds an image to the family. If another image is already present at the | |
| 101 // same size, it will be overwritten. | |
| 102 void Add(const gfx::ImageSkia& image_skia); | |
| 103 | |
| 104 // Gets the best image to use in a rectangle of |width|x|height|. | |
| 105 // Gets an image at the same aspect ratio as |width|:|height|, if possible, or | |
| 106 // if not, the closest aspect ratio. Among images of that aspect ratio, | |
| 107 // returns the smallest image with both its width and height bigger or equal | |
| 108 // to the requested size. If none exists, returns the largest image of that | |
| 109 // aspect ratio. If there are no images in the family, returns NULL. | |
| 110 const gfx::Image* Get(int width, int height) const; | |
| 111 | |
| 112 private: | |
| 113 // An <aspect ratio, DIP width> pair. | |
| 114 // A 0x0 image has aspect ratio 1.0. 0xN and Nx0 images are treated as 0x0. | |
| 115 struct MapKey : std::pair<float, int> { | |
| 116 MapKey(float aspect, int width) | |
| 117 : std::pair<float, int>(aspect, width) {} | |
| 118 | |
| 119 float aspect() const { return first; } | |
| 120 | |
| 121 float width() const { return second; } | |
|
pkotwicz
2013/04/02 16:06:31
Should this be int?
Matt Giuca
2013/04/03 00:37:43
Done. (Good catch)
| |
| 122 }; | |
| 123 | |
| 124 // Find the closest aspect ratio in the map to |desired_aspect|. | |
| 125 // Ties are broken by the thinner aspect. | |
| 126 // map_ must not be empty. |desired_aspect| must be > 0.0. | |
|
pkotwicz
2013/04/02 16:06:31
Nit: |map_|.
Matt Giuca
2013/04/03 00:37:43
Done.
| |
| 127 float GetClosestAspect(float desired_aspect) const; | |
| 128 | |
| 129 // Gets an image with aspect ratio |aspect|, at the best size for |width|. | |
| 130 // Returns the smallest image of aspect ratio |aspect| with its width bigger | |
| 131 // or equal to |width|. If none exists, returns the largest image of aspect | |
| 132 // ratio |aspect|. Behavior is undefined if there is not at least one image in | |
| 133 // map_ of aspect ratio |aspect|. | |
|
pkotwicz
2013/04/02 16:06:31
Nits: "is not at least one image in map_" -> "no i
Matt Giuca
2013/04/03 00:37:43
Done.
| |
| 134 const gfx::Image* GetWithExactAspect(float aspect, int width) const; | |
| 135 | |
| 136 // Map from (aspect ratio, width) to image. | |
| 137 std::map<MapKey, gfx::Image> map_; | |
| 138 }; | |
| 139 | |
| 140 } // namespace gfx | |
| 141 | |
| 142 #endif // UI_GFX_IMAGE_IMAGE_FAMILY_H_ | |
| OLD | NEW |