ย - Last update: 2021-01-27
Basic of Electron (Udemy ๊ฐ์)
Basic of Electron
- ํ๋ซํผ. ๋ฐ์คํฌํฑ ์ฑ์ ๋ง๋ค๊ธฐ ์ํจ.
- ์น ์ฑ์ ์ต๊ทผ๋ค์ด ๊ต์ฅํ๊ฒ ๋ณต์กํด์ก๋ค. Electron์ ์ด๋ฌํ ํน์ฑ์ ๋ฐ์คํฌํฑ ์ฑ์์ ์ฌ์ฉํ๊ธฐ ์ํด ๋ฑ์ฅํ ํ๋ซํผ์ด๋ผ๊ณ ๋ณด๋ฉด ๋จ.
- ๊ธฐ๋ณธ์ ์ผ๋ก ์๊ฐํด์ผํ ๋ถ๋ถ์ ์น ๋ธ๋ผ์ฐ์ ๋ ์ ์ ์ ํ๋๋์คํฌ์ ์ ๊ทผ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ๊ฒ. ์ด๋ฌํ ์ ์ฝ์ ํ์ด์ฃผ๋ ๊ฒ์ด Electron ์ด๋ผ๋ ํ๋ซํผ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
- ์ฌ์ฉ์์ ์ปดํจํฐ ๋ฆฌ์์ค์ ์ ๊ทผ ๊ฐ๋ฅํ๊ฒ ๋๋ค๋ ์ ์ด ํต์ฌ!!
- Electron === Google Chrome (Mostly..)
- Task Manager๋ฅผ ๋ณด๋ฉด Google Chrome Helper๋ผ๋ ๋์ด ์ฌ๋ฌ๊ฐ๊ฐ ๊ฐ์ด ์กด์ฌํจ. ์ด๊ฒ์ ์ดํดํ๋ฉด Electron์ ์ดํดํ๋ ๋ฐ์ ๋์์ด ๋๋ค.
- ์ ๋ค๋ฅธ ํ๋ก์ธ์ค๊ฐ ๊ฐ์ด ๋๋๊ฐ?
- ๊ฐ์ด ์ฃฝ์ง ์๊ฒ ํ๊ธฐ ์ํด. ๋๋๋ง ์ด์๋ฅผ ๋ง๊ธฐ ์ํด. Isolate ์ํค๊ธฐ ์ํด.
- RendererProcess๊ฐ ํต์ฌ์. ์ด ํ๋ก์ธ์ค๊ฐ ์นํ์ด์ง๋ฅผ ๊ทธ๋ฆฐ๋ค.
- IPC(Inter Process Communication)์ ํตํด MainWindow, RendererProcess๋ค์ด ํต์ ํ๋ค.
- ์น๋ธ๋ผ์ฐ์ ์ ๋ค๋ฅธ ์ ์, ์น๋ธ๋ผ์ฐ์ ์ ์ฃผ์ํ์์ค๊ณผ ๊ฐ์ ์์ ๋ธ๋ผ์ฐ์ ์์ฒด์ ๋ด์ฉ๊น์ง๋ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค๋ ์ .
- Electron์ ์ญ์ฌ(?)
- GitHub -> Electron -> Atom (์ง๊ธ์ VS Code๊ฐ ๋จน์์ง๋ง..)
- GitHub๋ ์ฒ์์ Atom์ ์ํด Electron์ ๋ง๋ค์๊ณ (Atom์ JS ์ ์ ๊ฐ ๋ง์ JS๋ก ๋ง๋ค์๋ค ํจ), ์ด๊ฒ์ด ์ธ๋งํ๋ค๊ณ ํ๋จํด์ Open Source๋ก ๋ง๋ค์์
- ์ด๊ฒ์ Slack, Discord, VS Code ๋ฑ์์ ์ฌ์ฉํ๊ธฐ ์์ํ๋ค.
Handling Electron Project
- Electron (Run at terminal, Overall Chrome Process๋ก ์๊ฐ) -> Create MainWindow(BrowserWindow) -> index.html์ ๋ก๋ฉ.
- BrowserWindow๋ฅผ ์์ฑํ๋ ๋ก์ง ๋ํ js๋ก ์์ฑํ๋ค๋ ์ ! (ํ ์ธ์ด ์ฌ์ฉ 0%)
- nodemon์ ์ฌ์ฉํ๋ฉด ์ง์์ ์ธ F5 ํค๋ฅผ ๋๋ฌ์ผ ํ๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์์. -> ์๋ ์์์ ํด์ฃผ๋ ์๋ฒ side์์๋ ์ด๋ฏธ ์์ฃผ ์ฐ์ด๊ณ ์๋ ๋
์์
- ์๋ ๋ช
๋ น์ด๋, src/js/index.js ๋๋ ํ ๋ฆฌ์์ ์ฝ๋๋ณํ๊ฐ ๊ฐ์งํ๋ฉด ์ฌ์์์ ํ๋๋ก ์ค์ .
"start": "npm-run-all --parallel watch:webpack watch:nodemon",
"watch:webpack": "webpack --config=webpack.config.js --mode=development --watch",
"watch:nodemon": "nodemon --exec \"electron .\" --watch \"./src/js/setup.js\""
Status Tray Application
Tray
์ BrowserWindow
๋ ๋น์ฐํ๊ฒ๋ ๋ค๋ฅด๋ค.
Tray Icon
์ ๋ง๋๋ ๋ฐฉ๋ฒ?
new Tray(iconPath)
tray.on('click', () => { mainWindow.show(); });
์์ฃผ ์ฐ์ด๋ ํจํด
Tray
๊ทผ์ฒ์ ์ฐฝ์ ์์ฑํ๋ ๋ฐฉ๋ฒ? (ํนํ OS, ์ฌ์ฉ์๋ง๋ค ์์น๊ฐ ๋ค๋ฅผ ๊ฒ..)
click
์ด๋ฒคํธ๊ฐ ์ฌ ๋, bounds
๊ฐ์ฒด๊ฐ ์ ๋ฌ๋๋ค. ์ด ๊ฐ์ฒด์ x, y ์ขํ๊ฐ ์ ๊ณต๋จ. ์ด๊ฒ์ ์ฐธ๊ณ ํ์ฌ window ์ ์์น๋ฅผ ์กฐ์ ํ๋ฉด ๋๋ค.
mainWindow
์ setBounds
๋ฅผ ์ค์ ํ๋ฉด ์ง์ ํ ์์ญ์ ์๋์ฐ๋ฅผ ์ ์์ฑํ ์ ์๋ค.
ETC (๋ค๋ฅธ ๊ฐ์ ๋ด์ฉ ํฌํจ)
- app ์ webContent์
new-window
์ด๋ฒคํธ์ preventDefault
๋ฅผ ๊ฑธ์ด๋๋ฉด ๋งํฌ์์ ์ ์ฐฝ์ด ๋จ๋ ๊ฒ์ ๋ง์ ์ ์์.
- session์ ๋ชจ๋ browserWindow์ ๊ณต์ ๋จ.
mainWindow.webContents.session
const {session} = require('electron')
๋ ๊ฐ์ ๊ฐ ์ฐธ์กฐ.
- custom session๋ ๋ง๋ค ์ ์๊ธด ํ๋ค.
cookie
๋ session
์ ํ์ object ์.
- download ๋ control ๊ฐ๋ฅํ๋ค. (์ฐฝ ์๋จ๊ณ ์๋ ๋ค์ด ๊ฐ๋ฅ)
globalShortcut
: ํ๋ฉด์ด ๋ ์์ง ์๋๋ผ๋ ์ ์ฒด์ ์ผ๋ก ๋ฑ๋ก๋๋ Shortcut
- ์ฌ์ง์ด ์ฑ์ด ์ผ์ ธ์์ง ์๋๋ผ๋ ์คํ๋จ! ๊ทธ๋ฌ๋ ์ถฉ๋๋๋ ๊ฒ์ ์์์ ํํผํด์ผ ํ ๊ฒ..
const {screen} = require('electron')
์ ํตํด์ ์คํฌ๋ฆฐ์ ๊ด๋ จ๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์
- getAllDisplay(): ์ ์ฒด ์ฐ๊ฒฐ๋ display์ ๋ํ ์ ๋ณด๋ค์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
- touchSupport ๋ฑ๋ฑ ํฌํจ..
- Main Process๊ฐ BrowserWindow๋ฅผ ์์ ํ๋ฉฐ, Browser Window๋ Renderer Process ๊ทธ ์์ฒด๋ค.
- Main Process๋ ์ฌ๋ฌ๊ฐ์ BrowserWindow๋ฅผ ์์ ํ๋ค.
- BrowserWindow๋ webFrame > webContents ๋ฅผ ์์ ํ๋ค.
electron.desktopCapturer
: ๋ฐ์คํฌํ์ ํ๋ฉด์ ์ ์ฅ ๊ฐ๋ฅํจ
process.version
: ๊ฐ ๋ฒ์ ์ ๊ฐ์ง๊ณ ์์..
- ex) chrome, napi, electron, v8, opensssl ๋ฑ๋ฑ..
process.hang()
: Renderer๋ฅผ Hang์ ๊ฑธ ์ ์์.
process.crash()
: crash ๋ ๋๋ฅผ ์๋ฎฌ๋ ์ดํ
ํ ์ ์์.
- ์ด ๋ reload ๊ฑธ๋ฉด ๋ณต๊ตฌ๋ ์ ์์. ๋ณต๊ตฌ ๋๋๊ฒ ๋ซ๋?..
- ๋ช๋ช electron API๋ค์ Shared API๋ก, renderer์ main ๋ชจ๋์๊ฒ์ ์ฐ์ผ ์ ์์. (ex:
shell
๋ฑ..)
- electron.remote ์์จ๋ ๋๋ค๋ ๋ป
Distribution
- Electron-Builder ๊ฐ ์ง๋ฆฌ.
- cli ์ต์
๋ ์ง์ํ๋ค. ์ด๋ค platform์ผ๋ก distribute ํ ์ง๋ ์ด๊ฑธ๋ก ๊ฒฐ์
- ๊ฒฐ๊ณผ๋ฌผ์ dist ๋๋ ํ ๋ฆฌ์ ์ ๊ณต๋จ.
- ์ ๋๋ก ํ๋ ค๋ฉด code-signing๋ ํด์ผ ํจ. (์๋ง mac์ ๋ ๊ฐ์ ๋๋๋ฏ?)
- Electron-Builder์ ์ด๋ฏธ AutoUpdater๊ฐ ์๋ค.
- GitHub ๋ฅผ ํตํด ๋ฐฐํฌํ๋ ค๋ฉด, Personal access token์ ๋ฐ๊ธ๋ฐ์์ผ ํ๋ค.
- ์ด๋ฅผ ๋ฑ๋กํ๋ฉด, ๋น๋ ํ ์๋์ผ๋ก GitHub release์ ์ฌ๋ ค๋ฒ๋ฆด ์ ์์. (์ด๋ฏธ ๋ง๋ค์ด์ง tag๋ฅผ ์ฐธ์กฐํด์ ์ฌ๋ฆฌ๋ ๋ฏ?)
- (์ฐธ๊ณ ) setx ์ปค๋งจ๋๋ฅผ ์๋์ฐ์์ ์ฌ์ฉํ๋ฉด ํ๊ฒฝ๋ณ์๋ฅผ ์ ์ ์ค์ ํ ์ ์๋ค.
- ์๋ ์
๋ฐ์ดํธ๋ electron-updater์ ํจ๊ป~