Skip to content

Added isMaximized option #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export declare const ipc: {
MINIMIZE: string;
TOGGLE_MAXIMIZE: string;
CLOSE: string;
MAXIMIZED: string;
};
3 changes: 2 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ exports.ipc = void 0;
exports.ipc = {
MINIMIZE: 'WINDOW_MINIMIZE',
TOGGLE_MAXIMIZE: 'WINDOW_TOGGLE_MAXIMIZE',
CLOSE: 'WINDOW_CLOSE'
CLOSE: 'WINDOW_CLOSE',
MAXIMIZED: 'WINDOW_MAXIMIZED'
};
11 changes: 11 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var helpers_1 = require("./helpers");
BrowserWindow.toggleMaximize()

BrowserWindow.close()

BrowserWindow.isMaximized()
```
*/
var ElectronWindowControls = /** @class */ (function () {
Expand Down Expand Up @@ -60,6 +62,12 @@ var ElectronWindowControls = /** @class */ (function () {
throw new Error('[electron-window-controls] No window found');
win.close();
});
electron_1.ipcMain.on(helpers_1.ipc.MAXIMIZED, function (event) {
var win = electron_1.BrowserWindow.fromWebContents(event.sender);
if (!win)
throw new Error('[electron-window-controls] No window found');
event.returnValue = win.isMaximized();
});
};
/**
* Initialize the Electron main process.
Expand Down Expand Up @@ -97,6 +105,9 @@ var ElectronWindowControls = /** @class */ (function () {
},
toggleMaximize: function () {
electron_1.ipcRenderer.send(helpers_1.ipc.TOGGLE_MAXIMIZE);
},
isMaximized: function() {
return electron_1.ipcRenderer.sendSync(helpers_1.ipc.MAXIMIZED);
}
};
};
Expand Down
4 changes: 4 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export interface WindowMethods {
* Toggle the maximized state of the window
*/
toggleMaximize: () => void;
/**
* Returns the maximization state of the window
*/
isMaximized: () => boolean;
}
export declare type FinalOptions = SetRequired<Options, 'isMaximized'>;
5 changes: 3 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const ipc = {
MINIMIZE: 'WINDOW_MINIMIZE',
TOGGLE_MAXIMIZE: 'WINDOW_TOGGLE_MAXIMIZE',
CLOSE: 'WINDOW_CLOSE'
}
CLOSE: 'WINDOW_CLOSE',
MAXIMIZED: 'WINDOW_MAXIMIZED'
}
33 changes: 22 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@ import { Options, FinalOptions, WindowMethods } from './types'
/**
* Control your Electron Browser Window securely from the renderer.
* @example
```
// Main process
import WindowControls from 'electron-window-controls'
```
// Main process
import WindowControls from 'electron-window-controls'

WindowControls.initMain()
WindowControls.initMain()

// Renderer process
import { BrowserWindow } from 'electron-window-controls'
// Renderer process
import { BrowserWindow } from 'electron-window-controls'

BrowserWindow.minimize()
BrowserWindow.minimize()

BrowserWindow.toggleMaximize()
BrowserWindow.toggleMaximize()

BrowserWindow.close()
```
BrowserWindow.close()

BrowserWindow.isMaximized()
```
*/
class ElectronWindowControls {

Expand Down Expand Up @@ -66,6 +68,12 @@ class ElectronWindowControls {

win.close()
})

ipcMain.on(ipc.MAXIMIZED, (event) => {
var win = ElectronBrowserWindow.fromWebContents(event.sender);
if (!win) throw new Error('[electron-window-controls] No window found');
event.returnValue = win.isMaximized();
});
}

/**
Expand Down Expand Up @@ -106,6 +114,9 @@ class ElectronWindowControls {
},
toggleMaximize: () => {
ipcRenderer.send(ipc.TOGGLE_MAXIMIZE)
},
isMaximized: () => {
return ipcRenderer.sendSync(ipc.MAXIMIZED);
}
}
}
Expand All @@ -116,4 +127,4 @@ class ElectronWindowControls {
*/
export const BrowserWindow = ElectronWindowControls.initRenderer()

export default ElectronWindowControls
export default ElectronWindowControls
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export interface WindowMethods {
* Toggle the maximized state of the window
*/
toggleMaximize: () => void
/**
* Returns the maximization state of the window
*/
isMaximized: () => boolean
}

export type FinalOptions = SetRequired<Options, 'isMaximized'>
export type FinalOptions = SetRequired<Options, 'isMaximized'>