Создание приложения в трее.Нажатие на иконку в трее переключает видимость приложения.Когда я прячусь и затем показываю его, окно, кажется, каждый раз немного сдвигается вправо и вниз.
Спам, щелкнув по значку в трее сто раз, заставляет мое окно исчезать на половину ширины ...
const {app, BrowserWindow, ipcMain, Tray} = require('electron');
const path = require('path');
let tray = undefined;
let window = undefined;
const sceneWidth = 418;
const sceneHeight = 700;
const taskBarHeight = 45;
let toggleWindow = () => {
window.isVisible() ? hideWindow() : showWindow();
};
let hideWindow = () => {
console.log("[Info] Application frame hidden.");
window.hide();
};
let showWindow = () => {
console.log("[Info] Application frame visible.");
const position = getWindowPosition();
window.setPosition(position.x, position.y, false);
window.show();
};
app.on('ready', () => {
console.log("[Info] Application ready.");
createTray();
createWindow();
const windowProperties = getWindowPosition();
console.log("[Info] Screen resolution: " + windowProperties.dx + " x " + windowProperties.dy);
});
let createTray = () => {
tray = new Tray(path.join('assets/img/TrayIcon.png'));
tray.on('click', function (event) {
toggleWindow();
});
};
const getWindowPosition = () => {
const electron = require('electron');
const screenElectron = electron.screen;
const mainScreen = screenElectron.getPrimaryDisplay();
const dimensions = mainScreen.size;
const x = dimensions.width - sceneWidth;
const y = (dimensions.height - (sceneHeight - taskBarHeight));
return {
x: x,
y: y,
dx: dimensions.width,
dy: dimensions.height
};
};
const createWindow = () => {
window = new BrowserWindow({
width: sceneWidth,
height: sceneHeight,
show: false,
frame: false,
skipTaskbar: true,
fullscreenable: false,
resizable: false,
transparent: false,
webPreferences: {
backgroundThrottling: false
}
});
window.loadURL(`file://${path.join(__dirname, 'index.html')}`)
};
Я не вижу, что может вызвать эту проблему. Также , когда окно не находится в фокусе, приложение не скрывается, поэтому, чтобы отобразить его снова, вам придется дважды щелкнуть по нему.Много пытался это исправить, но не смог этого сделать.Событие размытия вызывается, когда вы скрываете окно, щелкая его, это то, что я обнаружил, но не смог заставить его работать (попытался добавить функцию hideWindow()
к событию размытия, но затем я не смог скрыть окнощелкнув по нему, он переходит из невидимого в видимое).