|
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="switch.js" xml:lang="fr">
|
|
Packit |
1470ea |
<info>
|
|
Packit |
1470ea |
<title type="text">Switch (JavaScript)</title>
|
|
Packit |
1470ea |
<link type="guide" xref="beginner.js#buttons"/>
|
|
Packit |
1470ea |
<revision version="0.1" date="2012-06-18" 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 interrupteur à glissière qui peut être allumé ou éteint</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>Switch (interrupteur)</title>
|
|
Packit |
1470ea |
<media type="image" mime="image/png" src="media/switchanimals.png"/>
|
|
Packit |
1470ea |
A Switch has two positions, on and off. This example shows how you can use multiple switches together to control which <link xref="image.js">Image</link> is shown in the window. The pictures used in this example <link href="https://live.gnome.org/TarynFox?action=AttachFile&do=get&target=Animal+Photos.zip">can be downloaded here</link>.
|
|
Packit |
1470ea |
<note>Si les fichiers <file>redfox.png</file>, <file>muteswan.png</file>, <file>fruitbat.png</file> et <file>gentoopenguin.png</file> ne se trouvent pas dans le même dossier, la fenêtre affichera une icône « image brisée ». Vous pouvez modifier le code et les images comme bon vous semble, mais sachez que les photos sous licence Creative Commons utilisées dans cet exemple proviennent des sources suivantes et ont été rognées à 640x435 :
|
|
Packit |
1470ea |
<list>
|
|
Packit |
1470ea |
<item><link href="http://en.wikipedia.org/wiki/File:Fuzzy_Freddy.jpg">Red fox photo</link> (renard rouge) par Rob Lee, licence <link href="http://creativecommons.org/licenses/by/2.0/deed.en">CC-By</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://en.wikipedia.org/wiki/File:Pygoscelis_papua_-Nagasaki_Penguin_Aquarium_-swimming_underwater-8a.jpg">Gentoo penguinphoto</link> (pingouin) par Ken Funakoshi, licence <link href="http://creativecommons.org/licenses/by-sa/2.0/deed.en">CC-By-SA</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://www.flickr.com/photos/shekgraham/127431519/in/photostream/">Fruit bat photo</link> (chauve-souris) par Shek Graham, licence <link href="http://creativecommons.org/licenses/by/2.0/deed.en">CC-By</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://commons.wikimedia.org/wiki/File:Mute_Swan-Mindaugas_Urbonas.jpg">Mute swan photo</link> (cygne) par Mindaugas Urbonas, licence <link href="http://creativecommons.org/licenses/by-sa/2.5/deed.en">CC-By-SA</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
Photo credits and licensing information are shown in the application's <link xref="aboutdialog.js">AboutDialog</link>. Always remember to credit the original artist when using <link href="http://creativecommons.org">Creative Commons-licensed works!</link> </note>
|
|
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 SwitchExample = new Lang.Class({
|
|
Packit |
1470ea |
Name: 'Switch 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.jsswitch',
|
|
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 creates the menu and builds the UI
|
|
Packit |
1470ea |
_onStartup: function() {
|
|
Packit |
1470ea |
this._initMenus();
|
|
Packit |
1470ea |
this._buildUI ();
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
Tout le code de cet exemple est contenu dans la classe SwitchExample. 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 |
<note>Avant d'appeler _buildUI pour créer la fenêtre et les éléments graphiques qu'elle contient, il nous faut appeler _initMenus, qui indique à GNOME de générer le menu. Nous pouvons mettre le code actuel pour _initMenus après le code pour _buildUI, car l'ordre n'a pas d'importance du moment que _initMenus est appelé en premier dans _onStartup. </note>
|
|
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: "Animal Creator"});
|
|
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="button">
|
|
Packit |
1470ea |
<title>Création des interrupteurs</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the image widget and set its default picture
|
|
Packit |
1470ea |
this._image = new Gtk.Image ({file: "redfox.png"});
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Créons en premier l'<link xref="image.js">Image</link> que les interrupteurs vont contrôler. Souvenez-vous qu'un fichier nommé <file>redfox.png</file> doit être présent dans le même dossier que cette application.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the first switch
|
|
Packit |
1470ea |
this._flyLabel = new Gtk.Label ({
|
|
Packit |
1470ea |
label: "Make it fly",
|
|
Packit |
1470ea |
margin_right: 30});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the first switch and set its default position
|
|
Packit |
1470ea |
this._flySwitch = new Gtk.Switch ({active: false});
|
|
Packit |
1470ea |
this._flySwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the second switch
|
|
Packit |
1470ea |
this._birdLabel = new Gtk.Label ({
|
|
Packit |
1470ea |
label: "Make it a bird",
|
|
Packit |
1470ea |
margin_right: 30});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the second switch
|
|
Packit |
1470ea |
this._birdSwitch = new Gtk.Switch ({active: false});
|
|
Packit |
1470ea |
this._birdSwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
Utilisons un <link xref="label.js">Label</link> pour étiqueter chaque interrupteur et laissons-leur un peu de marge à droite pour qu'ils ne débordent pas les uns sur les autres. Créons ensuite les interrupteurs et paramétrons-les pour qu'ils soient en position « éteint » par défaut.
|
|
Packit |
1470ea |
Le signal qu'un interrupteur envoie quand il est basculé dans un sens ou dans l'autre est appelé notify::active. Après avoir créé chaque interrupteur, connectons son signal notify::active à une fonction appelée _switchFlip. Si vous avez plein d'interrupteurs qui font chacun quelque chose de différent, vous pouvez les connecter à différentes fonctions, mais ici, ils sont tous les deux utilisés pour la même chose : contrôler quelle image est affichée par _image.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid for the labels and switches beneath the picture
|
|
Packit |
1470ea |
this._UIGrid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
margin_top: 20});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the labels and switches to that grid
|
|
Packit |
1470ea |
this._UIGrid.attach (this._flyLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._flySwitch, 1, 0, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._birdLabel, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._birdSwitch, 1, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a master grid to put both the UI and the picture into
|
|
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 picture and the UI grid to the master grid
|
|
Packit |
1470ea |
this._mainGrid.attach (this._image, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._mainGrid.attach (this._UIGrid, 0, 1, 1, 1);
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
Créons une <link xref="grid.js">grille</link> pour les étiquettes et les interrupteurs de façon à les organiser dans un agencement 2x2 avec une marge entre eux et l'image. Ensuite, mettons cette grille dans une plus grande au format 2x1 qui contient l'image en haut et la grille avec les étiquettes et les interrupteurs en bas.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the master 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="switch-handler">
|
|
Packit |
1470ea |
<title>Fonction prenant en charge la commutation des interrupteurs</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_switchFlip: function() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Change the picture depending on which switches are flipped
|
|
Packit |
1470ea |
if (this._flySwitch.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._birdSwitch.get_active()) this._image.set_from_file ("muteswan.png");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else this._image.set_from_file ("fruitbat.png");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._birdSwitch.get_active()) this._image.set_from_file ("gentoopenguin.png");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
else this._image.set_from_file ("redfox.png");
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
À chaque fois qu'un interrupteur est activé, cette fonction vérifie lequel des deux interrupteurs est allumé à l'aide de la fonction intégrée get_active(). Elle affiche ensuite l'image correspondante. Vous pouvez modifier les noms des fichiers comme bon vous semble, tant que les images correspondantes sont dans le même dossier.
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
<section id="about">
|
|
Packit |
1470ea |
<title>Création de la boîte de dialogue « À propos »</title>
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_initMenus: function() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's menu so we can have an "About" button
|
|
Packit |
1470ea |
let menu = new Gio.Menu();
|
|
Packit |
1470ea |
menu.append("About", 'app.about');
|
|
Packit |
1470ea |
menu.append("Quit",'app.quit');
|
|
Packit |
1470ea |
this.application.set_app_menu(menu);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the "About" button to the _showAbout() function
|
|
Packit |
1470ea |
let aboutAction = new Gio.SimpleAction ({ name: 'about' });
|
|
Packit |
1470ea |
aboutAction.connect('activate', Lang.bind(this,
|
|
Packit |
1470ea |
function() {
|
|
Packit |
1470ea |
this._showAbout();
|
|
Packit |
1470ea |
}));
|
|
Packit |
1470ea |
this.application.add_action(aboutAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the "Quit" button to the function that closes the window
|
|
Packit |
1470ea |
let quitAction = new Gio.SimpleAction ({ name: 'quit' });
|
|
Packit |
1470ea |
quitAction.connect('activate', Lang.bind(this,
|
|
Packit |
1470ea |
function() {
|
|
Packit |
1470ea |
this._window.destroy();
|
|
Packit |
1470ea |
}));
|
|
Packit |
1470ea |
this.application.add_action(quitAction);
|
|
Packit |
1470ea |
},
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
La première étape est de créer le <link xref="gmenu.js">GMenu</link> dans lequel s'insère le bouton « À propos ». C'est ce menu qui s'affiche quand vous cliquez dans le coin gauche supérieur de l'écran, à côté du menu Activités. Notre menu ne contient que deux options : À propos et Quitter.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_showAbout: function () {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// String arrays of the names of the people involved in the project
|
|
Packit |
1470ea |
var artists = ['Rob Lee http://en.wikipedia.org/wiki/File:Fuzzy_Freddy.jpg', 'Ken Funakoshi http://en.wikipedia.org/wiki/File:Pygoscelis_papua_-Nagasaki_Penguin_Aquarium_-swimming_underwater-8a.jpg', 'Shek Graham http://www.flickr.com/photos/shekgraham/127431519/in/photostream/', 'Mindaugas Urbonas http://commons.wikimedia.org/wiki/File:Mute_Swan-Mindaugas_Urbonas.jpg'];
|
|
Packit |
1470ea |
var authors = ["GNOME Documentation Team"];
|
|
Packit |
1470ea |
var documenters = ["GNOME Documentation Team"];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the About dialog
|
|
Packit |
1470ea |
let aboutDialog = new Gtk.AboutDialog({
|
|
Packit |
1470ea |
title: "AboutDialog Example",
|
|
Packit |
1470ea |
program_name: "Animal Creator",
|
|
Packit |
1470ea |
copyright: "Copyright \xa9 2012 GNOME Documentation Team\n\nRed fox photo licensed CC-By by Rob Lee\nGentoo penguin photo licensed CC-By-SA by Ken Funakoshi\nFruit bat photo licensed CC-By by Shek Graham\nMute swan photo licensed CC-By-SA by Mindaugas Urbonas\nLinks to the originals are available under Credits.\n\nHave you hugged a penguin today?",
|
|
Packit |
1470ea |
artists: artists,
|
|
Packit |
1470ea |
authors: authors,
|
|
Packit |
1470ea |
documenters: documenters,
|
|
Packit |
1470ea |
website: "http://developer.gnome.org",
|
|
Packit |
1470ea |
website_label: "GNOME Developer Website" });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the About dialog to the window
|
|
Packit |
1470ea |
aboutDialog.modal = true;
|
|
Packit |
1470ea |
aboutDialog.transient_for = this._window;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the About dialog
|
|
Packit |
1470ea |
aboutDialog.show();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the Close button to the destroy signal for the dialog
|
|
Packit |
1470ea |
aboutDialog.connect('response', function() {
|
|
Packit |
1470ea |
aboutDialog.destroy();
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
Vous pouvez définir beaucoup de choses très différentes dans une boîte de dialogue <link xref="aboutdialog.js">À propos</link>, comme créditer tous les contributeurs ayant participé au développement de l'application et laisser une note à qui veut la lire. Dans notre cas, la section licence contient nos notes et les crédits aux photographes auteurs, alors que la section artistes affiche une liste des photographes avec les liens vers les images originales quand vous cliquez sur le bouton Crédits. Les liens URL qui suivent leurs noms les transforment en liens cliquables vous renvoyant à la section Crédits.
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new SwitchExample ();
|
|
Packit |
1470ea |
app.application.run (ARGV);
|
|
Packit |
1470ea |
]]>
|
|
Packit |
1470ea |
Enfin, créons une nouvelle instance de la classe SwitchExample 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 SwitchExample {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the application itself
|
|
Packit |
1470ea |
constructor() {
|
|
Packit |
1470ea |
this.application = new Gtk.Application({
|
|
Packit |
1470ea |
application_id: 'org.example.jsswitch'
|
|
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 creates the menu and builds the UI
|
|
Packit |
1470ea |
_onStartup() {
|
|
Packit |
1470ea |
this._initMenus();
|
|
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: "Animal Creator"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the image widget and set its default picture
|
|
Packit |
1470ea |
this._image = new Gtk.Image ({file: "redfox.png"});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the first switch
|
|
Packit |
1470ea |
this._flyLabel = new Gtk.Label ({
|
|
Packit |
1470ea |
label: "Make it fly",
|
|
Packit |
1470ea |
margin_right: 30});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the first switch and set its default position
|
|
Packit |
1470ea |
this._flySwitch = new Gtk.Switch ({active: false});
|
|
Packit |
1470ea |
this._flySwitch.connect ('notify::active', this._switchFlip.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a label for the second switch
|
|
Packit |
1470ea |
this._birdLabel = new Gtk.Label ({
|
|
Packit |
1470ea |
label: "Make it a bird",
|
|
Packit |
1470ea |
margin_right: 30});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the second switch
|
|
Packit |
1470ea |
this._birdSwitch = new Gtk.Switch ({active: false});
|
|
Packit |
1470ea |
this._birdSwitch.connect ('notify::active', this._switchFlip.bind(this));
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a grid for the labels and switches beneath the picture
|
|
Packit |
1470ea |
this._UIGrid = new Gtk.Grid ({
|
|
Packit |
1470ea |
halign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
valign: Gtk.Align.CENTER,
|
|
Packit |
1470ea |
margin_top: 20});
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the labels and switches to that grid
|
|
Packit |
1470ea |
this._UIGrid.attach (this._flyLabel, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._flySwitch, 1, 0, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._birdLabel, 0, 1, 1, 1);
|
|
Packit |
1470ea |
this._UIGrid.attach (this._birdSwitch, 1, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create a master grid to put both the UI and the picture into
|
|
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 picture and the UI grid to the master grid
|
|
Packit |
1470ea |
this._mainGrid.attach (this._image, 0, 0, 1, 1);
|
|
Packit |
1470ea |
this._mainGrid.attach (this._UIGrid, 0, 1, 1, 1);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Add the master 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 |
_switchFlip() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Change the picture depending on which switches are flipped
|
|
Packit |
1470ea |
if (this._flySwitch.get_active()) {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._birdSwitch.get_active())
|
|
Packit |
1470ea |
this._image.set_from_file ("muteswan.png");
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._image.set_from_file ("fruitbat.png");
|
|
Packit |
1470ea |
} else {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
if (this._birdSwitch.get_active())
|
|
Packit |
1470ea |
this._image.set_from_file ("gentoopenguin.png");
|
|
Packit |
1470ea |
else
|
|
Packit |
1470ea |
this._image.set_from_file ("redfox.png");
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_initMenus() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Build the application's menu so we can have an "About" button
|
|
Packit |
1470ea |
let menu = new Gio.Menu();
|
|
Packit |
1470ea |
menu.append("About", 'app.about');
|
|
Packit |
1470ea |
menu.append("Quit",'app.quit');
|
|
Packit |
1470ea |
this.application.set_app_menu(menu);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the "About" button to the _showAbout() function
|
|
Packit |
1470ea |
let aboutAction = new Gio.SimpleAction ({ name: 'about' });
|
|
Packit |
1470ea |
aboutAction.connect('activate', () => { this._showAbout(); });
|
|
Packit |
1470ea |
this.application.add_action(aboutAction);
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Bind the "Quit" button to the function that closes the window
|
|
Packit |
1470ea |
let quitAction = new Gio.SimpleAction ({ name: 'quit' });
|
|
Packit |
1470ea |
quitAction.connect('activate', () => { this._window.destroy(); });
|
|
Packit |
1470ea |
this.application.add_action(quitAction);
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
_showAbout() {
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// String arrays of the names of the people involved in the project
|
|
Packit |
1470ea |
var artists = ['Rob Lee http://en.wikipedia.org/wiki/File:Fuzzy_Freddy.png', 'Ken Funakoshi http://en.wikipedia.org/wiki/File:Pygoscelis_papua_-Nagasaki_Penguin_Aquarium_-swimming_underwater-8a.png', 'Shek Graham http://www.flickr.com/photos/shekgraham/127431519/in/photostream/', 'Mindaugas Urbonas http://commons.wikimedia.org/wiki/File:Mute_Swan-Mindaugas_Urbonas.png'];
|
|
Packit |
1470ea |
var authors = ["GNOME Documentation Team"];
|
|
Packit |
1470ea |
var documenters = ["GNOME Documentation Team"];
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Create the About dialog
|
|
Packit |
1470ea |
let aboutDialog = new Gtk.AboutDialog({
|
|
Packit |
1470ea |
title: "AboutDialog Example",
|
|
Packit |
1470ea |
program_name: "Animal Creator",
|
|
Packit |
1470ea |
copyright: "Copyright \xa9 2012 GNOME Documentation Team\n\nRed fox photo licensed CC-By by Rob Lee\nGentoo penguin photo licensed CC-By-SA by Ken Funakoshi\nFruit bat photo licensed CC-By by Shek Graham\nMute swan photo licensed CC-By-SA by Mindaugas Urbonas\nLinks to the originals are available under Credits.\n\nHave you hugged a penguin today?",
|
|
Packit |
1470ea |
artists: artists,
|
|
Packit |
1470ea |
authors: authors,
|
|
Packit |
1470ea |
documenters: documenters,
|
|
Packit |
1470ea |
website: "http://developer.gnome.org",
|
|
Packit |
1470ea |
website_label: "GNOME Developer Website" });
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Attach the About dialog to the window
|
|
Packit |
1470ea |
aboutDialog.modal = true;
|
|
Packit |
1470ea |
aboutDialog.transient_for = this._window;
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Show the About dialog
|
|
Packit |
1470ea |
aboutDialog.show();
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Connect the Close button to the destroy signal for the dialog
|
|
Packit |
1470ea |
aboutDialog.connect('response', function() {
|
|
Packit |
1470ea |
aboutDialog.destroy();
|
|
Packit |
1470ea |
});
|
|
Packit |
1470ea |
}
|
|
Packit |
1470ea |
};
|
|
Packit |
1470ea |
|
|
Packit |
1470ea |
// Run the application
|
|
Packit |
1470ea |
let app = new SwitchExample ();
|
|
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://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link> </item>
|
|
Packit |
1470ea |
<item><link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</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.Image.html">Gtk.Image</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.Switch.html">Gtk.Switch</link> </item>
|
|
Packit |
1470ea |
</list>
|
|
Packit |
1470ea |
</section>
|
|
Packit |
1470ea |
</page>
|