Repository: electron/electron (language: cpp)

Issue:
[Bug]:  AlwaysOnTop is not working
### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/main/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [X] I have searched the [issue tracker](https://www.github.com/electron/electron/issues) for a bug report that matches the one I want to file, without success.

### Electron Version

v17.0.0-alpha.4

### What operating system are you using?

Ubuntu

### Operating System Version

Linux 5.16.8-051608-generic #202202101327-Ubuntu SMP PREEMPT Thu Feb 10 13:36:40 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

### What arch are you using?

x64

### Last Known Working Electron version

v17.0.0-alpha.3

### Expected Behavior

Window stays always on top.
The gnome menu should indicate that  "always on top" is set.

### Actual Behavior

Window does not stays always on top.
The gnome menu does not indicate that  "always on top" is set.

### Testcase Gist URL

https://gist.github.com/69c948d4ed20768b71d6332efe61fc77

### Additional Information

_No response_


Discussion / hints:
Thanks for reporting this and helping to make Electron better!

Because of time constraints, triaging code with third-party dependencies is usually not feasible for a small team like Electron's. Would it be possible for you to make a standalone testcase with only the code necessary to reproduce the issue? [Electron Fiddle](https://www.electronjs.org/fiddle) is a great tool for making small testcases and publishing them as [gists](https://gist.github.com) that Electron maintainers can use.

If the bug you've found can be tested with a pass/fail test, please make the testcase [exit 0 on success or nonzero on failure](https://github.com/electron/bugbot#by-bug-reporters). The lets [Electron's issue bot](https://github.com/electron/bugbot#readme) see what releases are affected by the bug by checking your test against different OSes and Electron versions.

I'm adding the `blocked/need-repro` label for this reason. After you make a test case, please link to it in a followup comment.

@ckerr I created a testcase: https://gist.github.com/69c948d4ed20768b71d6332efe61fc77

I have updated the url in the main comment.
I have done some more testing. The last working version is `v17.0.0-alpha.3`. And the first broken version is `v17.0.0-alpha.4`
Possibly related, we're seeing no issues with `alwaysOnTop` in development with `17.0.0` but once packaged the behavior changes and the window is no longer "always on top."

The issue we're seeing in particular is related to having two windows with `alwaysOnTop`, and showing one of those over the other.

We're seeing this issue with KDE desktop environment.

This worked as intended in `13.6.0` - but no longer works in `17.0.0`
Update: Also works as intended in `16.0.0`. I've been trying to find a PR that could potentially be the cause, but so far I can't find it.

`16.0.9` also works as expected.
Confirming our issue originated with `17.0.0-alpha.4`. 

The previous build, `17.0.0-alpha.3`, works as expected, identically to v16, v13, and probably even further back.

Thanks @MatthijsBurgh for your investigative work!
## [Release notes for 17.0.0-alpha.4](https://www.electronjs.org/releases/alpha?page=2#release-notes-for-v1700-alpha4-1700-alpha4)

I don't see anything there that wasn't also updated in `16.x.x` - except for upgrading the Chromium version. I sure hope the bug/behavior change isn't in Chromium itself.
@ckerr could we get the `blocked/need-repro` tag removed from this? I think all the user-collectable data needed to diagnose this issue is now collected.
It looks like possibly this is related to us using `setAlwaysOnTop` before `show`. Previously this worked, but it no longer seems to. Using `show` before `setAlwaysOnTop` seems to be a viable workaround for our situation, but very definitely there has been a behavior change between `alpha.3` and `alpha.4`.
@ckerr friendly ping;)
@ckerr friendly ping;)
@ckerr friendly ping;)
@ckerr a little bit less friendly ping;)
@ckerr I think this is no longer `blocked/need-repro`.

Fix patch:
diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc
index f33a3705cf031..c2f7a31fb566b 100644
--- a/shell/browser/native_window_views.cc
+++ b/shell/browser/native_window_views.cc
@@ -495,6 +495,13 @@ void NativeWindowViews::Show() {
   if (global_menu_bar_)
     global_menu_bar_->OnWindowMapped();
 #endif
+
+#if defined(USE_OZONE_PLATFORM_X11)
+  // On X11, setting Z order before showing the window doesn't take effect,
+  // so we have to call it again.
+  if (IsX11())
+    widget()->SetZOrderLevel(widget()->GetZOrderLevel());
+#endif
 }
 
 void NativeWindowViews::ShowInactive() {


Test patch: