|
Packit |
1470ea |
#!/usr/bin/gjs
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
imports.gi.versions.Gtk = '3.0';
|
|
Packit |
1470ea |
const Gtk = imports.gi.Gtk;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// We start out with 0 cookies
|
|
Packit |
1470ea |
var cookies = 0;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
class GettingTheSignal {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application();
|
|
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: 200,
|
|
Packit |
1470ea |
default_width: 400,
|
|
Packit |
1470ea |
border_width: 20,
|
|
Packit |
1470ea |
title: "Spell 'cookie' to get a cookie!"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the text entry field
|
|
Packit |
1470ea |
this._spellCookie = new Gtk.Entry ();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the cookie button
|
|
Packit |
1470ea |
this._cookieButton = new Gtk.Button ({
|
|
Packit |
1470ea |
label: "Get a cookie" });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the cookie button to the function that handles clicking it
|
|
Packit |
1470ea |
this._cookieButton.connect ('clicked', this._getACookie.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the label
|
|
Packit |
1470ea |
this._cookieLabel = new Gtk.Label ({
|
|
Packit |
1470ea |
label: "Number of cookies: " + cookies });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid to arrange everything inside
|
|
Packit |
1470ea |
this._grid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
row_spacing: 20 });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Put everything inside the grid
|
|
Packit |
1470ea |
this._grid.attach (this._spellCookie, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._cookieButton, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._grid.attach (this._cookieLabel, 0, 2, 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 |
|
|
Packit |
1470ea |
_getACookie() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Did you spell "cookie" correctly?
|
|
Packit |
1470ea |
if ((this._spellCookie.get_text()).toLowerCase() == "cookie") {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Increase the number of cookies by 1 and update the label
|
|
Packit |
1470ea |
cookies++;
|
|
Packit |
1470ea |
this._cookieLabel.set_label ("Number of cookies: " + cookies);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new GettingTheSignal ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|