dotMorten / WinUIEx

WinUI Extensions
https://dotmorten.github.io/WinUIEx
MIT License
572 stars 36 forks source link

Transparent Window Randomly Losing Transperancy #156

Open Dealman opened 5 months ago

Dealman commented 5 months ago

This is mostly reproducible when moving a window around close to the edges of a monitor, or when resizing a lot/quickly.

image

These windows are usually a green tint, but if I resize them quickly with my mouse they will bug out like this and only a restart seems to fix it.

Not quite sure how to debug and potentially fix this.

This is the window style I use;

WindowStyle windowStyle = WindowStyle.Overlapped;
windowStyle |= WindowStyle.OverlappedWindow;
windowStyle ^= WindowStyle.ThickFrame;
this.SetWindowStyle(windowStyle);

The intended purpose is for the windows to be used as a sort of overlay and this styling disable the horrendously annoying aero snapping.

dotMorten commented 5 months ago

Could you please share a simple app that reproduces the issue? Also which version of Windows?

Dealman commented 5 months ago

Sure, here's a simple one setup via Template Studio. It was too big to upload directly via GitHub.

It seems to be mostly reproducible when resizing via the lower right corner, unless it's just a weird coincidence. The window may also randomly close whilst resizing.

I'm on Windows 11 Pro - Version 10.0.22631 Build 22631

Edit:

I grabbed a quick video to help showcase how I get the issue to occur.

https://github.com/dotMorten/WinUIEx/assets/7038067/a87b4df6-a1aa-4c8c-9887-1155833950f2

dotMorten commented 5 months ago

I was able to reproduce the closing of the window. I never saw the transparency disappear, but did observe closing. No message/error was sent. To track what's going on, I added this to overlay window:

       this.Closed += OverlayWindow_Closed;
       this.AppWindow.Closing += AppWindow_Closing;
       this.AppWindow.Destroying += AppWindow_Destroying;
   }

   private void AppWindow_Destroying(Microsoft.UI.Windowing.AppWindow sender, object args)
   {
   }

   private void AppWindow_Closing(Microsoft.UI.Windowing.AppWindow sender, Microsoft.UI.Windowing.AppWindowClosingEventArgs args)
   {
   }

   private void OverlayWindow_Closed(object sender, WindowEventArgs args)
   {
   }

Then the problem went away, which led me to believe this is WinAppSDK issue with references getting lost. So in MainPage.xaml.cs I created a variable to hold a reference to the Overlay window instead of just declaring a local reference to it in your click handler, and again problem went away, confirming you found an interesting C#WinRT issue, and not a WinUIEx issue.

Again I haven't been able to repro your transparency issue, but I do wonder if it also gets addressed by keeping a reference to the window?

dotMorten commented 5 months ago

I do wonder if you're hitting this issue that's been an issue on Windows for YEARS https://github.com/microsoft/WindowsAppSDK/issues/4068

Dealman commented 5 months ago

Very interesting, since there can potentially be more than one of these windows I'm now adding their references to a List and potentially a Dictionary in the future. Hasn't closed on me yet, the transparency however is very reproducible by just resizing it for a while.

When my window does this however it doesn't affect any other windows. Only the window I'm resizing.

Tried doing some more testing;

  1. If I have multiple instances open, they're all affected by this glitch once I've "triggered" it on one of them.
  2. If I try and open a new window after this glitch has occurred, I will get an error which I can't track down. It wants to attach a debugger, but there's already one attached.
  3. It seems to be some kind of buffer/memory relation potentially, it takes on average ~8 seconds of resizing for this glitch to trigger. Regardless if continuous or if I do it in segments with some pauses in between.
  4. Restarting the Desktop Window Manager does restore some level of transparency, but they're still glitchy.

I'm guessing some SDK version differences between us if you can't reproduce it? Unless it's hardware/driver related.

dotMorten commented 5 months ago

most likely hardware related. We're using the same version since I'm using your project to repro. I'm closing this out since this doesn't seem to be caused by WinUIEx itself - all the transparency rendering and maintaining it is handled by WinUI.

dotMorten commented 5 months ago

Could also be this: https://github.com/microsoft/microsoft-ui-xaml/issues/8423 It was fixed in the latest 1.5exp2. Perhaps you could confirm with that version whether you still see it?

Dealman commented 5 months ago

I tried updating it and sadly, no difference. I also tried disabling the SystemBackdrop on my main window but the issue persists. I've also updated my GPU drivers to the latest.

Guess maybe I should post my findings as an issue over there instead?

softworkz commented 2 months ago

most likely hardware related. We're using the same version since I'm using your project to repro. I'm closing this out since this doesn't seem to be caused by WinUIEx itself - all the transparency rendering and maintaining it is handled by WinUI.

@dotMorten - Not quite. You are creating a new brush for clearing the background on each call.

https://github.com/dotMorten/WinUIEx/blob/a830af73719a65c05c0b9f514afbc5fe4f32abc3/src/WinUIEx/TransparentTintBackdrop.cs#L103-L112

This simply hits a limit at some point and then you cannot create any more new brushes - then the transparency gets lost.

To reproduce, you just need to move the window around for a while and be a bit patient, then it will happen.

You should create the brush once and cache it. On dispose, pinvoke DeleteObject, etc..

dotMorten commented 2 months ago

@softworkz Nice find. Not sure what I was thinking there :-)

softworkz commented 2 months ago

Not sure what I was thinking there :-)

In my case it's usually "first, try whether it works and do it right later". And then forget about it... ;-)