forked from foxt/node-system-icon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_icon_mac.mm
57 lines (51 loc) · 1.47 KB
/
system_icon_mac.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "system_icon.hpp"
#import <AppKit/AppKit.h>
std::vector<unsigned char> ImageToPng(NSImage* image)
{
auto imageRef = [image CGImageForProposedRect:nil context:nil hints:nil];
auto imageRep = [[NSBitmapImageRep alloc] initWithCGImage:imageRef];
[imageRep setSize:[image size]];
auto imageData =
[imageRep representationUsingType:NSPNGFileType
properties:[[NSDictionary alloc] init]];
auto p = static_cast<const unsigned char*>([imageData bytes]);
return std::vector<unsigned char>{p, p + [imageData length]};
}
NSSize GetSize(IconSize size)
{
switch (size)
{
case IconSize::ExtraSmall:
return NSMakeSize(16, 16);
case IconSize::Small:
return NSMakeSize(32, 32);
case IconSize::Medium:
return NSMakeSize(128, 128);
case IconSize::Large:
return NSMakeSize(256, 256);
case IconSize::ExtraLarge:
return NSMakeSize(512, 512);
}
}
template <>
void SystemIconAsyncWorker<ExtensionTag>::Execute()
{
auto image = [[NSWorkspace sharedWorkspace]
iconForFileType:[NSString stringWithUTF8String:this->name.c_str()]];
[image setSize:GetSize(this->size)];
if (image.valid)
{
this->result = ImageToPng(image);
}
}
template <>
void SystemIconAsyncWorker<PathTag>::Execute()
{
auto image = [[NSWorkspace sharedWorkspace]
iconForFile:[NSString stringWithUTF8String:this->name.c_str()]];
[image setSize:GetSize(this->size)];
if (image.valid)
{
this->result = ImageToPng(image);
}
}