主にプログラミング関連のメモ帳 ♪(✿╹ヮ╹)ノ
書いてあるコードは自己責任でご自由にどうぞ。記事本文の無断転載は禁止です。
2015/12/24
Windows PC が修理しても壊れている状態で UWP の勉強ができなくなってしまったので、
最近よくきく Electron とやらを学習してみる。
おのれ修理センター、ちゃんと修理せずに送りつけてきおって。
Electron は、 GitHub の Atom エディターで使われている、
Web の技術を使って、クロスプラットフォームのデスクトップアプリが作れるやつ。
Visual Studio Code や Slack、 Wordpress とかのアプリで使われているみたい。
まずは、基本の環境を整えてみる。
これは結構簡単で、
$ npm install -g electron-prebuild
で、開発環境が整います。
package.json
に入れる場合は
"devDependencies" : {
"electron-prebuild" : "^0.36.0"
}
で、 npm install
する。
次に、 main.js
をいじります。 Quick Start のをそのままもってきてます。
'use strict';
const electron = require('electron');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform != 'darwin') {
app.quit();
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
});
あとは適当に、 index.html
をつくります。
まぁここは Hello World を1つ。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Electron</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
そして、 electron .
とすればアプリが起動します。
広く使われている Web の技術をほぼそのまま流用できるみたいなので、すごく便利です。
とりあえず、 PC が直る(もしくは買い換える)その日までに、
なにか1つ作るのを目標にやっていきます。