Skip to content

Commit fb28902

Browse files
harshit7962singhashish-wpfpchaurasia14Anjalidipeshmsft
authored
Windows 11 Theming Changes (#8870)
* Adding Win11 styles * Working code - Window and Theming API changes. * Cleanup - Removing WindowCornerPreference and minor refactoring * Moving types from public to internal + bug fix for Window caption * Moving types from public to internal * Added Menu.xaml.cs to address x:Class attribute in Menu.xaml * Fix namespaces and assembly names, added textbox xaml * Changed x:Class attribute value (#8607) * Adding win11 styles to RichTextBox and Separator (#8581) * Adding win11 styles to RichTextBox and Separator * Changed the IsReadOnly flag to "False" * Fixed win11 theming changes for ListView and ListViewItem (#8629) * Added win11 styles for ListView and ListViewItem * Updated header information and namespace for controls : Calendar, DataGrid, DatePicker, Expander, MenuItem, RichTextBox, ScrollBar, ScrollViewer, Separator Removing unwanted references * Added null check here on CanExecuteChanged to avoid exception (#8619) * Adding Win11 Styles to Combo box (#8593) * Added win11 styles to ComboBox * Removed TranslateTransform from ComboBox's border * Removing unused references * Resolving Merge Conflicts Resolving Merge Conflicts Removing IconProperty Reference * Import xaml styles in the integrated styles page * Moving ThemesDictionary and ControlsDictionary to parent namespace * Update ApplicationThemeManager.cs to use correct namespace * Add missing FontIcon and ImageIcon classes * Fix for fall back colours for high contrast theme * Making SymbolExtensions internal * Removing extra properties for ribbon * fix(win11theme): ExpandDirection not respected in expander * Fix(win11theme): TabStripPlacement not Respected in tabcontrol * Import FluentWindow and TitleBar * Adding win11 styles to PasswordBox (#8612) * Added TextBox styles Todo: Clear button not showing up * Added styles for PasswordBox Todo: Reveal button functionality not supported * fix(win11theming): Ally and Navigation Issues in DataGrid * Fix(Win11Theming): Adding RepeatButton Styles * Add(Win11Theming): Bold, Italics, Underline, Floater and Paragraph Styles * Add(Win11Theming): DocumentViewer, Hyperlink, List, ResizeGrip and Thumb Styles * Add(Win11Theming): ContentControl, GridSplitter, GroupBox, GroupItem and HeaderedContentControl Styles * Add(Win11Theming): Border, ButtonBase, Control, ItemsControl, TextBoxBase, UserControl Styles * Add(Win11Theming): NavigationWindow Styles * Fixing margins in TreeViewItem * Fix for Mica style dropping on theme change * Fix(Win11Theming): TextBlock Foreground and FontIcon Issue * Fixed Border linear gradient for TextBox and PasswordBox * Fix(Win11Theming): MenuItems Dark Theme Visibility * Fix(Win11Theming): TextBlock Foreground and FontIcon Issue * Fix(Win11Theming): Menu Foreground Issue * Fix(win11theming): MenuBar hover highlighting * Fix(win11theming): MenuItems hover highlighting * Fix(Win11Theme): Add PasswordBox Styles to Resource Dictionary * Update resources on theme change * Accent Updates in SystemResources.cs Signed-off-by: Ashish Kumar Singh <singhashish@microsoft.com> * Refactoring accent colorization part * Changing Accent Color and Theme * Remove use of WinDef classes * Fix (Win11Theming): MainWindow does not load with latest system themes (Accent Colors, Background and System Theme) * Fix (win11theming): Delayed theme change (Optimizing theme change time) * Removed Button.Appearance and introduced Accent Button Style * Made extra converters internal * Enabling High Contrast Switching * Fix(win11theming): dark theme not working properly * Update(Win11Theming): Changing access type of non-essential methods to internal * Removed all chromes from Win11 styles * Removed SystemDropShadowChrome from Win11 style * Remove extra properties from PasswordBox * Removed extra public API changes in TextBox and RichTextBox * Made ClearButtonEnabled property internal in TextBox * Removed ContextMenu and Menu code-behind * Made extra methods internal in TextBox * Refactor(Win11Theming): Reusing currentTheme component and minor fixes * Fix(Win11Theming): Removing unused SystemThemeManager and SystemTheme references * Removed IsTextSelectionEnabled from RichTextBox * Making DataGridCheckboxColumn Style property internal * Revert "Removed ContextMenu and Menu code-behind" This reverts commit c7df285. * Removed ContextMeny and Menu Style code behinds * Removed RelayCommands and WindowCornerPreference * Code Cleanup and Removal of ControlsDictionary * Fix(Win11Theming): Adding Opt-in for New styles and controls * Removing unused ObservedWindow class * Removed FluentWindow and made WindowBackdrop internal. Fixed RTB CM * Fix(Win11Theming): Build Failure and High Contrast Switching * Removing Files/Classes not being compiled * Remove TitleBar and FluentWindow * Fix(Win11Theming): CheckBox Design Issues * Fixed gradient for border brush * Fix(Win11Theming): Removal of unused RDM, changing requested method name, and code refactoring * Removed extra classes and replaced FontIcon with TextBlock * Removed color extension and fixed CheckBox style * Fix(Win11Theming): Discarded need for using theme dictionary i app.xaml while not being respected and minor fixes * Removed DisplayDpi and fixed icon animations * Remove unused styles and methods * Fix(Win11Theming): Get Exact Accent color variations * Removed placeholder from the controls * Removed unnecessary code * Removed extra files and renamed AppInitNewTheme flag * Fix(Win11Theming): Updated Common xaml resources to support exact accent colors * Removed some files and renamed some methods * Fix(Win11Theming): Accent Button incorrect styles * Removed some extra code from styles * Fixed calendar header font weight * Revert "Fixed calendar header font weight" This reverts commit bf57add. * Merge branch 'CompleteWin11Changes' of https://dev.azure.com/dnceng/internal/_git/dotnet-wpf into CompleteWin11Changes * Fix(Win11Theming): ListBox Design Issues * Fix(Win11Theming): Progress Bar's Color Issues * Fix(Win11Theming): ProgressBar Design Issues * Fixed RadioButton disabled state colour * Fix(Win11Design): RadioButton and Checkbox Design Issues * Fix(Win11Theming): Checkbox Glyph Design Issues * Candidate Fix for Button Design Issues * Fix(Win11Theming): Removing Extra Border * Fix(Win11Theming): Dark accent button background color * Fix(Win11Theming): Disbaled Button Design Issues * Extended application based theme switching * Updating Theme name * Fix: Dictionary paths * Remove hard-coded reference * Removing unused Acrylic References * Unsubscribing the Loaded handler for applying window backdrop and glass frame * Update src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/DWMColorization.cs Co-authored-by: lindexi <lindexi@users.noreply.github.com> * Fixing ProgressBar's incorrect brush assignment * Adding a fallback if appsuselightthemekey is not present * Replacing the highcontrast detection * Addressing PR Review Comments : 1. Removed Fonts and Pallete resources 2. Fixed Slider styling 3. Removed extra structs from DwmAPI * Change : Removed Dwmapi.cs and merged it with NativeMethods * Removing fallback accent color variation calculation logic and assigning fixed values * Refactoring WindowBackdrop and removing undocumented APIs (#21) * Trimmed WindowBackdrop code * Removed undocumented DWMWA APIs * Fixed theme apply in child windows * Changes : Reverting decoupling of backdrop and background 2. Updated check to AllowsTransparency for applying backdrop 3. Refactored ThemeColorization * Fixed HighContrast and other review comments * Removing unused dwColorSetIndex and updating default value of dwmValue * Changing GetColorFromPreference arguments * Changing the system accent color to get value from IMCLR_SystemAccent * Fixing Typo * Addressing Review Comments - Part 4 (#23) * Moved Clear command to Editing commands * Added DwmSetWindowAttribute* methods and refactoring * Removed ByteColor struct * Fixed DwmColorization ParseDWordColor * Fixed datagrid checkbox column styling * Renamed WindowBackdropType enum values * Remove button from PasswordBox * Replaced sizeof with Marshal.SizeOf * Fix Border Highlight color on focus * Fix Progress Bar Tracker Height and Color Issues * Update Accent color for some controls in Dark mode * Fix MenuFlyout Issues * Slider accent color and ticks fix * Added animations for mouseover and pressed feedback * Fixes review comments - Refactoring (#25) * Refactored WindowBackdrop, BackgroundManager, ThemeColorization * Fix Window implicit style and Textblock inheritence * Moved WindowBackdrop to OnSourceInitialized and added app context switch for disabling backdrop * Initial Accent Color update fix * Added initialization in ThemeManager. Added comments * Added UISettings to handle accent color fetching (#34) * Added UISettings RCW * Made UISettings handle fallback * Fixes UISetting Initialization * Renamed style names (#40) Co-authored-by: dipeshmsft <dipeshkumar@microsoft.com> * ListBox and Slider Design Fix (#41) * Updating ListBox design * Fixed Slider selected range * Fixing ListBox style colors * Fixing selected range offset issue --------- Co-authored-by: Rishabh Chauhan <rchauhan@microsoft.com> Co-authored-by: dipeshmsft <dipeshkumar@microsoft.com> * Updatating OS version check --------- Co-authored-by: dipeshmsft <dipeshkumar@microsoft.com> * Removed extra MenuItem styles (#43) * Made Font Names Dynamic (#45) * Removed TextBlock Style (#44) * Added comments for new default font size * Modified control elevation brush and button style (#46) * Fix control elevation brush (#47) * Renaming the theme to Fluent (#38) * Correcting FluentWindows path of fonts file --------- Co-authored-by: dipeshmsft <dipeshkumar@microsoft.com> --------- Signed-off-by: Ashish Kumar Singh <singhashish@microsoft.com> Co-authored-by: Ashish Kumar Singh <SINGHASHISH@MICROSOFT.COM> Co-authored-by: Pankaj Chaurasia <pchaurasia@microsoft.com> Co-authored-by: Anjali <anjalisheel@microsoft.com> Co-authored-by: dipeshmsft <dipeshkumar@microsoft.com> Co-authored-by: Rishabh Chauhan <rchauhan@microsoft.com> Co-authored-by: lindexi <lindexi@users.noreply.github.com> Co-authored-by: Dipesh Kumar <85861525+dipeshmsft@users.noreply.github.com> Co-authored-by: Rishabh Chauhan <107022063+rchauhan18@users.noreply.github.com>
1 parent 19722f3 commit fb28902

File tree

85 files changed

+12418
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+12418
-56
lines changed

Documentation/projects-and-assemblies.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ The following assemblies are being produced today:
118118
| PresentationFramework.dll
119119
| PresentationFramework.Luna.dll
120120
| PresentationFramework.Luna.pdb
121-
| PresentationFramework.pdb
121+
| PresentationFramework.pdb
122122
| PresentationFramework.Royale.dll
123123
| PresentationFramework.Royale.pdb
124+
| PresentationFramework.Fluent.dll
125+
| PresentationFramework.Fluent.pdb
124126
| PresentationUI.dll
125127
| PresentationUI.pdb
126128
| ReachFramework.dll
@@ -452,7 +454,8 @@ src\Themes\PresentationFramework.Luna\PresentationFramework.Luna.csproj
452454
src\Themes\PresentationFramework.Luna\ref\PresentationFramework.Luna-ref.csproj
453455
src\Themes\PresentationFramework.Royale\PresentationFramework.Royale.csproj
454456
src\Themes\PresentationFramework.Royale\ref\PresentationFramework.Royale-ref.csproj
455-
457+
src\Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj
458+
src\Themes\PresentationFramework.Fluent\ref\PresentationFramework.Fluent-ref.csproj
456459
src\UIAutomation\UIAutomationClient\UIAutomationClient.csproj
457460
src\UIAutomation\UIAutomationClient\ref\UIAutomationClient-ref.csproj
458461
src\UIAutomation\UIAutomationClientSideProviders\UIAutomationClientSideProviders.csproj

Microsoft.Dotnet.Wpf.sln

+38
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTests", "UnitTests", "{
249249
EndProject
250250
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Xaml.Tests", "src\Microsoft.DotNet.Wpf\tests\UnitTests\System.Xaml.Tests\System.Xaml.Tests.csproj", "{B2F2A89C-55C9-41B1-A645-0948609BD8BE}"
251251
EndProject
252+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PresentationFramework.Fluent", "src\Microsoft.DotNet.Wpf\src\Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj", "{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}"
253+
EndProject
254+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PresentationFramework.Fluent-ref", "src\Microsoft.DotNet.Wpf\src\Themes\PresentationFramework.Fluent\ref\PresentationFramework.Fluent-ref.csproj", "{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}"
255+
EndProject
252256
Global
253257
GlobalSection(SolutionConfigurationPlatforms) = preSolution
254258
Debug|Any CPU = Debug|Any CPU
@@ -1942,6 +1946,38 @@ Global
19421946
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.ActiveCfg = Release|Win32
19431947
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.Build.0 = Release|Win32
19441948
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.Deploy.0 = Release|Win32
1949+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1950+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
1951+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|ARM64.ActiveCfg = Debug|arm64
1952+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|ARM64.Build.0 = Debug|arm64
1953+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x64.ActiveCfg = Debug|x64
1954+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x64.Build.0 = Debug|x64
1955+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x86.ActiveCfg = Debug|Any CPU
1956+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x86.Build.0 = Debug|Any CPU
1957+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
1958+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|Any CPU.Build.0 = Release|Any CPU
1959+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|ARM64.ActiveCfg = Release|arm64
1960+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|ARM64.Build.0 = Release|arm64
1961+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x64.ActiveCfg = Release|x64
1962+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x64.Build.0 = Release|x64
1963+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x86.ActiveCfg = Release|Any CPU
1964+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x86.Build.0 = Release|Any CPU
1965+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1966+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
1967+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|ARM64.ActiveCfg = Debug|arm64
1968+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|ARM64.Build.0 = Debug|arm64
1969+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x64.ActiveCfg = Debug|x64
1970+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x64.Build.0 = Debug|x64
1971+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x86.ActiveCfg = Debug|Any CPU
1972+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x86.Build.0 = Debug|Any CPU
1973+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
1974+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|Any CPU.Build.0 = Release|Any CPU
1975+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|ARM64.ActiveCfg = Release|arm64
1976+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|ARM64.Build.0 = Release|arm64
1977+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x64.ActiveCfg = Release|x64
1978+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x64.Build.0 = Release|x64
1979+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x86.ActiveCfg = Release|Any CPU
1980+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x86.Build.0 = Release|Any CPU
19451981
EndGlobalSection
19461982
GlobalSection(SolutionProperties) = preSolution
19471983
HideSolutionNode = FALSE
@@ -2062,6 +2098,8 @@ Global
20622098
{34B64A4A-7C83-4F92-8C47-80E9FA10D660} = {C9ECC1A1-917F-43D3-B340-2035B4938507}
20632099
{A48B585E-6AB0-4F8D-8484-77F37CB44437} = {34B64A4A-7C83-4F92-8C47-80E9FA10D660}
20642100
{B2F2A89C-55C9-41B1-A645-0948609BD8BE} = {A48B585E-6AB0-4F8D-8484-77F37CB44437}
2101+
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9} = {5ACFB055-649D-4A01-98C2-B0BFE7E543D6}
2102+
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD} = {60F4058B-D35B-42D2-B276-D44B3AC579BD}
20652103
EndGlobalSection
20662104
GlobalSection(ExtensibilityGlobals) = postSolution
20672105
SolutionGuid = {B4340004-DAC0-497D-B69D-CFA7CD93F567}

eng/WpfArcadeSdk/tools/GenApi.props

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
PresentationFramework.Classic;
1111
PresentationFramework.Luna;
1212
PresentationFramework.Royale;
13+
PresentationFramework.Fluent;
1314
ReachFramework;
1415
System.Windows.Controls.Ribbon;
1516
System.Windows.Input.Manipulations;

eng/WpfArcadeSdk/tools/ShippingProjects.props

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
PresentationFramework.Classic;
2929
PresentationFramework.Luna;
3030
PresentationFramework.Royale;
31+
PresentationFramework.Fluent;
3132
WindowsFormsIntegration;
3233
PenImc;
3334
wpfgfx;
@@ -98,6 +99,7 @@
9899
PresentationFramework.Classic-ref;
99100
PresentationFramework.Luna-ref;
100101
PresentationFramework.Royale-ref;
102+
PresentationFramework.Fluent-ref;
101103
PresentationUI-ref;
102104
System.Printing-ref;
103105
System.Windows.Controls.Ribbon-ref;
@@ -136,6 +138,8 @@
136138
PresentationFramework.Luna-ref;
137139
PresentationFramework.Royale;
138140
PresentationFramework.Royale-ref;
141+
PresentationFramework.Fluent;
142+
PresentationFramework.Fluent-ref;
139143
PresentationUI;
140144
ReachFramework;
141145
ReachFramework-ref;

eng/WpfArcadeSdk/tools/WpfProjectReference.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<WpfProjectPath Include="PresentationFramework.Classic" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Classic\PresentationFramework.Classic.csproj" />
4848
<WpfProjectPath Include="PresentationFramework.Luna" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Luna\PresentationFramework.Luna.csproj" />
4949
<WpfProjectPath Include="PresentationFramework.Royale" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Royale\PresentationFramework.Royale.csproj" />
50-
50+
<WpfProjectPath Include="PresentationFramework.Fluent" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj" />
5151
<WpfProjectPath Include="UIAutomationTypes" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationTypes\UIAutomationTypes.csproj" />
5252
<WpfProjectPath Include="UIAutomationProvider" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationProvider\UIAutomationProvider.csproj" />
5353
<WpfProjectPath Include="UIAutomationClient" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationClient\UIAutomationClient.csproj" />

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/WindowsRuntime/Windows/UI/ViewManagement/NativeMethods.cs

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ internal static extern unsafe int WindowsCreateString([MarshalAs(UnmanagedType.L
3131
[DllImport(DllImport.ApiSetWinRT, CallingConvention = CallingConvention.StdCall)]
3232
internal static extern unsafe int RoGetActivationFactory(IntPtr runtimeClassId, ref Guid iid, [MarshalAs(UnmanagedType.Interface)] out object factory);
3333

34+
[DllImport(DllImport.ApiSetWinRT, CallingConvention = CallingConvention.StdCall)]
35+
internal static extern unsafe int RoActivateInstance(IntPtr runtimeClassId, [MarshalAs(UnmanagedType.Interface)] out object instance);
36+
3437
internal const int E_NOINTERFACE = unchecked((int)0x80004002);
3538

3639
internal const int REGDB_E_CLASSNOTREG = unchecked((int)0x80040154);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using System.Runtime.InteropServices;
5+
using System.Security;
6+
using System.Windows;
7+
using System.Windows.Interop;
8+
using System.Windows.Media;
9+
using MS.Internal.PresentationCore.WindowsRuntime;
10+
11+
namespace MS.Internal.WindowsRuntime
12+
{
13+
namespace Windows.UI.ViewManagement
14+
{
15+
internal class UISettings : IDisposable
16+
{
17+
private readonly bool _isSupported;
18+
19+
private UISettingsRCW.IUISettings3 _uisettings;
20+
21+
private static readonly Color _fallbackAccentColor = Color.FromArgb(0xff, 0x00, 0x78, 0xd4);
22+
23+
private Color _accentColor, _accentLight1, _accentLight2, _accentLight3;
24+
private Color _accentDark1, _accentDark2, _accentDark3;
25+
26+
private bool _useFallbackColor = false;
27+
28+
internal UISettings()
29+
{
30+
_isSupported = false;
31+
32+
try
33+
{
34+
_uisettings = GetWinRTInstance() as UISettingsRCW.IUISettings3;
35+
}
36+
catch (COMException)
37+
{
38+
// We don't want to throw any exceptions here.
39+
// If we can't get the instance, we will use the default accent color.
40+
}
41+
42+
if (_uisettings != null)
43+
{
44+
_isSupported = true;
45+
TryUpdateAccentColors();
46+
}
47+
}
48+
49+
/// <summary>
50+
/// Gets the accent color value for the desired color type.
51+
/// </summary>
52+
/// <returns>
53+
/// Returns true if fetching value from UISettings was successful.
54+
/// If the fetch fails, we return false and return the default accent color.
55+
/// </returns>
56+
internal bool TryGetColorValue(UISettingsRCW.UIColorType desiredColor, out Color color)
57+
{
58+
if(_isSupported)
59+
{
60+
try
61+
{
62+
var uiColor = _uisettings.GetColorValue(desiredColor);
63+
color = Color.FromArgb(uiColor.A, uiColor.R, uiColor.G, uiColor.B);
64+
return true;
65+
}
66+
catch (COMException)
67+
{
68+
// We don't want to throw any exceptions here.
69+
// If we can't get the instance, we will use the default accent color.
70+
}
71+
}
72+
color = _fallbackAccentColor;
73+
return false;
74+
}
75+
76+
/// <summary>
77+
/// Tries to update the accent colors properties.
78+
/// If any call to TryGetColorValue fails, we set _useFallbackColor to true.
79+
/// After which all the accent values will be the default color.
80+
/// This is to ensure that we don't have inconsistent set of accent color values stored.
81+
/// </summary>
82+
internal void TryUpdateAccentColors()
83+
{
84+
_useFallbackColor = true;
85+
86+
if(_isSupported)
87+
{
88+
try
89+
{
90+
if(TryGetColorValue(UISettingsRCW.UIColorType.Accent, out Color systemAccent))
91+
{
92+
// For verifying if any of the calls to TryGetColorValue fails.
93+
bool result = true;
94+
if(_accentColor != systemAccent)
95+
{
96+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight1, out _accentLight1);
97+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight2, out _accentLight2);
98+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight3, out _accentLight3);
99+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark1, out _accentDark1);
100+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark2, out _accentDark2);
101+
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark3, out _accentDark3);
102+
_accentColor = systemAccent;
103+
}
104+
// If result is false, hence atleast one call, use fallback values.
105+
_useFallbackColor = !result;
106+
}
107+
}
108+
catch
109+
{
110+
// We don't want to throw any exceptions here.
111+
// If we can't get any one of the color values, we will use the default accent color.
112+
}
113+
}
114+
}
115+
116+
/// <summary>
117+
/// Gets the WinRT instance of UISettings.
118+
/// </summary>
119+
private static object GetWinRTInstance()
120+
{
121+
object winRtInstance = null;
122+
try
123+
{
124+
winRtInstance = UISettingsRCW.GetUISettingsInstance();
125+
}
126+
catch (Exception e) when (e is TypeLoadException || e is FileNotFoundException)
127+
{
128+
winRtInstance = null;
129+
}
130+
131+
return winRtInstance;
132+
}
133+
134+
#region Color Properties
135+
136+
internal Color AccentColor => _useFallbackColor ? _fallbackAccentColor : _accentColor;
137+
internal Color AccentLight1 => _useFallbackColor ? _fallbackAccentColor : _accentLight1;
138+
internal Color AccentLight2 => _useFallbackColor ? _fallbackAccentColor : _accentLight2;
139+
internal Color AccentLight3 => _useFallbackColor ? _fallbackAccentColor : _accentLight3;
140+
internal Color AccentDark1 => _useFallbackColor ? _fallbackAccentColor : _accentDark1;
141+
internal Color AccentDark2 => _useFallbackColor ? _fallbackAccentColor : _accentDark2;
142+
internal Color AccentDark3 => _useFallbackColor ? _fallbackAccentColor : _accentDark3;
143+
144+
#endregion
145+
146+
#region IDisposable
147+
148+
bool _disposed = false;
149+
150+
~UISettings()
151+
{
152+
Dispose(false);
153+
}
154+
155+
public void Dispose()
156+
{
157+
Dispose(true);
158+
GC.SuppressFinalize(this);
159+
}
160+
161+
private void Dispose(bool disposing)
162+
{
163+
if (!_disposed)
164+
{
165+
if (_uisettings != null)
166+
{
167+
try
168+
{
169+
// Release the _uiSettings instance here
170+
Marshal.ReleaseComObject(_uisettings);
171+
}
172+
catch
173+
{
174+
// Don't want to raise any exceptions in a finalizer, eat them here
175+
}
176+
177+
_uisettings = null;
178+
}
179+
180+
_disposed = true;
181+
}
182+
}
183+
184+
#endregion
185+
}
186+
}
187+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
using System.Security;
5+
using System.Windows.Media;
6+
7+
namespace MS.Internal.WindowsRuntime
8+
{
9+
namespace Windows.UI.ViewManagement
10+
{
11+
/// <summary>
12+
/// Contains internal RCWs for invoking the UISettings
13+
/// </summary>
14+
internal static class UISettingsRCW
15+
{
16+
17+
public static object GetUISettingsInstance()
18+
{
19+
const string typeName = "Windows.UI.ViewManagement.UISettings";
20+
IntPtr hstring = IntPtr.Zero;
21+
int hr = NativeMethods.WindowsCreateString(typeName, typeName.Length, out hstring);
22+
Marshal.ThrowExceptionForHR(hr);
23+
try
24+
{
25+
hr = NativeMethods.RoActivateInstance(hstring, out object instance);
26+
Marshal.ThrowExceptionForHR(hr);
27+
return instance;
28+
}
29+
finally
30+
{
31+
hr = NativeMethods.WindowsDeleteString(hstring);
32+
Marshal.ThrowExceptionForHR(hr);
33+
}
34+
}
35+
36+
[Guid("03021BE4-5254-4781-8194-5168F7D06D7B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
37+
[ComImport]
38+
internal interface IUISettings3
39+
{
40+
void GetIids(out uint iidCount, out IntPtr iids);
41+
42+
void GetRuntimeClassName(out string className);
43+
44+
void GetTrustLevel(out TrustLevel TrustLevel);
45+
46+
UIColor GetColorValue(UIColorType desiredColor);
47+
}
48+
49+
internal enum TrustLevel
50+
{
51+
BaseTrust,
52+
PartialTrust,
53+
FullTrust
54+
}
55+
56+
public enum UIColorType
57+
{
58+
Background = 0,
59+
Foreground = 1,
60+
AccentDark3 = 2,
61+
AccentDark2 = 3,
62+
AccentDark1 = 4,
63+
Accent = 5,
64+
AccentLight1 = 6,
65+
AccentLight2 = 7,
66+
AccentLight3 = 8,
67+
Complement = 9
68+
}
69+
70+
internal readonly record struct UIColor(byte A, byte R, byte G, byte B);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)