[英]Electron: Make the browser window stuck to the side of screen
Am trying to make a browser window that sticks to the side of the screen, which I did manage to achieve however, when I change my display settings to a smaller aspect ratio and turn it back (my way of testing) the window stays in the place it moved too and not back to the side of my screen.我正在尝试制作一个粘在屏幕一侧的浏览器窗口,我确实设法实现了,但是当我将显示设置更改为较小的纵横比并将其转回(我的测试方式)时,窗口停留在放置它也移动了而不是回到我的屏幕一侧。
This is my current code for the browser window:这是我当前浏览器窗口的代码:
function createWindow() {
const {width,height} = screen.getPrimaryDisplay().workAreaSize;
const mainWindow = new BrowserWindow({
frame: false,
width: 500,
height: 500,
useContentSize: true,
backgroundColor: '#000000',
transparent: true,
resizable: true,
x: width - 500,
y: height - 450,
visibleOnAllWorkspaces: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
I was wondering if the window positions only get run once then the x and y positions arent checked again?我想知道窗口位置是否只运行一次然后 x 和 y 位置不会再次检查?
PS : Am pretty new to Electron any tips going forward will be appreciated PS:我对 Electron 还很陌生,以后的任何提示都将不胜感激
You need to dynamically adjust the bounds of the main window each time the display settings change (could be the macOS Dock becoming visible or hidden, or the screen resolution being set to a different aspect ratio, etc.).每次显示设置更改时,您都需要动态调整主窗口的边界(可能是 macOS Dock 变得可见或隐藏,或者屏幕分辨率设置为不同的纵横比等)。
The screen API defines several events the app can listen to, including 'display-metrics-changed'
: 屏幕API 定义了应用程序可以侦听的多个事件,包括
'display-metrics-changed'
:
Event:
'display-metrics-changed'
事件:
'display-metrics-changed'
Returns:
返回:
Emitted when one or more metrics change in a
display
.当一个或多个指标在
display
发生变化时发出。 ThechangedMetrics
is an array of strings that describe the changes.changedMetrics
是一个描述更改的字符串数组。 Possible changes arebounds
,workArea
,scaleFactor
androtation
.可能的变化是
bounds
、workArea
、scaleFactor
和rotation
。
Your createWindow
function could be updated that way (quickly tested in Electron Fiddle , may need more adjustments):您的
createWindow
函数可以通过这种方式更新(在Electron Fiddle 中快速测试,可能需要更多调整):
function createWindow() {
const {width,height} = screen.getPrimaryDisplay().workAreaSize;
const mainWindow = new BrowserWindow({
frame: false,
width: 500,
height: 500,
useContentSize: true,
backgroundColor: '#000000',
transparent: true,
resizable: true,
x: width - 500,
y: height - 450,
visibleOnAllWorkspaces: true,
webPreferences: {
nodeIntegration: true,
enableRemoteModule: true
}
});
screen.on('display-metrics-changed', (event, display, changedMetrics) =>
{
// console.log(display, changedMetrics);
const {x, y, width, height} = display.workArea;
// console.log(x, y, width, height);
mainWindow.setBounds({x: width - 500, y: height - 450, width: 500, height: 500})
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.