How to change Mica TintColor which does support Dark/Light Theme?

Mahdi Hosseini 135 Reputation points
2024-01-21T20:13:18.5766667+00:00

Hi, i am trying to use Mica with Custom TintColor, i ended with using this helper class:

internal class MicaSystemBackdrop : MicaBackdrop
{
    public Window window;
    public WindowsSystemDispatcherQueueHelper m_wsdqHelper;
    public MicaController micaController;
    public SystemBackdropConfiguration m_configurationSource;
    public MicaKind kind;
    public MicaSystemBackdrop(Window window, MicaKind kind)
    {
        this.window = window;
        this.kind = kind;
    }

    public void Disconnect()
    {
        window.Activated -= Window_Activated;
        window.Closed -= Window_Closed;
        ((FrameworkElement)window.Content).ActualThemeChanged -= Window_ThemeChanged;
        micaController.RemoveAllSystemBackdropTargets();
        micaController.ResetProperties();
        micaController.Dispose();
        micaController = null;
    }
    public bool TrySetMicaBackdrop()
    {
        if (MicaController.IsSupported())
        {
            m_wsdqHelper = new WindowsSystemDispatcherQueueHelper();
            m_wsdqHelper.EnsureWindowsSystemDispatcherQueueController();

            // Hooking up the policy object
            m_configurationSource = new SystemBackdropConfiguration();
            window.Activated -= Window_Activated;
            window.Activated += Window_Activated;
            window.Closed -= Window_Closed;
            window.Closed += Window_Closed;
            ((FrameworkElement)window.Content).ActualThemeChanged -= Window_ThemeChanged;
            ((FrameworkElement)window.Content).ActualThemeChanged += Window_ThemeChanged;

            // Initial configuration state.
            m_configurationSource.IsInputActive = true;
            SetConfigurationSourceTheme();

            micaController = new MicaController();
            micaController.Kind = kind;

            // Enable the system backdrop.
            // Note: Be sure to have "using WinRT;" to support the Window.As<...>() call.
            micaController.AddSystemBackdropTarget(window.As<Microsoft.UI.Composition.ICompositionSupportsSystemBackdrop>());
            micaController.SetSystemBackdropConfiguration(m_configurationSource);
            return true; // succeeded
        }

        return false; // Mica is not supported on this system
    }

    private void Window_Activated(object sender, WindowActivatedEventArgs args)
    {
        m_configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated;
    }

    private void Window_Closed(object sender, WindowEventArgs args)
    {
        // Make sure any Mica/Acrylic controller is disposed so it doesn't try to
        // use this closed window.
        if (micaController != null)
        {
            micaController.Dispose();
            micaController = null;
        }
        window.Activated -= Window_Activated;
        m_configurationSource = null;
    }

    private void Window_ThemeChanged(FrameworkElement sender, object args)
    {
        if (m_configurationSource != null)
        {
            SetConfigurationSourceTheme();
        }
    }

    private void SetConfigurationSourceTheme()
    {
        switch (((FrameworkElement)window.Content).ActualTheme)
        {
            case ElementTheme.Dark: m_configurationSource.Theme = SystemBackdropTheme.Dark; break;
            case ElementTheme.Light: m_configurationSource.Theme = SystemBackdropTheme.Light; break;
            case ElementTheme.Default: m_configurationSource.Theme = SystemBackdropTheme.Default; break;
        }
    }
}

i can change mica tintcolor by using micaController.TintColor = color; however it seems that this only works in Light Theme and does not reflect to Dark Theme! this is TintColor in Light Theme which is correct: User's image

and this is TintColor in Dark theme which i think is wrong and should be changed: User's image

so: Are the changes made in the dark theme correctly? (Is there a problem with my eyes? :) ) i dont want to change tint opacity so tint opacity should be obtained automatically for light and dark them so i guess issue come from here? there is another way for implementing Mica with Class : MicaBackdrop and Overriding TargetConnected but in ms-doc mentationed that if we change tint color, backdrop theme does not update automatically and we should update it manually. i dont care about which method we should use, i just want to fix this issue and i can have a tinted backdrop in Light/Dark correctly.

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
747 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,574 questions
{count} votes