Skip to content

Commit

Permalink
Add SetWindowAttribute bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
xanderfrangos committed Oct 7, 2024
1 parent 7ec1174 commit 94c367e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/modules/tt-windows-utils/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
},
{
"target_name": "windows_window_material",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"sources": [ "windows_window_material.cc" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ],
},
{
"target_name": "windows_media_status",
"cflags!": [ "-fno-exceptions" ],
Expand Down
16 changes: 16 additions & 0 deletions src/modules/tt-windows-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const WindowUtils = require("bindings")("windows_window_utils");
const PowerEvents = require("bindings")("windows_power_events");
const MediaStatus = require("bindings")("windows_media_status");
const WindowMaterial = require("bindings")("windows_window_material");

module.exports = {
WindowUtils: {
Expand All @@ -21,5 +22,20 @@ module.exports = {
MediaStatus: {
getPlaybackStatus: MediaStatus.getPlaybackStatus,
getPlaybackInfo: MediaStatus.getPlaybackInfo
},
WindowMaterial: {
setWindowMaterial: (hwnd, materialType = 1, cornersType = 2, darkModeSupported = true) => {
WindowMaterial.setWindowMaterial(hwnd, materialType, cornersType, (darkModeSupported ? 1 : 0))
},
setWindowAttribute: WindowMaterial.setWindowAttribute,
setWindowCorners: (hwnd, cornerType = 0) => {
WindowMaterial.setWindowAttribute(hwnd, 33, cornerType)
},
setDarkModeSupported: (hwnd, enabled = 1) => {
WindowMaterial.setWindowAttribute(hwnd, 20, enabled)
},
setTransitionSupported: (hwnd, enabled = 1) => {
WindowMaterial.setWindowAttribute(hwnd, 3, enabled)
}
}
}
35 changes: 35 additions & 0 deletions src/modules/tt-windows-utils/windows_window_material.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <napi.h>
#include <windows.h>
#include <dwmapi.h>
#pragma comment(lib, "dwmapi.lib")

Napi::Boolean SetWindowMaterial(const Napi::CallbackInfo &info) {
Napi::Number hwnd = info[0].As<Napi::Number>();
Napi::Number backdrop = info[1].As<Napi::Number>();
Napi::Number corners = info[2].As<Napi::Number>();
Napi::Number mode = info[3].As<Napi::Number>();
int backdropType = backdrop.Int32Value();
int darkMode = mode.Int32Value();
int borderType = corners.Int32Value();
DwmSetWindowAttribute((HWND) hwnd.Int32Value(), 20, &darkMode, sizeof(darkMode)); // DWMWA_USE_IMMERSIVE_DARK_MODE: 20
DwmSetWindowAttribute((HWND) hwnd.Int32Value(), 38, &backdropType, sizeof(backdropType)); // DWMWA_SYSTEMBACKDROP_TYPE: 38
DwmSetWindowAttribute((HWND) hwnd.Int32Value(), 33, &borderType, sizeof(borderType)); // DWMWA_WINDOW_CORNER_PREFERENCE: 33
return Napi::Boolean::New(info.Env(), true);
}

Napi::Boolean SetWindowAttribute(const Napi::CallbackInfo &info) {
Napi::Number hwnd = info[0].As<Napi::Number>();
Napi::Number type = info[1].As<Napi::Number>();
Napi::Number value = info[2].As<Napi::Number>();
int intval = value.Int32Value();
DwmSetWindowAttribute((HWND) hwnd.Int32Value(), (DWORD) type.Int32Value(), &intval, sizeof(intval));
return Napi::Boolean::New(info.Env(), true);
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "setWindowMaterial"), Napi::Function::New(env, SetWindowMaterial));
exports.Set(Napi::String::New(env, "setWindowAttribute"), Napi::Function::New(env, SetWindowAttribute));
return exports;
}

NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);

0 comments on commit 94c367e

Please sign in to comment.