Repository navigation
Description
In WPF we have two types of styles for any FrameworkElement - Style and ThemeStyle. Style attribute allows developers to provide custom styles for the controls, whereas ThemeStyle provides a default style which will be used in the absence of custom styles provided by the developer or when developers override only a few properties for the control in the custom style. ThemeStyle ( from here on I will refer to them as default style ) is an internal property and developers don't have access to this property. The default styles are loaded by the framework based on the OS version and color scheme.
As of now, Fluent theme can be loaded by including the resource dictionary in App.xaml, which means that the framework treats them as non-default style and they are loaded in Style property for the framework. Ideally we would want to load them as default styles however, there are some constraints to that approach.
Constraints of making Fluent a theme style
- Theme style lookup is different from the lookup of other styles. When a resource dictionary is loaded as default style, we can not search for resources ( like brushes , colors or styles ) which have a key of type
string. Theme resource dictionaries are only searched for resources when the key is either aTypeor aResourceKey. This will prevent a developer from using resources defined in Fluent theme dictionary. Essentially, that means, we won't be able to access AccentButtonStyle, ControlElevationBrush, etc. by referencing them in custom style as it is possible in WinUI.
<!-- These scenarios will be blocked, where AccentButtonStyle and ControlElevationBorderBrush is present in Fluent theme dictionary -->
<Button Style="{DynamicResoucrce AccentButtonStyle}" />
<CustomTextBlock BorderBrush="{DynamicResource ControlElevationBorderBrush}" /> - Theme styles do not support DynamicResource very well. Uptil now, if you had to use DynamicResource in theme style, you would have to use ComponentResourceKey or follow a pattern similar to SystemColors, where we have a set of colors and brushes that can be referenced. This restricts the way in which we can define and use accent colors and brushes. For theme dictionaries when we use DynamicResource, we get back DeferredThemeResourceReference instead of DeferredResourceReference, which gets inflated, frozen and cached.
To deal with the above constraints, we have two options :
- Modify the fluent theme resources and styles to fit in the current system : That would require exposing public API for accent colors. Making some resources static, creating seperate theme files for light and dark mode ( we are on top of this already ).
- Enhance the capabilities of theme resources : Allow searching for resources using string keys. Allowing DynamicResource to work with theme resource in full capacity. ( I have been working on this, but this is more complex than I thought and will require more time and efforts. ). This will also open up the framework for more flexibility, but at the same time make it easier for things to go wrong.
We don't face these issues in the current setup ( i.e. fluent theme resources are loaded in app.xaml ). In the current setup, the styles are treated as implicit styles ( when we don't provide a key for a style or we provide the Type as the key, it get's used for all controls without specifying it everywhere ) and we can search for any resource defined in the resource dictionary. Now, when we use this resource dictionary via app.xaml, DynamicResource works as we all know it.
Although the current setup is temporary, and even if we continue with the current setup in .NET 9, it has it's own disadvantages.
Issues with using Fluent as implicitly loaded style
- Whenever, framework elements are generated via code ( like in DataGrid ) the framework creates the object and does not set the Style property, it relies on ThemeStyle for it's styling. This will cause inconsistent UI in the application. That is the reason why, we had to add checks at some places in code to maintain that consistency in UI. ( Still need some more investigations here )
- It becomes very easy to modify resources. This can lead to different scenarios of using the themes, it makes it easy to remove some particular styles or resources which can again lead to inconsistencies in UI.
- The whole setup depends on the presence of an instance of class Application, which may not be present in some scnarios. As this current theme loading and switching is temporary, and once we move from this model to the default one, we will hit the constraints there. Now, where we can access the resources by string key names, once we make the theme default, won't work anymore until and unless the capabilities of theme resource are increased. However, doing that would would essentially mean that we are treating resource names as public contracts, and would need to deal with performance issues.
Keeping in mind the time frame for .NET 9 and that we are already past preview 6, getting all the features completed is difficult. So, we would like to seek community opinion on what path shall we take for now.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fields🥅 Todo
Activity
dipeshmsft commentedon Jun 21, 2024
cc @miloush @batzen @pomianowski @ThomasGoulet73 @lindexi @MichaeIDietrich @robert-abeo @riverar @KirillOsenkov @Symbai
lindexi commentedon Jun 22, 2024
Given the current time constraints, our development window is rapidly closing. I am inclined to opt for "Modify the fluent theme resources and styles to fit in the current system". My rationale is that it's preferable to choose a fully functional option with minor flaws, rather than a more refined but potentially incomplete solution. There's no perfect balance between functionality and resource allocation. My only concern is that this choice might result in legacy issues, making it difficult to modify the framework behavior in the future.
Currently, my concerns are as follows:
miloush commentedon Jun 22, 2024
Thanks Dipesh!
First of all, I don't think the Fluent theme should become the default theme. It is designed for different kind of apps (with less information density) than what I expect most developers to be using WPF for, and it could hamper the adoption of newer .NET versions. I am open to be convinced otherwise, but I would think we should have a few releases with the theme shipped and perfected before making it default.
Second, a theme should be a "proper" theme. Having it in App.xaml as merged dictionary (i.e. "implicitly loaded" above) is not how themes should be shipped or used in production. In addition to the issues discussed above, it changes the precedence of resources for dependency properties. However, I understand that not everything is or will be ready in time for .NET 9 and if we want to keep the new theme "for testing" through next release, I am guessing keeping it as is would continue to serve the purpose.
For the technical concerns raised:
That makes me support the "Modify the fluent theme resources and styles to fit in the current system" option.
My preference would be:
If there is time:
If there is no time, continue having the theme available for testing and debugging purposes "on the side" for .NET 9, but do not ship any API changes in support of it.
Symbai commentedon Jun 22, 2024
Lets say I have made an app which I have overridden some colors on some controls for some purpose. Then I just recompile it on .NET 9.0 or whatever and suddenly it has this "fluent theme" which then collided with my changes. I don't think this is a good idea. IF you make breaking changes then it should be something worth it. Like WPF now allows AOT, trimming and what not. (and they're on by default) Then I would be okay with making these changes too. Not because they are necessary but because we are already having huge breaking changes that it won't matter anyways. But we don't have them yet. The only thing we have is this fluent theme which, at the time I was testing it, contains issues. It makes no sense to me to make fluent theme a default theme, at this moment! This is not a technical perspective but a rational.
dipeshmsft commentedon Jun 22, 2024
Before this discussion moves on, when talking in terms of .NET 9, there will be a switch that will allow the framework to load Fluent as the default theme. I know there are issues with styles and Fluent specific style for all controls is not yet present, so there is no question of Fluent being system theme by default.
@miloush @Symbai @lindexi, hope this clears our stand when we are talking about Fluent as default theme.
As mentioned above, there are wide differences in the capabilities of both methods. In the current system, there is flexibility of overriding brushes and colors, referencing default styles and creating custom styles and this will setup a precedence that all of these features will be supported in next versions. However, loading as default ( still opt-in ) currently does not support all this, so moving ahead we will be adding functionalities to the theme rather than subtracting them.
Loading themes implicitly, i.e. adding the fluent dictionary to app.xaml is always present as a choice to developers to support higher level of customizations.
Coming to the API proposals, there are two proposals that we can work on :
robert-abeo commentedon Jun 24, 2024
There is a lot to this discussion:
I was expecting you all to ship this with the current method: implicit styles that we have now registered in the app merged dictionaries overriding the theme styles. That said, I have personally encountered the issue of theme styles being used for programmatically created controls and then design inconsistency as the style is forcefully changed. It's not ideal in this way but seems the lesser of two evils (different for all of your assessments above).
The reason I say this is the lesser of two evils is simply because we MUST have access to the resources in the fluent styles. There are several custom controls that need to use shared resources (by key) in their control themes. Accent button is one example noted above as well. All of the Fluent v2 theme is designed to work this way and all existing code (even in WPF) is setup to use this. We are going to have a really difficult time moving away from other Fluent theme implementations if we loose this capability here. It's probably going to end up meaning we just copy the theme and register it in merged dictionaries ourselves.
As far as dynamic resource usage everywhere, it's required because in modern windows we are expected to be able to dynamically change between light/dark themes. I suppose I would be OK with a mechanism that supports this change only at startup rather than runtime though. Still, ThemeResource in WinUI is looking important right about now.
The concern about Fluent NOT being the default theme I somewhat agree with. Most legacy applications are designed for different information density than modern Windows desktop apps. HOWEVER, we have the compact density styles in WinUI to compensate for this. Hopefully that will be carried over here but I haven't dug into the code in that area.
Personally, like WinUI/Uno/Avalonia, I would expect a way to choose the theme in the application class at startup. In WinUI2 you could switch between Fluentv1/2 as desired and set the density style. Avalonia also allows registering the theme to use in App.xaml and there are a number of themes available even from 3rd parties. WPF locking this all down for theme styles and registering them behind the scenes was a mistake.
There are too many points to address right now but I'm advocating for just using the method we have now:
The ONLY downside I see to this is for programmatically generated elements like popups that might retain Aero2 in some cases. But I consider that the lesser of two evils until the WPF theme system is upgraded to support what we need here (as discussed in the original description). My assumption is we can move to a more integrated design in the future with minimal breaking changes.
miloush commentedon Jun 24, 2024
Can you give some examples beside the accented button?
You don't need dynamic resources for that, just two versions of the theme.
robert-abeo commentedon Jun 24, 2024
No, I can't share specific code examples. But think of it this way: If you are writing a ColorPicker or other custom control you need to have access to the underlying Fluent theme resources. These resources are consumed by all custom controls to implement their themes that match Fluent. If you are using nothing but stock controls, sure, you can let it all be hidden away. But as soon as you start extending things you need access to the keyed resources. (Please don't recommend duplicated code here too).
Edit: Another few cases in an existing app: accent colors need to be customized (user has some ability to adjust themes) and certain in-box control themes are customized too. If you can't have access to the underlying resources (think colors, brushes, etc.) this is a lot more difficult.
That's a lot of duplicated code that is definitely worse in my book that using dynamic resources. If the two themes could be GENERATED from what we have now I would be OK with it. But we are well past the point in time where it makes sense to duplicate code like this.
miloush commentedon Jun 24, 2024
I meant examples of resources you think need to be accessible.
Which ones?
We have had the current system for years spanning several themes, so it's not like new limitations are being introduced. If you are lacking features, it would be useful to keep track of them.
Sure, that's how it has been for decades now.
wpf/src/Microsoft.DotNet.Wpf/src/Themes/XAML/GroupBox.xaml
Lines 97 to 138 in 6d26b95
robert-abeo commentedon Jun 24, 2024
@miloush You misunderstand my viewpoint. I have used the existing Fluent v1/v2 themes in UWP/WinUI and am currently using a customized version of ModernWPF that supports Fluentv2. What I am referring to are features that are expected in modern Fluent themes. I'm not referring to features that are in legacy WPF. So when you say "We have had the current system for years spanning several themes" that's true BUT we have all moved past that with third party libraries now.
For example, all Fluent2 themes for WPF (third party) have used the dynamic resource approach or some variant that allows switching between light/dark WITHOUT duplicating the entire theme and customizing it for light or dark. What I'm saying is the ecosystem has evolved quite a bit here and the expectations are now different than what was in Aero2. If you take a lot of that functionality away we simply will not be able to use the Fluent2 theme in WPF and will continue using third-party implementations that have the expected functionality. I'm trying to make it possible and more streamlined for those applications that are already using a third-party Fluent2 theme to be able to migrate to the official one here.
I'm not going to invest the time digging up all the Fluent2 resource keys we use in custom controls right now. I think I was clear in why it's important to be able to access resources by key:
miloush commentedon Jun 25, 2024
I believe 2. was explicitly stated as undesirable. For 3., the colors and brushes in SystemColors can be overridden, it's only string keys that cannot be looked up. Basically there needs to be a contract between themes and the framework of known resources. To extend the current contract, you need to know which ones are missing for 1.
That makes it difficult to argue a point, I am sure your experiences would be valuable. There is no need to list them all. If you can find a few, especially referencing a Style like in the case of accented button, it would be helpful.
I don't remember that being a goal at any point. People are also free to continue using whatever works for them.
What I'm trying to avoid is 1) having two completely separate, incompatible theme systems 2) unnecessary development and maintenance of something that will be obsoleted with next theme 3) significant performance degradation.
batzen commentedon Jun 25, 2024
@miloush
dipeshmsft commentedon Jun 25, 2024
@batzen
Mangling is something that we can control,, earlier it was done for optimization and because developers did not have access to the
stringbased key resources. So, not mangling but being able to findstringbased key resources in theme style resources is the issue.We haven't tried this yet.
@robert-abeo
Regarding the point @miloush said, although loading in app.xaml gives us a great amount of flexibility and even if we continue with the same model, I don't think that is the right places as it disables the scenarios where
Applicationinstance may not be present. It would have to be in a more neutral location.For this, I have written a script to combine the resource dictionaries into one, similar to the
ThemeGeneratorthat we have for earlier themes. I will put it up by the end of the week.riverar commentedon Jun 25, 2024
23 remaining items
robert-abeo commentedon Jul 1, 2024
@miloush
This has been discussed a lot above but I strongly disagree. It is basically "string keys" or "nothing". Without access to string keys you can't customize or use Fluent2 as it was designed. Yes, the app can just copy the entire theme in itself (which I said above we would have to do if you went this route) but that is defeating the purpose of providing a built-in Fluent theme. Why even bother at that point?
miloush commentedon Jul 1, 2024
I don't see how that prevents you from customization of the theme. Instead of
{StaticResource TextControlElevationBorderBrush}you would do{StaticResource fluent:ThemeResources.TextControlElevationBroderBrush}or something.robert-abeo commentedon Jul 1, 2024
@miloush
Yes, we can just provide our own color and brush resources, then our own styles and control templates we need to customize. In fact we can just provide our own theme. You are missing the point here.
Go use Fluent in real-world apps with WinUI 3. Learn about ThemeResource, the light-weight styling resource concept etc. Then use some WPF 3rd party Fluent theme libraries. With the same features supported by DynamicResource and the same string keys.
I really don't think you understand how Fluent theme is used today and what we have in 3rd party libraries. We need others to jump in on this discussion to break the tie but then we just restart all over again. It's pointless at this point for me to debate this further with you.
miloush commentedon Jul 1, 2024
Doesn't have to be static, you could as well do
{DynamicResource fluent:ThemeResources.TextControlElevationBorderBrushKey}. I am setting aside the discussion on how themes should work or why, I am suggesting a compromise that gives you access to all the resources a theme deems to be replaceable without having to redesign the whole infrastructure.robert-abeo commentedon Jul 1, 2024
@miloush
Yes, I know you can use DynamicResource as well. But you are missing the forest for the trees. You cannot for some reason see the big picture here. If you were on my team you would be out of the discussion at this point.
SystemColors in WPF today is what we have to avoid the above. In Fluent the system colors are built-in as resources. You are essentially saying you can customize Aero2 without SystemColors. That's true but then you have to copy and re-create the entire theme piece-by-piece as needed. Of course the WPF designers thought that was bad practice so provided SystemColors to begin with. Of course we are not going to invest this level of effort either. We will just use another Fluent theme that does what it was designed to do and gives use the resources by default.
It's frustrating for me to debate this with you. You fundamentally do not understand my viewpoint and you do not understand Fluent. I will tell you for the last time: If you release Fluent as only built-in system theme styles with all the limitations discussed above it will be unusable for us. Since I have an enormous amount of experience with Fluent you might want to listen to this data point.
I'm now stepping out of the discussion. It's not worth my time going in circles like this.
miloush commentedon Jul 1, 2024
No I am saying the theme assembly can have its own theme-specific equivalent of SystemColors.
jaelys commentedon Jul 28, 2024
I've been using WPF for more than 10 years, and I've had a lot of designs and user needs, so I'd like to share the actual development process
1.Win7, I pursue the establishment of acrylic effect, Win10, I pursue the establishment of Mica effect, can only simulate, the effect performance is not good, Win11, WPF is still in place, WinUI 3 sees hope, but can only wait, can not support my project production migration
2.Win7,I pursue Fluent,Win10,I pursue Fluent2,Win11...., yes, all my projects never use the system preset style and color, because whether it is custom or using a third-party Fluent control, I always have to copy and merge all the templates and style keys, including the style of handling light and dark, including the color of the system preset, to ensure that the definition is not repeated
I'm looking forward to WPF Win11 becoming the system preset, and if I can customize it directly with the preset, it will take more than a decade of burden off my mind to focus on functionality and performance optimization
In fact, most of the current third-party Fluent controls, I still have to extract some of them and re-customize the style, and I also have to understand how the third-party used was originally defined, and it would be best if WPF could be unified, so that third-party controls can focus on more control extensions, which is also better for the WPF ecosystem
I don't have great skills, but I love WPF