简体   繁体   English

Linux 上的透明 Windows (Electron)

[英]Transparent Windows on Linux (Electron)

Using the transparent argument and setting it to true when creating a new BrowserWindow in Electron usually gives the window a transparent background... But on Linux that isn't the case for my knowledge在 Electron 中创建新的 BrowserWindow 时,使用透明参数并将其设置为 true 通常会给窗口一个透明的背景......但在 Linux 上,据我所知,情况并非如此

Now I heard you can set some Command Line Arguments... But that isn't working... It just displays black or white no matter what...现在我听说你可以设置一些命令行参数......但这不起作用......它只是显示黑色或白色无论如何......

// Should set the commandLine arguments and work...

const {app} = require('electron')

app.commandLine.appendSwitch('enable-transparent-visuals');
app.commandLine.appendSwitch('disable-gpu');

Now i have heard this is no problem with electron rather a problem with hardware... But i just needed to make sure therefore creating this question!现在我听说这不是电子的问题,而是硬件的问题......但我只需要确保因此创建这个问题!

I have encounter the same problem as you and so I have written :我遇到了和你一样的问题,所以我写了:

Till the requested feature is implemented, the solution is simple just add a delay before launching the window.在实现请求的功能之前,解决方案很简单,只需在启动窗口之前添加延迟即可。

You can clone this git repo , put the delay to 500, and normally magic will appear. 您可以克隆 这个 git repo ,将延迟设置为 500,通常会出现魔法。

EDIT 1 : Use this repo : https://gitlab.com/doom-fr/electron-transparency-demo编辑 1:使用这个 repo: https : //gitlab.com/doom-fr/electron-transparency-demo

git clone https://gitlab.com/doom-fr/electron-transparency-demo
cd electron-transparency-demo
npm install
npm start
# or npm run startWithTransparentOption
# or npm run startWithAllOptions

For me, it works out of the box with Debian Jessie and electron 4.0.5, for npm start , npm run startWithTransparentOption but not with npm run startWithAllOptions .对我来说,它与 Debian Jessie 和电子 4.0.5 一起开箱即用,对于npm startnpm run startWithTransparentOption但不适用于npm run startWithAllOptions

NB : be carefull to set at least 500ms to have chance it works.注意:请小心设置至少 500 毫秒以使其有机会工作。 After you can reduce the delay but it is not stable.之后可以减少延迟但不稳定。 It is why an event on transparentReady is needed.这就是为什么需要transparentReady 事件的原因。

Doom厄运

There do seem to be multiple issues with transparency, on different distributions with differing hardware.在具有不同硬件的不同发行版上,透明度似乎存在多个问题。 There are various suggested workarounds.有各种建议的解决方法。 You might not be able to make transparency work acceptably for your scenario on all hardware and distributions.您可能无法在所有硬件和发行版上为您的方案提供可接受的透明度。

Examples例子

From the Electron docs:从电子文档:

On Linux, users have to put --enable-transparent-visuals --disable-gpu in the command line to disable GPU and allow ARGB to make transparent window, this is caused by an upstream bug that alpha channel doesn't work on some NVidia drivers on Linux.在 Linux 上,用户必须在命令行中放置 --enable-transparent-visuals --disable-gpu 以禁用 GPU 并允许 ARGB 制作透明窗口,这是由上游错误引起的,即 alpha 通道在某些情况下不起作用Linux 上的 NVidia 驱动程序。

https://github.com/electron/electron/blob/master/docs/api/frameless-window.md https://github.com/electron/electron/blob/master/docs/api/frameless-window.md

This code might work for you.此代码可能对您有用。 I add explain in comment.我在评论中添加了解释。

//electron can't be transparent on linux
//see issue on github: https://github.com/electron/electron/issues/2170

// app.disableHardwareAcceleration(); //use this
//or use these two lines code
app.commandLine.appendSwitch('enable-transparent-visuals');
app.commandLine.appendSwitch('disable-gpu');

//createWindow need to wait(more than about 100ms) if you want the window to be transparent
// app.whenReady().then(createWindow); //this won't work
app.commandLine.appendSwitch('enable-transparent-visuals');
app.commandLine.appendSwitch('disable-gpu');
app.on('ready', () => {
    setTimeout(() => {
        createWindow();
    }, 200);
});

This worked to me:这对我有用:

if(process.platform === "linux") {
  app.commandLine.appendSwitch('enable-transparent-visuals');
  app.disableHardwareAcceleration();

  app.on('ready', () => setTimeout(createWindow, 400));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM