Skip to content

[API Proposal]: FluentWindows Theme Switch in WPF #8932

Open

Description

@dipeshmsft

Background and motivation

With the introduction of FluentWindows ( Win11 ) theme, we want to allow developers to be able to enable disable and switch themes in their WPF applications. Irrespective of if we provide an API in .NET 9, I believe that we should provide a way for developers to choose only Light, Dark theme or respond to system theme changes.

Part of effort - #8655

This is inspired and a minified version of the #8759

Before going through the alternatives, I would like to bring to the point, since we are so close to the code completion deadline for .NET Preview 7, that if we choose to implement the API, we keep it experimental, so that we have room for further discussion in future.

API Proposal

Current Proposal

    public class Application
    {
        [Experimental]
        public string Theme { get; set; }
    }

    public class Window
    {
        [Experimental]
        public string Theme { get; set; }
    }

The accepted values for the theme properties can be - None, System, Light and Dark

As mentioned above, these APIs will be Experimental until we have further discussions and have a better way to handle theme ( default ) styles in .NET 10.

Behavior of the APIs

  1. When Window.Theme = System / Light / Dark it will take precdence over Application.Theme and we will load the Fluent styles for window. In case, of System Window will respond to System theme changes.
  2. When Application.Theme = System / Light / Dark and Window.Theme = None then window will follow the application.
  3. In case none of the above properties are set, we will get Aero2 by default.
  4. Accent color changes will be received by every window irrespective of the Theme property values. If Theme value is None, then Accent colors don't come into play.
  5. Default values for both Theme properties is None.

Alternative Designs

No response

Risks

No response

cc: @dotnet/dotnet-wpf-triage @pomianowski

Activity

  1. added
    API suggestion
    on Mar 20, 2024
  2. pomianowski commented on Mar 20, 2024

    @pomianowski
    Contributor

    Will this property automatically affect all windows for which current one is Parent? I think that changing e.g. MainWindow should affect all additional windows for this below

  3. batzen commented on Mar 20, 2024

    @batzen
    Contributor

    The default should come from Application.Current, if there is one.

    I don't think getting it from the parent window is a good idea.

  4. dipeshmsft commented on Mar 20, 2024

    @dipeshmsft
    MemberAuthor

    @pomianowski @batzen I have updated the proposal. The new DP was meant to be at the application level.

  5. batzen commented on Mar 20, 2024

    @batzen
    Contributor

    @dipeshmsft Only having it at the application level won't be enough for all use-cases, i think.

  6. dipeshmsft commented on Mar 20, 2024

    @dipeshmsft
    MemberAuthor

    @batzen, apart from the application one place where we should have this, I guess is at the window level. Do you have some other use cases for this, that we can consider ?

  7. batzen commented on Mar 24, 2024

    @batzen
    Contributor

    I think Application and Window should be enough.

  8. dipeshmsft commented on Mar 27, 2024

    @dipeshmsft
    MemberAuthor

    @batzen, what is the behavior that you expect when a developer sets the Theme property on only one window in the application ?

  9. batzen commented on Mar 28, 2024

    @batzen
    Contributor

    @dipeshmsft That only that one specific window, and thus it's content, changes it's theme.

  10. miloush commented on Mar 28, 2024

    @miloush
    Contributor

    @dipeshmsft Only having it at the application level won't be enough for all use-cases, i think.

    @batzen can you please elaborate on which use cases are you expecting for having different theme per window in one app? I don't think I have seen an app that offers that. It would also mess up the precedence of resources I think...

  11. MichaeIDietrich commented on Mar 29, 2024

    @MichaeIDietrich
    Contributor

    A prominent example is Visual Studio. Visual Studio supports dark and light themes for quite some time now, but the options window still ignores it, most probably due to its complexity.

    In general I can see where it can be useful to disable dark theme (force light theme) for some windows where it just will take some time to fix all the issues to correctly support dark theming.
    Or when you embed 3rd party views where you are not in control whether theming is correctly implemented.

    Then it may be better to force a light theme for a specific window rather than mixing dark and light themes.

  12. batzen commented on Mar 30, 2024

    @batzen
    Contributor

    @MichaeIDietrich That's exactly what i would have said.
    And we must not forget that WPF has to compete with MahApps.Metro, MaterialDesignInXAML, WPFUI, Fluent.Ribbon, ControlzEx etc. all of those allow changing the theme even further down at the FrameworkElement level.

  13. miloush commented on Mar 30, 2024

    @miloush
    Contributor

    Visual Studio case is more about "themed" window vs "non-themed" window. They probably just did not bother investing in theming the legacy Settings window, focusing on the new settings tab experience instead (which is themed). Same with some of the dialogs that were "forgotten" unthemed but newer ones are themed. Note that if you turn on one of the high contrasty themes, they will apply to the settings too. This is not a very convincing example.

    WPF has to compete

    It does not. People can still keep using 3rd party libraries to fill in functionality that the framework does not provide.

    I am not against having window/element level theming in principle if it was a low-hanging fruit, despite me not seeing a good use case for it yet, but it is not. You would have to design and change the whole DP precedence system to squeeze per window/element themes in. The team is already too busy with delivering this feature and I would rather if we not increase the release bar further. Let's get application-wide theme switching out and if there is enough calls for having this controlled more granularly, we can do add this later.

    As for the proposed API itself, I would prefer if this was a string property where you can put any of the themes available, such as "Classic" or "Royale", which would require no changes for future themes (or possibly when custom ones can be shipped with apps).

  14. MichaeIDietrich commented on Mar 30, 2024

    @MichaeIDietrich
    Contributor

    Visual Studio case is more about "themed" window vs "non-themed" window.

    Not quite sure whether I would agree on the "themed" vs "non-themed" argument. From the WPF point of view every visual representation is a theme, so there isn't really something like "non-themed". That's why I would differentiate between light and dark theming, mixing two different light themes (e.g. Aero and Aero2) is visually less an issue than mixing dark and light themes.

    They probably just did not bother investing in theming the legacy Settings window, focusing on the new settings tab experience instead (which is themed). Same with some of the dialogs that were "forgotten" unthemed but newer ones are themed.

    I'm pretty sure that the idea of moving the settings to a new tab experience is by far not as old as the Settings window not supporting a different theme. So they had their reasons to not support it, we can only guess at that point.

    Note that if you turn on one of the high contrasty themes, they will apply to the settings too. This is not a very convincing example.

    Good point indeed. I'm pretty surprised it doesn't look as scrambled as I would have expected it.

    Still, my point stays that we need to keep in mind real world applications that consists of more than 3 windows and have developed over years with hard coded colors at several places and stuff that cannot be fixed within two days.

    I just see applications like from the company I work at that has support for 3rd party plugins bringing their own UI. Such plugins won't support dark theme on day one. And now there will be only the option to disable dark theme for the whole application or to let the user live with the result.

    if it was a low-hanging fruit

    I agree with that. It's more work to do if we want to support this. And I'm also on your side that even with the current state I'm not 100% confident that Win11 themes will make it to the next .NET release seeing the progress so far.

    As for the proposed API itself, I would prefer if this was a string property where you can put any of the themes available, such as "Classic" or "Royale", which would require no changes for future themes (or possibly when custom ones can be shipped with apps).

    Same for me, I would also prefer to make all the themes just identifiable by a key and not implement any specific logic for Win11 into WPF. That way custom themes could also be implemented.

    But this doesn't mean the theme to use couldn't be also resolved on window level, since resources are already resolved that way by walking up the hierarchy, this could be also done for themes.

    This all being said, focus should be on bringing Win11 themes with the next .NET version and if such feature wishes would delay the release, then it can be, of course, argued to give those a lower priority.

  15. 27 remaining items

  16. bartonjs commented on Jul 18, 2024

    @bartonjs
    Member

    Video

    • Instead of loose strings, it should use strongly-typed strings.
    • After discussion of what the property means, and what the OS calls it, we think "ThemeName" is the best name.
    namespace System.Windows
    {
        public class Application
        {
            [Experimental]
            public ThemeMode ThemeMode { get; set; }
        }
    
        public class Window
        {
            [Experimental]
            public ThemeMode ThemeMode { get; set; }
        }
    
        [Experimental]
        public readonly struct ThemeMode : IEquatable<ThemeMode>
        {
            public static ThemeMode None => new ThemeMode();
            public static ThemeMode System => new ThemeMode("System");
            public static ThemeMode Light => new ThemeMode("Light");
            public static ThemeMode Dark => new ThemeMode("Dark");
    
            public string Value => _value ?? "None";
    
            public ThemeMode(string value) => _value = value;
    
            // + IEquatable members
        }
    }
  17. added
    api-approved
    and removed
    API suggestion
    api-ready-for-review
    on Jul 18, 2024
  18. robert-abeo commented on Jul 18, 2024

    @robert-abeo

    Since this is experimental I realize you are avoiding most feedback; however, for the future:

    1. You are conflating two different ideas here: Theme (Aero2, Fluent, etc.) with Theme "variants" (light, dark, etc.) to use a term from Avalonia. Every XAML framework handles variants differently:
      • WPF has no mechanism. Meaning FluentLight, FluentDark, etc. will all have to be considered separate themes
      • Avalonia has Theme and ThemeVariant
      • UWP/WinUI has RequestedTheme -- but the resource dictionaries themselves can handle light/dark variations by setting the key on the resource dictionary.
    2. This isn't well thought out in terms of None/System/Default. None shouldn't exist. Default should mean match the system. That means both Aero2 on pre-Windows 10 vs Fluent on Windows11 but it also means matching light/dark in this case because WPF has no concept to manage the variants.
    3. As stated, you should absolutely not have magic strings here
    4. You haven't defined high-contrast, etc.
  19. miloush commented on Jul 18, 2024

    @miloush
    Contributor

    I have to agree the strongly-typed string was an unfortunate decision. The value of having a loose string was especially to support the experimental nature of the API and avoid having to approve further API changes when any of the strings or their set needs to change, for example to include more information than just a theme name. The name ThemeMode is not helping either.

    While WPF has cases of enumerated strings as static members, this way of strongly typing is unprecedented in the framework. As also noted above, it doesn't bring any value for the testing, since it would be used mostly in XAML without any autocompletion support. The type will need to eventually go away and if the aim is to minimize future breaking changes, removing a public type and members feels more disruptive than just changing a property type, although formally that is probably equivalent.

    Thanks for approving the API though, it would be good to make it easier for more people to try out the theming!

    (@robert-abeo WPF has ThemeColor)

  20. robert-abeo commented on Jul 18, 2024

    @robert-abeo

    @miloush

    (@robert-abeo WPF has ThemeColor)

    Please elaborate as I'm not familiar with ThemeColor

  21. terrajobst commented on Jul 18, 2024

    @terrajobst
    Contributor

    (FYI, I'm not a WPF expert but was part of the API review group, so my comments are a reflection of how we understood the feature.)

    @robert-abeo

    1. Not sure you watched the review, but this was discussed. The conclusion was that there is inherent overlap between what is already there and what is being added as both are sometimes referred to as "theme". We landed on calling it ThemeMode, which is similar to what you call it ("theme variant"). That said, I think a big part of why we think shipping something here is better than nothing is precisely to figure out how the new built-in style support for dark mode will interplay with the ecosystem. This includes both terminology as well as interop (i.e. when one is hosted in the other). That also applies to the WinForm/WPF interop.

    2. None usually means "the user hasn't made a decision". For regular enums, this is usually equivalent to the value 0. In C# terms that's default(ThemeMode), which means "the .NET runtime decides for you" while System would mean "use the OS configuration". For instance, .NET 9 might decide to use Light while .NET 10 might use System. That said, I think it's not unreasonable to collapse them. Personally, I wouldn't call it Default and rather go with System because it makes it clear what default means.

    3. My understanding was that there is a desire for strings to support an extension point where a user/component vendor can supply their own themes. WinForms decided to scope out custom theming entirely; it's basically just Light/Dark/System.

    4. High contrast was discussed and my understanding is that it's treated as system-wide override.

    @miloush

    The value of having a loose string was especially to support the experimental nature of the API and avoid having to approve further API changes when any of the strings or their set needs to change, for example to include more information than just a theme name.

    I think using loose strings to avoid review time isn't a good reason. Rather, we should think about the desired UX and ship that. I'd rather cut corners in internal processes than compromise the UX. However, if we do strings then the value of this type is to allow an open-ended set while also making well-known names more discoverable. This pattern is used in the .NET platform in various places and has proven to be an effective UX.

    As also noted above, it doesn't bring any value for the testing, since it would be used mostly in XAML without any autocompletion support.

    That is a very fair point and one that wasn't considered in the review. If the primary use is indeed from XAML, then this design has no benefits. My assumption was that this would be part of the app's start up code i.e in code-behind.

    The type will need to eventually go away and if the aim is to minimize future breaking changes.

    If you already know that you don't want this type because you don't think you want to use strings but some other to-be-designed type, then one can argue that this design doesn't help much. However, it also doesn't hurt much because you're basically saying "I believe this feature will work completely differently, I just don't know what it will look like". IOW, you're saying "I expect all users of this features having to completely change what code I expect them to write".

    Removing a public type and members feels more disruptive than just changing a property type, although formally that is probably equivalent.

    In general yes, removing types is more disruptive than adding/removing members. However, in your case the entire feature (type and member) is opt-in. The only people that can use it are the ones that suppressed the error "this type is marked as experimental, suppress me if you want to use it anyway". So the break only impacts people that have agreed to be broken.

    You still want to minimize breaking changes for early adopters but it sounds to me like any design we cook up right now is guaranteed to change anyway, so we might as well choose one that we think is the easiest to use.

    Whether or not this approved design will be easier to use is a different question; we didn't consider usage from XAML. If this is the primary use and having this type shape makes it harder, then I think it's reasonable to revert back to the original plain string design.

  22. pomianowski commented on Jul 18, 2024

    @pomianowski
    Contributor

    I know this may be wrong BUT leaving aside what is currently implemented there and what maybe we should be added, it seems to me that from the point of view of the UX of a new developer the division into Theme(Kind/Mode) and ThemeVariant seems optimal and future-proof.

    enum ThemeMode
    {
      System,
      Light,
      Dark
    }
    default(ThemeMode) = ThemeMode.System

    We can consider that we have an Application Theme that is simply light or dark (High contrast is more difficult for me because it can simply be a variant of light or dark), there are no other combinations. And its default value can be System. With ThemeMode.System from Window's point of view means Application, and from Application's point of view means operating system.

    By default, the new Application (if it exists) could simply follows the system theme. Likewise, the Window could simply follows the application (or os if it is a dialog).

    Variant can be some enum or const that simply suggests the name of the theme file to load.

    Maybe something like:

    public class ThemeVariant
    {
      public const string HighContrastWhite = "HCWhite";
      // or
      public const string HighContrast = "HC";
      // or
      public const string Desert = "HCWhite";
    }
    namespace System.Windows
    {
        public class Application
        {
            [Experimental]
            public ThemeMode Theme { get; set; }
    
            [Experimental]
            public string ThemeVariant { get; set; }
    
            // or composite like public sealed record ThemeContext(ThemeMode Mode, ThemeVariant Variant);
            [Experimental]
            public ThemeContext Theme { get; set; }
        }
    
        public class Window
        {
            [Experimental]
            public ThemeMode Theme { get; set; }
    
            [Experimental]
            public string ThemeVariant { get; set; }
        }
    }

    From ThemeContext, we create a decision matrix to load files / select resources / update resources

    Mode (Light/Dark) Variant (Name) Result
    Light HC HighContrastWhite.xaml
    Dark HC HighContrastDark.xaml
    Light Default DefaultLight.xaml
    Dark Default DefaultDark.xaml
    <Button ThemeDictionary="{ThemeDictionary Mode=Dark, Variant=HighContrast}" />
    <Button ThemeDictionary="{ThemeDictionary Mode=Light, Variant=Default}" />

    From the Windows 11 system themes there's also a table

    Windows Theme Mode (Light/Dark) Variant (Name)
    Light Light Default
    Dark Dark Default
    Dusk Light HC
    Nightsky Dark HC
    Sunrise Light CustomPrimary
    Captured Motion Dark CustomPrimary
  23. robert-abeo commented on Jul 18, 2024

    @robert-abeo

    @terrajobst Thanks for some background

    We landed on calling it ThemeMode, which is similar to what you call it ("theme variant")

    No haven't seen the video and probably won't have time. I'm glad you discussed the nuance here. I would just try to use existing conventions in WinUI as much as possible in the future.

    None usually means "the user hasn't made a decision". For regular enums, this is usually equivalent to the value 0. In C# terms that's default(ThemeMode), which means "the .NET runtime decides for you" while System would mean "use the OS configuration". For instance, .NET 9 might decide to use Light while .NET 10 might use System. That said, I think it's not unreasonable to collapse them. Personally, I wouldn't call it Default and rather go with System because it makes it clear what default means.

    API context is VERY important here. When you set Theme="None" you are saying "Don't set a theme". That implies you are inserting your own theme resource dictionaries somewhere in the tree I suppose. What this actually is doing is "Default" so my naming is correct. If you used WinUI terminology RequestedTheme=None then I would agree with you.

    My understanding was that there is a desire for strings to support an extension point where a user/component vendor can supply their own themes. WinForms decided to scope out custom theming entirely; it's basically just Light/Dark/System.

    That does make some sense. Nobody has solved this problem in any XAML framework as far as I know. You just have to manually insert the resource dictionaries.

    High contrast was discussed and my understanding is that it's treated as system-wide override.

    Ok, that is how it's handled most places anyway so as long as it's supported automatically behind the scenes it makes sense to me.

  24. miloush commented on Jul 18, 2024

    @miloush
    Contributor

    the division into Theme(Kind/Mode) and ThemeVariant seems optimal and future-proof

    More people arrived to the same conclusion, which is probably why WPF originally distinguishes "theme name" and "theme color" (which is also why adding another "theme X" is adding to the confusion). For example, the Luna theme has 3 variants, NormalColor, Homestead, Metallic. More recent themes (AeroLite/Aero2 etc.) have only one color variant, NormalColor. One option is to make the Fluent theme fit into this pattern with color variants of Dark, Light and System. The existing themes typically have high contrast look built-in using triggers, which might or might not provide enough flexibility for the modern concept of high-contrast themes. I can see HC variants being treated as another color variants.

    The WPF already seems to have a way to load not only a different theme, but theme from any 3rd party assembly, possibly per-element, using ThemeDictionaryExtension. It accepts assembly name and theme name, but currently does not support specifying the theme color, so I think that scoping out custom theming would therefore be an unnecessary step back for WPF.

    Ultimately, we might want a way for developers to specify 1) an assembly 2) theme name 3) theme color, while keeping the XAML syntax to a minimum. Adding 3 new properties to everything is my least favorite option, especially since not all combinations of values will be valid. At the moment I think it will either have to be a custom type carrying a combination of these values, or, quite WPF-naturally and as the existing infrastructure uses, an Uri, possibly utilizing a markup extension as mentioned couple of times above.

    However, it also doesn't hurt much because you're basically saying "I believe this feature will work completely differently, I just don't know what it will look like". IOW, you're saying "I expect all users of this features having to completely change what code I expect them to write".

    That very accurately describes my understanding of the current state. Not only that, but I am realizing that changing a property type might be a worse outcome if we will have another year of people documenting and blogging about how to use the current API (it's easier to figure out that the required type/property does not exist than that it subtly changed).

    My worry also was that having more complex experimental API is an investment in everyone's time and there will be more reluctance to "fix" it later, while I very strongly believe it needs to be fixed. Knowing that the API review team is happy to engage with the WPF team and open to the future changes is much appreciated.

    With the approved API being experimental and learning the expectations from both sides I don't have objections going ahead as approved. Thanks @terrajobst for your time and expertise!

  25. dipeshmsft commented on Jul 22, 2024

    @dipeshmsft
    MemberAuthor

    @terrajobst, for setting ThemeMode from XAML we want to add a ThemeModeConverter which allows to set the ThemeMode properties as attributes of Application and Window

    namespace System.Windows;
    
    [Experimental]
    public class ThemeModeConverter : TypeConverter
    {
        public ThemeModeConverter();
        public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType);
        public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType);
        public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source);
        public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType);
    }
  26. terrajobst commented on Jul 22, 2024

    @terrajobst
    Contributor

    I assume this would go into the System.Windows namespace. This makes sense to me.

    @dotnet/fxdc unless anyone objects I'll consider this change request approved as well.

  27. terrajobst commented on Jul 23, 2024

    @terrajobst
    Contributor

    Consider it approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Win 11 Themingapi-approvedblocking

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions