dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.89k stars 1.69k forks source link

[Windows] Acrylic backdrops problem with contrast or color #12328

Open navi705 opened 1 year ago

navi705 commented 1 year ago

Description

I use acrylic backdrop, but I have a problem with color or how I think with contrast. I use the same code in winui 3 and maui, but the result is different. In maui color more dimmer. I would like it to be like winui. Sorry if i did something wrong, please correct me. image

Steps to Reproduce

  1. Create a new Default MAUI App
  2. Add in MauiProgram.cs
    
    #if WINDOWS
    using TestMauiApp.Platforms.Windows;
    #endif

builder.ConfigureLifecycleEvents(events => {

if WINDOWS10_0_17763_0_OR_GREATER

        events.AddWindows(wndLifeCycleBuilder =>
        {
            wndLifeCycleBuilder.OnWindowCreated(window =>
            {
                window.TryMicaOrAcrylic(); // requires 'using YOUR_APP.WinUI;'
            });
        });

endif

    });
3. Add class file in your Platforms/Windows folder

using Microsoft.UI.Composition.SystemBackdrops; using Microsoft.UI.Composition; using Microsoft.UI.Xaml; using Windows.UI.ViewManagement; using WinRT; using Windows.UI;

namespace TestMauiApp.Platforms.Windows { public static class WindowHelpers { public static void TryMicaOrAcrylic(this Microsoft.UI.Xaml.Window window) { var dispatcherQueueHelper = new WindowsSystemDispatcherQueueHelper(); // in Platforms.Windows folder dispatcherQueueHelper.EnsureWindowsSystemDispatcherQueueController();

        // Hooking up the policy object
        var configurationSource = new SystemBackdropConfiguration();
        configurationSource.IsInputActive = true;

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

        if (DesktopAcrylicController.IsSupported())
        {
            var acrylicController = new DesktopAcrylicController();
            acrylicController.LuminosityOpacity = 0.3f;
            acrylicController.TintOpacity = 0.7f;
            var uiSettings = new UISettings();
            var color = uiSettings.GetColorValue(UIColorType.AccentDark2);
            acrylicController.TintColor = color;
            acrylicController.FallbackColor = color;
            acrylicController.AddSystemBackdropTarget(window.As<ICompositionSupportsSystemBackdrop>());
            acrylicController.SetSystemBackdropConfiguration(configurationSource);

            window.Activated += (object sender, WindowActivatedEventArgs args) =>
            {
                if (args.WindowActivationState is WindowActivationState.CodeActivated or WindowActivationState.PointerActivated)
                {
                    // Handle situation where a window is activated and placed on top of other active windows.
                    if (acrylicController == null)
                    {
                        acrylicController = new DesktopAcrylicController();
                        acrylicController.AddSystemBackdropTarget(window.As<ICompositionSupportsSystemBackdrop>()); // Requires 'using WinRT;'
                        acrylicController.SetSystemBackdropConfiguration(configurationSource);
                    }
                }

                if (configurationSource != null)
                    configurationSource.IsInputActive = args.WindowActivationState != WindowActivationState.Deactivated;
            };

            window.Closed += (object sender, WindowEventArgs args) =>
            {
                if (acrylicController != null)
                {
                    acrylicController.Dispose();
                    acrylicController = null;
                }

                configurationSource = null;
            };
        }

    }

}

}

4. Add class file in your Platforms/Windows folder

using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Runtime.InteropServices; using Windows.System; // Required for DllImport attribute and DispatcherQueue

namespace TestMauiApp.Platforms.Windows { public class WindowsSystemDispatcherQueueHelper { [StructLayout(LayoutKind.Sequential)] struct DispatcherQueueOptions { internal int dwSize; internal int threadType; internal int apartmentType; }

    [DllImport("CoreMessaging.dll")]
    private static extern int CreateDispatcherQueueController([In] DispatcherQueueOptions options, [In, Out, MarshalAs(UnmanagedType.IUnknown)] ref object dispatcherQueueController);

    object m_dispatcherQueueController = null;

    public void EnsureWindowsSystemDispatcherQueueController()
    {
        if (DispatcherQueue.GetForCurrentThread() != null)
        {
            // one already exists, so we'll just use it.
            return;
        }

        if (m_dispatcherQueueController == null)
        {
            DispatcherQueueOptions options;

            options.dwSize = Marshal.SizeOf(typeof(DispatcherQueueOptions));
            options.threadType = 2;    // DQTYPE_THREAD_CURRENT
            options.apartmentType = 2; // DQTAT_COM_STA

            CreateDispatcherQueueController(options, ref m_dispatcherQueueController);
        }
    }
}

}

5. Comment out this line line in App.xaml 


### Link to public reproduction project repository

https://github.com/navi705/Maui-Acrylic-Report

### Version with bug

7.0 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

Windows

### Affected platform versions

Windows 11 Pro Version 22H2 22621.674

### Did you find any workaround?

_No response_

### Relevant log output

_No response_
ghost commented 1 year ago

Hi @navi705. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

navi705 commented 1 year ago

https://github.com/navi705/Maui-Acrylic-Report

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

XamlTest commented 1 year ago

Verified this on Visual Studio Enterprise 17.7.0 Preview 1.0. Repro on Windows 11 with below Project: Maui-Acrylic-Report.zip

Left is WinUI, right is Maui. image