question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamically changing themes since 0.10 takes much longer

See original GitHub issue

My themes are just changing colors and icons. I change themes by simply doing:

App.Current.Styles[0] = theme.Style;

This was using Avalonia 0.9.11. Changing themes was instant ezgif-5-815164378c67

But since Avalonia 0.10 this process takes a minimum of 30 seconds up to 5 minutes (depends on how long the application was running) and uses 100% CPU on one core (so it’s faster to just restart). The same goes for changing the editor font. I noticed that the longer the application is running the longer changing themes take

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
amwxcommented, Mar 16, 2021

Just some of my own findings relating to this… (note this is from the Fluent theme only)

FluentLight.xaml (for example) contains all the color/brush/etc resources AND the control templates.

<StyleInclude Source="avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml" />
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Accents/Base.xaml" />  
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Accents/FluentBaseLight.xaml" />
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Accents/FluentControlResourcesLight.xaml" />
<StyleInclude Source="avares://Avalonia.Themes.Fluent/Controls/FluentControls.xaml" />

So, if you switch themes simply by replacing FluentLight.xaml with FluentDark.xaml (or using the new FluentTheme syntax) every control ends up being re-templated. In a simple app, with two buttons to switch theme (light/dark) and an items control with 50 buttons within it (so 52 total), this takes over 1 second to complete*.

If you isolate out FluentControls.xaml and only change the resource xaml files (the first four above), this time gets cut down to somewhere between 270-300ms*, which is a significant improvement.

In addition, within the resource files, all the margins, border thicknesses, etc… are also included and get swapped out every time. AFAIK, those resources don’t change between light/dark mode (even though WinUI does include some of them in their respective ThemeDictionaries), they might change with the high contrast theme, but I’m not worrying about that right now. So, to test this, I isolated all color/brush resources to their own xaml file, and had all other resources to their own:

Light.xaml / Dark.xaml - Colors/brushes only CommonResources.xaml - Border thicknesses/Margins/etc FluentControls.xaml - Templates

Then only switched the color/brush xaml file, which further cut down the time to about 120-130ms*

*= This was while in debug mode, on my machine w/ i7-9700.

So I think part of this could be solved by isolating the templates (and probably the non brush/color resources) from the brush/color resources to that only the latter is switched.

Though, if you increase the number of active controls (50 buttons to 300 for ex), the time does still slow down (using the last method, for example, takes about 400ms), so there’s still some more to this, but I’m not sure what.

Also side note: when I was trying to condense the four resource files down, I noticed there are a lot of duplicate resources among the files (I’m assuming this came from copying both from generic.xaml and from the WinUI source).

Also, the <FluentTheme/> doesn’t handle mode switching (there’s no property changed handler), so you have to completely replace the current <FluentTheme/> when switching themes (not sure if this was just overlooked or is by design).

1reaction
grokyscommented, Sep 11, 2020

Hmm, strange - I thought the slowness of changing themes was caused by the fluent theme, but looks like there’s something else going on. Will investigate.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamically changing themes in nixOS
I was wondering if anyone had any example configs or tips for dynamically changing the theme with nixos. I'm not talking anything fancy, ......
Read more >
Dynamic Theme Stopping After 10 Minutes : r/PS4
Try setting a dynamic theme, then going to your home screen and not touching the system for 10 minutes. Upvote 1
Read more >
Create powerful dynamic themes and layouts using ...
Create powerful dynamic themes and layouts using ElectronCSS 0.10 new framework agnostic dynamicCSS feature | by Yann s | ITNEXT.
Read more >
Dynamic theme change in Semantic-UI
I know it's possible to change the theme dynamically since the semantic-ui demos all do it. Unfortunately, the Theming page and all ...
Read more >
Phanerozoic Diversity Patterns: Profiles in Macroevolution
... species numbers for an internally, dynamically changing group of plants. ... the origination rates of the Mesozoic cycads and cycadeoids (0.10 and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found