Scale (JavaScript) Taryn Fox jewelfox@fursona.net 2012 A slider which corresponds to a numerical value Scale

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.

Libraries to import

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.

Creating the application window

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.

Creating the scales

The new_with_range method is one way to create a new Scale widget. The parameters it takes are a Gtk.Orientation, the minimum value, the maximum value, and the increment for a single step. After that we use the Scale's methods to set its starting value, and how many decimal places it runs to. We also set its vertical alignment in this case, to control where it appears in the window.

We can use the set_draw_value method to tell it whether or not to show the number next to the sliding scale. It's commented out in this example.

An Adjustment is an object we can use to simplify things when creating a new Scale. The Adjustment's "value" property is what the Scale's default value is, while "upper" and "lower" make the high and low ends of the numerical range. Meanwhile, the increment values show how much the slider moves when you do things like click on it.

Here we create a new Scale object using _adjustment as its "adjustment" property. This is a great shortcut. We still have to tell it to round off the decimal place, though. Note that the draw_value property is commented out; this is how you tell it not to show the number next to the Scale when you're creating one this way.

We can use the get_value method to find out the numerical value a Scale is set at. We can then do whatever we want with it, including multiply the two Scales' values together and have a Label show us the product. We set the label's text to wrap around, because we're having it display a silly message too.

After we create the Label, we connect the two Scales' "value-changed" signals to _recalc, a function that will recalculate the number of penguins on the iceberg and come up with a new message.

Here we create a Grid to put everything in, then attach all our widgets to it. Note that here and on some of the widgets themselves we're using margins to keep things neatly spaced.

Finally, we add the Grid to the window, then tell the window to show itself and all the widgets inside of it.

Function which handles the scales' values changing 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.

Complete code sample
In-depth documentation

Gtk.Adjustment

Gtk.Application

Gtk.ApplicationWindow

Gtk.Grid

Gtk.Label

Gtk.Scale