@@ -13,11 +13,6 @@ struct Process {
1313 std::string path;
1414};
1515
16- struct Window {
17- Process process;
18- int64_t id;
19- };
20-
2116template <typename T>
2217T getValueFromCallbackData (const Napi::CallbackInfo& info, unsigned handleIndex) {
2318 return reinterpret_cast <T> (info[handleIndex].As <Napi::Number> ().Int64Value ());
@@ -58,11 +53,10 @@ Napi::Number getActiveWindow (const Napi::CallbackInfo& info) {
5853 return Napi::Number::New (env, reinterpret_cast <int64_t > (handle));
5954}
6055
61- std::vector<Window > _windows;
56+ std::vector<int64_t > _windows;
6257
6358BOOL CALLBACK EnumWindowsProc (HWND hwnd, LPARAM lparam) {
64- auto process = getWindowProcess (hwnd);
65- _windows.push_back ({ process, reinterpret_cast <int64_t > (hwnd) });
59+ _windows.push_back (reinterpret_cast <int64_t > (hwnd));
6660 return TRUE ;
6761}
6862
@@ -75,13 +69,38 @@ Napi::Array getWindows (const Napi::CallbackInfo& info) {
7569 auto arr = Napi::Array::New (env);
7670 auto i = 0 ;
7771 for (auto _win : _windows) {
78- if (_win.process .path .empty ()) continue ;
79- arr.Set (i++, Napi::Number::New (env, _win.id ));
72+ arr.Set (i++, Napi::Number::New (env, _win));
8073 }
8174
8275 return arr;
8376}
8477
78+ std::vector<int64_t > _monitors;
79+
80+ BOOL CALLBACK EnumMonitorsProc (HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
81+ _monitors.push_back (reinterpret_cast <int64_t > (hMonitor));
82+ return TRUE ;
83+ }
84+
85+ Napi::Array getMonitors (const Napi::CallbackInfo& info) {
86+ Napi::Env env{ info.Env () };
87+
88+ _monitors.clear ();
89+ if (EnumDisplayMonitors (NULL , NULL , &EnumMonitorsProc, NULL )) {
90+ auto arr = Napi::Array::New (env);
91+ auto i = 0 ;
92+
93+ for (auto _mon : _monitors) {
94+
95+ arr.Set (i++, Napi::Number::New (env, _mon));
96+ }
97+
98+ return arr;
99+ }
100+
101+ return Napi::Array::New (env);
102+ }
103+
85104Napi::Number getMonitorFromWindow (const Napi::CallbackInfo& info) {
86105 Napi::Env env{ info.Env () };
87106
@@ -247,6 +266,38 @@ Napi::Boolean isVisible (const Napi::CallbackInfo& info) {
247266 return Napi::Boolean::New (env, IsWindowVisible (handle));
248267}
249268
269+ Napi::Object getMonitorInfo (const Napi::CallbackInfo& info) {
270+ Napi::Env env{ info.Env () };
271+
272+ auto handle{ getValueFromCallbackData<HMONITOR> (info, 0 ) };
273+
274+ MONITORINFO mInfo ;
275+ mInfo .cbSize = sizeof (MONITORINFO);
276+ GetMonitorInfoA (handle, &mInfo );
277+
278+ Napi::Object bounds{ Napi::Object::New (env) };
279+
280+ bounds.Set (" x" , mInfo .rcMonitor .left );
281+ bounds.Set (" y" , mInfo .rcMonitor .top );
282+ bounds.Set (" width" , mInfo .rcMonitor .right - mInfo .rcMonitor .left );
283+ bounds.Set (" height" , mInfo .rcMonitor .bottom - mInfo .rcMonitor .top );
284+
285+ Napi::Object workArea{ Napi::Object::New (env) };
286+
287+ workArea.Set (" x" , mInfo .rcWork .left );
288+ workArea.Set (" y" , mInfo .rcWork .top );
289+ workArea.Set (" width" , mInfo .rcWork .right - mInfo .rcWork .left );
290+ workArea.Set (" height" , mInfo .rcWork .bottom - mInfo .rcWork .top );
291+
292+ Napi::Object obj{ Napi::Object::New (env) };
293+
294+ obj.Set (" bounds" , bounds);
295+ obj.Set (" workArea" , workArea);
296+ obj.Set (" isPrimary" , (mInfo .dwFlags & MONITORINFOF_PRIMARY) != 0 );
297+
298+ return obj;
299+ }
300+
250301Napi::Object Init (Napi::Env env, Napi::Object exports) {
251302 exports.Set (Napi::String::New (env, " getActiveWindow" ), Napi::Function::New (env, getActiveWindow));
252303 exports.Set (Napi::String::New (env, " getMonitorFromWindow" ), Napi::Function::New (env, getMonitorFromWindow));
@@ -263,7 +314,9 @@ Napi::Object Init (Napi::Env env, Napi::Object exports) {
263314 Napi::Function::New (env, toggleWindowTransparency));
264315 exports.Set (Napi::String::New (env, " setWindowOwner" ), Napi::Function::New (env, setWindowOwner));
265316 exports.Set (Napi::String::New (env, " getWindowInfo" ), Napi::Function::New (env, getWindowInfo));
317+ exports.Set (Napi::String::New (env, " getMonitorInfo" ), Napi::Function::New (env, getMonitorInfo));
266318 exports.Set (Napi::String::New (env, " getWindows" ), Napi::Function::New (env, getWindows));
319+ exports.Set (Napi::String::New (env, " getMonitors" ), Napi::Function::New (env, getMonitors));
267320
268321 return exports;
269322}
0 commit comments