main.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { app, BrowserWindow, ipcMain, dialog } from "electron";
  2. import OAuthWindow from "./OAuthWindow";
  3. import * as path from "path";
  4. let mainWindow: Electron.BrowserWindow | null;
  5. function createWindow() {
  6. mainWindow = new BrowserWindow({
  7. minWidth: 500,
  8. height: 600,
  9. webPreferences: {
  10. preload: path.join(__dirname, "web/preload.js"),
  11. devTools: true,
  12. enableRemoteModule: false,
  13. defaultEncoding: "UTF8",
  14. },
  15. width: 800,
  16. });
  17. mainWindow.setMenu(null);
  18. mainWindow.webContents.openDevTools();
  19. mainWindow.loadFile(path.join(__dirname, "../web/index.html"));
  20. mainWindow.on("closed", () => {
  21. mainWindow = null;
  22. });
  23. }
  24. app.on("ready", createWindow);
  25. app.on("window-all-closed", () => {
  26. if (process.platform !== "darwin") {
  27. app.quit();
  28. }
  29. });
  30. app.on("activate", () => {
  31. if (mainWindow === null) {
  32. createWindow();
  33. }
  34. });
  35. let OAuthWinInstance: OAuthWindow | undefined = undefined;
  36. ipcMain.on('twitch-oauth', (event, arg) => {
  37. if(arg != ""){
  38. if(!OAuthWinInstance){
  39. OAuthWinInstance = new OAuthWindow(arg);
  40. OAuthWinInstance.openWindow({
  41. width: 400,
  42. height: 600,
  43. resizable: false
  44. });
  45. OAuthWinInstance.on("close", ()=>{
  46. OAuthWinInstance = undefined;
  47. })
  48. OAuthWinInstance.on("authcode", (authcode: string)=>{
  49. console.log("Authcode:", authcode);
  50. event.sender.send("twitch-oauth", authcode)
  51. })
  52. }
  53. }
  54. });
  55. let fileLock = false;
  56. ipcMain.on("chooseFile", (ev, id: string)=>{
  57. if(fileLock){
  58. return;
  59. }
  60. fileLock = true;
  61. dialog.showOpenDialog(mainWindow, {
  62. title: "Wybierz wideo",
  63. filters: [
  64. {
  65. name: "Movies",
  66. extensions: ["mp4", "avi", "mkv"]
  67. },
  68. {
  69. name: "All",
  70. extensions: ["*"]
  71. }
  72. ],
  73. properties: [
  74. "openFile",
  75. ]
  76. })
  77. .then((file)=>{
  78. if(file.filePaths[0]){
  79. ev.sender.send("filepath", id, file.filePaths[0])
  80. }
  81. fileLock = false;
  82. })
  83. .catch((err)=>{
  84. console.log("Some error with open dialog");
  85. console.log(err);
  86. fileLock = false;
  87. })
  88. })