Files
Time4Midi/main.js
T

137 lines
3.3 KiB
JavaScript

const { app, BrowserWindow, dialog, ipcMain } = require("electron");
const fs = require("node:fs/promises");
const path = require("node:path");
app.commandLine.appendSwitch("enable-blink-features", "WebMidi");
function createWindow() {
const win = new BrowserWindow({
width: 1600,
height: 980,
minWidth: 1200,
minHeight: 760,
backgroundColor: "#0f1117",
icon: path.join(__dirname, "time4midi.png"),
webPreferences: {
contextIsolation: true,
sandbox: false,
preload: path.join(__dirname, "preload.js")
}
});
win.loadFile(path.join(__dirname, "src", "index.html"));
}
ipcMain.handle("project:save", async (_event, payload) => {
if (payload?.filePath) {
await fs.writeFile(payload.filePath, payload.data, "utf8");
return { canceled: false, filePath: payload.filePath };
}
const result = await dialog.showSaveDialog({
defaultPath: payload?.defaultPath || "time4midi-project.json",
filters: [
{ name: "Time4Midi Project", extensions: ["json"] }
]
});
if (result.canceled || !result.filePath) {
return { canceled: true };
}
await fs.writeFile(result.filePath, payload.data, "utf8");
return { canceled: false, filePath: result.filePath };
});
ipcMain.handle("project:load", async () => {
const result = await dialog.showOpenDialog({
properties: ["openFile"],
filters: [
{ name: "Time4Midi Project", extensions: ["json"] }
]
});
if (result.canceled || result.filePaths.length === 0) {
return { canceled: true };
}
const filePath = result.filePaths[0];
const data = await fs.readFile(filePath, "utf8");
return { canceled: false, filePath, data };
});
ipcMain.handle("audio:pickFile", async () => {
const result = await dialog.showOpenDialog({
properties: ["openFile"],
filters: [
{ name: "Audio", extensions: ["mp3", "m4a", "flac", "wma"] }
]
});
if (result.canceled || result.filePaths.length === 0) {
return { canceled: true };
}
return {
canceled: false,
filePath: result.filePaths[0]
};
});
ipcMain.handle("app:getUserDataPath", () => {
return app.getPath("userData");
});
ipcMain.handle("mappingLibrary:pickLoadFile", async (_event, payload) => {
const result = await dialog.showOpenDialog({
properties: ["openFile"],
defaultPath: payload?.defaultPath || "time4midi-mappings.json",
filters: [
{ name: "Time4Midi Mapping", extensions: ["json"] }
]
});
if (result.canceled || result.filePaths.length === 0) {
return { canceled: true };
}
return {
canceled: false,
filePath: result.filePaths[0]
};
});
ipcMain.handle("mappingLibrary:pickSaveFile", async (_event, payload) => {
const result = await dialog.showSaveDialog({
defaultPath: payload?.defaultPath || "time4midi-mappings.json",
filters: [
{ name: "Time4Midi Mapping", extensions: ["json"] }
]
});
if (result.canceled || !result.filePath) {
return { canceled: true };
}
return {
canceled: false,
filePath: result.filePath
};
});
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});