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

Add transparent windows support on Windows #1250

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ArcticFqx
Copy link

@ArcticFqx ArcticFqx commented May 4, 2024

Description

This enables support for transparent windows on Windows.
Resolves #1201.

image

Changes proposed

  • Add WS_EX_LAYERED to GWL_EXSTYLE
  • Overwrite GWL_STYLE and only keep WS_THICKFRAME, this in effect makes the window borderless, apart from the drag handles for resizing.
  • Set the WEBVIEW2_DEFAULT_BACKGROUND_COLOR environment variable to make the WebView2 background transparent white.
  • Set the initial window size to 8000x8000. There is an issue I haven't been able to figure out where the initial window size of a layered window dictates the active area of the window. This works around it for most cases, as most people don't have monitors with greater than 8k resolution.

How to test it

Set "transparent": true in neutralino.config.json.

@shalithasuranga
Copy link
Member

shalithasuranga commented May 4, 2024

Hello @ArcticFqx thanks so much for sending this pull request. Please ignore CI test failures (There is an issue with the Windows test suite). I'll merge this soon 🎉

@@ -881,7 +881,7 @@ class win32_edge_engine {
});
RegisterClassEx(&wc);
m_window = CreateWindow(L"Neutralinojs_webview", L"", WS_OVERLAPPEDWINDOW, 99999999,
CW_USEDEFAULT, 640, 480, nullptr, nullptr,
CW_USEDEFAULT, 8000, 8000, nullptr, nullptr,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to make the window so large?

Copy link

@bxsti-dev bxsti-dev May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. There is something about this in the description of the Pull Request.
There was an issue otherwise.
(Its not actually that big tho)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue may stem from creating the window as WS_OVERLAPPEDWINDOW see Win32 docs.

It may be worthwhile to call CreateWindowEx instead like so:

m_window = CreateWindowEx(WS_EX_LAYERED, L"Neutralinojs_webview", L"", WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nullptr, nullptr,
                          GetModuleHandle(nullptr), nullptr);

and then enable transparency via SetLayeredWindowAttributes(m_window, 0, XXX, LWA_ALPHA); where XXX is the alpha value.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that does work for the entire window and it keeps the resizing behavior consistent, but this also affects the transparency of the webview content, which I think we don't want to happen as we want to control that with CSS instead.

It's a good start though, it's half of a better solution than my proposed PR. We have to be a little clever if we want to keep the window itself transparent, but not the webview.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, would changing

SetEnvironmentVariable(L"WEBVIEW2_DEFAULT_BACKGROUND_COLOR", L"00FFFFFF");

to

SetEnvironmentVariable(L"WEBVIEW2_DEFAULT_BACKGROUND_COLOR", L"FFFFFFFF");

prevent the webview from becoming transparent?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the format for that string is AARRGGBB.

@Sadaf-A
Copy link
Contributor

Sadaf-A commented May 28, 2024

Hey! @ArcticFqx,
could you please confirm the windows version you used for testing this? It's working differently on windows 11

@bxsti-dev
Copy link

Hey! @ArcticFqx,
could you please confirm the windows version you used for testing this? It's working differently on windows 11

I used Windows 11 and it worked.

@Sadaf-A
Copy link
Contributor

Sadaf-A commented May 29, 2024

Hey! @ArcticFqx,
could you please confirm the windows version you used for testing this? It's working differently on windows 11

I used Windows 11 and it worked.

can you share a screenshot?

@bxsti-dev
Copy link

bxsti-dev commented May 29, 2024

can you share a screenshot?

@Sadaf-A here: (I am on Windows 11 23H2

image

@Sadaf-A
Copy link
Contributor

Sadaf-A commented May 29, 2024

can you share a screenshot?

@Sadaf-A here: (I am on Windows 11 23H2

image

Hey, thanks for sharing the screenshot are you able to move the window and close it?

@bxsti-dev
Copy link

bxsti-dev commented May 29, 2024

Hey, thanks for sharing the screenshot are you able to move the window and close it?

Unfortunately no :( I would have wished that too. But apparently it has to be borderless. (I guess that`s still the case?)
You have to create a custom "titlebar".

@ArcticFqx
Copy link
Author

ArcticFqx commented May 30, 2024

Hey! @ArcticFqx, could you please confirm the windows version you used for testing this? It's working differently on windows 11

I am using Windows 11

can you share a screenshot?

@Sadaf-A here: (I am on Windows 11 23H2
image

Hey, thanks for sharing the screenshot are you able to move the window and close it?

As mentioned in the second bullet point, the window is in effect borderless, you need to create a custom titlebar. You can use this if you need to move the window https://neutralino.js.org/docs/api/window#windowsetdraggableregiondomid

@ArcticFqx
Copy link
Author

Is there some other requirement that I'm missing in this PR? I don't really have the other platforms to compare too.

@Sadaf-A
Copy link
Contributor

Sadaf-A commented May 31, 2024

Is there some other requirement that I'm missing in this PR? I don't really have the other platforms to compare too.

I think the only issue is that even after the borderless property is set to false the window is coming out to be borderless
can you try this on your side once?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement window.transparent configuration option for Windows
5 participants