Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1335)

Side by Side Diff: chrome/browser/resource_delegate_mac.mm

Issue 1585013002: MacViews: Use Cocoa folder icons in tree views. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "chrome/browser/resource_delegate_mac.h"
6
7 #import <AppKit/AppKit.h>
8 #import <Carbon/Carbon.h>
9
10 #include "blink/grit/devtools_resources.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/image/image_skia_operations.h"
13 #include "ui/gfx/image/image_skia_util_mac.h"
14 #include "ui/resources/grit/ui_resources.h"
15 #include "ui/views/resources/grit/views_resources.h"
16
17 namespace ui {
18
19 namespace {
20 // The singleton instance
21 MacResourceDelegate* g_instance = nullptr;
tapted 2016/01/14 23:43:50 I don't think you need this
karandeepb 2016/01/18 00:31:18 Done.
22 } // namespace
23
24 // Make intercepted_icons_map_ const?
25 MacResourceDelegate::MacResourceDelegate() {
26 DCHECK(!g_instance) << "Cannot initialize resource bundle delegate twice.";
27 intercepted_icons_map_.insert(InterceptedIconsMap::value_type(
tapted 2016/01/14 23:43:50 We should try not to do anything in the constructo
karandeepb 2016/01/18 00:31:17 Done.
28 IDR_FOLDER_OPEN,
29 InterceptedIcon(NSFileTypeForHFSTypeCode(kOpenFolderIcon), false,
30 IconSize::SMALL)));
31 intercepted_icons_map_.insert(InterceptedIconsMap::value_type(
32 IDR_FOLDER_CLOSED,
33 InterceptedIcon(NSFileTypeForHFSTypeCode(kGenericFolderIcon), false,
34 IconSize::SMALL)));
35 intercepted_icons_map_.insert(InterceptedIconsMap::value_type(
36 IDR_FOLDER_OPEN_RTL,
37 InterceptedIcon(NSFileTypeForHFSTypeCode(kOpenFolderIcon), true,
38 IconSize::SMALL)));
39 intercepted_icons_map_.insert(InterceptedIconsMap::value_type(
40 IDR_FOLDER_CLOSED_RTL,
41 InterceptedIcon(NSFileTypeForHFSTypeCode(kGenericFolderIcon), true,
42 IconSize::SMALL)));
43 intercepted_icons_map_.insert(InterceptedIconsMap::value_type(
44 IMAGES_FRAME_PNG,
45 InterceptedIcon(NSFileTypeForHFSTypeCode(kGenericFolderIcon), false,
46 IconSize::SMALL)));
47 g_instance = this;
48 }
49
50 MacResourceDelegate::~MacResourceDelegate() {
51 DCHECK_EQ(g_instance, this);
52 g_instance = nullptr;
53 }
54
55 base::FilePath MacResourceDelegate::GetPathForResourcePack(
56 const base::FilePath& pack_path,
57 ui::ScaleFactor scale_factor) {
58 return pack_path;
59 };
60
61 base::FilePath MacResourceDelegate::GetPathForLocalePack(
62 const base::FilePath& pack_path,
63 const std::string& locale) {
64 return pack_path;
65 };
66
67 gfx::Image MacResourceDelegate::GetImageNamed(int resource_id) {
68 return this->GetNativeImageNamed(resource_id,
tapted 2016/01/14 23:43:50 no need for `this->` but also when does this get u
karandeepb 2016/01/18 00:31:17 tree_view.cc uses the GetImageNamed(..) call. Shou
tapted 2016/01/18 04:15:42 So, I *think* what you have is correct. A Resource
69 ui::ResourceBundle::RTL_DISABLED);
70 };
71
72 gfx::Image MacResourceDelegate::GetNativeImageNamed(
73 int resource_id,
74 ui::ResourceBundle::ImageRTL rtl) {
75 DCHECK_EQ(rtl, ui::ResourceBundle::RTL_DISABLED);
tapted 2016/01/14 23:43:50 comment why this is safe? (do we need to worry abo
karandeepb 2016/01/18 00:31:17 Done. This is because on Mac and mostly other plat
76 InterceptedIconsMap::const_iterator it =
tapted 2016/01/14 23:43:50 I would just put a switch statement here for now,
karandeepb 2016/01/18 00:31:17 Done. The default return size is 32X32, which is b
77 intercepted_icons_map_.find(resource_id);
78 if (it == intercepted_icons_map_.end())
79 return gfx::Image();
80 return it->second.GetImage();
81 };
82
83 base::RefCountedStaticMemory* MacResourceDelegate::LoadDataResourceBytes(
84 int resource_id,
85 ui::ScaleFactor scale_factor) {
86 // Currently ignoring scale factor.
87 InterceptedIconsMap::const_iterator it =
88 intercepted_icons_map_.find(resource_id);
89 if (it == intercepted_icons_map_.end())
90 return nullptr;
91
92 gfx::Image image = it->second.GetImage();
93 scoped_refptr<base::RefCountedMemory> image_memory = image.As1xPNGBytes();
94 if (!image_memory->size())
95 return nullptr;
96
97 return new base::RefCountedStaticMemory(image_memory->front(),
98 image_memory->size());
99 };
100
101 bool MacResourceDelegate::GetRawDataResource(int resource_id,
102 ui::ScaleFactor scale_factor,
103 base::StringPiece* value) {
104 // Currently ignoring scale factor.
karandeepb 2016/01/14 04:21:33 I am not quite sure how to handle scale factor. Ge
karandeepb 2016/01/18 00:31:18 Done.
105 InterceptedIconsMap::const_iterator it =
106 intercepted_icons_map_.find(resource_id);
107 if (it == intercepted_icons_map_.end())
108 return false;
109
110 gfx::Image image = it->second.GetImage();
111 scoped_refptr<base::RefCountedMemory> image_memory = image.As1xPNGBytes();
112 if (!image_memory->size())
113 return false;
114 value->set(image_memory->front_as<char>(), image_memory->size());
115 return true;
116 };
117
118 bool MacResourceDelegate::GetLocalizedString(int message_id,
119 base::string16* value) {
120 return false;
121 };
122
123 scoped_ptr<gfx::Font> MacResourceDelegate::GetFont(
124 ui::ResourceBundle::FontStyle style) {
125 return scoped_ptr<gfx::Font>();
126 };
127
128 MacResourceDelegate::InterceptedIcon::InterceptedIcon(NSString* file_type,
129 bool flip_icon,
130 IconSize icon_size)
131 : file_type_(file_type),
132 flip_icon_(flip_icon),
133 icon_size_(icon_size),
134 is_image_loaded_(false) {}
135
136 MacResourceDelegate::InterceptedIcon::~InterceptedIcon() {}
137
138 const gfx::Image MacResourceDelegate::InterceptedIcon::GetImage() const {
karandeepb 2016/01/14 04:21:33 This can also use chrome/browser/icon_manager.h, w
139 if (!is_image_loaded_) {
140 is_image_loaded_ = true;
141 NSImage* icon =
142 [[[NSWorkspace sharedWorkspace] iconForFileType:file_type_] retain];
143 if (icon) {
144 NSSize desired_size;
145 switch (icon_size_) {
146 case IconSize::SMALL:
147 desired_size = NSMakeSize(16, 16);
148 break;
149 case IconSize::BIG:
150 desired_size = NSMakeSize(32, 32);
151 break;
152 default:
153 NOTREACHED();
154 }
155 gfx::ImageSkia image_skia =
156 gfx::ImageSkiaFromResizedNSImage(icon, desired_size);
157 if (flip_icon_)
158 image_skia = gfx::ImageSkiaOperations::CreateMirroredImage(image_skia);
karandeepb 2016/01/14 04:21:33 I couldn't find a simple enough way to mirror an i
karandeepb 2016/01/18 00:31:18 Done.
159 icon_image_ = gfx::Image(image_skia);
160 }
161 }
162 return icon_image_;
163 }
164
165 } // namespace ui
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698