Blame platform-demos/fr/spinbutton.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="spinbutton.js" xml:lang="fr">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">SpinButton (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#entry"/>
Packit 1470ea
    <link type="seealso" xref="GtkApplicationWindow.js"/>
Packit 1470ea
    <link type="seealso" xref="grid.js"/>
Packit 1470ea
    <link type="seealso" xref="label.js"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-24" 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>Un champ de saisie de nombres avec des boutons + et -</desc>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Rebert,</mal:name>
Packit 1470ea
      <mal:email>traduc@rebert.name</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Alain Lojewski,</mal:name>
Packit 1470ea
      <mal:email>allomervan@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-2012</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luc Pionchon</mal:name>
Packit 1470ea
      <mal:email>pionchon.luc@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Bruno Brouard</mal:name>
Packit 1470ea
      <mal:email>annoa.b@gmail.com</mal:email>
Packit 1470ea
      <mal:years>2011-12</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  
Packit 1470ea
    <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
Packit 1470ea
      <mal:name>Luis Menina</mal:name>
Packit 1470ea
      <mal:email>liberforce@freeside.fr</mal:email>
Packit 1470ea
      <mal:years>2014</mal:years>
Packit 1470ea
    </mal:credit>
Packit 1470ea
  </info>
Packit 1470ea
Packit 1470ea
  <title>SpinButton</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/spinbuttonkittens.png"/>
Packit 1470ea
  

A SpinButton is not related to a <link xref="spinner.js">Spinner</link>. It's a text entry field which only accepts numbers, and which has plus and minus buttons to let you change the value without having to type anything in.

Packit 1470ea
  

Son utilisation est optimale quand il apparaît évident que seul un nombre peut y être saisi. Dans cet exemple, nous utilisons deux BoutonDactivités pour définir le nombre de chatons et le nombre de boîtes de thon à leur donner.

Packit 1470ea
    <links type="section"/>
Packit 1470ea
Packit 1470ea
  <section id="imports">
Packit 1470ea
    <title>Bibliothèques à importer</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
    

Ce sont les bibliothèques que nous devons importer pour faire fonctionner cette application. N'oubliez pas que la ligne qui informe GNOME que nous allons utiliser Gjs doit toujours se trouver au début.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
  <section id="applicationwindow">
Packit 1470ea
    <title>Création de la fenêtre de l'application</title>
Packit 1470ea
    
Packit 1470ea
const SpinButtonExample = new Lang.Class({
Packit 1470ea
    Name: 'SpinButton 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.jsspinbutton'
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
    

Tout le code de cet exemple va dans la classe SpinButtonExample. Le code ci-dessus crée une <link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link> pour nos éléments graphiques et la fenêtre qui les contient.

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
            border_width: 20,
Packit 1470ea
            title: "Kitten Feeder"});
Packit 1470ea
]]>
Packit 1470ea
    

La fonction _buildUI est l'endroit où nous mettons tout le code nécessaire à la création de l'interface utilisateur de l'application. La première étape consiste à créer une <link xref="GtkApplicationWindow.js">Gtk.ApplicationWindow</link> pour y mettre tous nos éléments graphiques.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="spinbutton">
Packit 1470ea
    <title>Création des BoutonDactivités</title>
Packit 1470ea
    
Packit 1470ea
        // Create the first spinbutton using a function
Packit 1470ea
        this._kittens = Gtk.SpinButton.new_with_range (1, 9001, 1);
Packit 1470ea
        this._kittens.connect ("value-changed", Lang.bind (this, this._newValue));
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

Utilisons la fonction new_with_range pour créer rapidement un nouveau BoutonDactivité. Son premier paramètre est la valeur de départ du BoutonDactivité, le second est la valeur maximale et le troisième est la valeur de l'incrément lorsque les boutons plus ou moins sont pressés.

Packit 1470ea
    

Après avoir généré le premier BoutonDactivité, nous connectons son signal value-changed à une fonction qui gère ce qui se passe quand le nombre à l'intérieur d'un des deux BoutonDactivités change.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create an adjustment to use for the second spinbutton
Packit 1470ea
        this._adjustment = new Gtk.Adjustment ({
Packit 1470ea
            value: 1,
Packit 1470ea
            lower: 0,
Packit 1470ea
            upper: 9001,
Packit 1470ea
            step_increment: 1,
Packit 1470ea
            page_increment: 10 });
Packit 1470ea
Packit 1470ea
        // Create the second spinbutton
Packit 1470ea
        this._tuna = new Gtk.SpinButton ({ adjustment: this._adjustment });
Packit 1470ea
        this._tuna.connect ("value-changed", Lang.bind (this, this._newValue));
Packit 1470ea
Packit 1470ea
        // this._tuna.set_digits (1);
Packit 1470ea
        // this._tuna.set_wrap (true);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

If you want more fine-grained control over a SpinButton, or want to create a bunch of SpinButtons which all use the same parameters, you can create an object called an <link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Adjustment.html">Adjustment</link>. Then you can use that object as a new SpinButton's adjustment property, and it sets all the values at once. Afterwards, you can change all the SpinButtons which use that adjustment by changing the Adjustment object's properties.

Packit 1470ea
    

Ici, les lignes placées en commentaires vous montrent des moyens de personnaliser votre BoutonDactivité. Vous pouvez, par exemple, définir le nombre de décimales après la virgule, ou lui indiquer qu'il doit aller à la ligne s'il dépasse les limites supérieures ou inférieures que vous avez définies.

Packit 1470ea
    <note>

Dans l'intérêt des chatons, il est préférable de ne pas autoriser set_digits à paramétrer un nombre décimal de chatons.

</note>
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="UI">
Packit 1470ea
    <title>Création du reste de l'interface utilisateur</title>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create the text labels to go with the spinbuttons
Packit 1470ea
        this._startLabel = new Gtk.Label ({ label: "There are " });
Packit 1470ea
        this._kittenLabel = new Gtk.Label ({ label: " kitten(s), and "});
Packit 1470ea
        this._tunaLabel = new Gtk.Label ({ label: " can(s) of tuna."});
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()));
Packit 1470ea
        this._lastLabel = new Gtk.Label ({
Packit 1470ea
            label: "That's " + this.perKitten + " can(s) of tuna per kitten." });
Packit 1470ea
]]>
Packit 1470ea
    

Créons chaque <link xref="label.js">étiquette</link> individuellement et attachons-les ensemble avec les BoutonDactivités. La dernière étiquette doit afficher le nombre de boîtes de thon par chaton, elle comprend donc une variable au milieu qui correspond à une équation qui utilise les fonctions get_value des BoutonDactivités pour trouver à quelles valeurs ils sont définis. On utilise une méthode mathématique de base en JavaScript pour arrondir le nombre de boîtes de thon par chaton à l'entier le plus proche.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Create a grid to put the spinbuttons and their labels in
Packit 1470ea
        this._spinGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER,
Packit 1470ea
            margin_bottom: 20 });
Packit 1470ea
Packit 1470ea
        // Attach everything to the grid
Packit 1470ea
        this._spinGrid.attach (this._startLabel, 0, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittens, 1, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittenLabel, 2, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tuna, 3, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tunaLabel, 4, 0, 1, 1);
Packit 1470ea
Packit 1470ea
        // Create a main grid to hold it and the last label
Packit 1470ea
        this._mainGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
Packit 1470ea
        // Attach the smaller grid and the last label to the main grid
Packit 1470ea
        this._mainGrid.attach (this._spinGrid, 0, 0, 1, 1);
Packit 1470ea
        this._mainGrid.attach (this._lastLabel, 0, 1, 1, 1);
Packit 1470ea
]]>
Packit 1470ea
Packit 1470ea
    

Ici, nous utilisons des éléments graphiques <link xref="grid.js">grille</link> pour tout ranger. Une grille maintient les étiquettes et les BoutonDactivités dans l'ordre pendant que l'autre affiche la première grille en haut et la dernière étiquette en bas.

Packit 1470ea
    

Il n'y a pas de mauvaise façon de ranger les éléments dans les grilles, à partir du moment où cela vous convient. Dans notre cas, la grille du haut comporte une marge en bas pour la séparer de l'étiquette qui se trouve au-dessous et qui est dans une grille indépendante centrée par rapport aux étiquettes et BoutonDactivités du haut.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Add the main grid to the window
Packit 1470ea
        this._window.add (this._mainGrid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

Enfin, ajoutons la grille la plus grande à la fenêtre et indiquons à la fenêtre de s'afficher avec tous les éléments graphiques à l'intérieur.

Packit 1470ea
    </section>
Packit 1470ea
Packit 1470ea
    <section id="spinbutton-handler">
Packit 1470ea
    <title>Fonction prenant en charge les valeurs numériques des BoutonDactivités en cours de réglage</title>
Packit 1470ea
Packit 1470ea
    
Packit 1470ea
    _newValue: function () {
Packit 1470ea
Packit 1470ea
        // Update the label which shows how many cans there are per kitten
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()))
Packit 1470ea
        this._lastLabel.set_label ("That's " + this.perKitten + " can(s) of tuna per kitten.");
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
]]>
Packit 1470ea
    

Ici, nous mettons à jour la variable parchaton en fonction des nouvelles valeurs des BoutonDactivités et utilisons les propriétés de set_label pour actualiser ce qu'affiche _lastLabel. Comme les deux BoutonDactivités ont leur signal value-changed connecté à cette fonction, celle-ci met à jour l'étiquette à chaque fois qu'un des nombres change.

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

Enfin, nous créons un nouvel exemple de la classe SpinButtonExample et démarrons l'application.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="complete">
Packit 1470ea
    <title>Exemple complet de code</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 SpinButtonExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsspinbutton'
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
            border_width: 20,
Packit 1470ea
            title: "Kitten Feeder"});
Packit 1470ea
Packit 1470ea
        // Create the first spinbutton using a function
Packit 1470ea
        this._kittens = Gtk.SpinButton.new_with_range (1, 9001, 1);
Packit 1470ea
        this._kittens.connect ("value-changed", this._newValue.bind(this));
Packit 1470ea
Packit 1470ea
        // Create an adjustment to use for the second spinbutton
Packit 1470ea
        this._adjustment = new Gtk.Adjustment ({
Packit 1470ea
            value: 1,
Packit 1470ea
            lower: 0,
Packit 1470ea
            upper: 9001,
Packit 1470ea
            step_increment: 1,
Packit 1470ea
            page_increment: 10 });
Packit 1470ea
Packit 1470ea
        // Create the second spinbutton
Packit 1470ea
        this._tuna = new Gtk.SpinButton ({ adjustment: this._adjustment });
Packit 1470ea
        this._tuna.connect ("value-changed", this._newValue.bind(this));
Packit 1470ea
Packit 1470ea
        // this._tuna.set_digits (1);
Packit 1470ea
        // this._tuna.set_wrap (true);
Packit 1470ea
Packit 1470ea
        // Create the text labels to go with the spinbuttons
Packit 1470ea
        this._startLabel = new Gtk.Label ({ label: "There are " });
Packit 1470ea
        this._kittenLabel = new Gtk.Label ({ label: " kitten(s), and "});
Packit 1470ea
        this._tunaLabel = new Gtk.Label ({ label: " can(s) of tuna."});
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()));
Packit 1470ea
        this._lastLabel = new Gtk.Label ({
Packit 1470ea
            label: "That's " + this.perKitten + " can(s) of tuna per kitten." });
Packit 1470ea
Packit 1470ea
        // Create a grid to put the spinbuttons and their labels in
Packit 1470ea
        this._spinGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER,
Packit 1470ea
            margin_bottom: 20 });
Packit 1470ea
Packit 1470ea
        // Attach everything to the grid
Packit 1470ea
        this._spinGrid.attach (this._startLabel, 0, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittens, 1, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._kittenLabel, 2, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tuna, 3, 0, 1, 1);
Packit 1470ea
        this._spinGrid.attach (this._tunaLabel, 4, 0, 1, 1);
Packit 1470ea
Packit 1470ea
        // Create a main grid to hold it and the last label
Packit 1470ea
        this._mainGrid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
Packit 1470ea
        // Attach the smaller grid and the last label to the main grid
Packit 1470ea
        this._mainGrid.attach (this._spinGrid, 0, 0, 1, 1);
Packit 1470ea
        this._mainGrid.attach (this._lastLabel, 0, 1, 1, 1);
Packit 1470ea
Packit 1470ea
        // Add the main grid to the window
Packit 1470ea
        this._window.add (this._mainGrid);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _newValue() {
Packit 1470ea
        // Update the label which shows how many cans there are per kitten
Packit 1470ea
        this.perKitten = Math.floor((this._tuna.get_value() / this._kittens.get_value()))
Packit 1470ea
        this._lastLabel.set_label ("That's " + this.perKitten + " can(s) of tuna per kitten.");
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new SpinButtonExample ();
Packit 1470ea
app.application.run (ARGV);
Packit 1470ea
Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="in-depth">
Packit 1470ea
    <title>Documentation approfondie</title>
Packit 1470ea
<list>
Packit 1470ea
  <item>

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

</item>
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.Label.html">Gtk.Label</link>

</item>
Packit 1470ea
  <item>

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

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