-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
226 lines (212 loc) · 5.58 KB
/
index.js
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
const {
app,
BrowserWindow,
screen,
Menu,
ipcMain,
dialog,
TouchBar,
} = require('electron')
const path = require('path')
const { FileManager } = require('./libs/FileManager')
const fs = require('fs')
require('electron-reload')(__dirname, {
electron: require(`${__dirname}/node_modules/electron`),
})
const isMac = process.platform === 'darwin'
function format(sec_num) {
const hours = Math.floor(sec_num / 3600)
const minutes = Math.floor((sec_num - hours * 3600) / 60)
const seconds = sec_num - hours * 3600 - minutes * 60
if (minutes < 10) {
minutes = '0' + minutes
}
if (seconds < 10) {
seconds = '0' + seconds
}
return minutes + ':' + seconds
}
function createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize
const mainWindow = new BrowserWindow({
width: 800,
height: 800,
x: width * 0.5 - 400,
y: height * 0.3,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
contextIsolation: false,
},
backgroundColor: '#312450',
icon: path.join(__dirname, 'assets/favicon.ico'),
})
const fileManager = new FileManager(mainWindow)
// and load the index.html of the app.
mainWindow.loadFile('renderer.html')
const template = [
{
role: 'help',
label: 'Sorrygle Desktop',
submenu: [
{
label: 'About',
click: async () => {
const { shell } = require('electron')
await shell.openExternal(
'https://github.com/Muzihuzi/Sorrygle-Desktop'
)
},
},
],
},
{
label: 'File',
submenu: [
{
label: 'Open',
accelerator: 'CmdOrCtrl+O',
click: () => {
fileManager.openFileWindow()
},
},
{
label: 'Save',
accelerator: 'CmdOrCtrl+S',
click: () => {
fileManager.saveFileWindow()
},
},
],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
...(isMac
? [
{ role: 'pasteAndMatchStyle' },
{ role: 'delete' },
{ role: 'selectAll' },
{ type: 'separator' },
{
label: 'Speech',
submenu: [{ role: 'startSpeaking' }, { role: 'stopSpeaking' }],
},
]
: [{ role: 'delete' }, { type: 'separator' }, { role: 'selectAll' }]),
],
},
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forceReload' },
{ role: 'toggleDevTools' },
{
label: 'New Window',
accelerator: 'CmdOrCtrl+T',
click: async () => {
createWindow()
},
},
{
label: 'close',
accelerator: 'CmdOrCtrl+W',
click: async () => {
app.quit()
},
},
],
},
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
return mainWindow
}
let isPlaying = false
app.whenReady().then(async () => {
const window = createWindow()
const playButton = new TouchBar.TouchBarButton({
label: '▶ 재생',
click: () => {
isPlaying = !isPlaying
playButton.label = isPlaying ? '■ 정지' : '▶ 재생'
window.webContents.send('playToggle', isPlaying)
},
})
const saveButton = new TouchBar.TouchBarButton({
label: '💾 MID로 저장',
click: () => {
window.webContents.send('save2midi')
},
})
const player = new TouchBar.TouchBarSlider({
label: '00:00/00:00',
minValue: 0,
maxValue: 0,
value: 0,
change: (value) => {
window.webContents.send('timeChange', value)
},
})
const touchBar = new TouchBar({
items: [playButton, saveButton, player],
})
window.setTouchBar(touchBar)
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
ipcMain.on('stop', () => {
isPlaying = false
player.value = 0
player.maxValue = 0
playButton.label = '▶ 재생'
})
ipcMain.on('start', () => {
isPlaying = true
playButton.label = '■ 정지'
})
ipcMain.on('update', (event, payload) => {
if (payload.duration) {
player.label = `00:00/${new Date(Math.floor(payload.duration) * 1000)
.toISOString()
.slice(14, 19)}`
player.maxValue = Math.floor(payload.duration * 100)
}
if (!payload.duration || !payload.current) return
player.maxValue = Math.floor(payload.duration * 100)
player.value = Math.floor(payload.current * 100)
// player.value = Math.floor(payload.current/payload.duration * 1000)
player.label = `${new Date(Math.floor(payload.current) * 1000)
.toISOString()
.slice(14, 19)}/${new Date(Math.floor(payload.duration) * 1000)
.toISOString()
.slice(14, 19)}`
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
ipcMain.on('save2midi', (e, arg) => {
const path = dialog
.showSaveDialog({
title: '쏘리글을 mid로 저장하기',
filters: [{ name: 'mid file', extensions: ['mid'] }],
})
.then((path) => path)
.then((data) => {
if (data.canceled) return
fs.writeFileSync(data.filePath, arg)
dialog.showMessageBox(null, {
type: 'info',
title: '저장을 완료했습니다!',
message: '저장을 완료했습니다!',
})
})
})