Using Node.js, we can create web applications easily. Now, thanks to the node-webkit we can also create desktop apps with it, using a unique combination of HTML5 and Node.
Introduction
The library combines WebKit engine and Node.js in a unique way. Both WebKit and Node share the same context, allowing you to write your code like it's meant to be executed in a browser, but with the addition of all Node's features. The list of uses is endless. You can create business apps, text and image editors, games, presentations, admin panels, etc. Just name the desktop app that you would like to create, and I can assure you that it will be possible with node-webkit. In this tutorial, I will show you how to get started by showing you how to create a simple text editor.
Preparation
First, you have to get the library. Download the version appropriate for your operating system (the application itself will run on all of them) from github and unzip it wherever you'd like. Now, lets create a basic folder structure. We will have folders for HTML files (.html) and for JavaScript files (.js). Also, create the package.json file in the same directory that the nw executable is in and a node_modules folder to keep the modules we will create.
Package.json
The first thing to do is to fill the required fields in the package.json file. In the case of node-webkit these are name and main (on Github you can find the full list of available package.json options). The first one is exactly the same as in the case of a plain Node.js app. The second one should be a (relative or absolute) path to the main HTML file that will be displayed upon the application starting. In the case of our example, the package.json file should look like this:
Now if you run your app using the nw executable, you should see a blank screen like this:
The Main File
Creating the user interface using node-webkit is no different than creating a webpage (with the slight difference that you know the rendering engine, so you don't have to provide any fallbacks for older browsers, or use libraries like jQuery - and actually you shouldn't, because they include fallback code that will slow down your app). Let's create an index.html file that we will use:
1
2
3
4
5
6
7
8
9
<!DOCTYPEhtml>
<html>
<head>
<title>Simple Text Editor</title>
</head>
<body>
</body>
</html>
Let's also include (and create) the main JavaScript file that we will use, in the body, so it will be executed after the DOM is loaded:
1
<scriptsrc="../js/main.js"></script>
Now the only thing that will change when you open the app will be the title.
Let's Use Some Node
To show you how easily you can use Node.js in node-webkit, let's read the contents of package.json and print it on the screen. Create the js/main.js file and put the following code in it:
var fs = require('fs');
fs.readFile('./package.json', 'utf-8', function (error, contents) { document.write(contents); });
As you can see, the code looks exactly the same as if you were writing it for Node. But then we use document.write to put the file contents on the page. No need to setup any local server. Now open the app and you should see something like this:
Modules
There is another nice thing about node-webkit; You don't have to include a series of <script> tags in your HTML if you want to split your code into modules. You can do it just like you do in Node.js - using require. Let's create a simple module to read file contents to a textarea or write it to a file. Name the file file.js and put it in the node_modules directory. Now put the overall module structure in it:
var fs = require('fs');
function File() { function open(path, document) {
}
function save(path, document) {
}
this.open = open; this.save = save; }
module.exports = new File;
As you can see this will be a static class with two public methods - one for opening the files and one for saving them. The open method will look like this:
function open(path, document) { fs.readFile(path, 'utf-8', function (error, contents) { document.getElementById('editor').value = contents; }); }
Pretty simple right? The function accepts the path to the file as the first parameter and puts the file's contents into the element with an id of "editor". We also need to pass the document object to the function because the script is called using Node's require and it doesn't have access to the WebKit stuff directly. The save method is as simple as the previous one:
function save(path, document) { var text = document.getElementById('editor').value; fs.writeFile(path, text); }
Now let's test if everything works. Change the contents of js/main.js to this:
var file = require('file.js');
console.log(file.open, file.save);
Now if you go to the developer tools console and click the developer refresh button in the right corner of the window you should see the two functions from this module logged. This is another useful thing in node-webkit - your console.log calls are being displayed in the developer tools console, so it's easier to debug your apps.
Node-Webkit's File Inputs
Let's add two file inputs that we will need later:
Notice the nwsaveas attribute in the second input. It is a special type of input from node-webkit, that allows the user to select a non-existing file. Both of the inputs are hidden, because we will only access them from JavaScript. In node-webkit, file inputs are modified in a way that allows you to fire a click event on them, so you can open the file dialog without the user clicking on the input (with no need for the workarounds like invisible inputs above the button). Now we can move to the JavaScript. First, remove the console.log call from the js/main.js file. Now put this code in there:
function clickInput(id) { var event = document.createEvent('MouseEvents'); event.initMouseEvent('click'); document.getElementById(id).dispatchEvent(event); }
document.addEventListener('keyup', function (e) { if (e.keyCode == 'O'.charCodeAt(0) && e.ctrlKey) { clickInput('open'); } else if (e.keyCode == 'S'.charCodeAt(0) && e.ctrlKey) { clickInput('save'); } });
This code is responsible for showing the Open and Save dialogs. The clickInput function does the main job here - it simulates the click event on the input element - in a normal browser, this would be impossible from security reasons, but here it's not a security threat at all. Later, there is a normal keyup event handler that checks if the proper key combination was pressed (Ctrl+O or Ctrl+S) and "clicks" on the inputs. Note that this would also be impossible to achieve in a browser - combinations like Ctrl+O and Ctrl+S are reserved for the browser's internal use and no event fires when they are pressed (only Firefox fires events for them). Now press the developer refresh button and you should see the appropriate dialog when you press Ctrl+S or Ctrl+O. They of course do nothing for now.
Creating the Editor
Now, since we will be creating a text editor, we need something to write on. Add the textarea to the HTML:
Next we have to finish the opening/saving code. Let's create the onchange event listeners for the open and save inputs:
document.getElementById('open').addEventListener('change', function (e) { file.open(this.value, document); });
document.getElementById('save').addEventListener('change', function (e) { file.save(this.value, document); });
Thanks to the module created earlier, the code is really simple. This is also possible thanks to node-webkit, because in browsers (again for security reasons) the value of the file input element is set to a fake path - here it's the path which was selected. Now open the app (or press the developer refresh button, if you didn't closed it) and you should have a perfectly working text editor.
Further Enhancements
We can also do a few other things to make the editor a bit more advanced and useful. For example, let's open a new window when a user presses Ctrl+N. First, add this require to the top of the script:
var gui = require('nw.gui');
The nw.gui module is a node-webkit's library related to the user interface (you can read more about this on Node-webkit's Github). Next add this else if to the document's keyup listener:
} else if (e.keyCode == 'N'.charCodeAt(0) && e.ctrlKey) { gui.Window.open('index.html}
And viola! If you refresh the app, now you can press Ctrl+N to open a new window. This function does differ from the normal window.open though. You can pass various window options as the second parameter. The list of what you can set there is available in the documentation. Another thing that may be useful in a text editor is an application menu (the one that sits under the title bar in Windows/Linux and on the top of the screen on Mac). In node-webkit it's pretty simple. First, let's create the menu:
var menu = new gui.Menu({ type: 'menubar' });
The type menubar is reserved for the application menu. Now we can add items to it. Let it be a File menu:
menu.append(new gui.MenuItem({ label: 'File', submenu: new gui.Menu() }));
menu.items[0] is the first item of our application menu (you can also assign it to a variable when creating it if you want to). We are appending new items to its submenu, and each item gets a click callback to handle the click on it. The gui.Window.get method gets the current window, so we can close it when the user selects the Close option in the File menu. Finally, we can assign the menu to the window:
gui.Window.get().menu = menu;
Again, we are using gui.Window.get to get the current window, then we are assigning our menu to its menu property. Please note, that even though we can assign a different menu to every window, on OSX (Mac) one app can only have one menu (which is global for all windows), so if you want your app to be used on Macs, you should avoid using different menus on different windows. Now if you open or refresh your app, you should see a system menu under the titlebar:
Advertisement
Packaging the App
Now if you would like to share your app with other users, you can package it into one file, so the users can download the node-webkit's executable appropriate to their platform and launch your app with it. First, let's remove the toolbar that makes the window look like a browser - it's nice to have during development, but your users probably don't want to see it. We can achieve this by setting window.toolbar to false in package.json, so it looks like this:
Now if you open the app (nothing will change if you just refresh it, package.json is loaded only on startup), you should see the final result: Packaging the app is pretty simple. Just create a .zip archive with all your assets (all of the files you created, without the files supplied with node-webkit) and change its extension to .nw. That's all. If a user downloads node-webkit and your package, they only need to put it in the node-webkit's directory and run the nw executable. Detailed descriptions, with a few more tips, are available on the node-webkit's github. Now your editor is ready to be shipped to your users.
Conclusion
As you can see, node-webkit is a very promising and powerful library. With frequent updates and fixes, and being supported by Intel, there's almost no chance that it will be discontinued, which can sometimes happen with open source projects. Share your thoughts about this project in the comments below. I personally think it's the best thing out there if you want to create a desktop application using Node.js and HTML.
As the music begins to play, a soft breeze fills the space with the undeniable smell of magnolia blossoms. She gracefully enters and begins a “tree pose” with her hands in Namaste form. Our female figure is inspired by the Indian culture for its gorgeous use of color and the grace of its yoga formations. I hope you’ll enjoy the journey as we retrace, step by step to enlightenment. 1. Becoming Flesh and Blood Step 1 Our first step will be to bring in our sketch. I've scanned her in at 300 dpi to catch as much of the detail as possible. I'm not as picky about my choice of initial drawing tools, and this particular project was completed with a mechanical pencil. We'll be working with a clean line quality from Adobe Photoshop and now we can begin to work. I'm working in Adobe Illustrator at a size of 8.5 x 11 inches for this specific project. When I'm ready to start my layers, I'll label my first layer as "Sketch" and lock the layer to make su...
In this tutorial, we are going to design a set of icons in Adobe Photoshop. An icon set needs to have the same background and theme. For learning purposes, we are going to design a sun icon, an ice flake icon, and an RSS icon. Let's get started. 1. Preparing the Canvas Step 1 Start by making a new file with size 350 px × 350 px . Click the small box next to the Background Contents option to change the new canvas color. Step 2 In the Color Picker dialog box, select grey ( #e0e0e2 ) for the canvas background color. Step 3 It is always a good idea to keep your work structured from the start. Make a new layer group and name it sun . This is where we will place all layers used in the sun icon. 2. Designing the Icon Base Step 1 Use the Rounded Rectangle Tool to draw a rectangle with size 83 px × 64 px and 8 px radius. To get accurate results, use the Properties panel. In this panel, you can simply enter the exact siz...
Most Android devices don't have a physical keyboard. Instead, they rely on a virtual or soft keyboard to accept user input. If you're into Android personalization, knowing how to build a custom, soft keyboard can take your hobby to a whole new level. Using the Android SDK, you can quickly create a soft keyboard with surprisingly few lines of code, because the SDK takes care of a lot of the low level tasks, such as recognizing key touches, drawing the keyboard, and establishing connections between the keyboard and input fields. In this tutorial, you will learn how to create a fully functional soft keyboard that can serve as your Android device's default keyboard. 1. Prerequisites You will need the Eclipse ADT Bundle installed. You can download it from the Android Developer website. 2. Create a New Project Fire up Eclipse and create a new Android application. Call this application, SimpleKeyboard . Make sure you choose a unique package n...
Comments
Post a Comment