123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- const {dialog, ipcMain} = require('electron');
- const window = require('../../../helper/window');
- const config = require('../../../config');
- const dlg = {};
- dlg.messagePromise = function(title, content, type = '', buttons = ['Ok'], defaultButton = 0, attach = true) {
- const opts = {
- type: type,
- title: title,
- message: content,
- buttons: buttons,
- defaultId: defaultButton
- };
- if (attach) {
- return dialog.showMessageBox(mainWindow, opts);
- } else {
- return dialog.showMessageBox(opts);
- }
- }
- dlg.message = function(title, content, type = 'info', buttons = ['Ok'], defaultButton = 0, attach = true, callback = null) {
- const promise = dlg.messagePromise(title, content, type, buttons, defaultButton, attach);
- if (callback !== null) {
- promise.then(data => {
- callback(data.response, buttons[data.response]);
- });
- }
- }
- dlg.fileSelectorPromise = function(openFile = true, openDir = false, filters = [{name: 'Tous les fichiers', extensions: ['*']}]) {
- const opts = {
- properties: [],
- filters: filters
- }
- if (openFile) {
- opts.properties.push('openFile');
- }
- if (openDir) {
- opts.properties.push('openDirectory');
- }
- return dialog.showOpenDialog(opts)
- }
- dlg.fileSelector = function(openFile = true, openDir = false, filters = [{name: 'Tous les fichiers', extensions: ['*']}], callback = null) {
- const promise = dlg.fileSelectorPromise(openFile, openDir, filters);
- if (callback !== null) {
- promise.then(data => {
- callback(data.canceled, data.filePaths[0]);
- });
- }
- }
- dlg.error = function (title, content) {
- dialog.showErrorBox(title, content);
- }
- dlg.customPromise = function (file) {
- let win = null;
- return new Promise(resolve => {
- // Genererate key for answer
- const key = '-' + Math.random().toString(36).substr(2, 9);
- // Wait win answser
- ipcMain.once('dialog-answer' + key, (arg, answer) => {
- win.close();
- resolve(answer);
- });
- // Create win
- win = window.frameless(file, mainWindow, config.dialog.width, config.dialog.height);
- // When win is ready send the key
- win.on('focus', () => {
- setTimeout(() => {
- win.webContents.send('dialog-key', key);
- }, 500);
- })
- });
- }
- dlg.custom = function (file, callback) {
- const promise = dlg.customPromise(file);
- if (callback) {
- promise.then(data => {
- callback(data);
- });
- }
- }
- module.exports = dlg;
|