winit icon indicating copy to clipboard operation
winit copied to clipboard

Transparent example not working

Open mgalos999 opened this issue 3 years ago • 26 comments
trafficstars

When I run cargo run --example transparent instead of a transparent window I just get a white window with no decorations.

It seems the problem was introduced in this PR: https://github.com/rust-windowing/winit/pull/2419 If I revert to a commit before that PR was merged, then I see no GUI window showing up at all which I believe is the intended result (I see the icon in the taskbar and it appears in the alt+tab window list).

When I run the transparent example on the latest commit I just get a white window (with the expected icon in the taskbar)

I am running Windows 10.

mgalos999 avatar Sep 23 '22 09:09 mgalos999

Ok I think I have a fix, but I'm not sure if it will really do the job.

I think you just have to insert this in src/platform_impl/windows.rs

if self.contains(WindowFlags::TRANSPARENT) {
    style_ex |= WS_EX_LAYERED;
}

Note you won't want to use the WS_EX_TRANSPARENT style since that doesn't actually make the window transparent, it just makes mouse clicks passthrough

mgalos999 avatar Sep 23 '22 15:09 mgalos999

thanks for the report. It should work fine when actually drawing something onto the window surface, which the examples don't do. With the changes from the PR the screens windows will be completely invisible for me, no matter what I draw on it.

msiglreith avatar Sep 24 '22 14:09 msiglreith

It should work fine when actually drawing something onto the window surface

However this would be an overhead for apps that don't need to draw the window surface, for example when using Webview2.

Transparency used to work out of the box for undecorated windows before https://github.com/rust-windowing/winit/pull/1933 . I really don't know what caused the regression or if it can be fixed but it is the reason why I kept TAO (the winit fork for WRY) using subclasses.

This could also be a blocker for us to migrate back to using winit directly.

Edit: Seems it is indeed #2419 not #1933, I thought it was #1933 because at the time I was testing it along with #1891 which is similar to #2419

amrbashir avatar Oct 10 '22 12:10 amrbashir

I might be not understand how transparency works on windows, but aren't you required to provide a buffer with alpha value in the end?

Is it just blending with black background now if you draw into subview(not sure how that called on windows, but that's my understanding of what happens). You might just clear it with fully transparent color it or do something like.

kchibisov avatar Oct 10 '22 12:10 kchibisov

Webview2 works a bit differently. I don't remember the correct term for it but it is a sort of a child window which has its own window surface. So in order to have transparency, we would need to draw the window surface which is just a bit annoying since we don't need to draw the window surface for any other purpose.

This could also be a blocker for us to migrate back to using winit directly.

Also this was an overstatement from me, I think I wanted to say it will be a minor setback.

amrbashir avatar Oct 10 '22 12:10 amrbashir

In general if you don't feel the root window with something it could contain random data leading to graphical artifacts. Conveniently some GPU drivers do clear the buffer with zeros for you, so you have some sort of initialized thingy, but it's not always the case and on linux users can disable that for example leading you to blending with random data.

I'm not an expert here, but you should always explicitly initialize the buffers you blend against.

kchibisov avatar Oct 10 '22 12:10 kchibisov

Window transparency is broken on Macos as well. This is a regression as it works if I use winit legacy (0.19.5) after some tweaks to make the compiler happy

ericguedespinto avatar Aug 04 '23 01:08 ericguedespinto

I'm not sure what are you talking about, transparency works just fine on all the available backends, only example is broken, nothing more. Real code with e.g. OpenGL is not affected.

kchibisov avatar Aug 04 '23 02:08 kchibisov

The transparent example also does not work on macos.

I dug a little bit, and I believe the bug is with the platform_impl/window.rs where the code is using the setOpaque method of NSWindow. As far as I can tell, this method is deprecated and no longer exists, and the NSWindow API created using objc2 crate is out of date. Because of the way message passing works in objective-C, it just fails silently. Does anyone know how to set a transparent background in NSWindow in modern AppKit? I tried adding the API for alphaValue, and it worked, but I think alphaValue affects the whole window and not just the background.

ericguedespinto avatar Aug 04 '23 11:08 ericguedespinto

I'm not sure what are you talking about, transparency works just fine on all the available backends, only example is broken, nothing more. Real code with e.g. OpenGL is not affected.

You mean that using with_transparent will not work but using openGL on the context directly will? Can you provide an example, please?

ericguedespinto avatar Aug 04 '23 11:08 ericguedespinto

idk, just run https://github.com/rust-windowing/glutin example from here, it's transparent. There's also alacritty which uses Opaque hints and has transparency. Also, softbuffer, which we're using for examples don't support transparency at all as of now...

kchibisov avatar Aug 04 '23 14:08 kchibisov

setOpaque definitely exists, it's just documented as opaque due to how Objective-C renames property setters.

Because of the way message passing works in objective-C, it just fails silently.

Nope, that would've thrown an error. It only does that if the receiver/window is NULL.

madsmtm avatar Aug 04 '23 18:08 madsmtm

@ericguedespinto Do you have a minimal reproducible example for the MacOS behavior? If yes, could you open a new issue for that? If not, could you try out https://github.com/rust-windowing/winit/pull/3048 ?

stefnotch avatar Aug 25 '23 13:08 stefnotch

are any workarounds available? Transparency also is not working for me as well.

CoryRobertson avatar Nov 20 '23 04:11 CoryRobertson

having the same issue here, reported to RioTerm as well: https://github.com/raphamorim/rio/issues/361

orhun avatar Dec 02 '23 10:12 orhun

idk, just run https://github.com/rust-windowing/glutin example from here, it's transparent. There's also alacritty which uses Opaque hints and has transparency. Also, softbuffer, which we're using for examples don't support transparency at all as of now...

image

Could you provide some more guidance?

raggi avatar Apr 14 '24 21:04 raggi

can confirm the glutin example works for me

from their readme:

git clone https://github.com/rust-windowing/glutin
cd glutin
cargo run --example window

can correctly see my web browser through the transparent window

image

Edition	Windows 10 Pro
Version	22H2
Installed on	‎2020-‎11-‎03
OS build	19045.4291
Experience	Windows Feature Experience Pack 1000.19056.1000.0

TeamDman avatar Apr 16 '24 22:04 TeamDman

Quite right, I forgot versioning information, doubling down:

image

~\src\glutin> cargo run --example window
warning: unused import: `std::mem`
 --> C:\Users\raggi\src\glutin\target\debug\build\glutin_wgl_sys-580181e13c8eaeb7\out/wgl_bindings.rs:3:21
  |
3 |             pub use std::mem;
  |                     ^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `glutin_wgl_sys` (lib) generated 1 warning (run `cargo fix --lib -p glutin_wgl_sys`
to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 2.01s
     Running `target\debug\examples\window.exe`
Picked a config with 16 samples
Running on NVIDIA GeForce RTX 4090/PCIe/SSE2
OpenGL Version 3.3.0 NVIDIA 551.86
Shaders version on 3.30 NVIDIA via Cg compiler
OS Name:                   Microsoft Windows 11 Pro
OS Version:                10.0.22631 N/A Build 22631
~\src\glutin> rustc --version
rustc 1.77.2 (25ef9e3d8 2024-04-09)
~\src\glutin> g log --oneline -n1
9228126 (HEAD -> master, origin/master, origin/HEAD) ci: add typos

Also to confirm, here's transparency effects from native composited windows: image

raggi avatar Apr 17 '24 02:04 raggi

You might have to check your NVIDIA driver settings in case some presentation compatibility has been set. The WSI parts of OpenGL and Vulkan are unfortunately not great. Using DXGI/DirectComposition should give more control.

msiglreith avatar Apr 17 '24 18:04 msiglreith

Is there any solution at present?

han1548772930 avatar Jun 04 '24 06:06 han1548772930