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="sv">
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>En animerad väntesnurra</desc>
15
16    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
17      <mal:name>Sebastian Rasmussen</mal:name>
18      <mal:email>sebras@gmail.com</mal:email>
19      <mal:years>2019</mal:years>
20    </mal:credit>
21
22    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
23      <mal:name>Anders Jonsson</mal:name>
24      <mal:email>anders.jonsson@norsjovallen.se</mal:email>
25      <mal:years>2021</mal:years>
26    </mal:credit>
27  </info>
28
29  <title>Spinner</title>
30  <media type="image" mime="image/png" src="media/spinner.png"/>
31  <p>Denna Spinner stoppas och startas genom tryck på blankstegstangenten.</p>
32
33<code mime="application/javascript" style="numbered">#!/usr/bin/gjs
34
35imports.gi.versions.Gdk = '3.0';
36imports.gi.versions.Gtk = '3.0';
37
38const Gio = imports.gi.Gio;
39const GLib = imports.gi.GLib;
40const Gtk = imports.gi.Gtk;
41const Gdk = imports.gi.Gdk;
42
43class SpinnerExample {
44
45    // Create the application itself
46    constructor() {
47        this.application = new Gtk.Application({
48            application_id: 'org.example.jsspinner',
49            flags: Gio.ApplicationFlags.FLAGS_NONE
50        });
51
52        // Connect 'activate' and 'startup' signals to the callback functions
53        this.application.connect('activate', this._onActivate.bind(this));
54        this.application.connect('startup', this._onStartup.bind(this));
55    }
56
57    // Callback function for 'activate' signal presents windows when active
58    _onActivate() {
59        this._window.present();
60    }
61
62    // Callback function for 'startup' signal builds the UI
63    _onStartup() {
64        this._buildUI();
65    }
66
67    // Build the application's UI
68    _buildUI() {
69
70        // Create the application window
71        this._window = new Gtk.ApplicationWindow({
72            application: this.application,
73            window_position: Gtk.WindowPosition.CENTER,
74            title: "Spinner Example",
75            default_height: 200,
76            default_width: 200,
77            border_width: 30
78        });
79
80        // Create a spinner which starts spinning automatically
81        this._spinner = new Gtk.Spinner ({active: true});
82        this._window.add (this._spinner);
83
84        // Connect a keypress event to the function that makes it start or stop spinning
85        this._window.connect("key-press-event", this._onKeyPress.bind(this));
86
87        // Show the window and all child widgets
88        this._window.show_all();
89    }
90
91    _onKeyPress(widget, event) {
92
93        // Get the value of the key that was pressed
94        let [, keyval] = event.get_keyval();
95
96        // If it was the spacebar, toggle the spinner to start or stop
97        if (keyval == Gdk.KEY_space) {
98            if (this._spinner.active == true)
99                this._spinner.stop();
100            else
101                this._spinner.start();
102        }
103    }
104};
105
106// Run the application
107let app = new SpinnerExample ();
108app.application.run (ARGV);
109</code>
110<p>I detta exempel använde vi följande:</p>
111<list>
112  <item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gdk.html">Gdk - Tangentvärden</link></p></item>
113  <item><p><link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link></p></item>
114  <item><p><link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link></p></item>
115    <item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Spinner.html">Gtk.Spinner</link></p></item>
116</list>
117</page>
118