A Scale is a horizontal or vertical slider, that represents a value inside a numerical range. When you create a new Scale, you set what its default position is, what the numbers at the top and bottom of the range are, and things like how much it moves up or down when you click on the Scale to either side of the knob. To keep from having to type all that in every time you create a new Scale, you can create an object called an Adjustment which keeps track of all that, then tell each new Scale to use that Adjustment.
This scale is a simple widget that lets you adjust the size of an iceberg that penguins live on. The number of penguins on the iceberg is the product of the values of the two sliders. Try playing with them and seeing what happens.
These are the libraries we need to import for this application to run. Remember that the line which tells GNOME that we're using Gjs always needs to go at the start.
All the code for this sample goes in the ScaleExample class. The above code creates a Gtk.Application for our widgets and window to go in.
The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new Gtk.ApplicationWindow to put all our widgets into.
9000) {
comment = "It's over 9000!";
}
else if (product < 1000 && product > 0) {
comment = "They're getting lonely.";
}
else if (product == 0) {
comment = "They're all gone ...";
}
else comment = "";
// Set ._label's new text
this._label.set_label (String (product) + " penguins on the iceberg. " + comment);
}
});
]]>
Remember, we can get a Scale's value using its get_value method. Here we simply recalculate what the product of the two values is after one of the Scales is moved, add in a silly message depending on how many penguins are left, and change the wording on _label to show the new number and message.
Finally, we create a new instance of the finished ScaleExample class, and set the application running.
Gtk.Adjustment
Gtk.Application
Gtk.ApplicationWindow
Gtk.Grid
Gtk.Label
Gtk.Scale