Creating Asha Web Apps with Brunch and Jade

Asha web apps?

Asha web apps are a lightweight application development framework for creating apps for low end Nokia devices (Asha series and earlier). While JME is the most popular technology for creating apps for Asha, web apps are a reasonably quick and convenient way to expose content on the web – basically, your garden variety front ends for restful services, spiced by some simple navigation.

Since Asha devices don’t have local javascript execution, javascript is executed on proxy server. “Normal” web pages execute all javascript on the proxy, but Asha web apps provide some “shortcuts” for doing simple DOM manipulation (add/remove css classes, set values in input fields) without the proxy roundtrip. Check out “MWL” library for details.

Standard workflow

Normally, you use Eclipse to develop and test Asha web apps. Nokia provides the “Nokia Web Tools” IDE for this; it’s a rather mainstream Eclipse based IDE, spiced with webkit based “local” simulator (that runs javascript locally), and the “cloud” simulator that runs Javacript on the real Nokia proxy servers (but still renders the DOM on the webkit based simulator).

While this is fine for most web developers, there are lots of us relentlessly looking to optimize our workflows, to make it little more efficient, little more fun (or more tolerable, depending how bad an aversion you have for Javascript and HTML) and little more readable and maintainable.

For this kind of developer, there are tools like CoffeeScript, Brunch, Jade, SASS, LESS, HAML. In this exposition we’ll use CoffeeScript, Brunch and Jade.

CoffeeScript is a language that compiles to idiomatic and readable/debuggable JavaScript. It makes producing JavaScript more tolerable for people used to modern languages without semicolons, `function` keywords
and a selection of other nastiness that plagues JavaScript.

JADE is a language that compiles to HTML.

This Jade:

        doctype 5
	html(lang="en")
	  head
	    title= pageTitle
	  body
	    h1 Jade - node template engine
	    #container
	      if youAreUsingJade
	        p You are amazing
	      else
	        p Get on it!

Compiles to this html:

<h1>Jade - node template engine</h1>
<div id="container">
 
You are amazing
 
</div>

As you can see, this makes it easier to “see forest from the trees”, as the eye is not misdirected by stream of redundant characters and endless <div>’s.

Brunch is an “assembler” for HTML5 applications. It picks up all the pieces you have (CoffeeScript files, JADE templates, etc.) and produces a runnable web application out of that. It can watch the files on the background, compiling CoffeeScript files to JavaScript the second you save them, running unit tests immediately, refreshing the browser in real time etc.

We use Brunch as the “skeleton” of our Asha web application.

Getting started

Prerequisites: This flow has been tested on Linux and Windows. Have to install Node.js, git, the usual stuff every self respecting web developer has set up on day one. Anecdotal evidence suggests it’s a good idea to compile Node.js from source instead of using the packaged versions, if you are lucky enough to be on Linux.

We clone the basic scaffolding from github and install the dependencies:

 

$ git clone git@github.com:vivainio/awa-brunch.git awa-helloworld
$ cd awa-helloworld/
$ npm install
npm http GET https://registry.npmjs.org/javascript-brunch
npm http GET https://registry.npmjs.org/css-brunch
npm http GET https://registry.npmjs.org/uglify-js-brunch
npm http GET https://registry.npmjs.org/clean-css-brunc
.. etc ..
This install Brunch, CoffeeScript compile and lots of other packages with npm.

Let’s take a look at the project structure:


ville@ville-tp:~/p/awa-helloworld$ tree app
app
├── assets
│   ├── config.xml
│   ├── icon.png
│   ├── index.html
│   └── s40-theme
│   ├── css
│   │   ├── s40-theme.css
│   │   ├── single_landscape.css
│   │   └── single_portrait.css
│   ├── images
│   │   ├── arrow-close.png
│   │   ├── arrow-open.png
│   │   ├── back_40x40.png
│   │   ├── button-bg_40x40.png
│   │   ├── close_40x40.png
│   │   ├── grid_40x40.png
│   │   ├── option-menu_titlebar.png
│   │   ├── refresh_40x40.png
│   │   ├── search_40x40.png
│   │   ├── tab_bg.png
│   │   └── title-bar.png
│   └── js
│   ├── s40-theme.js
│   └── screensize.js
├── index.jade
├── README.md
├── templates
│   └── mainlist.jade
├── testCs.coffee
└── testJs.js

 

Now, we start “brunch watch” that compiles the application in real time as we edit the files, and for added convenience launches a webserver serving on :3333:


ville@ville-tp:~/p/awa-helloworld$ brunch watch --server
26 Apr 22:03:48 - info: application started on http://localhost:3333/
26 Apr 22:03:48 - info: compiled in 115ms
26 Apr 22:03:49 - info: compiled in 206ms

 

The resulting application that we can deploy, or preview in browser, gets generated in awa-helloworld/public directory:

 

ville@ville-tp:~/p/awa-helloworld$ tree public
public
├── config.xml
├── icon.png
├── index.html
├── javascripts
│   ├── app.js
│   ├── templates.js
│   └── vendor.js
├── js
│   └── templates.js
└── s40-theme
├── css
│   ├── s40-theme.css
│   ├── single_landscape.css
│   └── single_portrait.css
├── images
│   ├── arrow-close.png
│   ├── arrow-open.png
│   ├── back_40x40.png
│   ├── button-bg_40x40.png
│   ├── close_40x40.png
│   ├── grid_40x40.png
│   ├── option-menu_titlebar.png
│   ├── refresh_40x40.png
│   ├── search_40x40.png
│   ├── tab_bg.png
│   └── title-bar.png
└── js
├── s40-theme.js
└── screensize.js

 

“Sources” under app/ directory were processed to produce these files; .coffee files were compiled and concatenated to “app.js”, index.jade was converted to index.html, and so on.

There is one extra directory we didn’t show yet, the “vendor” directory:


ville@ville-tp:~/p/awa-helloworld$ tree vendor/
vendor/
├── jade_runtime.js
└── README.md

If you are using JavaScript libraries like jQuery, you would just copy it under vendor/ and all of them get automatically concatenated to vendor.js. This makes the app fast to load as it reduces the amount of separate files to download. For production, Brunch can also minify the Javascript files to further reduce page weight. jade_runtime.js is here to support *precompiled Jade templates*

Shut up and show it running

In the interest of making things as “familiar” and frictionless as possible, we will preview the application in standard Chrome browser instead of Nokia Web Tools. So we launch Chromium and disable web security to prevent CORS from cramping our style and killing our Ajax requests:

	$ chromium-browser --disable-web-security http://localhost:3333

This doesn’t look at all what you would expect on the phone screen, because we haven’t specified a viewport size:

Chromium inspector has a handy feature of overriding the viewport size (Settings -> Overrides -> Device Metrics), so we use it to set the resolution to match Asha Full Touch devices (240 x 400):

Now, we have a low-fidelity representation of the phone experience! While this certainly wouldn’t satisfy an UI designer, it’s sufficient for a busy software developer that mostly needs to focus on the “logic” side of the web app.

Launching in NWT

Now is the time to get this running with “real” IDE, i.e. Nokia Web Tools. Launch NWT, do File -> New -> Import web app, select public/config.xml and launch the application in local preview:

Is this it?

There is more to show: how to utilize precompiled templates, how to inject the MWL library, and how Jade can help you create MWL UI “components” with reusable, encapsulated logic. These, and other things to smoothen the development flow will be covered in another blog post, as this already got way too long.

Symbian style Qt Components on N900

It may not be obvious to everyone, but the current shape of Symbian components on Qt Components gitorious repository is pretty good. Just for kicks, I compiled it in N900 scratchbox and took a screenshot:

Symbian components on N900

No special steps were needed to build the repository, just

git clone git://gitorious.org/qt-components/qt-components.git

cd qt-components

./configure -config symbian3 

make

make install 

And then run  examples/symbian/qmlgallery. Earlier, we packaged "meego" style components for N900, but as the public version still relies on MeeGo Touch libraries, the Symbian set of components could turn out to be a lighter solution. If there is interest, we will package a snapshot of this version as well.

Image loading fix for N900 &amp; QML

Now that Qt SDK 1.1 is finally out (in form of Tech Preview), people are rightfully hacking with QML. There is, however, a snag with QML on N900: Image elements with remote URL don’t load. So, if you are doing a QML application, remember to install "mcsp" on your device (sudo gainroot; apt-get install mcsp), and add it as a dependency on your debian package. (mcsp is short for "Maemo Community Service Pack", which is our way of delivering community hotfixes). 

The package has been added as dependency for mad-developer, so new developers are likely to not see the problem. It is still important to add the dependency to your app’s packaging metadata (debian/control), so that the program will work for end users as well.

Your debian/control will probably end up looking like this:

 

… 

Build-Depends: debhelper (>= 5), libqt4-dev

… 

Depends: ${shlibs:Depends}, ${misc:Depends}, mcsp

… 

Thank you, Qt Creator

 
C++ gets occasional flak because of memory management issues and language complexity. While those issues are mostly obsoleted by Qt (regardless of the objections presented by the ever decreasing segment of developers still keeping the faith with managed runtimes ;-) , a real problem is the fact that you need to manually duplicate function signature in header and cpp file. 

Not anymore (thanks, Qt Creator guys!). Looking forward to ever increasing amount of refactoring features you can throw at us (here, less is not more). Blog post for Qt Creator 2.1 beta announcement here.

 

 

Coming up – gPodder for MeeGo, with native MeeGo Touch UI

Clearly, I’m jumping the gun by pre-announcing gPodder on behalf of Thomas Perl, esp. since what he has now is just a C++ UI prototypegPodder is one of the must-have programs for Maemo, and I’m glad to see it’s not going to be held back by the existing Gtk+/Hildon codebase. Once PySide bindings for MeeGo Touch (MTF) are in place, I’m optimistic about getting a proper version for testing soon enough.
If you (too) are interested in kicking the tires of MeeGo Touch (with the eye on porting over an existing app, or beginning a new one), the easiest way is to run it on Ubuntu Lucid from my PPA:
 
sudo add-apt-repository ppa:villemvainio/meego 
sudo apt-get update
sudo apt-get install meegotouch-demos-widgetsgallery libmeegotouch-dev
 
(Caveat: this version is a few months old, as it’s the last version of MTF that is compatible with Qt 4.6. New version using Qt 4.7 will be made available at a later date). 
 
Now, you can run "widgetsgallery" (possibly familiar from N900 extras-devel) on your Ubuntu desktop.
You’ll probably need some examples to get started – grab them by
git clone git://gitorious.org/meegotouch/libmeegotouch.git
Look at the examples/ directory, start opening the .pro files in Qt Creator, stepping through the code in debugger, or whatever strikes your fancy.
 
Protip: press ctrl + shift + R to rotate the screen in a MTF desktop application, to ensure that your application looks nice in both portrait and landscape. 
 
Protip2: You can find more real life MeeGo Touch code in MeeGo Handset UX repos:
 

gPodder MTF mockup  

MeeGo Touch documentation on the web, finally

While enterprising souls have been able to generate the documentation for MeeGo Touch Framework (MTF) from gitorious, you may not have bothered to if you are merely curious about what it might have to offer.

In the likely case that this has evaded you, you may be happy to learn that you can browse the documentation online now (well, it has been possible for a few weeks already).

The documentation is here:

 http://apidocs.meego.com/mtf/

Or, you can jump straight to tutorial about writing your first MTF application:

 http://apidocs.meego.com/mtf/tutorial.html 

I think I’ll proceed with porting my qtdone & qreddit applications to MTF one of these days – MTF works in N900 as well, so the effort would not be "wasted" despite most people not having access to the next_device yet. MTF also makes it possible to avoid using device (or scratchbox) for testing, because on desktop MTF looks just like on device (whereas plain Qt doesn’t, due to how the Fremantle style is implemented). This might peel out some idle seconds from the development flow.

20+ million N900 application downloads from extras

It seems extras is on fire. I tried to check extras download count regularly for the last few days, to catch when it hits 20 million, and now it suddenly jumped to 

20 899 827 

downloads. 

You can stare at the statistics at maemo.org download page

This is also a reminder that you should really move your application to proper extras, instead of letting it gather dust at extras-devel. You might be surprised how quickly you can score downloads by having the application in extras; it appears quite a few Maemo users actually follow the recommendation not to enable extras-devel unless they know what they are doing.

Opera Mobile 10 for Maemo

Opera Mobile 10 preview build for Maemo now available.

What makes this particularly cool is that Opera went all the way to release an N800/N810 version as well. It’s not a second rate build either; it scrolls around smoothly, has nice animated transitions and seems quite reliable on a quick trial.

If you can score a cheap second hand N810 somewhere, you may want to buy it just because of this release.

TweeGo: Qt Graphics View-based twitter app

TweeGo is a new Twitter application for N900. What makes it interesting that it’s not a mere dumb frontend to the twitter protocol, but a nice showcase application for Qt Graphics View and Qt Animation Framework. If you’ve been wondering what you could do with graphics view (and how you should go about it), the codebase of TweeGo, available on gitorious, is a good place to get started!

While the app is intended for N900, there doesn’t seem to be anything N900-specific about it. I was able to compile it for desktop Ubuntu just fine, though it does need Qt 4.6 or newer (the default Qt on karmic won’t cut it). 

Note that this kind of application is exactly where QML will shine, once it’s widely deployed.