Blame platform-demos/de/togglebutton.js.page

Packit 1470ea
Packit 1470ea
<page xmlns="http://projectmallard.org/1.0/" xmlns:its="http://www.w3.org/2005/11/its" xmlns:xi="http://www.w3.org/2001/XInclude" type="guide" style="task" id="togglebutton.js" xml:lang="de">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">ToggleButton (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#buttons"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-16" status="draft"/>
Packit 1470ea
Packit 1470ea
    <credit type="author copyright">
Packit 1470ea
      <name>Taryn Fox</name>
Packit 1470ea
      <email its:translate="no">jewelfox@fursona.net</email>
Packit 1470ea
      <years>2012</years>
Packit 1470ea
    </credit>
Packit 1470ea
Packit 1470ea
    <desc>Stays pressed until you click it again</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Mario Blättermann</mal:name>
Packit 1470ea
      <mal:email>mario.blaettermann@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011, 2013</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>ToggleButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/togglebutton.png"/>
Packit 1470ea
  

A ToggleButton is like a normal <link xref="button.js">Button</link>, except that it stays pressed in when you click it. You can use it like an on/off switch, to control things like the <link xref="spinner.js">Spinner</link> in this example.

Packit 1470ea
  

A ToggleButton's get_active method returns true if it's pressed in, and false if it's not. Its set_active method is used if you want to change its state without needing to click on it. When it changes state from pressed in to popped out and vice-versa, it sends out the "toggled" signal, which you can connect to a function to do something.

Packit 1470ea
    <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Zu importierende Bibliotheken</title>
Packit 1470ea
    
Packit 1470ea
#!/usr/bin/gjs
Packit 1470ea
Packit 1470ea
const Gio = imports.gi.Gio;
Packit 1470ea
const Gtk = imports.gi.Gtk;
Packit 1470ea
const Lang = imports.lang;
Packit 1470ea
Packit 1470ea
    

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.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Entwurf des Anwendungsfensters</title>
Packit 1470ea
    
Packit 1470ea
const ToggleButtonExample = new Lang.Class({
Packit 1470ea
    Name: 'ToggleButton Example',
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    _init: function() {
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', Lang.bind(this, this._onActivate));
Packit 1470ea
    this.application.connect('startup', Lang.bind(this, this._onStartup));
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'activate' signal presents window when active
Packit 1470ea
    _onActivate: function() {
Packit 1470ea
        this._window.present();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    // Callback function for 'startup' signal builds the UI
Packit 1470ea
    _onStartup: function() {
Packit 1470ea
        this._buildUI ();
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
    

All the code for this sample goes in the RadioButtonExample class. The above code creates a <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> for our widgets and window to go in.

Packit 1470ea
    
Packit 1470ea
    // Build the application's UI
Packit 1470ea
    _buildUI: function() {
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
    

The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> to put all our widgets into.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="togglebutton">
Packit 1470ea
    <title>Creating the ToggleButton and other widgets</title>
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
Packit 1470ea
    

We want this <link xref="spinner.js">Spinner</link> to expand vertically and horizontally, to take up as much space as possible inside the window.

Packit 1470ea
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', Lang.bind (this, this._onToggle));
Packit 1470ea
Packit 1470ea
Packit 1470ea
    

Creating a ToggleButton is a lot like creating a normal <link xref="button.js">Button</link>. The biggest difference is that you're handling a "toggled" signal instead of a "clicked" signal. This code binds the _onToggle function to that signal, so that it's called whenever our ToggleButton is toggled.

Packit 1470ea
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
    

Here we create a simple <link xref="grid.js">Grid</link> to organize everything in, then attach the Spinner and ToggleButton to it.

Packit 1470ea
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
    

Now we add the Grid to the Window, and tell the Window to show itself and its child widgets when the application is started.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    <section id="toggled">
Packit 1470ea
    <title>Making something happen when the ToggleButton is toggled</title>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
    _onToggle: function() {
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
Packit 1470ea
    

Whenever someone toggles the button, this function checks what its state is afterwards using get_active and starts or stops the spinner accordingly. We want it to spin only while the button is pressed in, so if get_active returns true we start the spinner. Otherwise, we tell it to stop.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
// Run the application
Packit 1470ea
let app = new ToggleButtonExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
    

Finally, we create a new instance of the finished RadioButtonExample class, and set the application running.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Complete code sample</title>
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);
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>WeiterfĂĽhrende Dokumentation</title>
Packit 1470ea
<list>
Packit 1470ea
  <item>

<link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link>

</item>
Packit 1470ea
  <item>

<link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Grid.html">Gtk.Grid</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Spinner.html">Gtk.Spinner</link>

</item>
Packit 1470ea
  <item>

<link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.ToggleButton.html">Gtk.ToggleButton</link>

</item>
Packit 1470ea
</list>
Packit 1470ea
  </section>
Packit 1470ea
</page>