-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
252 lines (240 loc) · 7.13 KB
/
main.js
File metadata and controls
252 lines (240 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
const {app, Menu, BrowserWindow, ipcMain, Tray, nativeTheme, nativeImage} = require('electron');
const path = require('path');
// 計る「3」分間
const MAX_MILLI_SECONDS = 3*60*1000;
let milliseconds = MAX_MILLI_SECONDS;
// トレイアイコン
let tray = null;
// メインウィンドウ
let mainWindow;
// タイマー動作中フラグ
let isWorking = false;
// タイマーID
let idTimer;
// ダークテーマ
let isDarkTheme = false;
const createWindow = () => {
mainWindow = new BrowserWindow({
title: app.name,
width: 1024,
height: 640,
minWidth: 1024,
minHeight: 640,
"window.autoDetectColorScheme": true,
webPreferences: {
// In Electron 12, the default will be changed to true.
worldSafeExecuteJavaScript: true,
// XSS対策としてnodeモジュールをレンダラープロセスで使えなくする
nodeIntegration: false,
// レンダラープロセスに公開するAPIのファイル
//(Electron 11 から、デフォルト:falseが非推奨となった)
contextIsolation: true,
preload: path.resolve(`${__dirname}/preload.js`)
},
// icon: iconPath
});
// load a local HTML file
mainWindow.loadURL(`file://${__dirname}/index.html`)
// debug: ディベロッパーツールの表示
mainWindow.openDevTools();
mainWindow.on('closed', () => {
mainWindow = null;
});
}
// メイン・メニューの生成
const createMainMenu = () => {
const { shell } = require('electron')
const isMac = process.platform === 'darwin'
const template = [
// { role: 'appMenu' }
...(isMac ? [{
label: app.name,
submenu: [
{ role: 'about' },
{ type: 'separator' },
{ role: 'services' },
{ type: 'separator' },
{ role: 'hide' },
{ role: 'hideothers' },
{ role: 'unhide' },
{ type: 'separator' },
{ role: 'quit' }
]
}] : []),
{
label: 'View',
submenu: [
{ role: 'reload', enabled: false },
{ role: 'forcereload', enabled: false },
{ role: 'toggledevtools', accelerator: 'CmdOrCtrl+Alt+I' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
},
// { role: 'windowMenu' }
{
label: 'Window',
submenu: [
{ role: 'minimize' },
{ role: 'zoom' },
...(isMac ? [
{ type: 'separator' },
{ role: 'front' },
{ type: 'separator' },
{ role: 'window' }
] : [
{ role: 'close' }
])
]
},
{
role: 'help',
submenu: [
{
label: 'GitHub Repository',
click: async () => {
await shell.openExternal('https://github.com/hibara/SimpleTimer')
}
},
{
label: "Qiitaの記事",
click: async () => {
await shell.openExternal('https://qiita.com/hibara/items/c59fb6924610fc22a9db')
}
},
{
label: "作者ウェブサイト",
click: async () => {
await shell.openExternal('https://hibara.org/')
}
}
]
}
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
// トレイアイコンを生成する
const createTrayIcon = async () => {
tray = null;
let imgFilePath;
if (nativeTheme.shouldUseDarkColors === true){
console.log("Dark: true");
isDarkTheme = true;
imgFilePath = __dirname + '/images/tray-icon/white/100.png';
}
else{
console.log("Dark: false");
imgFilePath = __dirname + '/images/tray-icon/black/100.png';
}
console.log(nativeTheme.themeSource);
console.log("HighContrastColors: " + nativeTheme.shouldUseHighContrastColors);
console.log("InvertedColorScheme: " + nativeTheme.shouldUseInvertedColorScheme);
const contextMenu = Menu.buildFromTemplate([
{ label: '終了', role: 'quit' }
]);
tray = new Tray(imgFilePath);
tray.setToolTip(app.name);
tray.setContextMenu(contextMenu)
}
// タイマーの表示
const displayTimer = (valMilliSeconds) => {
mainWindow.webContents.send("ipc-display-timer", valMilliSeconds);
// タスクトレイのアイコン表示
const percent = valMilliSeconds / MAX_MILLI_SECONDS;
const percent100 = Math.floor(percent * 100);
// 5の倍数で丸める
let multipleOfFive = Math.round(percent100 / 5) * 5;
let imgFilePath;
// Windowsは「.ico」、macOSは「.png」形式
let imgFileName = ('000' + multipleOfFive).slice(-3) + (process.platform === 'win32' ? '.ico' : '.png');
// Windows、あるいはダークテーマの場合は基本「白色」のアイコンテーマを使う
if ( isDarkTheme === true ) {
imgFilePath = __dirname + '/images/tray-icon/white/' + imgFileName;
}
else {
imgFilePath = __dirname + '/images/tray-icon/black/' + imgFileName;
}
// プログレスバー
if (isWorking === false && valMilliSeconds === MAX_MILLI_SECONDS) {
mainWindow.setProgressBar(-1);
}
else {
mainWindow.setProgressBar(percent);
}
// Tray アイコンの差し替え
tray.setImage(nativeImage.createFromPath(imgFilePath));
tray.setToolTip(percent100 + "% - " + app.name);
};
// タイマー開始
const StartTimer = () => {
idTimer = setInterval(() => {
milliseconds-=100;
displayTimer(milliseconds);
if ( milliseconds <= 0) {
StopTimer();
// macOS の場合のみ、タイマー終了時にアイコンがバウンスします
if ( process.platform === 'darwin' ) {
app.dock.bounce();
}
}
}, 100);
}
// タイマー停止
const StopTimer = () => {
isWorking = false;
clearInterval(idTimer);
}
// タイマーリセット
const ResetTimer = () => {
milliseconds = MAX_MILLI_SECONDS;
mainWindow.setProgressBar(-1);
displayTimer(milliseconds);
}
// システムカラーの変更イベント
nativeTheme.on("updated", () => {
isDarkTheme = nativeTheme.shouldUseDarkColors === true;
displayTimer(milliseconds);
});
app.on('ready', () => {
// create window
createWindow();
createMainMenu();
createTrayIcon();
});
app.on('window-all-closed', () => {
StopTimer();
if (process.platform !== 'darwin') { // macOS以外
app.quit();
}
});
app.on('activate', () => {
if (mainWindow === null) {
createWindow();
ResetTimer();
}
});
// タイマー開始(レンダラープロセスからのIPC通信:invokeメソッド)
ipcMain.handle("ipc-timer-start", () => {
if ( isWorking === true ) {
return true
}
else {
StartTimer();
isWorking = true;
}
return true;
});
// タイマー停止(レンダラープロセスからのIPC通信)
ipcMain.on("ipc-timer-stop", () => {
StopTimer();
});
// タイマーのリセット(レンダラープロセスからのIPC通信)
ipcMain.on("ipc-timer-reset", () => {
ResetTimer();
});