electron/docs/tutorial/quick-start.md

147 lines
4.5 KiB
Markdown
Raw Normal View History

2013-09-09 07:35:57 +00:00
# Quick start
2013-08-14 22:43:35 +00:00
## Introduction
Generally, atom-shell enables you to create desktop applications with pure
JavaScript by providing a runtime with rich native APIs, you could see it as
an variant of node.js runtime that focused on desktop applications instead of
web server.
2013-08-14 22:43:35 +00:00
But it doesn't mean atom-shell is a JavaScript binding to GUI libraries, instead
atom-shell uses web pages as GUI, so you could also see it as a minimal Chromium
browser, controlled by JavaScript.
2013-08-14 22:43:35 +00:00
### The browser side
2013-08-14 22:43:35 +00:00
If you had experience with node.js web applications, you would notice that there
are types of JavaScript scripts: the server side scripts and the client side
2014-05-07 01:30:58 +00:00
scripts. The server side JavaScript, are the scripts that run on the node.js
runtime, and the client side JavaScript, are the ones that run on user's browser.
2013-08-14 22:43:35 +00:00
In atom-shell we have similar concepts, since atom-shell displays GUI by showing
web pages, we would have **scripts that run in the web page**, and also have
**scripts ran by the atom-shell runtime**, which created those web pages.
Like node.js, we call them **client scripts**, and **browser scripts**.
In traditional node.js applications, communication between server side and
client side are usually done by web sockets. In atom-shell, we have provided
the [ipc](../api/ipc-renderer.md) module for browser side to client
communication, and the [remote](../api/remote.md) module for easy RPC
support.
### Web page and node.js
Normal web pages are designed to not touch outside world, which makes them not
suitable for interacting with native systems, atom-shell provides node.js APIs
in web pages so you could access native resources in web pages, just like
[node-webkit](https://github.com/rogerwang/node-webkit).
But unlike node-webkit, you could not do native GUI related operations in web
pages, instead you need to do them on the browser side by sending messages or
use the easy [remote](../api/remote.md) module.
## Write your first atom-shell app
2013-08-14 22:43:35 +00:00
Generally, an atom-shell app would be like this:
2013-08-14 22:43:35 +00:00
```text
app/
├── package.json
├── main.js
└── index.html
```
The format of `package.json` is exactly the same with node's modules, and the
script specified by the `main` field is the startup script of your app, which
will run under the browser side. An example of your `package.json` is like
this:
2013-08-14 22:43:35 +00:00
```json
{
"name" : "your-app",
2013-08-14 22:43:35 +00:00
"version" : "0.1.0",
"main" : "main.js"
}
```
The `main.js` should create windows and handle system events, and an typical
example is:
2013-08-14 22:43:35 +00:00
```javascript
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
// Report crashes to our server.
require('crash-reporter').start();
2013-08-14 22:43:35 +00:00
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the javascript object is GCed.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function() {
if (process.platform != 'darwin')
app.quit();
2013-08-14 22:43:35 +00:00
});
// This method will be called when atom-shell has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600});
2013-08-14 22:43:35 +00:00
// and load the index.html of the app.
mainWindow.loadUrl('file://' + __dirname + '/index.html');
// Emitted when the window is closed.
2013-08-14 22:43:35 +00:00
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;
});
});
2013-08-14 22:43:35 +00:00
```
Finally the `index.html` is the web page you want to show:
```html
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using node.js <script>document.write(process.version)</script>
and atom-shell <script>document.write(process.versions['atom-shell'])</script>.
</body>
</html>
```
## Run your app
After done writing your app, you could create a distribution of your app by
following the [Application distribution](./application-distribution.md) guide
and then execute the packaged app, or you can just use the downloaded atom-shell
binary to execute your app directly.
On Window:
```cmd
2014-05-05 06:49:05 +00:00
$ .\atom-shell\atom.exe path-to-app\
```
On Linux:
```bash
2014-05-05 06:49:05 +00:00
$ ./atom-shell/atom path-to-app/
```
On Mac OS X:
```bash
2014-05-05 06:49:05 +00:00
$ ./Atom.app/Contents/MacOS/Atom path-to-app/
```