如何获得OS窗口在电子中的位置?

时间:2019-04-08 00:11:15

标签: user-interface position electron

我正试图让一个电子应用程序在屏幕上与上次关闭时相同的位置重新打开。

为此,我有一个配置文件,该文件记录了关闭窗口时的边界。

function set(settingKey, settingValue) {
  nconf.set(settingKey, settingValue);
  nconf.save();
};

mainWindow.on('close', function () {
  config.set('bounds', mainWindow.getBounds());
});

但是,当我重新启动应用并通过指定mainWindowxy,{{ 1}}选项或致电width

height

该窗口显示为比以前更低。我发现我得到的setBounds值没有考虑到窗口标题栏的高度。

This question类似,但是解决方案导致相同的问题。

我尝试过:

  • mainWindow.setBounds(config.get('bounds'));
  • ymainWindow.getPosition
  • mainWindow.getContentBounds

无济于事。前两种方法给我完全相同的结果。最后一个为setContentBounds

有人知道如何在电子中获得OS窗口的位置吗?

如果有帮助的话,我在Wayland(Gnome 3.32)上。

1 个答案:

答案 0 :(得分:2)

看起来像是Linux上一个长期未解决的错误:

On Linux, returned position of fixed BrowserWindow gets unexpectedly modified

例如,以下内容:

const { app, BrowserWindow } = require ('electron');
let mainWindow = null;
function onAppReady ()
{
    let position = [ 200, 100 ];
    console.log ('init:', position);
    mainWindow = new BrowserWindow ({ x: position[0], y: position[1], width: 800, height: 600, show: false });
    console.log ('new:', mainWindow.getPosition ());
    mainWindow.loadURL (`file://${__dirname}/index.html`);
    mainWindow.on ('ready-to-show', () => { 
        mainWindow.show ();
        console.log ('show:', mainWindow.getPosition ()); });
    mainWindow.on ('close', () => { console.log ('close:', mainWindow.getPosition ()); });
    mainWindow.on ('closed', () => { app.quit (); });
}
app.on ('ready', onAppReady);

导致:

Linux Mint:

init: [ 200, 100 ]
new: [ 200, 100 ]
show: [ 201, 125 ]
close: [ 201, 125 ]

Ubuntu:

init: [ 200, 100 ]
new: [ 200, 100 ]
show: [ 200, 100 ]
close: [ 200, 128 ]