Blame platform-demos/fr/statusbar.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="statusbar.js" xml:lang="fr">
Packit 1470ea
  <info>
Packit 1470ea
  <title type="text">Statusbar (JavaScript)</title>
Packit 1470ea
    <link type="guide" xref="beginner.js#display-widgets"/>
Packit 1470ea
    <revision version="0.1" date="2012-06-10" 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>Afficher les notifications dans une barre de statut donnée</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>Barre de statut</title>
Packit 1470ea
  <media type="image" mime="image/png" src="media/statusbar2.png"/>
Packit 1470ea
  

Cette barre de statut enregistre le nombre de fois que vous avez cliqué sur un bouton. Des applications comme <link href="http://projects.gnome.org/gedit/">gedit</link> utilisent une barre de statut pour rendre les informations visibles d'un coup d'œil sans interrompre l'utilisateur.

Packit 1470ea
  

Les nouveaux messages sont ajoutés au sommet de la pile d'une barre de statut et peuvent être affichés pour consulter les plus récents. Vous pouvez aussi effacer tous les messages d'un type spécifique d'un seul coup. Cet exemple d'application explique ces fonctions.

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 StatusbarExample = new Lang.Class({
Packit 1470ea
    Name: 'Statusbar 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.jsstatusbar',
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
    

Tout le code de cet exemple va dans la classe StatusbarExample. 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
            default_height: 120,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            title: "Button Clicker"});
Packit 1470ea
Packit 1470ea
        // Create a paned interface
Packit 1470ea
        this._panes = new Gtk.Paned ({
Packit 1470ea
            orientation: Gtk.Orientation.VERTICAL });
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 href="GtkApplicationWindow.js.page">Gtk.ApplicationWindow</link> to put all our widgets into. The next step is to create a vertically-oriented Gtk.Paned interface, to divide the window up into two sections. This way the statusbar looks like those used in other applications, and it stays at the bottom of the window, even if the user resizes it.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="buttons">
Packit 1470ea
    <title>Création des boutons</title>
Packit 1470ea
    
Packit 1470ea
        // Create the main button
Packit 1470ea
        this._clickMe = new Gtk.Button ({
Packit 1470ea
            label: "Click Me!" });
Packit 1470ea
        this._clickMe.connect ("clicked", Lang.bind (this, this._clicked));
Packit 1470ea
Packit 1470ea
        // Create the back button
Packit 1470ea
        this._backButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-go-back",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._backButton.connect ("clicked", Lang.bind (this, this._back));
Packit 1470ea
Packit 1470ea
        // Create the clear button
Packit 1470ea
        this._clearButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-clear",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._clearButton.connect ("clicked", Lang.bind (this, this._clear));
Packit 1470ea
]]>
Packit 1470ea
    

This code creates the three <link href="button.js.page">Gtk.Buttons</link> we'll use to push a new message to the statusbar, pop the last one off, and clear all existing messages. The "back" and "clear" buttons are <link href="https://developer.gnome.org/gtk3/3.4/gtk3-Stock-Items.html">stock buttons</link>, which are automatically translated into any language GNOME supports.

Packit 1470ea
Packit 1470ea
    
Packit 1470ea
        // Put the buttons in a grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
        this._grid.attach (this._backButton, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clickMe, this._backButton, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clearButton, this._clickMe, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
Packit 1470ea
        // Put the grid in a large frame that fills most of the window
Packit 1470ea
        this._topFrame = new Gtk.Frame ({
Packit 1470ea
            border_width: 20,
Packit 1470ea
            height_request: 90,
Packit 1470ea
            width_request: 300});
Packit 1470ea
        this._topFrame.add (this._grid);
Packit 1470ea
]]>
Packit 1470ea
    

Ce code génère la grille <link href="grid.js.page">Gtk.Grid</link> que nous utiliserons pour organiser les boutons et les mettre dans le bon ordre. Il génère ensuite un cadre <link href="paned.js.page">Gtk.Frame</link> qui occupe la majeure partie de la fenêtre et laisse beaucoup de marge autour des boutons, puis lui ajoute la grille. Il nous reste encore à placer le cadre dans le panneau interface et l'ajouter à l'ApplicationWindow.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="statusbar">
Packit 1470ea
    <title>Création de la barre de statut</title>
Packit 1470ea
    
Packit 1470ea
        // Create the statusbar
Packit 1470ea
        this._statusbar = new Gtk.Statusbar();
Packit 1470ea
Packit 1470ea
        // Keep track of the number of times the button has been clicked
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
        this.ContextID = this._statusbar.get_context_id ("Number of Clicks");
Packit 1470ea
Packit 1470ea
        // Give the statusbar an initial message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
        // Put the statusbar in its own frame at the bottom
Packit 1470ea
        this._barFrame = new Gtk.Frame ({
Packit 1470ea
            height_request: 30 });
Packit 1470ea
        this._barFrame.add (this._statusbar);
Packit 1470ea
]]>
Packit 1470ea
    

Ici, nous créons la barre de statuts Gtk.Statusbar et lui ajoutons un message pour commencer. Puis, nous lui attribuons son propre cadre étroit au bas de la fenêtre.

Packit 1470ea
    

Chaque message doit avoir un identifiant de contexte, une valeur entière qui peut vous être donnée par la fonction get_context_id(). Son seul paramètre est la valeur de la chaîne que vous utilisez pour décrire cet identifiant particulier. Normalement, il y a un identifiant différent selon le type de message, de façon à pouvoir utiliser la fonction remove() pour supprimer un message spécifique et non pas seulement le dernier au sommet de la pile. Voici un exemple simple avec un seul type de message, nous n'en utiliserons donc qu'un seul pour tout.

Packit 1470ea
    

La fonction push(), ajoute un nouveau message au-dessus de la pile. Son premier paramètre est l'identifiant de contexte, et le second, le message.

Packit 1470ea
    
Packit 1470ea
        // Assemble the frames into the paned interface
Packit 1470ea
        this._panes.pack1 (this._topFrame, true, false);
Packit 1470ea
        this._panes.pack2 (this._barFrame, false, false);
Packit 1470ea
Packit 1470ea
        // Put the panes into the window
Packit 1470ea
        this._window.add (this._panes);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    },
Packit 1470ea
]]>
Packit 1470ea
    

Ce code se termine par la création de la fenêtre, le placement du cadre dans le panneau et l'ordre donné à la fenêtre d'afficher tous ses éléments graphiques enfants.

Packit 1470ea
  </section>
Packit 1470ea
Packit 1470ea
  <section id="functions">
Packit 1470ea
    <title>Fonctions interagissant avec la barre de statut</title>
Packit 1470ea
    
Packit 1470ea
    _clicked: function() {
Packit 1470ea
Packit 1470ea
        // Increment the number of clicks by 1
Packit 1470ea
        this.Clicks++;
Packit 1470ea
Packit 1470ea
        // Update the statusbar with the new number of clicks
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
    _back: function () {
Packit 1470ea
Packit 1470ea
        // If there have been any clicks, decrement by 1 and remove last statusbar update
Packit 1470ea
        if (this.Clicks > 0 ) {
Packit 1470ea
            this.Clicks--;
Packit 1470ea
            this._statusbar.pop (this.ContextID);
Packit 1470ea
        };
Packit 1470ea
Packit 1470ea
    },
Packit 1470ea
Packit 1470ea
Packit 1470ea
Packit 1470ea
    _clear: function () {
Packit 1470ea
Packit 1470ea
        // Reset the number of clicks
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
Packit 1470ea
        // Wipe out all the messages pushed to the statusbar
Packit 1470ea
        this._statusbar.remove_all (this.ContextID);
Packit 1470ea
Packit 1470ea
        // Reset the statusbar's message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
});
Packit 1470ea
]]>
Packit 1470ea
    

Voici des fonctions qui montrent comment ajouter un message à la pile, comment afficher celui qui se trouve au sommet et comment effacer tous les messages avec un identifiant de contexte particulier. La fonction pop() ne prend qu'un argument, l'identifiant de contexte du type de message le plus récent à afficher. La fonction remove_all() se comporte de la même façon, sauf qu'elle supprime de la pile tous les messages de ce type.

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

Enfin, nous créons un nouvel exemple de la classe StatusbarExample terminée et nous 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 StatusbarExample {
Packit 1470ea
Packit 1470ea
    // Create the application itself
Packit 1470ea
    constructor() {
Packit 1470ea
        this.application = new Gtk.Application({
Packit 1470ea
            application_id: 'org.example.jsstatusbar',
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: 120,
Packit 1470ea
            default_width: 300,
Packit 1470ea
            title: "Button Clicker"});
Packit 1470ea
Packit 1470ea
        // Create a paned interface
Packit 1470ea
        this._panes = new Gtk.Paned ({
Packit 1470ea
            orientation: Gtk.Orientation.VERTICAL });
Packit 1470ea
Packit 1470ea
        // Create the main button
Packit 1470ea
        this._clickMe = new Gtk.Button ({
Packit 1470ea
            label: "Click Me!" });
Packit 1470ea
        this._clickMe.connect ("clicked", this._clicked.bind(this));
Packit 1470ea
Packit 1470ea
        // Create the back button
Packit 1470ea
        this._backButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-go-back",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._backButton.connect ("clicked", this._back.bind(this));
Packit 1470ea
Packit 1470ea
        // Create the clear button
Packit 1470ea
        this._clearButton = new Gtk.Button ({
Packit 1470ea
            label: "gtk-clear",
Packit 1470ea
            use_stock: true });
Packit 1470ea
        this._clearButton.connect ("clicked", this._clear.bind(this));
Packit 1470ea
Packit 1470ea
        // Put the buttons in a grid
Packit 1470ea
        this._grid = new Gtk.Grid ({
Packit 1470ea
            halign: Gtk.Align.CENTER,
Packit 1470ea
            valign: Gtk.Align.CENTER });
Packit 1470ea
        this._grid.attach (this._backButton, 0, 0, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clickMe, this._backButton, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
        this._grid.attach_next_to (this._clearButton, this._clickMe, Gtk.PositionType.RIGHT, 1, 1);
Packit 1470ea
Packit 1470ea
        // Put the grid in a large frame that fills most of the window
Packit 1470ea
        this._topFrame = new Gtk.Frame ({
Packit 1470ea
            border_width: 20,
Packit 1470ea
            height_request: 90,
Packit 1470ea
            width_request: 300});
Packit 1470ea
        this._topFrame.add (this._grid);
Packit 1470ea
Packit 1470ea
        // Create the statusbar
Packit 1470ea
        this._statusbar = new Gtk.Statusbar();
Packit 1470ea
Packit 1470ea
        // Keep track of the number of times the button has been clicked
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
        this.ContextID = this._statusbar.get_context_id ("Number of Clicks");
Packit 1470ea
Packit 1470ea
        // Give the statusbar an initial message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
Packit 1470ea
        // Put the statusbar in its own frame at the bottom
Packit 1470ea
        this._barFrame = new Gtk.Frame ({
Packit 1470ea
            height_request: 30 });
Packit 1470ea
        this._barFrame.add (this._statusbar);
Packit 1470ea
Packit 1470ea
        // Assemble the frames into the paned interface
Packit 1470ea
        this._panes.pack1 (this._topFrame, true, false);
Packit 1470ea
        this._panes.pack2 (this._barFrame, false, false);
Packit 1470ea
Packit 1470ea
        // Put the panes into the window
Packit 1470ea
        this._window.add (this._panes);
Packit 1470ea
Packit 1470ea
        // Show the window and all child widgets
Packit 1470ea
        this._window.show_all();
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _clicked() {
Packit 1470ea
Packit 1470ea
        // Increment the number of clicks by 1
Packit 1470ea
        this.Clicks++;
Packit 1470ea
Packit 1470ea
        // Update the statusbar with the new number of clicks
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _back() {
Packit 1470ea
Packit 1470ea
        // If there have been any clicks, decrement by 1 and remove last statusbar update
Packit 1470ea
        if (this.Clicks > 0 ) {
Packit 1470ea
            this.Clicks--;
Packit 1470ea
            this._statusbar.pop (this.ContextID);
Packit 1470ea
        };
Packit 1470ea
    }
Packit 1470ea
Packit 1470ea
    _clear() {
Packit 1470ea
Packit 1470ea
        // Reset the number of clicks
Packit 1470ea
        this.Clicks = 0;
Packit 1470ea
Packit 1470ea
        // Wipe out all the messages pushed to the statusbar
Packit 1470ea
        this._statusbar.remove_all (this.ContextID);
Packit 1470ea
Packit 1470ea
        // Reset the statusbar's message
Packit 1470ea
        this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks);
Packit 1470ea
    }
Packit 1470ea
};
Packit 1470ea
Packit 1470ea
// Run the application
Packit 1470ea
let app = new StatusbarExample ();
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.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.Button.html">Gtk.Button</link>

</item>
Packit 1470ea
  <item>

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

</item>
Packit 1470ea
  <item>

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

</item>
Packit 1470ea
  <item>

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

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