Electron: How to minimize a window from a rendered process
In case you stumble upon this thread and are using electron vue you can use this.$electron.remote.BrowserWindow.getFocusedWindow().minimize();
from directly inside your renderer.
This works for me:
const { remote } = require('electron')
remote.getCurrentWindow().minimize();
Old thread but it should be noted that the remote module is seen as a security issue and its use is discouraged (see here). It is also deprecated as-is in electron 12 (see here).
Instead, you can just send an command via IPC to invoke the minimize.
From the renderer:
ipcRenderer.send('minimize')
In main:
ipcMain.on('minimize', () => {
win.minimize()
// or depending you could do: win.hide()
})
You can also just make a toggle (in main):
ipcMain.on('minimize', () => {
win.isMinimized() ? win.restore() : win.minimize()
// or alternatively: win.isVisible() ? win.hide() : win.show()
})
For dynamically made windows (as noted in other answers and as it appears to be the use case) you can use (in place of 'win'):
BrowserWindow.getFocusedWindow()
This creates an issue with toggling if you have more than one window (which is the point) but you can use BrowserWindow.getAllWindows()
and iterate if you want to restore a specific window from outside the current renderer process (say by ID). Don't know why you would want to do this but I include it for completeness.
You can call minimize()
on the respective BrowserWindow
instance. The question is how to best get access to this instance and this, in turn, depends on how you open the windows and where your minimize button is. From your example, I take it that the minimize button is actually in the window you want to close, in that case you can just minimize the focused window, because to when a user clicks the button inside it, the window should currently have the focus:
const { remote } = require('electron')
remote.BrowserWindow.getFocusedWindow().minimize();
Alternatively you can use BrowserWindow.fromId()
to get access to the window in question if, for example, you want to minimize the task window from the other window.