2017-12-06 2 views
0

私の電子出願にはBrowserWindowのインスタンスがあります。mainWindowsecondaryWindowです。クリックするとsecondaryWindowを開くボタンがmainWindowにあります。電子ブラウザのウィンドウ

私の問題は、mainWindowにあるものをクリックしたくないということです。secondaryWindowが閉じられるまでです。

私が得ることができる最も近い

mainWindow.hide()を使用していたが、これはちょうど完全に私が secondaryWindowがアクティブである間に、まだ mainWindowを見たいけど、それは/非アクティブ無効にする必要があり、ウィンドウを非表示にします。

提案がありますか?メインプロセスから

1:

あなたがメインプロセスから子ウィンドウを開くことができ

+1

は、子ウィンドウモーダルウィンドウを作ります。 [window.open()](https://electronjs.org/docs/api/window-open)の例これにより、子ウィンドウが閉じられるまで親ウィンドウが無効になります。 – Rhayene

答えて

0

は、子ウィンドウを開くには2通りの方法があります。これは、たとえば、メニュー内のカスタムウィンドウに便利です。

ここでコンストラクタを使用して、parentの子にすることができます。 modal属性がtrueの場合、親ウィンドウは子ウィンドウが閉じられるまでアクセスできません。

function createChild(parent, html) { 
    //creates modal window 
    child = new BrowserWindow({ 
    width: 786, 
    height: 847, 
    parent: parent, 
    modal: true, 
    show: false 
    }); 
    child.loadURL(html); 
    child.webContents.openDevTools(); 
    child.once("ready-to-show",() => { 
    child.show(); 
    }); 
} 

2.レンダリングプロセス今

から、私たちは常に正しい、ちょうど子ウィンドウを開くには、メイン処理にIPCの上にイベントを送信したくないですか?

私たちはそうする必要はありません。そのために窓にopen関数を使用することができます。例えば

:これにより

parent.webContents.on(
    "new-window", 
    (event, url, frameName, disposition, options, additionalFeatures) => { 
     Object.assign(options, { 
     parent: parent, 
     modal: true 
     }); 
    } 
); 

window.open()が呼び出されます。

const button = document.querySelector('button') 
button.addEventListener('click', e => { 
    self.open(`file://${__dirname}/child.html`) 
}) 

このウィンドウをあなたの親やモーダルの子を作成するには、親ウィンドウ上のEventListenerを登録することができます親ウィンドウでは、モーダル子ウィンドウが開きます。

main.js

const { app, BrowserWindow } = require("electron"); 

let win; 
function createWindow() { 
    win = new BrowserWindow({ width: 1000, height: 800 }); 
    win.loadURL(`file://${__dirname}/index.html`); 
    win.webContents.openDevTools(); 
    win.on("closed",() => { 
    win = null; 
    }); 

    win.webContents.on(
    "new-window", 
    (event, url, frameName, disposition, options, additionalFeatures) => { 
     Object.assign(options, { 
     parent: win, 
     modal: true 
     }); 
    } 
); 
} 

app.on("ready", createWindow); 

index.htmlを

<!DOCTYPE html> 
<html> 

<body> 
    <p>I am the parent, you can't touch me until you closed my child!</p> 
    <button>Open child!</button> 
    <script> 
     const button = document.querySelector('button') 
     button.addEventListener('click', e => { 
      self.open(`file://${__dirname}/child.html`) 
     }) 
    </script> 
</body> 

</html> 

のchild.html

<!DOCTYPE html> 
<html> 

<body> 
    I'm the child! 
</body> 

</html> 
関連する問題