Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having a hard time applying ThemeService settings to all open windows #26

Open
MPITech opened this issue Jan 19, 2025 · 12 comments
Open
Labels
bug Something isn't working

Comments

@MPITech
Copy link

MPITech commented Jan 19, 2025

Hi. I had the tint working at one point but then you told me that the ThemeService shouldn't be initialized for every window and that TrackWindow should help changes apply to all open windows. However, I'm having a hard time getting it to work now after making some changes. The example in the gallery app only shows how to initialize and not how to make changes and apply to all open windows.

I've tried different combinations of these methods when I change any of my Settings.Local.XXX properties where I save the user's backdrop, tint color, and theme settings:

 ThemeService.ConfigureBackdrop(Settings.Local.BackdropType, true);
 ThemeService.SetBackdropTintColor(Settings.Local.TintColor);
 ThemeService.ConfigureTintColor(Settings.Local.TintColor, true);
 ThemeService.ConfigureElementTheme(Settings.Local.ActualTheme, true);
 ThemeService.SetElementThemeWithoutSave(Settings.Local.ActualTheme);

And still, I cannot get the TINT color to apply anymore on ANY window, and the dark/light setting only applies to the main window that I initialized the ThemeService on.

Can you please explain how to properly initialize the backdrop, theme, and tint color on startup? What is the difference between "Set___" and "Configure___" methods in ThemeService?

Also, can you please explain how to properly apply changes to tint color, for example, to all open windows? I checked in debug mode, my windows are properly added using DevWinUI.WindowHelper.TrackWindow in my custom CreateWindow method (I had to use my own).

Adding these explanations to the Gallery App may be helpful to others going forward as well. 🚀

Thanks a lot!

@MPITech
Copy link
Author

MPITech commented Jan 19, 2025

I just reviewed https://ghost1372.github.io/DevWinUI/themeService/ and I'm still missing something apparently because I still cannot get it to work right across multiple windows.

@ghost1372
Copy link
Owner

Hi @MPITech
you should use Configure methods when you are initialize themeservice, if you are using AutoInitialize you dont need to specify Backdrop/ElementTheme, if you are using Initialize method, you need to use Configure Methods to specify default options.
for changing Backdrop/Theme in runtime you should use Set methods. also SetElementThemeWithoutSave is a debug method which only change ElementTheme without saving.

https://ghost1372.github.io/DevWinUI/themeService/

i have a working sample which i can open some windows and change backdrop for all windows.

// Open Windows

private void myButton_Click(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        var window = new Window();
        WindowHelper.TrackWindow(window);
        window.SystemBackdrop = App.Current.ThemeService.GetSystemBackdrop();
        window.Activate();
    }

}

a button which can randomly change backdrop:

private void BtnChangeBackdrop_Click(object sender, RoutedEventArgs e)
{
    switch (App.Current.ThemeService.GetBackdropType())
    {
        case BackdropType.None:
            App.Current.ThemeService.SetBackdropType(BackdropType.Mica);
            break;
        case BackdropType.Mica:
            App.Current.ThemeService.SetBackdropType(BackdropType.MicaAlt);

            break;
        case BackdropType.MicaAlt:
            App.Current.ThemeService.SetBackdropType(BackdropType.DesktopAcrylic);

            break;
        case BackdropType.DesktopAcrylic:
            App.Current.ThemeService.SetBackdropType(BackdropType.AcrylicThin);

            break;
        case BackdropType.AcrylicThin:
            App.Current.ThemeService.SetBackdropType(BackdropType.AcrylicBase);

            break;
        case BackdropType.AcrylicBase:
        case BackdropType.Transparent:
            App.Current.ThemeService.SetBackdropType(BackdropType.None);

            break;
    }
}

you should know that when window is opening/Activated, we need to set default backdrop so i think here you have some issues and you think it is not working.
just set default backdrop like this:

window.SystemBackdrop = App.Current.ThemeService.GetSystemBackdrop();

Unfortunately, now that I've checked, TintColor has a bug and only applies to one window. I'll try to fix this problem soon.

@ghost1372 ghost1372 added the bug Something isn't working label Jan 19, 2025
@MPITech
Copy link
Author

MPITech commented Jan 19, 2025

Thanks a lot. I will make some changes and see if I can get it to work now that I have a better handle on what methods to use and where to use them.

And I am glad you found the tint color problem with multiple windows. I'll have to see why I couldn't get it to work on even my first window anymore. Will be in touch shortly.

ghost1372 added a commit that referenced this issue Jan 19, 2025
ghost1372 added a commit that referenced this issue Jan 19, 2025
@ghost1372
Copy link
Owner

Thanks a lot. I will make some changes and see if I can get it to work now that I have a better handle on what methods to use and where to use them.

And I am glad you found the tint color problem with multiple windows. I'll have to see why I couldn't get it to work on even my first window anymore. Will be in touch shortly.

Ok, i fixed TintColor issue and now we can change tint color for all windows.
I now need to find a way to apply all default settings (backdrop/theme/tint) to opened windows by default.

Image

@MPITech
Copy link
Author

MPITech commented Jan 19, 2025

Thanks, window.SystemBackdrop = App.Current.ThemeService.GetSystemBackdrop(); helped on each window so the backdrop is correct now when it changes. However, while I am waiting to try the Tint fix, I noticed that not all controls on each window re-color correctly when the ElementTheme/ActualTheme is changed from Dark to Light, for example.

It looks like the main window correctly recolors all controls, but the additional windows do not correctly change all controls. For example, many TextBlocks that were showing white text when Dark theme is set will still show white text when switching to Light in all of the secondary windows. If I exit the app and restart it in Light mode from the start, these windows/controls appear correctly, with black TextBlocks.

I verified and none of the controls' foregrounds are manually set anywhere, so that isn't the issue.

Are you able to replicate this as well?

Thanks again!

@MPITech
Copy link
Author

MPITech commented Jan 19, 2025

I think I isolated the issue to the problem only happening with controls that are inside UserControls in each window. They aren't recoloring properly when the parent window's content's ActualTheme is changed.

@ghost1372
Copy link
Owner

@MPITech
i dont see any issues

Image

ghost1372 added a commit that referenced this issue Jan 20, 2025
@ghost1372
Copy link
Owner

@MPITech I Fixed ThemeService
now, if you open multiple windows, Theme, Backdrop and TintColor will be set automatically. and changing backdrop, Tint, Theme in runtime will be affect all opened windows.

@MPITech
Copy link
Author

MPITech commented Jan 20, 2025

@MPITech i dont see any issues

Was "Test" in a UserControl?

I'll try the new version when it is posted to Nuget and see if it helps. Thanks!

@ghost1372
Copy link
Owner

@MPITech i dont see any issues

Was "Test" in a UserControl?

I'll try the new version when it is posted to Nuget and see if it helps. Thanks!

Yes.
this is my code:

for (int i = 0; i < 5; i++)
{
    var window = new Window();
    var uc = new UserControl();
    uc.Content = new TextBlock { Text = "Test", FontSize = 64, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center };
    window.Content = uc;
    WindowHelper.TrackWindow(window);
    window.Activate();
}

i get white text in dark and black text in Light which is correct.

Image

@MPITech
Copy link
Author

MPITech commented Jan 20, 2025

Thanks, I will try and get more details for replicating when I install the next preview. When do you anticipate that will be on nuget? Thanks!

@ghost1372
Copy link
Owner

Thanks, I will try and get more details for replicating when I install the next preview. When do you anticipate that will be on nuget? Thanks!

i am waiting for WinUI Community Call, If they release a new version soon, I'll wait. If there isn't a new version, I'll release it by the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants