Skip to content

Commit

Permalink
Windows 11 Theming Changes (#8870)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>

* 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 <[email protected]>
Co-authored-by: dipeshmsft <[email protected]>

* Updatating OS version check

---------

Co-authored-by: dipeshmsft <[email protected]>

* 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 <[email protected]>

---------

Signed-off-by: Ashish Kumar Singh <[email protected]>
Co-authored-by: Ashish Kumar Singh <[email protected]>
Co-authored-by: Pankaj Chaurasia <[email protected]>
Co-authored-by: Anjali <[email protected]>
Co-authored-by: dipeshmsft <[email protected]>
Co-authored-by: Rishabh Chauhan <[email protected]>
Co-authored-by: lindexi <[email protected]>
Co-authored-by: Dipesh Kumar <[email protected]>
Co-authored-by: Rishabh Chauhan <[email protected]>
  • Loading branch information
9 people authored Apr 19, 2024
1 parent 19722f3 commit fb28902
Show file tree
Hide file tree
Showing 85 changed files with 12,418 additions and 56 deletions.
7 changes: 5 additions & 2 deletions Documentation/projects-and-assemblies.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@ The following assemblies are being produced today:
| PresentationFramework.dll
| PresentationFramework.Luna.dll
| PresentationFramework.Luna.pdb
| PresentationFramework.pdb
| PresentationFramework.pdb
| PresentationFramework.Royale.dll
| PresentationFramework.Royale.pdb
| PresentationFramework.Fluent.dll
| PresentationFramework.Fluent.pdb
| PresentationUI.dll
| PresentationUI.pdb
| ReachFramework.dll
Expand Down Expand Up @@ -452,7 +454,8 @@ src\Themes\PresentationFramework.Luna\PresentationFramework.Luna.csproj
src\Themes\PresentationFramework.Luna\ref\PresentationFramework.Luna-ref.csproj
src\Themes\PresentationFramework.Royale\PresentationFramework.Royale.csproj
src\Themes\PresentationFramework.Royale\ref\PresentationFramework.Royale-ref.csproj
src\Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj
src\Themes\PresentationFramework.Fluent\ref\PresentationFramework.Fluent-ref.csproj
src\UIAutomation\UIAutomationClient\UIAutomationClient.csproj
src\UIAutomation\UIAutomationClient\ref\UIAutomationClient-ref.csproj
src\UIAutomation\UIAutomationClientSideProviders\UIAutomationClientSideProviders.csproj
Expand Down
38 changes: 38 additions & 0 deletions Microsoft.Dotnet.Wpf.sln
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UnitTests", "UnitTests", "{
EndProject
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}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PresentationFramework.Fluent", "src\Microsoft.DotNet.Wpf\src\Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj", "{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}"
EndProject
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -1942,6 +1946,38 @@ Global
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.ActiveCfg = Release|Win32
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.Build.0 = Release|Win32
{B2F2A89C-55C9-41B1-A645-0948609BD8BE}.Release|x86.Deploy.0 = Release|Win32
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|ARM64.ActiveCfg = Debug|arm64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|ARM64.Build.0 = Debug|arm64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x64.ActiveCfg = Debug|x64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x64.Build.0 = Debug|x64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x86.ActiveCfg = Debug|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Debug|x86.Build.0 = Debug|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|Any CPU.Build.0 = Release|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|ARM64.ActiveCfg = Release|arm64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|ARM64.Build.0 = Release|arm64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x64.ActiveCfg = Release|x64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x64.Build.0 = Release|x64
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x86.ActiveCfg = Release|Any CPU
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9}.Release|x86.Build.0 = Release|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|ARM64.ActiveCfg = Debug|arm64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|ARM64.Build.0 = Debug|arm64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x64.ActiveCfg = Debug|x64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x64.Build.0 = Debug|x64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x86.ActiveCfg = Debug|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Debug|x86.Build.0 = Debug|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|Any CPU.Build.0 = Release|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|ARM64.ActiveCfg = Release|arm64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|ARM64.Build.0 = Release|arm64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x64.ActiveCfg = Release|x64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x64.Build.0 = Release|x64
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x86.ActiveCfg = Release|Any CPU
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -2062,6 +2098,8 @@ Global
{34B64A4A-7C83-4F92-8C47-80E9FA10D660} = {C9ECC1A1-917F-43D3-B340-2035B4938507}
{A48B585E-6AB0-4F8D-8484-77F37CB44437} = {34B64A4A-7C83-4F92-8C47-80E9FA10D660}
{B2F2A89C-55C9-41B1-A645-0948609BD8BE} = {A48B585E-6AB0-4F8D-8484-77F37CB44437}
{3F2C0E0E-BB13-46D9-8D9A-08256A49ECA9} = {5ACFB055-649D-4A01-98C2-B0BFE7E543D6}
{3C43C553-2C1F-4EB9-8BF8-371D4A42E0FD} = {60F4058B-D35B-42D2-B276-D44B3AC579BD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B4340004-DAC0-497D-B69D-CFA7CD93F567}
Expand Down
1 change: 1 addition & 0 deletions eng/WpfArcadeSdk/tools/GenApi.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
PresentationFramework.Classic;
PresentationFramework.Luna;
PresentationFramework.Royale;
PresentationFramework.Fluent;
ReachFramework;
System.Windows.Controls.Ribbon;
System.Windows.Input.Manipulations;
Expand Down
4 changes: 4 additions & 0 deletions eng/WpfArcadeSdk/tools/ShippingProjects.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
PresentationFramework.Classic;
PresentationFramework.Luna;
PresentationFramework.Royale;
PresentationFramework.Fluent;
WindowsFormsIntegration;
PenImc;
wpfgfx;
Expand Down Expand Up @@ -98,6 +99,7 @@
PresentationFramework.Classic-ref;
PresentationFramework.Luna-ref;
PresentationFramework.Royale-ref;
PresentationFramework.Fluent-ref;
PresentationUI-ref;
System.Printing-ref;
System.Windows.Controls.Ribbon-ref;
Expand Down Expand Up @@ -136,6 +138,8 @@
PresentationFramework.Luna-ref;
PresentationFramework.Royale;
PresentationFramework.Royale-ref;
PresentationFramework.Fluent;
PresentationFramework.Fluent-ref;
PresentationUI;
ReachFramework;
ReachFramework-ref;
Expand Down
2 changes: 1 addition & 1 deletion eng/WpfArcadeSdk/tools/WpfProjectReference.targets
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<WpfProjectPath Include="PresentationFramework.Classic" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Classic\PresentationFramework.Classic.csproj" />
<WpfProjectPath Include="PresentationFramework.Luna" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Luna\PresentationFramework.Luna.csproj" />
<WpfProjectPath Include="PresentationFramework.Royale" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Royale\PresentationFramework.Royale.csproj" />

<WpfProjectPath Include="PresentationFramework.Fluent" ProjectPath="$(WpfSourceDir)Themes\PresentationFramework.Fluent\PresentationFramework.Fluent.csproj" />
<WpfProjectPath Include="UIAutomationTypes" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationTypes\UIAutomationTypes.csproj" />
<WpfProjectPath Include="UIAutomationProvider" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationProvider\UIAutomationProvider.csproj" />
<WpfProjectPath Include="UIAutomationClient" ProjectPath="$(WpfSourceDir)UIAutomation\UIAutomationClient\UIAutomationClient.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ internal static extern unsafe int WindowsCreateString([MarshalAs(UnmanagedType.L
[DllImport(DllImport.ApiSetWinRT, CallingConvention = CallingConvention.StdCall)]
internal static extern unsafe int RoGetActivationFactory(IntPtr runtimeClassId, ref Guid iid, [MarshalAs(UnmanagedType.Interface)] out object factory);

[DllImport(DllImport.ApiSetWinRT, CallingConvention = CallingConvention.StdCall)]
internal static extern unsafe int RoActivateInstance(IntPtr runtimeClassId, [MarshalAs(UnmanagedType.Interface)] out object instance);

internal const int E_NOINTERFACE = unchecked((int)0x80004002);

internal const int REGDB_E_CLASSNOTREG = unchecked((int)0x80040154);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using MS.Internal.PresentationCore.WindowsRuntime;

namespace MS.Internal.WindowsRuntime
{
namespace Windows.UI.ViewManagement
{
internal class UISettings : IDisposable
{
private readonly bool _isSupported;

private UISettingsRCW.IUISettings3 _uisettings;

private static readonly Color _fallbackAccentColor = Color.FromArgb(0xff, 0x00, 0x78, 0xd4);

private Color _accentColor, _accentLight1, _accentLight2, _accentLight3;
private Color _accentDark1, _accentDark2, _accentDark3;

private bool _useFallbackColor = false;

internal UISettings()
{
_isSupported = false;

try
{
_uisettings = GetWinRTInstance() as UISettingsRCW.IUISettings3;
}
catch (COMException)
{
// We don't want to throw any exceptions here.
// If we can't get the instance, we will use the default accent color.
}

if (_uisettings != null)
{
_isSupported = true;
TryUpdateAccentColors();
}
}

/// <summary>
/// Gets the accent color value for the desired color type.
/// </summary>
/// <returns>
/// Returns true if fetching value from UISettings was successful.
/// If the fetch fails, we return false and return the default accent color.
/// </returns>
internal bool TryGetColorValue(UISettingsRCW.UIColorType desiredColor, out Color color)
{
if(_isSupported)
{
try
{
var uiColor = _uisettings.GetColorValue(desiredColor);
color = Color.FromArgb(uiColor.A, uiColor.R, uiColor.G, uiColor.B);
return true;
}
catch (COMException)
{
// We don't want to throw any exceptions here.
// If we can't get the instance, we will use the default accent color.
}
}
color = _fallbackAccentColor;
return false;
}

/// <summary>
/// Tries to update the accent colors properties.
/// If any call to TryGetColorValue fails, we set _useFallbackColor to true.
/// After which all the accent values will be the default color.
/// This is to ensure that we don't have inconsistent set of accent color values stored.
/// </summary>
internal void TryUpdateAccentColors()
{
_useFallbackColor = true;

if(_isSupported)
{
try
{
if(TryGetColorValue(UISettingsRCW.UIColorType.Accent, out Color systemAccent))
{
// For verifying if any of the calls to TryGetColorValue fails.
bool result = true;
if(_accentColor != systemAccent)
{
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight1, out _accentLight1);
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight2, out _accentLight2);
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentLight3, out _accentLight3);
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark1, out _accentDark1);
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark2, out _accentDark2);
result &= TryGetColorValue(UISettingsRCW.UIColorType.AccentDark3, out _accentDark3);
_accentColor = systemAccent;
}
// If result is false, hence atleast one call, use fallback values.
_useFallbackColor = !result;
}
}
catch
{
// We don't want to throw any exceptions here.
// If we can't get any one of the color values, we will use the default accent color.
}
}
}

/// <summary>
/// Gets the WinRT instance of UISettings.
/// </summary>
private static object GetWinRTInstance()
{
object winRtInstance = null;
try
{
winRtInstance = UISettingsRCW.GetUISettingsInstance();
}
catch (Exception e) when (e is TypeLoadException || e is FileNotFoundException)
{
winRtInstance = null;
}

return winRtInstance;
}

#region Color Properties

internal Color AccentColor => _useFallbackColor ? _fallbackAccentColor : _accentColor;
internal Color AccentLight1 => _useFallbackColor ? _fallbackAccentColor : _accentLight1;
internal Color AccentLight2 => _useFallbackColor ? _fallbackAccentColor : _accentLight2;
internal Color AccentLight3 => _useFallbackColor ? _fallbackAccentColor : _accentLight3;
internal Color AccentDark1 => _useFallbackColor ? _fallbackAccentColor : _accentDark1;
internal Color AccentDark2 => _useFallbackColor ? _fallbackAccentColor : _accentDark2;
internal Color AccentDark3 => _useFallbackColor ? _fallbackAccentColor : _accentDark3;

#endregion

#region IDisposable

bool _disposed = false;

~UISettings()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (!_disposed)
{
if (_uisettings != null)
{
try
{
// Release the _uiSettings instance here
Marshal.ReleaseComObject(_uisettings);
}
catch
{
// Don't want to raise any exceptions in a finalizer, eat them here
}

_uisettings = null;
}

_disposed = true;
}
}

#endregion
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Windows.Media;

namespace MS.Internal.WindowsRuntime
{
namespace Windows.UI.ViewManagement
{
/// <summary>
/// Contains internal RCWs for invoking the UISettings
/// </summary>
internal static class UISettingsRCW
{

public static object GetUISettingsInstance()
{
const string typeName = "Windows.UI.ViewManagement.UISettings";
IntPtr hstring = IntPtr.Zero;
int hr = NativeMethods.WindowsCreateString(typeName, typeName.Length, out hstring);
Marshal.ThrowExceptionForHR(hr);
try
{
hr = NativeMethods.RoActivateInstance(hstring, out object instance);
Marshal.ThrowExceptionForHR(hr);
return instance;
}
finally
{
hr = NativeMethods.WindowsDeleteString(hstring);
Marshal.ThrowExceptionForHR(hr);
}
}

[Guid("03021BE4-5254-4781-8194-5168F7D06D7B"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
internal interface IUISettings3
{
void GetIids(out uint iidCount, out IntPtr iids);

void GetRuntimeClassName(out string className);

void GetTrustLevel(out TrustLevel TrustLevel);

UIColor GetColorValue(UIColorType desiredColor);
}

internal enum TrustLevel
{
BaseTrust,
PartialTrust,
FullTrust
}

public enum UIColorType
{
Background = 0,
Foreground = 1,
AccentDark3 = 2,
AccentDark2 = 3,
AccentDark1 = 4,
Accent = 5,
AccentLight1 = 6,
AccentLight2 = 7,
AccentLight3 = 8,
Complement = 9
}

internal readonly record struct UIColor(byte A, byte R, byte G, byte B);
}
}
}
Loading

0 comments on commit fb28902

Please sign in to comment.