Blame platform-demos/C/samples/grid.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 GLib = imports.gi.GLib;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
Packit 1470ea
class GridExample {
Packit 1470ea
Packit 1470ea
    /* Create the application itself
Packit 1470ea
       This boilerplate code is needed to build any GTK+ application. */
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application ({
Packit 1470ea
            application_id: 'org.example.jsgrid',
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 windows when active
Packit 1470ea
    _onActivate() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal initializes menus and 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  ({ application: this.application,
Packit 1470ea
                                                         window_position: Gtk.WindowPosition.CENTER,
Packit 1470ea
                                                         title: "Grid Example"});
Packit 1470ea
Packit 1470ea
        // Create the grid
Packit 1470ea
        this.Grid = new Gtk.Grid ();
Packit 1470ea
Packit 1470ea
        // Create the widgets inside the grid
Packit 1470ea
        this.progressBar = new Gtk.ProgressBar ();
Packit 1470ea
        this.Button = new Gtk.Button ({ label: "Button" });
Packit 1470ea
        this.Button.connect ("clicked", this._clickHandler.bind(this));
Packit 1470ea
Packit 1470ea
        // Assemble the grid
Packit 1470ea
        this._window.add (this.Grid);
Packit 1470ea
        this.Grid.attach (this.Button, 1, 1, 1, 1);
Packit 1470ea
        this.Grid.attach_next_to (this.progressBar, this.Button, Gtk.PositionType.BOTTOM, 1, 1);
Packit 1470ea
Packit 1470ea
                // Show the window and all child widgets
Packit 1470ea
                this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    // Here's the function that says what happens when the button is clicked
Packit 1470ea
    _clickHandler() {
Packit 1470ea
        this.progressBar.pulse ();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new GridExample ();
Packit 1470ea
app.application.run (ARGV);