MKANET

vibrancy.cc

Sep 15th, 2023
1,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include <napi.h>
  2. #include <dwmapi.h>
  3.  
  4. enum AccentState {
  5.     ACCENT_DISABLED = 0,
  6.     ACCENT_ENABLE_GRADIENT = 1,
  7.     ACCENT_ENABLE_TRANSPARENTGRADIENT = 2,
  8.     ACCENT_ENABLE_BLURBEHIND = 3,
  9.     ACCENT_ENABLE_ACRYLICBLURBEHIND = 4,
  10.     ACCENT_ENABLE_HOSTBACKDROP = 5, // RS5 1809
  11.     ACCENT_INVALID_STATE = 6
  12. };
  13.  
  14. enum WindowCompositionAttribute {
  15.     WCA_ACCENT_POLICY = 19
  16. };
  17.  
  18. struct AccentPolicy {
  19.     AccentState accentState;
  20.     int accentFlags;
  21.     int gradientColor;
  22.     int animationId;
  23. };
  24.  
  25. struct WindowCompositionAttributeData {
  26.     WindowCompositionAttribute attribute;
  27.     PVOID pData;
  28.     ULONG dataSize;
  29. };
  30.  
  31. typedef BOOL(WINAPI *pSetWindowCompositionAttribute)(HWND, WindowCompositionAttributeData*);
  32.  
  33. const HINSTANCE hModule = LoadLibrary(TEXT("user32.dll"));
  34.  
  35. void setVibrancy(const Napi::CallbackInfo &info) {
  36.     Napi::Env env = info.Env();
  37.     try {
  38.         if (info.Length() == 0) {
  39.             Napi::TypeError::New(env, "WINDOW_NOT_GIVEN").ThrowAsJavaScriptException();
  40.             return;
  41.         }
  42.         if (info.Length() != 6) {
  43.             Napi::TypeError::New(env, "PARAMETER_ERROR").ThrowAsJavaScriptException();
  44.             return;
  45.         }
  46.         if (!info[0].IsNumber()) {
  47.             Napi::TypeError::New(env, "UNKNOWN").ThrowAsJavaScriptException();
  48.             return;
  49.         }
  50.         HWND hWnd = (HWND)info[0].As<Napi::Number>().Int64Value();
  51.         int isRS4OrGreater = info[1].As<Napi::Number>().Int32Value();
  52.  
  53.         int redValue = info[2].As<Napi::Number>().Int32Value();
  54.         int greenValue = info[3].As<Napi::Number>().Int32Value();
  55.         int blueValue = info[4].As<Napi::Number>().Int32Value();
  56.         int alphaValue = info[5].As<Napi::Number>().Int32Value();
  57.  
  58.         if (hModule) {
  59.             // Get the function pointer
  60.             const pSetWindowCompositionAttribute SetWindowCompositionAttribute = (pSetWindowCompositionAttribute)GetProcAddress(
  61.                 hModule, "SetWindowCompositionAttribute");
  62.             if (SetWindowCompositionAttribute) {
  63.                 // Calculate gradient color
  64.                 int gradientColor = (alphaValue << 24) + (blueValue << 16) + (greenValue << 8) + (redValue);
  65.                 // Determine blur type based on OS version
  66.                 AccentState blurType = isRS4OrGreater == 1 ? ACCENT_ENABLE_ACRYLICBLURBEHIND : ACCENT_ENABLE_BLURBEHIND;
  67.                 // Configure the AccentPolicy
  68.                 AccentPolicy policy = { blurType, 2, gradientColor, 0 };
  69.                 // Configure WindowCompositionAttributeData
  70.                 WindowCompositionAttributeData data = { WCA_ACCENT_POLICY, &policy, sizeof(AccentPolicy) };
  71.                 // Apply the settings
  72.                 SetWindowCompositionAttribute(hWnd, &data);
  73.             }
  74.             else {
  75.                 // Error handling if function not found
  76.                 Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
  77.                 return;
  78.             }
  79.             // Free the library
  80.             FreeLibrary(hModule);
  81.         }
  82.         else {
  83.             // Error handling if library not loaded
  84.             Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
  85.             return;
  86.         }
  87.     }
  88.     catch (const char* ex) {
  89.         // Error handling for unknown exceptions
  90.         Napi::Error::New(env, "UNKNOWN").ThrowAsJavaScriptException();
  91.     }
  92. }
  93.  
  94. void disableVibrancy(const Napi::CallbackInfo& info) {
  95.     Napi::Env env = info.Env();
  96.     try {
  97.         if (info.Length() != 1) {
  98.             Napi::TypeError::New(env, "WINDOW_NOT_GIVEN").ThrowAsJavaScriptException();
  99.             return;
  100.         }
  101.         if (!info[0].IsNumber()) {
  102.             Napi::TypeError::New(env, "UNKNOWN").ThrowAsJavaScriptException();
  103.             return;
  104.         }
  105.         HWND hWnd = (HWND)info[0].As<Napi::Number>().Int64Value();
  106.         if (hModule) {
  107.             // Get the function pointer
  108.             const pSetWindowCompositionAttribute SetWindowCompositionAttribute = (pSetWindowCompositionAttribute)GetProcAddress(
  109.                 hModule, "SetWindowCompositionAttribute");
  110.             if (SetWindowCompositionAttribute) {
  111.                 // Configure the AccentPolicy to disable blur
  112.                 AccentPolicy policy = { ACCENT_DISABLED, 0, 0, 0 };
  113.                 // Configure WindowCompositionAttributeData
  114.                 WindowCompositionAttributeData data = { WCA_ACCENT_POLICY, &policy, sizeof(AccentPolicy) };
  115.                 // Apply the settings to disable vibrancy
  116.                 SetWindowCompositionAttribute(hWnd, &data);
  117.             }
  118.             else {
  119.                 // Error handling if function not found
  120.                 Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
  121.                 return;
  122.             }
  123.             // Free the library
  124.             FreeLibrary(hModule);
  125.         }
  126.         else {
  127.             // Error handling if library not loaded
  128.             Napi::Error::New(env, "FAIL_LOAD_DLL").ThrowAsJavaScriptException();
  129.             return;
  130.         }
  131.     }
  132.     catch (const char* ex) {
  133.         // Error handling for unknown exceptions
  134.         Napi::Error::New(env, "UNKNOWN").ThrowAsJavaScriptException();
  135.     }
  136. }
  137.  
  138. Napi::Object Init(Napi::Env env, Napi::Object exports) {
  139.     // Export the setVibrancy and disableVibrancy functions
  140.     exports.Set(Napi::String::New(env, "setVibrancy"),
  141.                 Napi::Function::New(env, setVibrancy));
  142.     exports.Set(Napi::String::New(env, "disableVibrancy"),
  143.                 Napi::Function::New(env, disableVibrancy));
  144.     return exports;
  145. }
  146.  
  147. // Define the module
  148. NODE_API_MODULE(vibrancy, Init)
  149.  
Advertisement
Add Comment
Please, Sign In to add comment