-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicon-object.cc
205 lines (179 loc) · 6.32 KB
/
icon-object.cc
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "icon-object.hh"
#include "unique.hh"
napi_status napi_get_value(napi_env env, napi_value value,
icon_size_t* result) {
NAPI_RETURN_IF_NOT_OK(
napi_get_named_property(env, value, "width", &result->width));
NAPI_RETURN_IF_NOT_OK(
napi_get_named_property(env, value, "height", &result->height));
return napi_ok;
}
napi_status load_icon(napi_env env, HINSTANCE hinstance, LPCWSTR path,
icon_size_t size, DWORD flags, napi_value* result) {
auto env_data = get_env_data(env);
auto shared = (flags & LR_SHARED) != 0;
auto icon = (HICON)LoadImageW(hinstance, path, IMAGE_ICON, size.width,
size.height, flags);
if (!icon) {
napi_throw_win32_error(env, "LoadImageW");
return napi_pending_exception;
}
Unique<HICON, DestroyIcon> owned_icon = shared ? icon : nullptr;
NAPI_RETURN_IF_NOT_OK(
IconObject::new_instance(env, env_data->icon_constructor, result));
IconObject* wrapped = nullptr;
NAPI_RETURN_IF_NOT_OK(IconObject::try_unwrap(env, *result, &wrapped));
wrapped->icon = icon;
wrapped->shared = shared;
wrapped->width = size.width;
wrapped->height = size.height;
return napi_ok;
}
napi_value export_Icon_loadBuiltin(napi_env env, napi_callback_info info) {
uint32_t id;
icon_size_t size;
NAPI_RETURN_NULL_IF_NOT_OK(napi_get_required_args(env, info, &id, &size));
napi_value result;
NAPI_RETURN_NULL_IF_NOT_OK(
load_icon(env, nullptr, MAKEINTRESOURCE(id), size, LR_SHARED, &result));
return result;
}
struct ResourceId {
LPWSTR id;
std::wstring storage;
ResourceId& operator=(uint32_t new_id) {
if (IS_INTRESOURCE(new_id)) {
id = MAKEINTRESOURCE(new_id);
storage.clear();
} else {
storage.assign(MAKEINTRESOURCE(new_id));
id = storage.data();
}
return *this;
}
ResourceId& operator=(LPWSTR new_id) {
if (IS_INTRESOURCE(new_id)) {
id = new_id;
} else {
storage.assign(new_id);
id = storage.data();
}
return *this;
}
operator LPWSTR() const {
return id;
}
};
napi_value export_Icon_loadResource(napi_env env, napi_callback_info info) {
icon_size_t size;
std::optional<uint32_t> id;
std::optional<std::wstring> path;
NAPI_RETURN_NULL_IF_NOT_OK(napi_get_args(env, info, 1, &size, &id, &path));
Unique<HMODULE, FreeLibrary> library;
HINSTANCE hinstance;
if (!path) {
hinstance = GetModuleHandle(nullptr);
} else {
library = hinstance = LoadLibraryExW(
path->c_str(), nullptr,
LOAD_LIBRARY_AS_DATAFILE | LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if (!hinstance) {
napi_throw_win32_error(env, "LoadLibraryExW");
return nullptr;
}
}
ResourceId resource;
if (id) {
resource = id.value();
} else {
// Use first RT_GROUP_ICON, the same as Windows uses for an .exe icon.
EnumResourceNamesW(
hinstance, RT_GROUP_ICON,
[](HMODULE hModule, LPCWSTR lpType, LPWSTR lpName, LONG_PTR lParam) {
*reinterpret_cast<ResourceId*>(lParam) = lpName;
return FALSE;
},
reinterpret_cast<LONG_PTR>(&resource));
if (auto error = GetLastError(); error != ERROR_RESOURCE_ENUM_USER_STOP) {
napi_throw_win32_error(env, "EnumResourceNamesW", error);
return nullptr;
}
}
napi_value result;
NAPI_RETURN_NULL_IF_NOT_OK(
load_icon(env, hinstance, resource, size, 0, &result));
return result;
}
napi_value export_Icon_loadFile(napi_env env, napi_callback_info info) {
std::wstring path;
icon_size_t size;
NAPI_RETURN_NULL_IF_NOT_OK(napi_get_required_args(env, info, &path, &size));
napi_value result;
NAPI_RETURN_NULL_IF_NOT_OK(
load_icon(env, nullptr, path.c_str(), size, LR_LOADFROMFILE, &result));
return result;
}
napi_property_descriptor system_metric_property(
const char* utf8name, int metric,
napi_property_attributes attributes = napi_enumerable) {
auto getter = [](napi_env env, napi_callback_info info) -> napi_value {
void* data;
NAPI_THROW_RETURN_NULL_IF_NOT_OK(
env, napi_get_cb_info(env, info, nullptr, nullptr, nullptr, &data));
auto value = GetSystemMetrics((int)(size_t)data);
napi_value result;
NAPI_THROW_RETURN_NULL_IF_NOT_OK(env, napi_create(env, value, &result));
return result;
};
return napi_getter_property(utf8name, getter, attributes,
(void*)(size_t)metric);
}
template <auto IconObject::*member>
napi_property_descriptor member_getter_property(
const char* utf8name,
napi_property_attributes attributes = napi_enumerable) {
auto getter = [](napi_env env, napi_callback_info info) -> napi_value {
IconObject* icon_object = nullptr;
NAPI_RETURN_NULL_IF_NOT_OK(napi_get_this_arg(env, info, &icon_object));
napi_value result;
NAPI_THROW_RETURN_NULL_IF_NOT_OK(
env, napi_create(env, icon_object->*member, &result));
return result;
};
return napi_getter_property(utf8name, getter, attributes, nullptr);
}
IconObject::~IconObject() {
if (!shared) {
DestroyIcon(icon);
}
}
napi_status IconObject::define_class(EnvData* env_data,
napi_value* constructor_value) {
auto env = env_data->env;
napi_value small_value, large_value;
NAPI_RETURN_IF_NOT_OK(napi_create_object(
env, &small_value,
{
system_metric_property("width", SM_CXSMICON, napi_static),
system_metric_property("height", SM_CYSMICON, napi_static),
}));
NAPI_RETURN_IF_NOT_OK(napi_create_object(
env, &large_value,
{
system_metric_property("width", SM_CXICON, napi_static),
system_metric_property("height", SM_CYICON, napi_static),
}));
return NapiWrapped::define_class(
env, "Icon", constructor_value, &env_data->icon_constructor,
{
napi_value_property("small", small_value, napi_static),
napi_value_property("large", large_value, napi_static),
napi_method_property("loadResource", export_Icon_loadResource,
napi_static),
napi_method_property("loadBuiltin", export_Icon_loadBuiltin,
napi_static),
napi_method_property("loadFile", export_Icon_loadFile, napi_static),
member_getter_property<&IconObject::width>("width"),
member_getter_property<&IconObject::height>("height"),
});
}