Repository: electron/electron (language: cpp)

Issue:
[Feature Request]: environment variable to enable wayland support and get rid of individual --enable-features=UseOzonePlatform --ozone-platform=wayland flags
### Preflight Checklist

- [X] I have read the [Contributing Guidelines](https://github.com/electron/electron/blob/master/CONTRIBUTING.md) for this project.
- [X] I agree to follow the [Code of Conduct](https://github.com/electron/electron/blob/master/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 feature request that matches the one I want to file, without success.

### Problem Description

Currently Wayland support in Electron needs to be turned on on per-app basis with `--enable-features=UseOzonePlatform --ozone-platform=wayland` flags. 

This is very inconvenient: we have to repeat this chore for every single Electron app, we need to take care of CLI aliases, etc, etc.

Without these flags Electron apps look blurry and are almost unusable when fractional scaling is on.

### Proposed Solution

Electron should read these options from an environment variable, like `ELECTRON_OPTS`,  this way the user would be able to turn on Wayland support globally. 

### Alternatives Considered

Alternatively Electron may support environment variables like 

`ELECTRON_OZONE_PLATFORM`
`ELECTRON_ENABLE_FEATURES`

### Additional Information

_No response_


Discussion / hints:
For what it's worth this is progressing slowly but surely upstream. [Ozone/X11 is on by default in Chromium](https://twitter.com/maksim_sisov/status/1431272534356471964), so hopefully the work to enable Ozone/Wayland will keep progessing, so it's enabled by default in Electron.

But yes an environment variable would be handy in the meantime.
With this, a global `flatpak override` to make all Flatpak'd Electron software use Wayland by default would be possible.

If it breaks, `flatpak override --unset-env=ELECTRON_OZONE_PLATFORM --unset-env=ELECTRON_ENABLE_FEATURES name.of.software`
What is the status here?
> With this, a global flatpak override to make all Flatpak'd Electron software use Wayland by default would be possible.

And a global override to activate the experimental client-side decorations, too, which is quite useful on GNOME, despite the bugs (#33161).
To run under Wayland, most electron applications need special flags to be passed at launch.[^1] This is a big headache for Linux distribution package maintainers and full-time Wayland users. Many apps (e.g. VS Code) don't respect `$XDG_CONFIG_HOME/electron-flags.conf`—for these applications, an environment variable may provide a decent, global alternative to manually patching `.desktop` entries and throwing wrapper scripts in `/usr/local/bin` whose sole purpose is to pass `--enable-features=UseOzonePlatform --ozone-platform=wayland`.

[^1]: These applications will run in XWayland if the flags are not passed, which looks awful on high-dpi screens, or when scaling is enabled, or when accessibility tools are used. There are also limitations for things like screen sharing, multiple displays, desktop notifications and alerts, window compositing, etc.

[Here's a short description](https://wiki.archlinux.org/title/Wayland#Electron) of the hoops users often have to jump through, from the Arch Wiki article on Wayland.

Allowing these to be configured with an environment variable would go a long way toward making it easier to configure electron applications to behave property under Wayland.

Of course, the ideal situation would be for Electron to enable proper Wayland support by default, but as long as users are going to have to manually ask electron apps to run under Wayland, I think it's worth making this a global setting.

For these reasons, it would be very nice to see support for environment-based configuration of OzonePlatform!
Related: recently merged #34937 added flag `--ozone-platform-hint=auto` , which at least allows moving back and forth between wayland and X11.
Still bad and not what we all need.
I'm going to second that this is a huge issue. I use a display with fractional scaling, so Wayland is a necessity & it'd be nice for apps to "just work" instead of jumping through these hoops. X11 is dying, Wayland is gaining market share, something should be done.
> something should be done

But who cares.

Fix patch:
diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md
index ba3d5cf99e6de..fa6d1e13fcc45 100644
--- a/docs/api/environment-variables.md
+++ b/docs/api/environment-variables.md
@@ -111,6 +111,16 @@ Options:
 * `kioclient5`
 * `kioclient`
 
+### `ELECTRON_OZONE_PLATFORM_HINT` _Linux_
+
+Selects the preferred platform backend used on Linux. The default one is `x11`. `auto` selects Wayland if possible, X11 otherwise.
+
+Options:
+
+* `auto`
+* `wayland`
+* `x11`
+
 ## Development Variables
 
 The following environment variables are intended primarily for development and
diff --git a/shell/browser/electron_browser_main_parts_linux.cc b/shell/browser/electron_browser_main_parts_linux.cc
index 2ad25d3116514..49408910c8805 100644
--- a/shell/browser/electron_browser_main_parts_linux.cc
+++ b/shell/browser/electron_browser_main_parts_linux.cc
@@ -15,6 +15,9 @@
 #include "shell/common/thread_restrictions.h"
 #endif
 
+constexpr base::StringPiece kElectronOzonePlatformHint(
+    "ELECTRON_OZONE_PLATFORM_HINT");
+
 #if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
 
 constexpr char kPlatformWayland[] = "wayland";
@@ -115,17 +118,20 @@ std::string MaybeFixPlatformName(const std::string& ozone_platform_hint) {
 }  // namespace
 
 void ElectronBrowserMainParts::DetectOzonePlatform() {
+  auto const env = base::Environment::Create();
   auto* const command_line = base::CommandLine::ForCurrentProcess();
   if (!command_line->HasSwitch(switches::kOzonePlatform)) {
-    const auto ozone_platform_hint =
+    auto ozone_platform_hint =
         command_line->GetSwitchValueASCII(switches::kOzonePlatformHint);
+    if (ozone_platform_hint.empty()) {
+      env->GetVar(kElectronOzonePlatformHint, &ozone_platform_hint);
+    }
     if (!ozone_platform_hint.empty()) {
       command_line->AppendSwitchASCII(
           switches::kOzonePlatform, MaybeFixPlatformName(ozone_platform_hint));
     }
   }
 
-  auto env = base::Environment::Create();
   std::string desktop_startup_id;
   if (env->GetVar("DESKTOP_STARTUP_ID", &desktop_startup_id))
     command_line->AppendSwitchASCII("desktop-startup-id", desktop_startup_id);


Test patch: