Blame platform-demos/C/statusbar.js.page

Packit 1470ea
Packit 1470ea
Packit 1470ea
      xmlns:its="http://www.w3.org/2005/11/its"
Packit 1470ea
      xmlns:xi="http://www.w3.org/2001/XInclude"
Packit 1470ea
      type="guide" style="task"
Packit 1470ea
      id="statusbar.js">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">Statusbar (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#display-widgets"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-10" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Taryn Fox</name>
Packit 1470ea
      <email its:translate="no">jewelfox@fursona.net</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Show notifications in a dedicated statusbar</desc>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>Statusbar</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/statusbar2.png"/>
Packit 1470ea
  

This statusbar keeps track of how many times you've clicked a button. Applications like <link href="http://projects.gnome.org/gedit/">gedit</link> use statusbars to display information at a glance, and show notifications without interrupting the user.

Packit 1470ea
  

Messages pushed to a statusbar go on top of its stack, and can be popped off to show the next-most recent one. You can also clear away every message of a specific type all at once. This sample application demonstrates these functions.

Packit 1470ea
    <links type="section" />
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Libraries to import</title>
Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Lang = imports.lang;
Packit 1470ea
]]>
Packit 1470ea
    

These are the libraries we need to import for this application to run. Remember that the line which tells GNOME that we're using Gjs always needs to go at the start.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Creating the application window</title>
Packit 1470ea
    
Packit 1470ea
const StatusbarExample = new Lang.Class({
Packit 1470ea
    Name: 'Statusbar Example',
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    _init: function() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsstatusbar',
Packit 1470ea
            flags: Gio.ApplicationFlags.FLAGS_NONE
Packit 1470ea
        });
Packit 1470ea
Packit 1470ea
    // Connect 'activate' and 'startup' signals to the callback functions
Packit 1470ea
    this.application.connect('activate', Lang.bind(this, this._onActivate));
Packit 1470ea
    this.application.connect('startup', Lang.bind(this, this._onStartup));
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate: function() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup: function() {
Packit 1470ea
        this._buildUI ();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

All the code for this sample goes in the StatusbarExample class. The above code creates a <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> for our widgets and window to go in.

Packit 1470ea
    
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI: function() {
Packit 1470ea
Packit 1470ea
        // Create the application window
Packit 1470ea
        this._window = new Gtk.ApplicationWindow({
Packit 1470ea
            application: this.application,
Packit 1470ea
            window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
            default_height: 120,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            title: "Button Clicker"});
Packit 1470ea
Packit 1470ea
        // Create a paned interface
Packit 1470ea
        this._panes = new Gtk.Paned ({
Packit 1470ea
            orientation: Gtk.Orientation.VERTICAL });
Packit 1470ea
]]>
Packit 1470ea
    

The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new <link href="GtkApplicationWindow.js.page">Gtk.ApplicationWindow</link> to put all our widgets into. The next step is to create a vertically-oriented Gtk.Paned interface, to divide the window up into two sections. This way the statusbar looks like those used in other applications, and it stays at the bottom of the window, even if the user resizes it.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="buttons">
Packit 1470ea
    <title>Creating the buttons</title>
Packit 1470ea
    
Packit 1470ea
        // Create the main button
Packit 1470ea
        this._clickMe = new Gtk.Button ({
Packit 1470ea
            label: "Click Me!" });
Packit 1470ea
        this._clickMe.connect ("clicked", Lang.bind (this, this._clicked));
Packit 1470ea
Packit 1470ea
        // Create the back button
Packit 1470ea
        this._backButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-go-back",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._backButton.connect ("clicked", Lang.bind (this, this._back));
Packit 1470ea
Packit 1470ea
        // Create the clear button
Packit 1470ea
        this._clearButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-clear",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._clearButton.connect ("clicked", Lang.bind (this, this._clear));
Packit 1470ea
]]>
Packit 1470ea
    

This code creates the three <link href="button.js.page">Gtk.Buttons</link> we'll use to push a new message to the statusbar, pop the last one off, and clear all existing messages. The "back" and "clear" buttons are <link href="https://developer.gnome.org/gtk3/3.4/gtk3-Stock-Items.html">stock buttons</link>, which are automatically translated into any language GNOME supports.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Put the buttons in a grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
        this._grid.attach (this._backButton, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clickMe, this._backButton, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clearButton, this._clickMe, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
Packit 1470ea
        // Put the grid in a large frame that fills most of the window
Packit 1470ea
        this._topFrame = new Gtk.Frame ({
Packit 1470ea
            border_width: 20,
Packit 1470ea
            height_request: 90,
Packit 1470ea
            width_request: 300});
Packit 1470ea
        this._topFrame.add (this._grid);
Packit 1470ea
]]>
Packit 1470ea
    

This code creates the <link href="grid.js.page">Gtk.Grid</link> that we'll use to organize the buttons, and attaches the buttons to it in order. It then creates a <link href="paned.js.page">Gtk.Frame</link> which will take up most of the window and has a large amount of padding around the buttons, and adds the Grid to the Frame. Note that we still need to put the Frame into the Paned interface, and then add it to the ApplicationWindow.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="statusbar">
Packit 1470ea
    <title>Creating the statusbar</title>
Packit 1470ea
    
Packit 1470ea
        // Create the statusbar
Packit 1470ea
        this._statusbar = new Gtk.Statusbar();
Packit 1470ea
Packit 1470ea
        // Keep track of the number of times the button has been clicked
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
        this.ContextID = this._statusbar.get_context_id ("Number of Clicks");
Packit 1470ea
Packit 1470ea
        // Give the statusbar an initial message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
        // Put the statusbar in its own frame at the bottom
Packit 1470ea
        this._barFrame = new Gtk.Frame ({
Packit 1470ea
            height_request: 30 });
Packit 1470ea
        this._barFrame.add (this._statusbar);
Packit 1470ea
]]>
Packit 1470ea
    

Here we create the Gtk.Statusbar, and push a message to it to start off with. Then we give it its own narrow frame at the bottom of the window.

Packit 1470ea
    

Every message needs to have a context id, which is an integer value you can get from the statusbar with the get_context_id() function. Its only parameter is the string value you use to describe that particular context id. Normally, you'll get a new context id for different kinds of messages, so that you can use the remove() function to remove a specific message and not just the most recent one on the stack. This is a simple example with only one kind of message, though, so we're just using one for everything.

Packit 1470ea
    

We use the push() function to push a new message onto the stack. Its first parameter is the context id, and its second is the message.

Packit 1470ea
    
Packit 1470ea
        // Assemble the frames into the paned interface
Packit 1470ea
        this._panes.pack1 (this._topFrame, true, false);
Packit 1470ea
        this._panes.pack2 (this._barFrame, false, false);
Packit 1470ea
Packit 1470ea
        // Put the panes into the window
Packit 1470ea
        this._window.add (this._panes);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

This code finishes up creating the window, by packing the frames into the pane, adding it to the window, and telling the window to show all child widgets.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="functions">
Packit 1470ea
    <title>Functions for interacting with the statusbar</title>
Packit 1470ea
    
Packit 1470ea
    _clicked: function() {
Packit 1470ea
Packit 1470ea
        // Increment the number of clicks by 1
Packit 1470ea
        this.Clicks++;
Packit 1470ea
Packit 1470ea
        // Update the statusbar with the new number of clicks
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
    _back: function () {
Packit 1470ea
Packit 1470ea
        // If there have been any clicks, decrement by 1 and remove last statusbar update
Packit 1470ea
        if (this.Clicks > 0 ) {
Packit 1470ea
            this.Clicks--;
Packit 1470ea
            this._statusbar.pop (this.ContextID);
Packit 1470ea
        };
Packit 1470ea
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
    _clear: function () {
Packit 1470ea
Packit 1470ea
        // Reset the number of clicks
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
Packit 1470ea
        // Wipe out all the messages pushed to the statusbar
Packit 1470ea
        this._statusbar.remove_all (this.ContextID);
Packit 1470ea
Packit 1470ea
        // Reset the statusbar's message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
]]>
Packit 1470ea
    

Here we have functions which demonstrate pushing a message onto the stack, popping the top one off of it, and clearing all messages of a particular context id. The pop() function just takes one parameter, which is the context id for the type of message you want to pop off the most recent one of. The remove_all() function works the same way, except it removes all messages of that type from the stack.

Packit 1470ea
    
Packit 1470ea
// Run the application
Packit 1470ea
let app = new StatusbarExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
]]>
Packit 1470ea
    

Finally, we create a new instance of the finished StatusbarExample class, and set the application running.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Complete code sample</title>
Packit 1470ea
<xi:include href="samples/statusbar.js" parse="text"><xi:fallback/></xi:include>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>In-depth documentation</title>
Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Button.html">Gtk.Button</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Frame.html">Gtk.Frame</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Paned.html">Gtk.Paned</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Statusbar.html">Gtk.Statusbar</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
  </section>
Packit 1470ea
</page>