Electronã§ä½ã常é§ã¢ããª
æ¥åãã¼ã«ãä½æããéã試ãã«Electronã§ä½ã£ã¦ã¿ã¾ããã 諸ã ã®çç±ã§çµå±æ¡ç¨ããªãã£ããã§ããããã£ãããªã®ã§ãã£ããã¨ã®è¨é²ããã¦ããããã¨æãã¾ãã
å®éãå
¬å¼ã®ããã¥ã¡ã³ããããããããã®ã§ããã§äºè¶³ãã¡ããã¾ãã
ãã®è¾ºã¾ã§ã¯è§¦ã£ããªã»ã»ã¨ããèªåç¨ã®ã¡ã¢ã§ãã®ã§ãã£ããå¦ã³ããæ¹ã¯å
¬å¼ã®クイック スタート | Electronãããããã§ãã®ãã
Electronã¨ã¯
Electronã¯GitHubãéçºããã½ããã¦ã§ã¢ãã¬ã¼ã ã¯ã¼ã¯ã§ãã
ããããã¼ã¸ã§ã¯
JavaScript, HTML, CSS ã§ã¯ãã¹ãã©ãããã©ã¼ã ãªãã¹ã¯ãããã¢ããªéçº
ããã«ãはじめに | Electronã§ã¯
Electron 㯠Chromium 㨠Node.js ããã¤ããªã«çµã¿è¾¼ããã¨ã§ãåä¸ã® JavaScript ã³ã¼ããã¼ã¹ãç¶æãã¤ã¤ããã¤ãã¤ãéçºçµé¨ç¡ãã§ã WindowsãmacOSãLinux ã§åä½ããã¯ãã¹ãã©ãããã©ã¼ã ã¢ããªãä½æã§ãã¾ãã
ã¨ããã¾ãã
ãã©ã¦ã¶ãªãåãã©ãããã©ã¼ã ã«ãã¼ãã£ã³ã°ããã¦ããããã®ä¸ã§åãJavascriptã§ã³ã¼ãæ¸ãã°ãã«ããã©ãããã©ã¼ã ã§ãããããã¨ããçºæ³ã§ããã èãã人ããããªãã¨ç´ ç´ã«æåããè¦ããããã¾ãã
ä»åä½ãã¢ããª
æ¥åãã¼ã«ã§å¿ è¦ã ã£ãè¦ç´ ã¯ä»¥ä¸ã®éãã§ããã
- 常é§ã¢ããªã§ãã¬ã¤ï¼éç¥é åï¼ã«æ ¼ç´
- PCã®æåï¼ãµã¹ãã³ãã»ãªã¸ã¥ã¼ã ã»ã·ã£ãããã¦ã³ï¼ã§å¦çãå®æ½
- ãã以å¤ã«å®æçãªå¦çãå®æ½ï¼ãã¼ãªã³ã°ï¼
ä»åã¯
- èµ·åããããã¬ã¤ã«ã¢ã¤ã³ã³è¡¨ç¤º
- PCã®æåï¼ãµã¹ãã³ãã»ãªã¸ã¥ã¼ã ã»ã·ã£ãããã¦ã³ï¼ããã°ã«è¨é²
- ä¸å®æéæ¯ã«å¨å²ã®WiFiã¢ã¯ã»ã¹ãã¤ã³ãæ°ãæ¤ç´¢ãã¦éç¥
ã¿ãããªå½¹ã«ç«ããªãã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ã¿ã¾ãã
ããã¸ã§ã¯ãä½æ
ã¯ã¤ã㯠ã¹ã¿ã¼ããåèã«ããã¸ã§ã¯ããä½æãã¾ãã
mkdir electron-tray-sample && cd electron-tray-sample yarn init
entry pointã ã main.js
ã«ãã¦ããã¾ãããã¤ãã§ã«ç©ºã£ã½ã®ãã¡ã¤ã«ãç¨æãã¦ããã¾ãããã
touch main.js
ç¶ãã¦devDependencies ã« Electronããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ãã¾ãã
devDependenciesã¯ã¢ããªã±ã¼ã·ã§ã³ãããªãã¦éçºã«å¿
è¦ãªããã±ã¼ã¸ã§ã---dev
ãªãã·ã§ã³ãã¤ãã¦ã¤ã³ã¹ãã¼ã«ã§ãã¾ãã
yarn add --dev electron
æå¾ã«start
ã§å®è¡ã§ããããã«package.jsonãæ¸ãæãã¾ãã
{ "name": "electron-tray-sample", "version": "1.0.0", "main": "index.js", "license": "MIT", "devDependencies": { "electron": "^19.0.4" }, "scripts": { "start": "electron ." } }
ãã®ç¶æ ã§å®è¡ãã¦ã¿ã¾ãããã
yarn start
Electronã®ã¢ã¤ã³ã³ãDockã«åºã¦ãã¾ãããæåã§ãã
ãã¬ã¤
ãã¬ã¤ã¸ã¢ã¤ã³ã³ã追å ãã¦ã¿ã¾ãã
ã¢ã¤ã³ã³ç»åãæºåããªãããªã®ã§é©å½ã«ä½ãã¾ããtray.pngã16x16ãtray@2x.pngã32x32ã§ä½æãã¾ããã
app.whenReady()
ã§ã¢ããªã±ã¼ã·ã§ã³ã®åæåå®äºæã®Promiseãåå¾ãã¦ãã¬ã¤ãçæãã¾ãã
ãã¬ã¤ã®ã¢ã¸ã¥ã¼ã«ã¯Tray
ã§ããããããããï¼
const { app, Tray, Menu, nativeImage } = require('electron') app.whenReady().then(() => { const img = nativeImage.createFromPath(__dirname + "/assets/tray.png") let tray = new Tray(img) tray.setToolTip('Tray app') tray.setContextMenu(Menu.buildFromTemplate([ { label: 'Quit', role: 'quit' } ])) })
å®è¡ãã¦ã¿ãã¨ãã¬ã¤ã«å ç¨ã®ã¢ã¤ã³ã³ãåºã¾ããã
å³ã¯ãªãã¯ã§ã¡ãã¥ã¼ãåºã¾ãã
macOSã ã¨Dockã«ã¢ã¤ã³ã³ã表示ããã¦ãã¾ãã®ã§ã常é§ã¢ããªã£ã½ããªãã§ãã
app.dock.hide()
ã§æ¶ãã¾ãã®ã§æ¶ãã¡ããã¾ãããã
if (process.platform === 'darwin') { app.dock.hide() }
é»æºç¶æ ã¨ãã°
é»æºç¶æ
ã®ã¢ãã¿ãããããpowerMonitor
ã¨ããã¢ã¸ã¥ã¼ã«ãç¨æããã¦ãã¾ãã
ãã¾ãã¾ãªé»æºããã¿ã®ã¤ãã³ããç¨æããã¦ãã¾ãããsuspend
ãresume
ãshutdown
ãåãã¦ã³ã³ã½ã¼ã«ã¸ã¡ãã»ã¼ã¸ãåºåãã¦ã¿ã¾ãã
const { app, powerMonitor } = require('electron') app.whenReady().then(() => { powerMonitor.on('suspend', () =>{ console.log('suspend') }) powerMonitor.on('resume', () =>{ console.log('resume') }) powerMonitor.on('shutdown', () =>{ console.log('shutdown') }) })
å®è¡å¾ã¹ãªã¼ãâ復帰ããã¦ã¿ãæ§åã§ããããããã»ã»ã¨ããè¨ãããã®ãªãç»åã§ããã
ã·ã£ãããã¦ã³ã¯ã³ã³ã½ã¼ã«ã ã¨è¦éãã®ã§ãã°ã«æ®ãã¾ãããã
electron-logã¨ããã¢ã¸ã¥ã¼ã«ãå©ç¨ãã¾ãã
yarn add electron-log
const { app, powerMonitor } = require('electron') const log = require('electron-log'); app.whenReady().then(() => { log.info('begin') powerMonitor.on('suspend', () =>{ log.info('suspend') }) powerMonitor.on('resume', () =>{ log.info('resume') }) powerMonitor.on('shutdown', () =>{ log.info('shutdown') }) })
ãã°ã®åºåå ã¯ãããªã«ã¼ã«ã§ãã
- macOS: ~/Library/Logs/{app name}/{process type}.log
- Windows: %USERPROFILE%\AppData\Roaming{app name}\logs{process type}.log
- Linux: ~/.config/{app name}/logs/{process type}.log
ã¡ã¤ã³ããã»ã¹ããåºåãã¦ããã®ã§ãmacOSã§åããããããã«æ¸ãè¾¼ã¾ãã¦ãã¾ãã
~/Library/Logs/electron-tray-sample/main.log
WiFiã¹ãã£ã
node-wifi-scannerã¨ããã¢ã¸ã¥ã¼ã«ãã¤ã³ã¹ãã¼ã«ãã¾ãã
yarn add node-wifi-scanner
Readmeã®ä¾ãåèã«ãã¹ãã£ã³ã§ããSSIDã®æ°ã表示ãã¦ã¿ã¾ãã
ããèãã¦ã¿ãã¨SSIDãããªãã¦MACã¢ãã¬ã¹ã®æ¹ãè¯ãã£ãã§ãããï¼
const scanner = require('node-wifi-scanner'); scanner.scan((err, networks) => { if (err) { console.error(err); return; } const ssids = networks.map(n => n['ssid']) if (ssids.length > 0) { console.log(`Found ${ssids.length} access point`) } });
ãã®å¦çãå®æçã«å¼ã³åºããçµæãã³ã³ã½ã¼ã«ãããªãã¦éç¥ãããããã«å¤æ´ãã¾ãã
const { app, Notification, powerMonitor } = require('electron') const scanner = require('node-wifi-scanner'); const findap = () => { scanner.scan((err, networks) => { if (err) { console.error(err) return } const ssids = networks.map(n => n['ssid']) if (ssids.length > 0) { new Notification({ title: 'WiFi AP', body: `Found ${ssids.length} access point`, silent: true, }).show() } }) } app.whenReady().then(() => { setInterval(() => findap(), 30 * 1000) })
ã¡ããã¨éç¥ãæ¥ã¾ãããããã«ãã¦ãæå³ããªãéç¥ã§ããã
Package
ãªãã¨ãªãå®æããæ°ãããã®ã§é å¸ç¨ããã±ã¼ã¸ãä½ãã¾ãã
ã¯ã¤ãã¯ã¹ã¿ã¼ãã«å£ã£ã¦ Electron Forge ãå©ç¨ãããã¨æãã¾ãã
yarn add --dev @electron-forge/cli
npx electron-forge import
ãã㧠yarn run make
ã§ããã±ã¼ã¸ãåºæ¥ä¸ããã¾ãããã¢ã¤ã³ã³ãElectronã®ã¾ã¾ãªã®ã§ãã£ããã ããå¤æ´ãã¾ãããã
åãã©ãããã©ã¼ã ç¨ã«ã¢ã¤ã³ã³ä½ãã®ã¯é¢åã§ãããelectron-icon-builderãå©ç¨ããã¨ç°¡åã«ã§ãã¾ãã
1024x1024ã®pngç»åããä½æããã¢ã¤ã³ã³ãã¡ã¤ã«ãassetsãã£ã¬ã¯ããªã«é ç½®ãã¾ããã
ã¢ã¤ã³ã³ã®æå®ã¯packagerConfig
ã®icon
ã«æå®ãã¾ãã
"config": { "forge": { "packagerConfig": { "icon": "assets/icon" }, "makers": [ : :
ããã§yarn run make
ããã¨ãout
ãã£ã¬ã¯ããªã«ããã±ã¼ã¸ãä½æããã¾ãã
ã¡ããã¨ã¢ããªã±ã¼ã·ã§ã³ã¢ã¤ã³ã³ãæå®ããéãã«ãªã£ã¦ãã¾ããã
ä½æããã¢ããªãmacOSã¨Windows10ã§åä½ãããæ§åã§ãã
ã©ã¡ããã¡ããã¨åãã¦ã¾ããã
Electronã§Notificationã®ãã¹ã pic.twitter.com/Nu0fA3heIq
— ã¤ãã (@hollyhockberry) 2022å¹´6æ15æ¥
Windowsã¯ãããªæããªãã§ããã
— ã¤ãã (@hollyhockberry) 2022å¹´6æ15æ¥
ã¢ããªåï¼ãã¡ããéãããã©ããã£ã¦å¤ããã®ã㪠pic.twitter.com/3L8FZJ8L2d
çµããã«
ãã¤ããªãµã¤ãºã大ãããªã©ããã¤ãä¸å©ãªç¹ã¯ããã¾ãããç°¡åã«ãã«ããªãã©ãããã©ã¼ã ã®ã¢ããªã±ã¼ã·ã§ã³ãã§ãã¦ãã¾ãã®ã¯ä¾¿å©ã ãªãã¨æãã¾ãã
ã¡ãªã¿ã«æ¡ç¨ããªãã£ãçç±ã§ãããWindowsç°å¢ã§Shutdownã®ã¤ãã³ããçºç«ããªãã£ãããã§ãã
ä½åº¦ãã·ã£ãããã¦ã³ãã¦ãããã°ãã¡ããã¾ããããã¡ããã¨APIã®ããã¥ã¡ã³ãã«Windows対å¿ãã¦ãªããã¨æ¸ãã¦ããã¾ããã
æè¨ï¼ããã¥ã¡ã³ãã¯ã¡ããã¨èªãã
ããã¸ã§ã¯ãã¯GitHubã«ããã¦ã¾ãã®ã§ãããã°ã©ãã