1<?xml version="1.0" encoding="utf-8"?>
2<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="spinner.js" xml:lang="de">
3  <info>
4  <title type="text">Spinner (JavaScript)</title>
5    <link type="guide" xref="beginner.js#display-widgets"/>
6    <revision version="0.1" date="2012-06-03" status="draft"/>
7
8    <credit type="author copyright">
9      <name>Taryn Fox</name>
10      <email its:translate="no">jewelfox@fursona.net</email>
11      <years>2012</years>
12    </credit>
13
14    <desc>A spinner animation</desc>
15
16    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
17      <mal:name>Mario Blättermann</mal:name>
18      <mal:email>mario.blaettermann@gmail.com</mal:email>
19      <mal:years>2011, 2013, 2016, 2018, 2021</mal:years>
20    </mal:credit>
21  </info>
22
23  <title>Spinner</title>
24  <media type="image" mime="image/png" src="media/spinner.png"/>
25  <p>This Spinner is stopped and started by pressing the spacebar.</p>
26
27<code mime="application/javascript" style="numbered">#!/usr/bin/gjs
28
29imports.gi.versions.Gdk = '3.0';
30imports.gi.versions.Gtk = '3.0';
31
32const Gio = imports.gi.Gio;
33const GLib = imports.gi.GLib;
34const Gtk = imports.gi.Gtk;
35const Gdk = imports.gi.Gdk;
36
37class SpinnerExample {
38
39    // Create the application itself
40    constructor() {
41        this.application = new Gtk.Application({
42            application_id: 'org.example.jsspinner',
43            flags: Gio.ApplicationFlags.FLAGS_NONE
44        });
45
46        // Connect 'activate' and 'startup' signals to the callback functions
47        this.application.connect('activate', this._onActivate.bind(this));
48        this.application.connect('startup', this._onStartup.bind(this));
49    }
50
51    // Callback function for 'activate' signal presents windows when active
52    _onActivate() {
53        this._window.present();
54    }
55
56    // Callback function for 'startup' signal builds the UI
57    _onStartup() {
58        this._buildUI();
59    }
60
61    // Build the application's UI
62    _buildUI() {
63
64        // Create the application window
65        this._window = new Gtk.ApplicationWindow({
66            application: this.application,
67            window_position: Gtk.WindowPosition.CENTER,
68            title: "Spinner Example",
69            default_height: 200,
70            default_width: 200,
71            border_width: 30
72        });
73
74        // Create a spinner which starts spinning automatically
75        this._spinner = new Gtk.Spinner ({active: true});
76        this._window.add (this._spinner);
77
78        // Connect a keypress event to the function that makes it start or stop spinning
79        this._window.connect("key-press-event", this._onKeyPress.bind(this));
80
81        // Show the window and all child widgets
82        this._window.show_all();
83    }
84
85    _onKeyPress(widget, event) {
86
87        // Get the value of the key that was pressed
88        let [, keyval] = event.get_keyval();
89
90        // If it was the spacebar, toggle the spinner to start or stop
91        if (keyval == Gdk.KEY_space) {
92            if (this._spinner.active == true)
93                this._spinner.stop();
94            else
95                this._spinner.start();
96        }
97    }
98};
99
100// Run the application
101let app = new SpinnerExample ();
102app.application.run (ARGV);
103</code>
104<p>In diesem Beispiel haben wir Folgendes verwendet:</p>
105<list>
106  <item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gdk.html">Gdk - Key Values</link></p></item>
107  <item><p><link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link></p></item>
108  <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link></p></item>
109    <item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Spinner.html">Gtk.Spinner</link></p></item>
110</list>
111</page>
112