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

Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
imports.gi.versions.Gtk = '3.0';
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
class StatusbarExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
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', this._onActivate.bind(this));
Packit 1470ea
        this.application.connect('startup', this._onStartup.bind(this));
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup() {
Packit 1470ea
        this._buildUI();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI() {
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
        // Create the main button
Packit 1470ea
        this._clickMe = new Gtk.Button ({
Packit 1470ea
            label: "Click Me!" });
Packit 1470ea
        this._clickMe.connect ("clicked", this._clicked.bind(this));
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", this._back.bind(this));
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", this._clear.bind(this));
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
        // 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
        // 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
    _clicked() {
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
    _back() {
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
    _clear() {
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
// Run the application
Packit 1470ea
let app = new StatusbarExample ();
Packit 1470ea
app.application.run (ARGV);