Skip to content

Commit

Permalink
🗂 #8 Put IPC Main code into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
c-o-l-i-n committed Aug 17, 2022
1 parent 9c67530 commit ec0126b
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 92 deletions.
97 changes: 97 additions & 0 deletions src/main/ipc-main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Store from 'electron-store'
import { app, BrowserWindow, dialog, ipcMain } from 'electron'
import separateSongParts from './separate-song-parts'
import generateInstrumentPartsAndMaster from './generate-instrument-parts-and-master'
import { IpcMainMessage, IpcRendererMessage, MessageBoxType } from '../types'

const setupIpcMain = (mainWindow: BrowserWindow) => {
const showMessageBox = (type: MessageBoxType, message: string) => {
dialog.showMessageBox(mainWindow, {
type: type,
message: message,
title: app.name,
})
}

const store = new Store()

ipcMain.handle(IpcMainMessage.STORE_GET, (e, storeItem) => {
return store.get(storeItem)
})

ipcMain.on(IpcMainMessage.STORE_SET, (e, storeItem, value) => {
store.set(storeItem, value)
})

ipcMain.on(IpcMainMessage.SHOW_MESSAGE_BOX, (e, type, message) => {
showMessageBox(type, message)
})

ipcMain.on(IpcMainMessage.CHOOSE_PDF_SOURCE_FILE, () => {
mainWindow.webContents.send(
IpcRendererMessage.USER_CHOSE_PDF_SOURCE_FILE,
dialog.showOpenDialogSync(mainWindow, {
title: 'Choose PDF source file',
message: 'Choose PDF source file',
filters: [{ name: 'PDF Files', extensions: ['pdf'] }],
properties: ['openFile'],
})
)
})

ipcMain.on(IpcMainMessage.CHOOSE_SONG_FOLDERS_LOCATION, () => {
mainWindow.webContents.send(
IpcRendererMessage.USER_CHOSE_SONG_FOLDERS_LOCATION,
dialog.showOpenDialogSync(mainWindow, {
title: 'Choose song folders location',
message: 'Choose song folders location',
properties: ['openDirectory'],
})
)
})

ipcMain.on(
IpcMainMessage.SEPARATE,
async (e, sourcePath, partsList, prefix) => {
mainWindow.webContents.send(IpcRendererMessage.SHOW_LOADER)
try {
const destinationDirectory = await separateSongParts(
sourcePath,
partsList,
prefix
)
showMessageBox(
'info',
`Success!\n\nSeparated PDFs created in folder "${destinationDirectory}"`
)
} catch (errorMessage) {
showMessageBox('error', errorMessage.toString())
}
mainWindow.webContents.send(IpcRendererMessage.HIDE_LOADER)
}
)

ipcMain.on(
IpcMainMessage.GENERATE,
async (e, pieceList, songFoldersLocation, partsList, destName) => {
mainWindow.webContents.send(IpcRendererMessage.SHOW_LOADER)
try {
const destinationDirectory = await generateInstrumentPartsAndMaster(
pieceList,
songFoldersLocation,
partsList,
destName
)
showMessageBox(
'info',
`Success!\n\nInstrument parts and Master PDF created in folder "${destinationDirectory}"`
)
} catch (errorMessage) {
showMessageBox('error', errorMessage.toString())
}
mainWindow.webContents.send(IpcRendererMessage.HIDE_LOADER)
}
)
}

export default setupIpcMain
94 changes: 3 additions & 91 deletions src/main/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { app, dialog, BrowserWindow, ipcMain, Menu, shell } from 'electron'
import { app, BrowserWindow, shell } from 'electron'
import contextMenu from 'electron-context-menu'
import Store from 'electron-store'
import unhandled from 'electron-unhandled'
import os from 'os'
import separateSongParts from './separate-song-parts'
import generateInstrumentPartsAndMaster from './generate-instrument-parts-and-master'
import createAppMenu from './menu'
import setupIpcMain from './ipc-main'

declare const MAIN_WINDOW_WEBPACK_ENTRY: string
declare const ABOUT_WINDOW_WEBPACK_ENTRY: string
Expand Down Expand Up @@ -58,23 +56,10 @@ const mainWindowMinimumWidth = mainWindowWidth
const mainWindowHeight = isMac ? 660 : 0
const mainWindowMinimumHeight = isMac ? mainWindowHeight : 700

const store = new Store()

let mainWindow: BrowserWindow
let aboutWindow: BrowserWindow
let appIsQuitting = false

const showMessageBox = (
type: 'none' | 'info' | 'error' | 'question' | 'warning',
message: string
) => {
dialog.showMessageBox(mainWindow, {
type: type,
message: message,
title: app.name,
})
}

contextMenu({
menu: (actions, props, browserWindow, dictionarySuggestions) => [
...dictionarySuggestions,
Expand Down Expand Up @@ -149,77 +134,4 @@ app.on('before-quit', () => {
appIsQuitting = true
})

ipcMain.handle('store-get', (e, storeItem) => {
return store.get(storeItem)
})

ipcMain.on('store-set', (e, storeItem, value) => {
store.set(storeItem, value)
})

ipcMain.on('show-message-box', (e, type, message) => {
showMessageBox(type, message)
})

ipcMain.on('choose-pdf-source-file', () => {
mainWindow.webContents.send(
'user-chose-pdf-source-file',
dialog.showOpenDialogSync(mainWindow, {
title: 'Choose PDF source file',
message: 'Choose PDF source file',
filters: [{ name: 'PDF Files', extensions: ['pdf'] }],
properties: ['openFile'],
})
)
})

ipcMain.on('choose-song-folders-location', () => {
mainWindow.webContents.send(
'user-chose-song-folders-location',
dialog.showOpenDialogSync(mainWindow, {
title: 'Choose song folders location',
message: 'Choose song folders location',
properties: ['openDirectory'],
})
)
})

ipcMain.on('separate', async (e, sourcePath, partsList, prefix) => {
mainWindow.webContents.send('show-loader')
try {
const destinationDirectory = await separateSongParts(
sourcePath,
partsList,
prefix
)
showMessageBox(
'info',
`Success!\n\nSeparated PDFs created in folder "${destinationDirectory}"`
)
} catch (errorMessage) {
showMessageBox('error', errorMessage.toString())
}
mainWindow.webContents.send('hide-loader')
})

ipcMain.on(
'generate',
async (e, pieceList, songFoldersLocation, partsList, destName) => {
mainWindow.webContents.send('show-loader')
try {
const destinationDirectory = await generateInstrumentPartsAndMaster(
pieceList,
songFoldersLocation,
partsList,
destName
)
showMessageBox(
'info',
`Success!\n\nInstrument parts and Master PDF created in folder "${destinationDirectory}"`
)
} catch (errorMessage) {
showMessageBox('error', errorMessage.toString())
}
mainWindow.webContents.send('hide-loader')
}
)
setupIpcMain(mainWindow)
28 changes: 27 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,30 @@ interface Tab {
page: Page
}

export { Page, ActivePageContextType, Tab }
type MessageBoxType = 'none' | 'info' | 'error' | 'question' | 'warning'

enum IpcMainMessage {
STORE_GET = 'store-get',
STORE_SET = 'store-set',
SHOW_MESSAGE_BOX = 'show-message-box',
CHOOSE_PDF_SOURCE_FILE = 'choose-pdf-source-file',
CHOOSE_SONG_FOLDERS_LOCATION = 'choose-song-folders-location',
SEPARATE = 'separate',
GENERATE = 'generate',
}

enum IpcRendererMessage {
USER_CHOSE_PDF_SOURCE_FILE = 'user-chose-pdf-source-file',
USER_CHOSE_SONG_FOLDERS_LOCATION = 'user-chose-song-folders-location',
SHOW_LOADER = 'show-loader',
HIDE_LOADER = 'hide-loader',
}

export {
Page,
ActivePageContextType,
Tab,
MessageBoxType,
IpcMainMessage,
IpcRendererMessage,
}

0 comments on commit ec0126b

Please sign in to comment.