OSDN Git Service

remove
[commentgenerator-electron/CommentGenerator-Electron.git] / src / main.js
1 //アプリケーションを制御し、ネイティブなブラウザウィンドウを作成するモジュール
2 const { app, Menu, BrowserWindow, dialog, Notification, globalShortcut } = require('electron')
3 const path = require('path')
4 const fs = require('fs');
5 app.setName("CommentGenerator-Electron")
6 let window
7
8
9 // 実行環境がmacOSならtrue
10 const isMac = (process.platform === 'darwin');  // 'darwin' === macOS
11
12 //------------------------------------
13 // About Panelの内容をカスタマイズする
14 //------------------------------------
15 const aboutPanel = function(){
16   dialog.showMessageBox({
17     title: `${app.name}について`,
18     message: `${app.name} ${app.getVersion()}`,
19     detail: `Created by Neko7sora\n©‌​‌‌​​​‌‌​​‌‌​‌​ ‌​​‌​‌​​‌​​‌​​​​2‌‌​​‌​​​‌​​​‌‌​​0‌​​‌​​​​‌​​​‌‌​‌2​‌‌‌‌​‌‌​‌‌‌‌‌​‌‌‌​‌‌‌​​‌​‌‌1 ${app.name}`,//特殊文字に注意
20     buttons: [],
21     //icon: resolve(__dirname, 'asset/image/icon.png')
22   });
23 }
24
25 //------------------------------------
26 // メニュー
27 //------------------------------------
28 // メニューを準備する
29 /*
30 const template = Menu.buildFromTemplate([
31   ...(isMac ? [{
32       label: app.name,
33       submenu: [
34         {role:'about',      label:`${app.name}について` },
35         {type:'separator'},
36         {role:'services',   label:'サービス'},
37         {type:'separator'},
38         {role:'hide',       label:`${app.name}を隠す`},
39         {role:'hideothers', label:'ほかを隠す'},
40         {role:'unhide',     label:'すべて表示'},
41         {type:'separator'},
42         {role:'quit',       label:`${app.name}を終了`}
43       ]
44     }] : []),
45   {
46     label: 'ファイル',
47     submenu: [
48       isMac ? {role:'close', label:'ウィンドウを閉じる'} : {C}
49     ]
50   },
51   {
52     label: '編集',
53     submenu: [
54       {role:'undo',  label:'元に戻す'},
55       {role:'redo',  label:'やり直す'},
56       {type:'separator'},
57       {role:'cut',   label:'切り取り'},
58       {role:'copy',  label:'コピー'},
59       {role:'paste', label:'貼り付け'},
60       ...(isMac ? [
61           {role:'pasteAndMatchStyle', label:'ペーストしてスタイルを合わせる'},
62           {role:'delete',    label:'削除'},
63           {role:'selectAll', label:'すべてを選択'},
64           {type:'separator' },
65           {
66             label: 'スピーチ',
67             submenu: [
68               {role:'startSpeaking', label:'読み上げを開始'},
69               {role:'stopSpeaking',  label:'読み上げを停止'}
70             ]
71           }
72         ] : [
73           {role:'delete',    label:'削除'},
74           {type:'separator'},
75           {role:'selectAll', label:'すべてを選択'}
76         ])
77      ]
78   },
79   {
80     label: '表示',
81     submenu: [
82       {role:'reload',         label:'再読み込み'},
83       {role:'forceReload',    label:'強制的に再読み込み'},
84       {role:'toggleDevTools', label:'開発者ツールを表示'},
85       {type:'separator'},
86       {role:'resetZoom',      label:'実際のサイズ'},
87       {role:'zoomIn',         label:'拡大'},
88       {role:'zoomOut',        label:'縮小'},
89       {type:'separator'},
90       {role:'togglefullscreen', label:'フルスクリーン'}
91     ]
92   },
93   {
94     label: 'ウィンドウ',
95     submenu: [
96       {role:'minimize', label:'最小化'},
97       {role:'zoom',     label:'ズーム'},
98       ...(isMac ? [
99            {type:'separator'} ,
100            {role:'front',  label:'ウィンドウを手前に表示'},
101            {type:'separator'},
102            {role:'window', label:'ウィンドウ'}
103          ] : [
104            {role:'close',  label:'閉じる'}
105          ])
106     ]
107   },
108   {
109     label:'ヘルプ',
110     submenu: [
111       {label:`${app.name} ヘルプ`},    // ToDo
112       ...(isMac ? [ ] : [
113         {type:'separator'} ,
114         {click: aboutPanel ,  label:`${app.name}について` }
115       ])
116     ]
117   }
118 ]);
119 */
120 // メニューを適用する
121 //Menu.setApplicationMenu(template);
122
123
124 function createWindow() {
125   //ブラウザウィンドウを作成します。
126   window = new BrowserWindow({
127     width: 400,
128     height: 600,
129     minWidth: 400,
130     minHeight: 600,
131     maxWidth: 600,
132     maxHeight: 800,
133     frame: true,
134     webPreferences: {
135       preload: path.join(__dirname, 'preload.js'),
136       enableRemoteModule: true,
137       nodeIntegration: false,
138     },
139   })
140   const menu = Menu.buildFromTemplate(exampleMenuTemplate());
141   Menu.setApplicationMenu(menu);
142   //アプリのindex.htmlを読み込みます。
143   window.loadFile('index.html')
144 }
145
146 function showNotification() {
147   //通知を表示します。
148   const notification = {
149     title: 'Basic Notification',
150     body: 'Notification from the Main process'
151   }
152   new Notification(notification).show()
153 }
154
155 // このメソッドは、Electronが初期化を終え、ブラウザウィンドウを作成する準備ができたときに呼び出されます。
156 // 初期化が完了し、ブラウザウィンドウを作成する準備ができたときに呼び出されます。
157 // いくつかのAPIは、このイベントが発生した後にのみ使用できます。
158 app.whenReady().then(() => {/*
159   globalShortcut.register('CommandOrControl+Shift+I', () => {
160     console.log('Devtool Block')
161   })//*/
162 }).then(() => {
163   createWindow()// <-- function createWindow のやつを動かす。
164 /*
165   window.setProgressBar(0.555555555555,{mode:"paused"})
166   setTimeout(() => {
167     window.setProgressBar(0)
168     setTimeout(() => {
169       window.setProgressBar(0.2)
170       setTimeout(() => {
171         window.setProgressBar(0.25)
172         setTimeout(() => {
173           window.setProgressBar(0.3,{mode:"error"})
174           setTimeout(() => {
175             window.setProgressBar(0.9)
176             setTimeout(() => {
177               window.setProgressBar(0.99)
178               setTimeout(() => {
179                 window.setProgressBar(1)
180                 setTimeout(() => {
181                   window.setProgressBar(-1)//win11では動作しない
182                   setTimeout(() => {
183                     window.setProgressBar(2)
184                   }, 2000)
185                 }, 2000)
186               }, 2000)
187             }, 2000)
188           }, 2000)
189         }, 2000)
190       }, 2000)
191     }, 2000)
192   }, 2000)
193 //*/
194   // このリスナーは、アプリケーションが起動した後に動きます。
195   app.on('activate', () => {
196
197     // 表示されているウィンドウがないときにのみ動きます。(バックグラウンド処理など)
198     /*
199     if (BrowserWindow.getAllWindows().length === 0) {
200       // 新しいブラウザウィンドウを作成します。
201       createWindow()
202     }
203     //*/
204   })
205
206 }).then(() => {
207   //showNotification()
208 })
209
210 // 開いているウィンドウがなくなったときにのみ動きます。
211 // !このリスナーは、macOSではOSのウィンドウ管理の動作のため、使用できません。
212 // !macOSでは、ユーザーがCmd + Qで明示的に終了させるまで、アプリケーションとそのメニューバーがアクティブなままであることが一般的です。
213 // !(余談:Neko7soraは、macを持っていないため詳しく分かりません。以上....)
214 app.on('window-all-closed', () => {
215   if (process.platform !== 'darwin') {
216     // アプリケーションを終了!!
217     app.quit()
218   }
219 })
220
221 function plugin() {
222   require("./plugin/NavigatorOnLine/index.js")(app, BrowserWindow, path);
223 }
224 plugin()
225
226 const exampleMenuTemplate = () => [
227   {
228     label: "Options",
229     submenu: [
230       {
231         label: `${app.name}を完全終了する`,
232         click: () => app.quit()
233       }
234     ]
235   }
236 ];