Blame platform-demos/C/samples/togglebutton.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 ToggleButtonExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jstogglebutton',
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: 300,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            border_width: 30,
Packit 1470ea
            title: "ToggleButton Example"});
Packit 1470ea
Packit 1470ea
        // Create the spinner that the button stops and starts
Packit 1470ea
        this._spinner = new Gtk.Spinner ({hexpand: true, vexpand: true});
Packit 1470ea
Packit 1470ea
        // Create the togglebutton that starts and stops the spinner
Packit 1470ea
        this._toggleButton = new Gtk.ToggleButton ({label: "Start/Stop"});
Packit 1470ea
        this._toggleButton.connect ('toggled', this._onToggle.bind(this));
Packit 1470ea
Packit 1470ea
        // Create a grid and put everything in it
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            row_homogeneous: false,
Packit 1470ea
            row_spacing: 15});
Packit 1470ea
        this._grid.attach (this._spinner, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach (this._toggleButton, 0, 1, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the grid to the window
Packit 1470ea
        this._window.add (this._grid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _onToggle() {
Packit 1470ea
Packit 1470ea
        // Start or stop the spinner
Packit 1470ea
        if (this._toggleButton.get_active ())
Packit 1470ea
            this._spinner.start ();
Packit 1470ea
        else this._spinner.stop ();
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new ToggleButtonExample ();
Packit 1470ea
app.application.run (ARGV);