| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/resource/resource_bundle.h" | 5 #include "ui/base/resource/resource_bundle.h" |
| 6 | 6 |
| 7 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 << " resource_id = " << resource_id; | 136 << " resource_id = " << resource_id; |
| 137 } | 137 } |
| 138 return images_[resource_id]; | 138 return images_[resource_id]; |
| 139 } | 139 } |
| 140 } | 140 } |
| 141 | 141 |
| 142 gfx::Image image; | 142 gfx::Image image; |
| 143 if (delegate_) | 143 if (delegate_) |
| 144 image = delegate_->GetNativeImageNamed(resource_id); | 144 image = delegate_->GetNativeImageNamed(resource_id); |
| 145 | 145 |
| 146 bool found_in_a_material_design_pack = false; | |
| 147 | |
| 148 if (image.IsEmpty()) { | 146 if (image.IsEmpty()) { |
| 149 base::scoped_nsobject<NSImage> ns_image; | 147 base::scoped_nsobject<NSImage> ns_image; |
| 148 // Material Design packs are meant to override the standard packs, so |
| 149 // search for the image in those packs first. |
| 150 for (size_t i = 0; i < data_packs_.size(); ++i) { | 150 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 151 if (!data_packs_[i]->HasOnlyMaterialDesignAssets()) |
| 152 continue; |
| 151 scoped_refptr<base::RefCountedStaticMemory> data( | 153 scoped_refptr<base::RefCountedStaticMemory> data( |
| 152 data_packs_[i]->GetStaticMemory(resource_id)); | 154 data_packs_[i]->GetStaticMemory(resource_id)); |
| 153 if (!data.get()) | 155 if (!data.get()) |
| 154 continue; | 156 continue; |
| 155 | 157 |
| 156 // This loop adds the image resource from each available pack, if it's | 158 base::scoped_nsobject<NSData> ns_data( |
| 157 // present. When Material Design packs are available, however, their | 159 [[NSData alloc] initWithBytes:data->front() length:data->size()]); |
| 158 // images are meant to override the same image in the standard packs. The | 160 if (!ns_image.get()) { |
| 159 // Material Design packs exist at the start of the data_packs_ vector, | 161 ns_image.reset([[NSImage alloc] initWithData:ns_data]); |
| 160 // so make a note that the image was pulled from a Material Design pack, | 162 } else { |
| 161 // and ignore the same image in the standard packs. | 163 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; |
| 162 if (found_in_a_material_design_pack) { | 164 if (image_rep) |
| 163 break; | 165 [ns_image addRepresentation:image_rep]; |
| 164 } | 166 } |
| 165 found_in_a_material_design_pack = | 167 } |
| 166 data_packs_[i]->HasOnlyMaterialDesignAssets(); | 168 |
| 169 if (ns_image.get()) { |
| 170 image = gfx::Image(ns_image.release()); |
| 171 } |
| 172 } |
| 173 |
| 174 if (image.IsEmpty()) { |
| 175 base::scoped_nsobject<NSImage> ns_image; |
| 176 for (size_t i = 0; i < data_packs_.size(); ++i) { |
| 177 if (data_packs_[i]->HasOnlyMaterialDesignAssets()) |
| 178 continue; |
| 179 scoped_refptr<base::RefCountedStaticMemory> data( |
| 180 data_packs_[i]->GetStaticMemory(resource_id)); |
| 181 if (!data.get()) |
| 182 continue; |
| 167 | 183 |
| 168 base::scoped_nsobject<NSData> ns_data( | 184 base::scoped_nsobject<NSData> ns_data( |
| 169 [[NSData alloc] initWithBytes:data->front() length:data->size()]); | 185 [[NSData alloc] initWithBytes:data->front() length:data->size()]); |
| 170 if (!ns_image.get()) { | 186 if (!ns_image.get()) { |
| 171 ns_image.reset([[NSImage alloc] initWithData:ns_data]); | 187 ns_image.reset([[NSImage alloc] initWithData:ns_data]); |
| 172 } else { | 188 } else { |
| 173 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; | 189 NSImageRep* image_rep = [NSBitmapImageRep imageRepWithData:ns_data]; |
| 174 if (image_rep) | 190 if (image_rep) |
| 175 [ns_image addRepresentation:image_rep]; | 191 [ns_image addRepresentation:image_rep]; |
| 176 } | 192 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 189 | 205 |
| 190 // Another thread raced the load and has already cached the image. | 206 // Another thread raced the load and has already cached the image. |
| 191 if (images_.count(resource_id)) | 207 if (images_.count(resource_id)) |
| 192 return images_[resource_id]; | 208 return images_[resource_id]; |
| 193 | 209 |
| 194 images_[resource_id] = image; | 210 images_[resource_id] = image; |
| 195 return images_[resource_id]; | 211 return images_[resource_id]; |
| 196 } | 212 } |
| 197 | 213 |
| 198 } // namespace ui | 214 } // namespace ui |
| OLD | NEW |