Statusbar (JavaScript) Taryn Fox jewelfox@fursona.net 2012 Afficher les notifications dans une barre de statut donnée Luc Rebert, traduc@rebert.name 2011 Alain Lojewski, allomervan@gmail.com 2011-2012 Luc Pionchon pionchon.luc@gmail.com 2011 Bruno Brouard annoa.b@gmail.com 2011-12 Luis Menina liberforce@freeside.fr 2014 Barre de statut

Cette barre de statut enregistre le nombre de fois que vous avez cliqué sur un bouton. Des applications comme gedit utilisent une barre de statut pour rendre les informations visibles d'un coup d'œil sans interrompre l'utilisateur.

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.

Bibliothèques à importer

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.

Création de la fenêtre de l'application

Tout le code de cet exemple va dans la classe StatusbarExample. Le code ci-dessus crée une Gtk.Application pour nos éléments graphiques et la fenêtre qui les contient.

The _buildUI function is where we put all the code to create the application's user interface. The first step is creating a new Gtk.ApplicationWindow 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.

Création des boutons

This code creates the three Gtk.Buttons 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 stock buttons, which are automatically translated into any language GNOME supports.

Ce code génère la grille Gtk.Grid que nous utiliserons pour organiser les boutons et les mettre dans le bon ordre. Il génère ensuite un cadre Gtk.Frame 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.

Création de la barre de statut

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.

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.

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.

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.

Fonctions interagissant avec la barre de statut 0 ) { this.Clicks--; this._statusbar.pop (this.ContextID); }; }, _clear: function () { // Reset the number of clicks this.Clicks = 0; // Wipe out all the messages pushed to the statusbar this._statusbar.remove_all (this.ContextID); // Reset the statusbar's message this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks); } }); ]]>

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.

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

Exemple complet de code #!/usr/bin/gjs imports.gi.versions.Gtk = '3.0'; const Gio = imports.gi.Gio; const Gtk = imports.gi.Gtk; class StatusbarExample { // Create the application itself constructor() { this.application = new Gtk.Application({ application_id: 'org.example.jsstatusbar', flags: Gio.ApplicationFlags.FLAGS_NONE }); // Connect 'activate' and 'startup' signals to the callback functions this.application.connect('activate', this._onActivate.bind(this)); this.application.connect('startup', this._onStartup.bind(this)); } // Callback function for 'activate' signal presents window when active _onActivate() { this._window.present(); } // Callback function for 'startup' signal builds the UI _onStartup() { this._buildUI(); } // Build the application's UI _buildUI() { // Create the application window this._window = new Gtk.ApplicationWindow({ application: this.application, window_position: Gtk.WindowPosition.CENTER, default_height: 120, default_width: 300, title: "Button Clicker"}); // Create a paned interface this._panes = new Gtk.Paned ({ orientation: Gtk.Orientation.VERTICAL }); // Create the main button this._clickMe = new Gtk.Button ({ label: "Click Me!" }); this._clickMe.connect ("clicked", this._clicked.bind(this)); // Create the back button this._backButton = new Gtk.Button ({ label: "gtk-go-back", use_stock: true }); this._backButton.connect ("clicked", this._back.bind(this)); // Create the clear button this._clearButton = new Gtk.Button ({ label: "gtk-clear", use_stock: true }); this._clearButton.connect ("clicked", this._clear.bind(this)); // Put the buttons in a grid this._grid = new Gtk.Grid ({ halign: Gtk.Align.CENTER, valign: Gtk.Align.CENTER }); this._grid.attach (this._backButton, 0, 0, 1, 1); this._grid.attach_next_to (this._clickMe, this._backButton, Gtk.PositionType.RIGHT, 1, 1); this._grid.attach_next_to (this._clearButton, this._clickMe, Gtk.PositionType.RIGHT, 1, 1); // Put the grid in a large frame that fills most of the window this._topFrame = new Gtk.Frame ({ border_width: 20, height_request: 90, width_request: 300}); this._topFrame.add (this._grid); // Create the statusbar this._statusbar = new Gtk.Statusbar(); // Keep track of the number of times the button has been clicked this.Clicks = 0; this.ContextID = this._statusbar.get_context_id ("Number of Clicks"); // Give the statusbar an initial message this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks); // Put the statusbar in its own frame at the bottom this._barFrame = new Gtk.Frame ({ height_request: 30 }); this._barFrame.add (this._statusbar); // Assemble the frames into the paned interface this._panes.pack1 (this._topFrame, true, false); this._panes.pack2 (this._barFrame, false, false); // Put the panes into the window this._window.add (this._panes); // Show the window and all child widgets this._window.show_all(); } _clicked() { // Increment the number of clicks by 1 this.Clicks++; // Update the statusbar with the new number of clicks this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks); } _back() { // If there have been any clicks, decrement by 1 and remove last statusbar update if (this.Clicks > 0 ) { this.Clicks--; this._statusbar.pop (this.ContextID); }; } _clear() { // Reset the number of clicks this.Clicks = 0; // Wipe out all the messages pushed to the statusbar this._statusbar.remove_all (this.ContextID); // Reset the statusbar's message this._statusbar.push (this.ContextID, "Number of clicks: " + this.Clicks); } }; // Run the application let app = new StatusbarExample (); app.application.run (ARGV);
Documentation approfondie

Gtk.Application

Gtk.ApplicationWindow

Gtk.Button

Gtk.Frame

Gtk.Paned

Gtk.Statusbar