-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
176 lines (157 loc) · 5.62 KB
/
Copy pathmain.js
File metadata and controls
176 lines (157 loc) · 5.62 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
const { app, BrowserWindow, globalShortcut, ipcMain, Tray, Menu, nativeImage, dialog, session } = require('electron');
const path = require('path');
const fs = require('fs');
// ─── Core App Setup ───
// Previne os erros de "Acesso negado" e "Gpu Cache Creation failed"
app.disableHardwareAcceleration();
app.commandLine.appendSwitch('disable-gpu-shader-disk-cache');
// Garante que o usuário não consiga abrir 2 SoundMax ao mesmo tempo (evita corromper cache e áudio)
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on('second-instance', () => {
// Alguém tentou abrir de novo, então vamos focar na janela que já existe
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
if (!mainWindow.isVisible()) mainWindow.show();
mainWindow.focus();
}
});
}
let mainWindow = null;
let tray = null;
// ─── App State ───
const appState = {};
// ─── Media Permissions ───
app.commandLine.appendSwitch('disable-features', 'HardwareMediaKeyHandling');
app.on('web-contents-created', (event, contents) => {
contents.session.setPermissionRequestHandler((webContents, permission, callback) => {
if (permission === 'media') {
callback(true);
} else {
callback(false);
}
});
});
// ─── Sound File Loading ───
function scanAudioFolder(folderPath) {
try {
const files = fs.readdirSync(folderPath);
const audioExts = ['.mp3', '.wav', '.ogg', '.flac', '.aac', '.m4a', '.webm'];
return files
.filter(f => audioExts.includes(path.extname(f).toLowerCase()))
.map(f => ({
name: path.basename(f, path.extname(f)),
fileName: f,
path: path.join(folderPath, f),
ext: path.extname(f).toLowerCase(),
size: fs.statSync(path.join(folderPath, f)).size,
}));
} catch (e) {
console.error('Error scanning folder:', e);
return [];
}
}
// ─── Window ───
function createWindow() {
mainWindow = new BrowserWindow({
width: 1100,
height: 720,
minWidth: 800,
minHeight: 500,
frame: false,
titleBarStyle: 'hidden',
titleBarOverlay: { color: '#0a0a0f', symbolColor: '#8888a0', height: 36 },
backgroundColor: '#0a0a0f',
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
preload: path.join(__dirname, 'preload.js'),
},
icon: path.join(__dirname, 'assets', 'icon.png'),
show: false,
});
mainWindow.loadFile('index.html');
mainWindow.once('ready-to-show', () => mainWindow.show());
mainWindow.on('close', (e) => {
if (!app.isQuitting) { e.preventDefault(); mainWindow.hide(); }
});
}
function createTray() {
const iconPath = path.join(__dirname, 'assets', 'icon.png');
let icon = nativeImage.createFromPath(iconPath);
icon = icon.resize({ width: 16, height: 16 }); // Resize for tray
tray = new Tray(icon);
tray.setToolTip('SoundMax');
tray.setContextMenu(Menu.buildFromTemplate([
{ label: 'Abrir SoundMax', click: () => mainWindow.show() },
{ label: 'Parar Todos', click: () => { mainWindow?.webContents.send('all-stopped'); }},
{ type: 'separator' },
{ label: 'Sair', click: () => { app.isQuitting = true; app.quit(); }},
]));
tray.on('double-click', () => mainWindow.show());
}
// ─── IPC Handlers ───
function setupIPC() {
// Scan audio folder
ipcMain.handle('scan-folder', (_, folderPath) => scanAudioFolder(folderPath));
// Read audio file as buffer for renderer to decode
ipcMain.handle('read-audio-file', (_, filePath) => {
try {
const buffer = fs.readFileSync(filePath);
return buffer;
} catch (e) {
return null;
}
});
// Open file dialog for multiple audio files
ipcMain.handle('open-file-dialog', async () => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile', 'multiSelections'],
filters: [{ name: 'Áudio', extensions: ['mp3', 'wav', 'ogg', 'flac', 'aac', 'm4a'] }]
});
return result.canceled ? [] : result.filePaths;
});
// Get internal audio folder (create if not exists)
ipcMain.handle('get-default-audio-path', () => {
const baseDir = app.isPackaged ? path.dirname(app.getPath('exe')) : app.getAppPath();
const soundsDir = path.join(baseDir, 'sounds');
if (!fs.existsSync(soundsDir)) {
fs.mkdirSync(soundsDir, { recursive: true });
}
return soundsDir;
});
// Copy files to internal folder
ipcMain.handle('copy-files-to-sounds', (_, filePaths) => {
const baseDir = app.isPackaged ? path.dirname(app.getPath('exe')) : app.getAppPath();
const soundsDir = path.join(baseDir, 'sounds');
if (!fs.existsSync(soundsDir)) fs.mkdirSync(soundsDir, { recursive: true });
let copied = 0;
for (const src of filePaths) {
try {
const dest = path.join(soundsDir, path.basename(src));
if (src !== dest && !fs.existsSync(dest)) {
fs.copyFileSync(src, dest);
copied++;
}
} catch (e) { console.error('Erro ao copiar', e); }
}
return copied;
});
}
// ─── App Lifecycle ───
app.whenReady().then(() => {
setupIPC();
createWindow();
createTray();
globalShortcut.register('CommandOrControl+Shift+S', () => {
mainWindow?.webContents.send('all-stopped');
});
globalShortcut.register('CommandOrControl+Shift+V', () => {
mainWindow?.webContents.send('vc-toggle');
});
});
app.on('window-all-closed', () => { if (process.platform !== 'darwin') app.quit(); });
app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) createWindow(); });
app.on('will-quit', () => { globalShortcut.unregisterAll(); });