Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Implement ThemeListener for winui3 #445

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 8 additions & 1 deletion components/Helpers/src/CommunityToolkit.WinUI.Helpers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(ToolkitExtensionsSourceProject)"/>
<ProjectReference Include="$(ToolkitExtensionsSourceProject)" />
</ItemGroup>

<ItemGroup>
Expand All @@ -20,6 +20,13 @@
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<!-- Sets this up as a toolkit component's source project -->
<Import Project="$(ToolingDirectory)\ToolkitComponent.SourceProject.props" />
Expand Down
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>
9 changes: 9 additions & 0 deletions components/Helpers/src/NativeMethods.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json",
"allowMarshaling": false,
"comInterop": {
"preserveSigMethods": [
"*"
]
}
}
4 changes: 4 additions & 0 deletions components/Helpers/src/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
RegisterClassEx
DefWindowProc
CreateWindowEx
WM_SETTINGCHANGE
11 changes: 11 additions & 0 deletions components/Helpers/src/ThemeListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

using Windows.Foundation.Metadata;
using Windows.UI.ViewManagement;
using CommunityToolkit.WinUI.HelpersRns;


#if !WINAPPSDK
using Windows.System;
Expand Down Expand Up @@ -83,6 +85,15 @@ public ThemeListener(DispatcherQueue? dispatcherQueue = null)

Window.Current.CoreWindow.Activated += CoreWindow_Activated;
}
#if WINAPPSDK
ThemeListenerHelperWindow.Instance.ThemeChanged += this.Instance_ThemeChanged;
#endif
}

private void Instance_ThemeChanged(ApplicationTheme theme)
{
CurrentTheme = theme;
ThemeChanged?.Invoke(this);
}

private async void Accessible_HighContrastChanged(AccessibilitySettings sender, object args)
Expand Down
79 changes: 79 additions & 0 deletions components/Helpers/src/ThemeListenerHelperWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Microsoft.Win32;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
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();

public event ThemeChangedHandler? ThemeChanged;

private static string s_className = "ThemeListenerHelperWindow";

private HWND m_hwnd;

private ThemeListenerHelperWindow()
{
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)
{
wcx.lpszClassName = pClassName;
wcx.lpfnWndProc = &WndProc;
if (PInvoke.RegisterClassEx(in wcx) is 0)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastPInvokeError());
}
}
}

private static unsafe HWND CreateWindow()
{
HWND hwnd = PInvoke.CreateWindowEx(0, s_className, string.Empty, 0, 0, 0, 0, 0, default, null, null, null);
if (hwnd == HWND.Null)
{
Marshal.ThrowExceptionForHR(Marshal.GetLastPInvokeError());
}
return hwnd;
}

[UnmanagedCallersOnly(CallConvs = [typeof(CallConvStdcall)])]
private static LRESULT WndProc(HWND hWnd, uint msg, WPARAM wParam, LPARAM lParam)
{
if (msg is PInvoke.WM_SETTINGCHANGE)
{
// 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 PInvoke.DefWindowProc(hWnd, msg, wParam, lParam);
}
}
#endif