Guest User

Electron dialog module

a guest
Jul 27th, 2020
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const electron = require('electron'), path = require('path'), url = require('url'), fs = require(`fs`);
  2.  
  3. class dialog {
  4.     constructor(dir, args) {
  5.         this.window = null;
  6.         this.show = null;
  7.         this.closePreviousDialog = true;
  8.         this.dir = dir;
  9.         this._displayingCenter = false;
  10.         this._cancelCloseEvent = false;
  11.         args = args || {};
  12.         this.window = new electron.remote.BrowserWindow({
  13.             fullscreen: args.fullscreen || false,
  14.             width: args.width || electron.remote.getCurrentWindow().getSize()[0],
  15.             height: args.height || electron.remote.getCurrentWindow().getSize()[1],
  16.             center: args.center || true,
  17.             closable: args.closable || false,
  18.             minimizable: args.minimizable || true,
  19.             maximizable: args.maximizable || false,
  20.             frame: args.frame || false,
  21.             transparent: args.transparent || true,
  22.             autoHideMenuBar: args.autoHideMenuBar || true,
  23.             thickFrame: args.thickFrame || false,
  24.             parent: args.parent || electron.remote.getCurrentWindow(),
  25.             modal: args.modal || true,
  26.             resizable: args.resizable || false,
  27.             movable: args.movable || false,
  28.             show: args.show || false,
  29.             fullscreenable: args.fullscreenable || false,
  30.             webPreferences: {
  31.                 nodeIntegration: true
  32.                 //          devTools: false
  33.             },
  34.             icon: path.join(__dirname, `./../images/icon.ico`)
  35.         });
  36.     }
  37.  
  38.     halfParentSize() {
  39.         let parentsSize = this.window.getParentWindow().getSize();
  40.         this.setSize(parentsSize[0] / 2, parentsSize[1] / 2)
  41.     }
  42.  
  43.     displayCenter() {
  44.         if (this.width === undefined || this.height === undefined) this.halfParentSize();
  45.         this._displayingCenter = true;
  46.         let parentsSize = this.window.getParentWindow().getSize();
  47.         let parentsPos = this.window.getParentWindow().getPosition();
  48.         this.x = (parentsSize[0] - this.width) / 2;
  49.         this.y = (parentsSize[1] - this.height) / 2;
  50.         this.x += parentsPos[0];
  51.         this.y += parentsPos[1];
  52.         this.window.setPosition(Math.round(this.x), Math.round(this.y));
  53.     }
  54.  
  55.     setSize(width, height) {
  56.         this.width = width;
  57.         this.height = height;
  58.         this.window.setSize(Math.floor(width), Math.floor(height));
  59.         if (this._displayingCenter) this.displayCenter();
  60.     }
  61.  
  62.     setPosition(x, y) {
  63.         this._displayingCenter = false;
  64.         this.x = x;
  65.         this.y = y;
  66.         this.window.setPosition(Math.round(this.x), Math.round(this.y));
  67.     }
  68.  
  69.     destroy() {
  70.         if(this.window.isClosable())this.window.close();
  71.         else this.window.destroy();
  72.     }
  73.  
  74.     display(context) {
  75.         return new Promise((cb) => {
  76.             electron.ipcRenderer.on("callback", (event,val) => {
  77.                 this._cancelCloseEvent = true;
  78.                 this.destroy();
  79.                 cb(val);
  80.             });
  81.             this.window.loadURL(url.format({
  82.                 pathname: path.join(__dirname, this.dir),
  83.                 protocol: 'file:',
  84.                 slashes: true
  85.             })).then(() => {
  86.                     if (this.closePreviousDialog && false) {
  87.                         let children = this.window.getParentWindow().getChildWindows();
  88.                         for (let i = 0; i < children.length; i++) {
  89.                             children[i].destroy();
  90.                         }
  91.                     }
  92.                     if (context) {
  93.                         this.window.webContents.send('store-data', context);
  94.                     }
  95.                     if (typeof this.onShow === "function") {
  96.                         this.window.once(`show`, () => {
  97.                             this.onShow();
  98.                         });
  99.                     }
  100.                     this.window.show();
  101.                 });
  102.             this.window.on(`close`, () => {
  103.                 if (!this._cancelCloseEvent) cb(false);
  104.             });
  105.  
  106.         });
  107.     }
  108. }
  109.  
  110. module.exports = dialog;
  111.  
Advertisement
Add Comment
Please, Sign In to add comment