Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/Helpers/src/CommunityToolkit.WinUI.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@

<!-- Sets this up as a toolkit component's source project -->
<Import Project="$(ToolingDirectory)\ToolkitComponent.SourceProject.props" />
<ItemGroup>
<None Remove="NativeMethods.json" />
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant

</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion components/Helpers/src/MultiTarget.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
-->
<MultiTarget>uwp;wasdk;wpf;wasm;linuxgtk;macos;ios;android;</MultiTarget>
</PropertyGroup>
</Project>
</Project>
3 changes: 3 additions & 0 deletions components/Helpers/src/NativeMethods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"allowMarshaling": false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add scheme and preserve sig.

Suggested change
"allowMarshaling": false
"$schema": "https://aka.ms/CsWin32.schema.json",
"allowMarshaling": false,
"comInterop": {
"preserveSigMethods": [
"*"
]
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only use

RegisterClassEx
DefWindowProc
CreateWindowEx
WM_SETTINGCHANGE

Not a single COM interface gets imported.
So I don't think comInterop.preserveSigMethods is needed.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Contributors may use com interfaces for other things. There's no reason to keep it out. Also add the schema.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0x5bfa Done

}
101 changes: 44 additions & 57 deletions components/Helpers/src/ThemeListenerHelperWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,90 +3,77 @@
// See the LICENSE file in the project root for more information.

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.WindowsAndMessaging;

namespace CommunityToolkit.WinUI.HelpersRns;

#if WINAPPSDK

internal class ThemeListenerHelperWindow
{
public delegate void ThemeChangedHandler(ApplicationTheme theme);

public static ThemeListenerHelperWindow Instance = new();
private static string s_className = "ThemeListenerHelperWindow";
private IntPtr m_hwnd;

public event ThemeChangedHandler? ThemeChanged;
delegate Windows.Win32.Foundation.LRESULT WndProc(Windows.Win32.Foundation.HWND hWnd, uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam);
private WndProc m_wnd_proc_delegate;
private void registerClass()

private static string s_className = "ThemeListenerHelperWindow";

private HWND m_hwnd;

private ThemeListenerHelperWindow()
{
unsafe
s_className = "ThemeListenerHelperWindow";
RegisterClass();
m_hwnd = CreateWindow();
}

private static unsafe void RegisterClass()
{
WNDCLASSEXW wcx = default;
wcx.cbSize = (uint)sizeof(WNDCLASSEXW);
fixed (char* pClassName = s_className)
{
var wcx = new Windows.Win32.UI.WindowsAndMessaging.WNDCLASSEXW();
wcx.cbSize = (uint)Marshal.SizeOf(wcx);
fixed (char* pClassName = s_className)
{
wcx.lpszClassName = new Windows.Win32.Foundation.PCWSTR(pClassName);
}
wcx.lpfnWndProc = wndProc;
if (PInvoke.RegisterClassEx(in wcx) == 0)
wcx.lpszClassName = pClassName;
wcx.lpfnWndProc = &WndProc;
if (PInvoke.RegisterClassEx(in wcx) is 0)
{
Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);
Marshal.ThrowExceptionForHR(Marshal.GetLastPInvokeError());
}
}

}

static Windows.Win32.Foundation.LRESULT wndProc(Windows.Win32.Foundation.HWND hWnd, uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam)
private static unsafe HWND CreateWindow()
{
switch (msg)
HWND hwnd = PInvoke.CreateWindowEx(0, s_className, string.Empty, 0, 0, 0, 0, 0, default, null, null, null);
if (hwnd == HWND.Null)
{
case PInvoke.WM_SETTINGCHANGE:
var value = Registry.GetValue(
"""HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize""",
"AppsUseLightTheme",
true
);
if (value != null)
{
Instance.ThemeChanged?.Invoke((int)value == 1 ? ApplicationTheme.Light : ApplicationTheme.Dark);
}
break;
Marshal.ThrowExceptionForHR(Marshal.GetLastPInvokeError());
}
return PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
return hwnd;
}

unsafe private static IntPtr createWindow()
[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static LRESULT WndProc(HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam)
{
var hwnd = PInvoke.CreateWindowEx(
0,
s_className,
"",
0,
0, 0, 0, 0,
new Windows.Win32.Foundation.HWND(),
null,
null,
null);
if (hwnd == IntPtr.Zero)
if (msg is PInvoke.WM_SETTINGCHANGE)
{
Debug.WriteLine(new Win32Exception(Marshal.GetLastWin32Error()).Message);
// REG_DWORD
object? value = Registry.GetValue(
@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize",
"AppsUseLightTheme",
true);

if (value is int dword)
{
Instance.ThemeChanged?.Invoke(dword is 1 ? ApplicationTheme.Light : ApplicationTheme.Dark);
}
}
return hwnd;
}
private ThemeListenerHelperWindow()
{
m_wnd_proc_delegate = wndProc;
s_className = "ThemeListenerHelperWindow";
registerClass();
m_hwnd = createWindow();

return PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
}
}
#endif