Blame platform-demos/C/samples/spinbutton.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 SpinButtonExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsspinbutton'
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
            border_width: 20,
Packit 1470ea
            title: "Kitten Feeder"});
Packit 1470ea
Packit 1470ea
        // Create the first spinbutton using a function
Packit 1470ea
        this._kittens = Gtk.SpinButton.new_with_range (1, 9001, 1);
Packit 1470ea
        this._kittens.connect ("value-changed", this._newValue.bind(this));
Packit 1470ea
Packit 1470ea
        // Create an adjustment to use for the second spinbutton
Packit 1470ea
        this._adjustment = new Gtk.Adjustment ({
Packit 1470ea
            value: 1,
Packit 1470ea
            lower: 0,
Packit 1470ea
            upper: 9001,
Packit 1470ea
            step_increment: 1,
Packit 1470ea
            page_increment: 10 });
Packit 1470ea
Packit 1470ea
        // Create the second spinbutton
Packit 1470ea
        this._tuna = new Gtk.SpinButton ({ adjustment: this._adjustment });
Packit 1470ea
        this._tuna.connect ("value-changed", this._newValue.bind(this));
Packit 1470ea
Packit 1470ea
        // this._tuna.set_digits (1);
Packit 1470ea
        // this._tuna.set_wrap (true);
Packit 1470ea
Packit 1470ea
        // Create the text labels to go with the spinbuttons
Packit 1470ea
        this._startLabel = new Gtk.Label ({ label: "There are " });
Packit 1470ea
        this._kittenLabel = new Gtk.Label ({ label: " kitten(s), and "});
Packit 1470ea
        this._tunaLabel = new Gtk.Label ({ label: " can(s) of tuna."});
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()));
Packit 1470ea
        this._lastLabel = new Gtk.Label ({
Packit 1470ea
            label: "That's " + this.perKitten + " can(s) of tuna per kitten." });
Packit 1470ea
Packit 1470ea
        // Create a grid to put the spinbuttons and their labels in
Packit 1470ea
        this._spinGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER,
Packit 1470ea
            margin_bottom: 20 });
Packit 1470ea
Packit 1470ea
        // Attach everything to the grid
Packit 1470ea
        this._spinGrid.attach (this._startLabel, 0, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittens, 1, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittenLabel, 2, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tuna, 3, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tunaLabel, 4, 0, 1, 1);
Packit 1470ea
Packit 1470ea
        // Create a main grid to hold it and the last label
Packit 1470ea
        this._mainGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
Packit 1470ea
        // Attach the smaller grid and the last label to the main grid
Packit 1470ea
        this._mainGrid.attach (this._spinGrid, 0, 0, 1, 1);
Packit 1470ea
        this._mainGrid.attach (this._lastLabel, 0, 1, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the main grid to the window
Packit 1470ea
        this._window.add (this._mainGrid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _newValue() {
Packit 1470ea
        // Update the label which shows how many cans there are per kitten
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()))
Packit 1470ea
        this._lastLabel.set_label ("That's " + this.perKitten + " can(s) of tuna per kitten.");
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new SpinButtonExample ();
Packit 1470ea
app.application.run (ARGV);