Skip to content

Commit

Permalink
Apply theme after reboot
Browse files Browse the repository at this point in the history
  • Loading branch information
MishaProductions committed Mar 28, 2024
1 parent be2fc7f commit 614a2a8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
26 changes: 16 additions & 10 deletions Rectify11Installer/Core/Backend/Themes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,30 +541,36 @@ private static bool UninstallMfe()
private static void ApplyScheme()
{
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce", true);
if (key != null)

var config = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Rectify11", true);
if (key != null && config != null)
{
// The goal here is to apply the theme and visual style. For some reason, running the .theme
// file only applies cursors, wallpaper, etc but does not change the visual style. Because of that,
// RectifyStart applies it on the next boot after theme patcher is installed. It is ran on the next boot
// to ensure that the UxTheme patcher is running.
if (InstallOptions.ThemeLight)
{
key.SetValue("ApplyR11Theme", Path.Combine(Variables.Windir, "Resources", "Themes", "lightrectified.theme"), RegistryValueKind.String);
RectifyThemeUtil.Utility.ApplyTheme("Rectify11 light theme");
Process.Start(Path.Combine(Variables.Windir, "Resources", "Themes", "lightrectified.theme"));
config.SetValue("ApplyThemeOnNextRun", "Rectify11 light theme");
}
else if (InstallOptions.ThemeDark)
{
key.SetValue("ApplyR11Theme", Path.Combine(Variables.Windir, "Resources", "Themes", "darkrectified.theme"), RegistryValueKind.String);
RectifyThemeUtil.Utility.ApplyTheme("Rectify11 dark theme");
Process.Start(Path.Combine(Variables.Windir, "Resources", "Themes", "darkrectified.theme"));
config.SetValue("ApplyThemeOnNextRun", "Rectify11 dark theme");
}
else if (InstallOptions.ThemePDark)
{
key.SetValue("ApplyR11Theme", Path.Combine(Variables.Windir, "Resources", "Themes", "darkpartial.theme"), RegistryValueKind.String);
RectifyThemeUtil.Utility.ApplyTheme("Rectify11 partial dark theme");
Process.Start(Path.Combine(Variables.Windir, "Resources", "Themes", "darkpartial.theme"));
config.SetValue("ApplyThemeOnNextRun", "Rectify11 partial dark theme");
}
else
{
key.SetValue("ApplyR11Theme", Path.Combine(Variables.Windir, "Resources", "Themes", "black.theme"), RegistryValueKind.String);
RectifyThemeUtil.Utility.ApplyTheme("Rectify11 Dark theme with Mica");
Process.Start(Path.Combine(Variables.Windir, "Resources", "Themes", "black.theme"));
config.SetValue("ApplyThemeOnNextRun", "Rectify11 Dark theme with Mica");
}


config.Close();
key.SetValue("DeleteJunk", "rmdir /s /q " + Path.Combine(Environment.SpecialFolder.LocalApplicationData.ToString(), "junk"), RegistryValueKind.String);
key.Close();
}
Expand Down
61 changes: 60 additions & 1 deletion RectifyStart/RectifyStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "framework.h"
#include "RectifyStart.h"
#include "..\RectifyControlPanel2\dui70\DirectUI\DirectUI.h"
#include "..\RectifyControlPanel2\Rectify11CPL\Guid.h"
#include "..\RectifyControlPanel2\Rectify11CPL\IRectifyUtil_h.h"
#pragma comment(lib,"dui70.lib")
using namespace DirectUI;

Expand All @@ -15,6 +17,7 @@ WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
HWNDElement* hwnd_element = NULL;
NativeHWNDHost* pwnd = NULL;
static HWND hwnd = NULL;
struct EventListener : public IElementListener {

using handler_t = std::function<void(Element*, Event*)>;
Expand Down Expand Up @@ -85,6 +88,59 @@ bool GetStartup()
return false;
}

void ApplyThemeIfNeeded()
{
// Apply theme after rectify11 was installed. For more details see Rectify11Installer/Core/Backend/Themes.cs
HKEY Rectify11;
if (RegCreateKey(HKEY_CURRENT_USER, TEXT("SOFTWARE\\Rectify11"), &Rectify11))
{
MessageBox(hwnd, TEXT("Failed to create rectify11 key"), TEXT("RectifyStart application"), MB_ICONERROR);
return;
}

// Buffer to store string read from registry
TCHAR szValue[1024];
DWORD cbValueLength = sizeof(szValue);

// Query string value
LSTATUS hr = RegQueryValueEx(
Rectify11,
TEXT("ApplyThemeOnNextRun"),
NULL,
NULL,
reinterpret_cast<LPBYTE>(&szValue),
&cbValueLength);
if (hr != ERROR_SUCCESS)
{
if (hr != ERROR_FILE_NOT_FOUND)
{
MessageBox(hwnd, TEXT("Failed to query ApplyThemeOnNextRun registry value. The rectify11 theme may not be applied correctly."), TEXT("RectifyStart application"), MB_ICONERROR);
}
return;
}

IRectifyUtil* util;
hr = CoCreateInstance(CLSID_CRectifyUtil, NULL, CLSCTX_INPROC_SERVER, IID_IRectifyUtil,
reinterpret_cast<void**>(&util));

if (util == NULL || hr != S_OK)
{
MessageBox(hwnd, TEXT("Failed to create CRectifyUtil COM Object when trying to apply theme. Please rerun the Rectify11 installer. If that doesn't work, report an issue on github."), TEXT("RectifyStart application: Critical error"), MB_ICONERROR);
return;
}

hr = util->ApplyTheme(szValue);
if (FAILED(hr))
{
MessageBox(hwnd, TEXT("Failed to apply the rectify11 theme."), TEXT("RectifyStart application"), MB_ICONERROR);
return;
}

util->Release();

// we are done
RegDeleteValue(Rectify11, TEXT("ApplyThemeOnNextRun"));
}
void HandleCloseButton(Element* elem, Event* iev)
{
if (iev->type == TouchButton::Click)
Expand Down Expand Up @@ -139,7 +195,10 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
CW_USEDEFAULT, CW_USEDEFAULT, 880, 720,
0, WS_EX_TOOLWINDOW | WS_VISIBLE | WS_SYSMENU, 0, &pwnd);

static HWND hwnd = pwnd->GetHWND();
hwnd = pwnd->GetHWND();

// apply theme after installation
ApplyThemeIfNeeded();

// Center the window on the screen
RECT rc;
Expand Down

0 comments on commit 614a2a8

Please sign in to comment.