From 56cca8b529c0861a729612fee7f72fab630e67bb Mon Sep 17 00:00:00 2001 From: Cutls Date: Sat, 15 Jun 2019 00:51:41 +0900 Subject: [PATCH] Change: mainWindow to event.sender on IPC #86 --- app/js/common/version.js | 30 ++++++------------------- app/js/platform/preload.js | 10 +++++++-- app/main/css.js | 16 ++++++------- app/main/dl.js | 46 +++++++++++++++++++------------------- app/main/img.js | 6 ++--- app/main/np.js | 6 ++--- app/main/system.js | 36 +++++++++++++++++++---------- 7 files changed, 76 insertions(+), 74 deletions(-) diff --git a/app/js/common/version.js b/app/js/common/version.js index c93af0aa..0f0b45cd 100644 --- a/app/js/common/version.js +++ b/app/js/common/version.js @@ -46,7 +46,6 @@ function verck(ver) { }); } } - var dialog = remote.dialog; var platform = localStorage.getItem("platform"); console.log("Your platform:" + platform) if (platform == "win32") { @@ -57,14 +56,7 @@ function verck(ver) { buttons: [lang.lang_no, lang.lang_yesno] } if (!localStorage.getItem("winstore")) { - - dialog.showMessageBox(options, function (arg) { - if (arg == 1) { - localStorage.setItem("winstore", "winstore") - } else { - localStorage.setItem("winstore", "localinstall") - } - }); + postMessage(["dialogStore", options], "*") } } else if (platform == "linux") { if (localStorage.getItem("winstore") == "unix") { @@ -77,13 +69,9 @@ function verck(ver) { message: lang.lang_version_platform_linux, buttons: [lang.lang_no, lang.lang_yesno] } - dialog.showMessageBox(options, function (arg) { - if (arg == 1) { - localStorage.setItem("winstore", "snapcraft") - } else { - localStorage.setItem("winstore", "localinstall") - } - }); + if (!localStorage.getItem("winstore")) { + postMessage(["dialogStore", options], "*") + } } } else if (platform == "darwin") { if (localStorage.getItem("winstore") == "unix") { @@ -96,13 +84,9 @@ function verck(ver) { message: lang.lang_version_platform_mac, buttons: [lang.lang_no, lang.lang_yesno] } - dialog.showMessageBox(options, function (arg) { - if (arg == 1) { - localStorage.setItem("winstore", "brewcask") - } else { - localStorage.setItem("winstore", "localinstall") - } - }); + if (!localStorage.getItem("winstore")) { + postMessage(["dialogStore", options], "*") + } } } else { localStorage.setItem("winstore", "unix") diff --git a/app/js/platform/preload.js b/app/js/platform/preload.js index 765d114d..df166a6d 100644 --- a/app/js/platform/preload.js +++ b/app/js/platform/preload.js @@ -8,10 +8,15 @@ onmessage = function (e) { } } +//version.js ipc.send("getPlatform", "") ipc.on('platform', function (event, arg) { localStorage.setItem("platform",arg) }) +ipc.on('winstore', function (event, arg) { + localStorage.setItem("winstore",arg) +}) + ipc.on('reload', function (event, arg) { location.reload(); }) @@ -36,11 +41,12 @@ ipc.on('shownotf', function (event, args) { function nano() { ipc.send('nano', ""); } - +/* var webviewDom = document.getElementById('webview'); const { shell } = require('electron'); webviewDom.addEventListener('new-window', function (e) { shell.openExternal(e.url); -}); \ No newline at end of file +}); +*/ \ No newline at end of file diff --git a/app/main/css.js b/app/main/css.js index c9e446dd..8b0271f2 100644 --- a/app/main/css.js +++ b/app/main/css.js @@ -10,7 +10,7 @@ function css(mainWindow) { ipc.on('custom-css-create', function (e, arg) { fs.writeFileSync(customcss, arg); - mainWindow.webContents.send('custom-css-create-complete', ""); + e.sender.webContents.send('custom-css-create-complete', ""); }) ipc.on('custom-css-request', function (e, arg) { try { @@ -18,29 +18,29 @@ function css(mainWindow) { } catch (e) { var css = ""; } - mainWindow.webContents.send('custom-css-response', css); + e.sender.webContents.send('custom-css-response', css); }) ipc.on('theme-json-create', function (e, arg) { var themecss = join(app.getPath("userData"), JSON5.parse(arg)["id"] + ".thedesktheme"); fs.writeFileSync(themecss, JSON.stringify(JSON5.parse(arg))); if (JSON5.parse(arg)["id"]) { - mainWindow.webContents.send('theme-json-create-complete', ""); + e.sender.webContents.send('theme-json-create-complete', ""); } else { - mainWindow.webContents.send('theme-json-create-complete', "error"); + e.sender.webContents.send('theme-json-create-complete', "error"); } }) ipc.on('theme-json-delete', function (e, arg) { var themecss = join(app.getPath("userData"), arg + ".thedesktheme"); console.log(themecss); fs.unlink(themecss, function (err) { - mainWindow.webContents.send('theme-json-delete-complete', ""); + e.sender.webContents.send('theme-json-delete-complete', ""); }); }) ipc.on('theme-json-request', function (e, arg) { var themecss = join(app.getPath("userData"), arg + ".thedesktheme"); var json = JSON.parse(fs.readFileSync(themecss, 'utf8')); - mainWindow.webContents.send('theme-json-response', json); + e.sender.webContents.send('theme-json-response', json); }) ipc.on('theme-css-request', function (e, arg) { var themecss = join(app.getPath("userData"), arg + ".thedesktheme"); @@ -76,7 +76,7 @@ function css(mainWindow) { ";--active:" + primary + ";--postbox:" + primary + ";--modalfooter:" + primary + ";}.blacktheme #imagemodal{background: url(\"../img/pixel.svg\");}"; - mainWindow.webContents.send('theme-css-response', css); + e.sender.webContents.send('theme-css-response', css); } catch (e) { var css = ""; } @@ -98,7 +98,7 @@ function css(mainWindow) { id: json.id }) } - mainWindow.webContents.send('theme-json-list-response', themes); + e.sender.webContents.send('theme-json-list-response', themes); }); }) } diff --git a/app/main/dl.js b/app/main/dl.js index b2ce8d79..0e2ea1b1 100644 --- a/app/main/dl.js +++ b/app/main/dl.js @@ -16,7 +16,7 @@ function dl(mainWindow, lang_path, base) { if (platform != "others") { updatewin = new BrowserWindow({ webPreferences: { - nodeIntegration:true + nodeIntegration: true }, width: 600, height: 400, @@ -35,7 +35,26 @@ function dl(mainWindow, lang_path, base) { }) //アプデDL ipc.on('download-btn', (e, args) => { + function dl(url, file, dir,e) { + e.sender.webContents.send('mess', "ダウンロードを開始します。"); + const opts = { + directory: dir, + openFolderWhenDone: true, + onProgress: function (e) { + e.sender.webContents.send('prog', e); + }, + saveAs: false + }; + download(updatewin, + url, opts) + .then(dl => { + e.sender.webContents.send('mess', "ダウンロードが完了しました。"); + app.quit(); + + }) + .catch(console.error); + } var platform = process.platform; var bit = process.arch; dialog.showSaveDialog(null, { @@ -55,7 +74,7 @@ function dl(mainWindow, lang_path, base) { if (isExistFile(savedFiles)) { fs.unlinkSync(savedFiles); } - dl(args[0], args[1], m[1]); + dl(args[0], args[1], m[1], e); }); }); @@ -68,26 +87,7 @@ function dl(mainWindow, lang_path, base) { } } - function dl(url, file, dir) { - updatewin.webContents.send('mess', "ダウンロードを開始します。"); - const opts = { - directory: dir, - openFolderWhenDone: true, - onProgress: function (e) { - updatewin.webContents.send('prog', e); - }, - saveAs: false - }; - download(updatewin, - url, opts) - .then(dl => { - updatewin.webContents.send('mess', "ダウンロードが完了しました。"); - app.quit(); - - }) - .catch(console.error); - } ipc.on('general-dl', (e, args) => { var name = ""; @@ -107,14 +107,14 @@ function dl(mainWindow, lang_path, base) { filename: name, openFolderWhenDone: false, onProgress: function (e) { - mainWindow.webContents.send('general-dl-prog', e); + e.sender.webContents.send('general-dl-prog', e); }, saveAs: false }; download(BrowserWindow.getFocusedWindow(), args[0], opts) .then(dl => { - mainWindow.webContents.send('general-dl-message', dir); + e.sender.webContents.send('general-dl-message', dir); }) .catch(console.error); }); diff --git a/app/main/img.js b/app/main/img.js index 46eae534..0c14d47c 100644 --- a/app/main/img.js +++ b/app/main/img.js @@ -24,7 +24,7 @@ function img(mainWindow, dir) { for (var i = 0; i < fileNames.length; i++) { var path = fileNames[i]; var bin = fs.readFileSync(path, 'base64'); - mainWindow.webContents.send('bmp-img-comp', [bin, 'new']); + e.sender.webContents.send('bmp-img-comp', [bin, 'new']); } }); }); @@ -39,7 +39,7 @@ function img(mainWindow, dir) { } dialog.showMessageBox(options, function (index) { if (index === 2) { - mainWindow.webContents.send('adobeagree', "true"); + e.sender.webContents.send('adobeagree', "true"); } if (index > 0) { adobeWindow(); @@ -65,7 +65,7 @@ function img(mainWindow, dir) { Jimp.read(args[0], function (err, lenna) { if (err) throw err; lenna.getBase64(Jimp.MIME_PNG, function (err, src) { - mainWindow.webContents.send('bmp-img-comp', [src, args[1]]); + e.sender.webContents.send('bmp-img-comp', [src, args[1]]); }); }); diff --git a/app/main/np.js b/app/main/np.js index 758d9f01..00da18d3 100644 --- a/app/main/np.js +++ b/app/main/np.js @@ -28,11 +28,11 @@ function np(mainWindow) { try { const nowplaying = require("itunes-nowplaying-mac"); const value = await nowplaying.getRawData(); - mainWindow.webContents.send('itunes-np', value); + e.sender.webContents.send('itunes-np', value); } catch (error) { // エラーを返す console.error(error); - mainWindow.webContents.send('itunes-np', error); + e.sender.webContents.send('itunes-np', error); } } else { var { NowPlaying, PlayerName } = require("nowplaying-node"); @@ -62,7 +62,7 @@ function np(mainWindow) { album: myAIMP.getAlbum(), path: bin } - mainWindow.webContents.send('itunes-np', value); + e.sender.webContents.send('itunes-np', value); } } diff --git a/app/main/system.js b/app/main/system.js index a1aa5f59..feaa7614 100644 --- a/app/main/system.js +++ b/app/main/system.js @@ -42,15 +42,26 @@ function system(mainWindow, dir, lang) { }); //プラットフォーム ipc.on('getPlatform', function (e, arg) { - mainWindow.webContents.send('platform', process.platform); + e.sender.webContents.send('platform', process.platform); }) //言語 ipc.on('lang', function (e, arg) { console.log("set:" + arg); fs.writeFileSync(lang_path, arg); - mainWindow.webContents.send('langres', ""); + e.sender.webContents.send('langres', ""); }) + //ストアから入れたかダイアログ + ipc.on('dialogStore', function (e, args) { + dialog.showMessageBox(args, function (arg) { + if (arg == 1) { + e.sender.webContents.send('winstore', "winstore"); + } else { + e.sender.webContents.send('winstore', "localinstall"); + } + }); + }) + //ハードウェアアクセラレーションの無効化 ipc.on('ha', function (e, arg) { @@ -93,7 +104,7 @@ function system(mainWindow, dir, lang) { console.log(lang); var options = language.delsel(lang) dialog.showMessageBox(options, function (index) { - mainWindow.webContents.send('column-del-reply', index); + e.sender.webContents.send('column-del-reply', index); }) }); ipc.on('nano', function (e, x, y) { @@ -128,7 +139,13 @@ function system(mainWindow, dir, lang) { var cbTimer1; ipc.on('startmem', (e, arg) => { - cbTimer1 = setInterval(mems, 1000); + cbTimer1 = setInterval(mems(e), 1000); + function mems(e) { + var mem = os.totalmem() - os.freemem(); + if (mainWindow) { + e.sender.webContents.send('memory', [mem, os.cpus()[0].model, os.totalmem()]); + } + } }); ipc.on('endmem', (e, arg) => { if (cbTimer1) { @@ -136,17 +153,12 @@ function system(mainWindow, dir, lang) { } }); - function mems() { - var mem = os.totalmem() - os.freemem(); - if (mainWindow) { - mainWindow.webContents.send('memory', [mem, os.cpus()[0].model, os.totalmem()]); - } - } + ipc.on('export', (e, args) => { fs.writeFileSync(args[0], args[1]); }); ipc.on('import', (e, arg) => { - mainWindow.webContents.send('config', fs.readFileSync(arg, 'utf8')); + e.sender.webContents.send('config', fs.readFileSync(arg, 'utf8')); }); //フォント function object_array_sort(data, key, order, fn) { @@ -184,7 +196,7 @@ function system(mainWindow, dir, lang) { const fm = require('font-manager'); var fonts = fm.getAvailableFontsSync(); object_array_sort(fonts, 'family', 'asc', function (fonts_sorted) { - mainWindow.webContents.send('font-list', fonts_sorted); + e.sender.webContents.send('font-list', fonts_sorted); }); }); }