'Pressing Win + D causes an electron window that is not allowed to minimize to minimize on Windows 10
Using electron 16.9.1, I have built an app whose window is not allowed to minimize and whose alwaysOnTop
property is set to false
. To put it another way, the idea is to have a window that cannot be minimized and which is always "underneath" other windows.
The configuration of the window looks like this:
const win = new BrowserWindow({
resizable: false,
movable: false,
minimizable: false,
maximizable: false,
alwaysOnTop: true,
fullscreenable: false,
show: false,
frame: false,
backgroundColor: "#00FFFFFF",
transparent: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
});
It works fine, the functions of the app function as I have planned, with but one tiny flaw: once I use the Win + D
shortcut, the window is minimized. I am aware that many apps behave this way, but what I really want to know is whether there is a way to avoid this.
[Update] I realized the impossibility of what I am trying to ask for, and instead, I am now trying another approach: to listen to a "show the desktop" event in electron. Every time that event is triggered, I will show the window again. And so, the problem has now changed to: how do I monitor such an event?
Solution 1:[1]
What I really want to achieve is allowing any window to get over it, but not the desktop.
Every time that event is triggered, I will show the window again. And so, the problem has now changed to: how do I monitor such an event?
When minimizable: true
is set, the BrowserWindow does fire minimize
event when Win+D
and Win+M
are pressed. So, we can set minimizable: true
and listen to those, including user minimize, events and restore the window. As frame:false
is set user won't see minimize button only option of clicking on taskbar icon will be available.
let mainWindow;
let counter = 0;
function createWindow() {
mainWindow = new BrowserWindow({
width: 600,
height: 300,
show: false,
resizable: false,
movable: false,
minimizable: true, /* keep it true */
maximizable: false,
alwaysOnTop: false,
fullscreenable: false,
frame: false,
backgroundColor: '#00FFFFFF',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
},
});
mainWindow.loadURL(indexPath);
//try to restore window on minimize
mainWindow.on('minimize', (e) => {
e.preventDefault();
console.log('minimize', ++counter);
//try changing delay value
setTimeout(() => mainWindow.restore(), 200);
});
mainWindow.on('restore', () => {
console.log('restore', counter);
});
mainWindow.once('ready-to-show', () => {
mainWindow.show();
if (dev) {
mainWindow.webContents.openDevTools();
}
});
mainWindow.on('closed', function () {
mainWindow = null;
});
}
Desktop doesn't come infront immediately it takes time so we can't keep delay too low to restore our window. I've deliberately kept the delay high in the code for the demo. Once you call restore, next restore calls have no effect. Drawback is user will be able click on the application icon in taskbar and the application will bounce back.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | the Hutt |