2017-09-20 70 views
0

所以這可能是一個簡單的修復,但我一直在研究,並沒有找到解決辦法。我認爲電子默認是這樣做的。在我的Electron應用程序中,我使用remote api從renderer進程調用對話框。一切正常,除此之外,我的對話框不會阻止用戶與BrowserWindow的其餘部分進行交互。我的兩個功能如下電子對話框不阻止與頁面的交互

// function for saving a gantt project projects are serialized into a JSON file 
// the JSON is then stringified for human readiblity then thru the dialog api is saved to 
// users computer 
const saveGantt =() => { 
    let content = gantt.serialize(); 
    content = JSON.stringify(content, null, '\t'); 
    dialog.showSaveDialog(
    { 
     defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`, 
     filters: [ 
     { 
      name: 'json', 
      extensions: ['json'], 
     }, 
     ], 
    }, 
    (filename) => { 
     if (filename === undefined) { 
     return; 
     } 
     fs.writeFile(filename, content, (err) => { 
     if (err) { 
      dialog.showErrorBox(
      'Save Failed', 
      `An error occured saving the file ${err.message}`, 
     ); 
      return; 
     } 
     dialog.showMessageBox({ 
      type: 'none', 
      title: 'Ganttron', 
      message: 'The chart was successfully saved', 
      buttons: ['OK'], 
     }); 
     }); 
    }, 
); 
}; 

// function that loads a gantt project uses the dialog api to open a JSON file from 
// the users computer then it is parsed to return a JSON object that is then parsed by 
// the gantt api 
const loadGantt =() => { 
    dialog.showMessageBox(
    { 
     type: 'none', 
     title: 'Ganttron', 
     message: 'This will clear the gantt chart and load new data', 
     buttons: ['Cancel', 'OK'], 
    }, 
    (response) => { 
     if (response === 1) { 
     gantt.clearAll(); 
     dialog.showOpenDialog(
      { 
      defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents`, 
      filters: [ 
       { 
       name: 'json', 
       extensions: ['json'], 
       }, 
      ], 
      }, 
      (fileName) => { 
      if (fileName === undefined) { 
       return; 
      } 
      fs.readFile(fileName[0], 'utf-8', (err, data) => { 
       quickSaveFileName = fileName[0].toString(); 
       if (err) { 
       dialog.showErrorBox(
        'Load Failed', 
        `Cannot read file ${err.message}`, 
       ); 
       } 
       const loadedData = JSON.parse(data); 
       gantt.parse(loadedData); 
      }); 
      }, 
     ); 
     } 
    }, 
); 
}; 

我正在通過回調函數和兩個函數。我知道如果你不傳遞迴調,它會阻止進程,但不會阻止用戶在對話框外進行交互。我是否錯過了一些簡單的東西,或者這是否會被黑入電子?

回答

0

所以對於任何人來說這個問題。我想到了。 我用remote api函數getCurrentWindow()返回一個BrowserWindow實例形式的主線程。在初始化對話框時,可以使用它將它放入第一個參數中。因此,

import electron, { remote } from 'electron'; 

const { dialog } = electron.remote; 

const win = remote.getCurrentWindow(); 

// function for saving a gantt project projects are serialized into a JSON file 
// the JSON is then stringified for human readiblity then thru the dialog api is saved to 
// users computer 
const saveGantt =() => { 
    let content = gantt.serialize(); 
    content = JSON.stringify(content, null, '\t'); 
    dialog.showSaveDialog(
    win, 
    { 
     defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`, 
     filters: [ 
     { 
      name: 'json', 
      extensions: ['json'], 
     }, 
     ], 
    }, 
    (filename) => { 
     if (filename === undefined) { 
     return; 
     } 
     fs.writeFile(filename, content, (err) => { 
     if (err) { 
      dialog.showErrorBox(
      win, 
      'Save Failed', 
      `An error occured saving the file ${err.message}`, 
     ); 
      return; 
     } 
     dialog.showMessageBox(
      win, 
      { 
      type: 'none', 
      title: 'Ganttron', 
      message: 'The chart was successfully saved', 
      buttons: ['OK'], 
      }, 
     ); 
     }); 
    }, 
); 
}; 

// function that loads a gantt project uses the dialog api to open a JSON file from 
// the users computer then it is parsed to return a JSON object that is then parsed by 
// the gantt api 
const loadGantt =() => { 
    dialog.showMessageBox(
    win, 
    { 
     type: 'info', 
     title: 'Ganttron', 
     message: 'This will clear the gantt chart and load new data', 
     buttons: ['Cancel', 'OK'], 
    }, 
    (response) => { 
     if (response === 1) { 
     gantt.clearAll(); 
     dialog.showOpenDialog(
      win, 
      { 
      defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents`, 
      filters: [ 
       { 
       name: 'json', 
       extensions: ['json'], 
       }, 
      ], 
      }, 
      (fileName) => { 
      if (fileName === undefined) { 
       return; 
      } 
      fs.readFile(fileName[0], 'utf-8', (err, data) => { 
       quickSaveFileName = fileName[0].toString(); 
       if (err) { 
       dialog.showErrorBox(
        win, 
        'Load Failed', 
        `Cannot read file ${err.message}`, 
       ); 
       } 
       const loadedData = JSON.parse(data); 
       gantt.parse(loadedData); 
      }); 
      }, 
     ); 
     } 
    }, 
); 
}; 

它將阻止與當前窗口的交互,直到關閉對話框。