/usr/share/help/fr/gnome-devel-demos/switch.js.page is in gnome-devel-docs 3.18.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | <?xml version="1.0" encoding="utf-8"?>
<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">
<info>
<title type="text">Switch (JavaScript)</title>
<link type="guide" xref="beginner.js#buttons"/>
<revision version="0.1" date="2012-06-18" status="draft"/>
<credit type="author copyright">
<name>Taryn Fox</name>
<email its:translate="no">jewelfox@fursona.net</email>
<years>2012</years>
</credit>
<desc>Un interrupteur à glissière qui peut être allumé ou éteint</desc>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Luc Rebert,</mal:name>
<mal:email>traduc@rebert.name</mal:email>
<mal:years>2011</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Alain Lojewski,</mal:name>
<mal:email>allomervan@gmail.com</mal:email>
<mal:years>2011-2012</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Luc Pionchon</mal:name>
<mal:email>pionchon.luc@gmail.com</mal:email>
<mal:years>2011</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Bruno Brouard</mal:name>
<mal:email>annoa.b@gmail.com</mal:email>
<mal:years>2011-12</mal:years>
</mal:credit>
<mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright">
<mal:name>Luis Menina</mal:name>
<mal:email>liberforce@freeside.fr</mal:email>
<mal:years>2014</mal:years>
</mal:credit>
</info>
<title>Switch (interrupteur)</title>
<media type="image" mime="image/png" src="media/switchanimals.png"/>
<p>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>.</p>
<note><p>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 :</p>
<list>
<item><p><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></p></item>
<item><p><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></p></item>
<item><p><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></p></item>
<item><p><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></p></item>
</list>
<p>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></p></note>
<links type="section"/>
<section id="imports">
<title>Bibliothèques à importer</title>
<code mime="application/javascript"><![CDATA[
#!/usr/bin/gjs
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
]]></code>
<p>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.</p>
</section>
<section id="applicationwindow">
<title>Création de la fenêtre de l'application</title>
<code mime="application/javascript"><![CDATA[
const SwitchExample = new Lang.Class({
Name: 'Switch Example',
// Create the application itself
_init: function() {
this.application = new Gtk.Application({
application_id: 'org.example.jsswitch',
flags: Gio.ApplicationFlags.FLAGS_NONE
});
// Connect 'activate' and 'startup' signals to the callback functions
this.application.connect('activate', Lang.bind(this, this._onActivate));
this.application.connect('startup', Lang.bind(this, this._onStartup));
},
// Callback function for 'activate' signal presents window when active
_onActivate: function() {
this._window.present();
},
// Callback function for 'startup' signal creates the menu and builds the UI
_onStartup: function() {
this._initMenus();
this._buildUI ();
},
]]></code>
<p>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.</p>
<note><p>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.</p></note>
<code mime="application/javascript"><![CDATA[
// Build the application's UI
_buildUI: function() {
// Create the application window
this._window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
border_width: 20,
title: "Animal Creator"});
]]></code>
<p>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.</p>
</section>
<section id="button">
<title>Création des interrupteurs</title>
<code mime="application/javascript"><![CDATA[
// Create the image widget and set its default picture
this._image = new Gtk.Image ({file: "redfox.png"});
]]></code>
<p>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.</p>
<code mime="application/javascript"><![CDATA[
// Create a label for the first switch
this._flyLabel = new Gtk.Label ({
label: "Make it fly",
margin_right: 30});
// Create the first switch and set its default position
this._flySwitch = new Gtk.Switch ({active: false});
this._flySwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
// Create a label for the second switch
this._birdLabel = new Gtk.Label ({
label: "Make it a bird",
margin_right: 30});
// Create the second switch
this._birdSwitch = new Gtk.Switch ({active: false});
this._birdSwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
]]></code>
<p>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.</p>
<p>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.</p>
<code mime="application/javascript"><![CDATA[
// Create a grid for the labels and switches beneath the picture
this._UIGrid = new Gtk.Grid ({
halign: Gtk.Align.CENTER,
valign: Gtk.Align.CENTER,
margin_top: 20});
// Attach the labels and switches to that grid
this._UIGrid.attach (this._flyLabel, 0, 0, 1, 1);
this._UIGrid.attach (this._flySwitch, 1, 0, 1, 1);
this._UIGrid.attach (this._birdLabel, 0, 1, 1, 1);
this._UIGrid.attach (this._birdSwitch, 1, 1, 1, 1);
// Create a master grid to put both the UI and the picture into
this._mainGrid = new Gtk.Grid ({
halign: Gtk.Align.CENTER,
valign: Gtk.Align.CENTER });
// Attach the picture and the UI grid to the master grid
this._mainGrid.attach (this._image, 0, 0, 1, 1);
this._mainGrid.attach (this._UIGrid, 0, 1, 1, 1);
]]></code>
<p>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.</p>
<code mime="application/javascript"><![CDATA[
// Add the master grid to the window
this._window.add (this._mainGrid);
// Show the window and all child widgets
this._window.show_all();
},
]]></code>
<p>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.</p>
</section>
<section id="switch-handler">
<title>Fonction prenant en charge la commutation des interrupteurs</title>
<code mime="application/javascript"><![CDATA[
_switchFlip: function() {
// Change the picture depending on which switches are flipped
if (this._flySwitch.get_active()) {
if (this._birdSwitch.get_active()) this._image.set_from_file ("muteswan.png");
else this._image.set_from_file ("fruitbat.png");
}
else {
if (this._birdSwitch.get_active()) this._image.set_from_file ("gentoopenguin.png");
else this._image.set_from_file ("redfox.png");
}
},
]]></code>
<p>À 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.</p>
</section>
<section id="about">
<title>Création de la boîte de dialogue « À propos »</title>
<code mime="application/javascript"><![CDATA[
_initMenus: function() {
// Build the application's menu so we can have an "About" button
let menu = new Gio.Menu();
menu.append("About", 'app.about');
menu.append("Quit",'app.quit');
this.application.set_app_menu(menu);
// Bind the "About" button to the _showAbout() function
let aboutAction = new Gio.SimpleAction ({ name: 'about' });
aboutAction.connect('activate', Lang.bind(this,
function() {
this._showAbout();
}));
this.application.add_action(aboutAction);
// Bind the "Quit" button to the function that closes the window
let quitAction = new Gio.SimpleAction ({ name: 'quit' });
quitAction.connect('activate', Lang.bind(this,
function() {
this._window.destroy();
}));
this.application.add_action(quitAction);
},
]]></code>
<p>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.</p>
<code mime="application/javascript"><![CDATA[
_showAbout: function () {
// String arrays of the names of the people involved in the project
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'];
var authors = ["GNOME Documentation Team"];
var documenters = ["GNOME Documentation Team"];
// Create the About dialog
let aboutDialog = new Gtk.AboutDialog({
title: "AboutDialog Example",
program_name: "Animal Creator",
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?",
artists: artists,
authors: authors,
documenters: documenters,
website: "http://developer.gnome.org",
website_label: "GNOME Developer Website" });
// Attach the About dialog to the window
aboutDialog.modal = true;
aboutDialog.transient_for = this._window;
// Show the About dialog
aboutDialog.show();
// Connect the Close button to the destroy signal for the dialog
aboutDialog.connect('response', function() {
aboutDialog.destroy();
});
}
});
]]></code>
<p>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.</p>
<code mime="application/javascript"><![CDATA[
// Run the application
let app = new SwitchExample ();
app.application.run (ARGV);
]]></code>
<p>Enfin, créons une nouvelle instance de la classe SwitchExample et démarrons l'application.</p>
</section>
<section id="complete">
<title>Exemple complet de code</title>
<code mime="application/javascript" style="numbered">#!/usr/bin/gjs
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const Lang = imports.lang;
const SwitchExample = new Lang.Class({
Name: 'Switch Example',
// Create the application itself
_init: function() {
this.application = new Gtk.Application({
application_id: 'org.example.jsswitch'
});
// Connect 'activate' and 'startup' signals to the callback functions
this.application.connect('activate', Lang.bind(this, this._onActivate));
this.application.connect('startup', Lang.bind(this, this._onStartup));
},
// Callback function for 'activate' signal presents window when active
_onActivate: function() {
this._window.present();
},
// Callback function for 'startup' signal creates the menu and builds the UI
_onStartup: function() {
this._initMenus();
this._buildUI ();
},
// Build the application's UI
_buildUI: function() {
// Create the application window
this._window = new Gtk.ApplicationWindow({
application: this.application,
window_position: Gtk.WindowPosition.CENTER,
border_width: 20,
title: "Animal Creator"});
// Create the image widget and set its default picture
this._image = new Gtk.Image ({file: "redfox.png"});
// Create a label for the first switch
this._flyLabel = new Gtk.Label ({
label: "Make it fly",
margin_right: 30});
// Create the first switch and set its default position
this._flySwitch = new Gtk.Switch ({active: false});
this._flySwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
// Create a label for the second switch
this._birdLabel = new Gtk.Label ({
label: "Make it a bird",
margin_right: 30});
// Create the second switch
this._birdSwitch = new Gtk.Switch ({active: false});
this._birdSwitch.connect ('notify::active', Lang.bind (this, this._switchFlip));
// Create a grid for the labels and switches beneath the picture
this._UIGrid = new Gtk.Grid ({
halign: Gtk.Align.CENTER,
valign: Gtk.Align.CENTER,
margin_top: 20});
// Attach the labels and switches to that grid
this._UIGrid.attach (this._flyLabel, 0, 0, 1, 1);
this._UIGrid.attach (this._flySwitch, 1, 0, 1, 1);
this._UIGrid.attach (this._birdLabel, 0, 1, 1, 1);
this._UIGrid.attach (this._birdSwitch, 1, 1, 1, 1);
// Create a master grid to put both the UI and the picture into
this._mainGrid = new Gtk.Grid ({
halign: Gtk.Align.CENTER,
valign: Gtk.Align.CENTER });
// Attach the picture and the UI grid to the master grid
this._mainGrid.attach (this._image, 0, 0, 1, 1);
this._mainGrid.attach (this._UIGrid, 0, 1, 1, 1);
// Add the master grid to the window
this._window.add (this._mainGrid);
// Show the window and all child widgets
this._window.show_all();
},
_switchFlip: function() {
// Change the picture depending on which switches are flipped
if (this._flySwitch.get_active()) {
if (this._birdSwitch.get_active()) this._image.set_from_file ("muteswan.png");
else this._image.set_from_file ("fruitbat.png");
}
else {
if (this._birdSwitch.get_active()) this._image.set_from_file ("gentoopenguin.png");
else this._image.set_from_file ("redfox.png");
}
},
_initMenus: function() {
// Build the application's menu so we can have an "About" button
let menu = new Gio.Menu();
menu.append("About", 'app.about');
menu.append("Quit",'app.quit');
this.application.set_app_menu(menu);
// Bind the "About" button to the _showAbout() function
let aboutAction = new Gio.SimpleAction ({ name: 'about' });
aboutAction.connect('activate', Lang.bind(this,
function() {
this._showAbout();
}));
this.application.add_action(aboutAction);
// Bind the "Quit" button to the function that closes the window
let quitAction = new Gio.SimpleAction ({ name: 'quit' });
quitAction.connect('activate', Lang.bind(this,
function() {
this._window.destroy();
}));
this.application.add_action(quitAction);
},
_showAbout: function () {
// String arrays of the names of the people involved in the project
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'];
var authors = ["GNOME Documentation Team"];
var documenters = ["GNOME Documentation Team"];
// Create the About dialog
let aboutDialog = new Gtk.AboutDialog({
title: "AboutDialog Example",
program_name: "Animal Creator",
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?",
artists: artists,
authors: authors,
documenters: documenters,
website: "http://developer.gnome.org",
website_label: "GNOME Developer Website" });
// Attach the About dialog to the window
aboutDialog.modal = true;
aboutDialog.transient_for = this._window;
// Show the About dialog
aboutDialog.show();
// Connect the Close button to the destroy signal for the dialog
aboutDialog.connect('response', function() {
aboutDialog.destroy();
});
}
});
// Run the application
let app = new SwitchExample ();
app.application.run (ARGV);
</code>
</section>
<section id="in-depth">
<title>Documentation approfondie</title>
<list>
<item><p><link href="http://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link></p></item>
<item><p><link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link></p></item>
<item><p><link href="http://www.roojs.com/seed/gir-1.2-gtk-3.0/gjs/Gtk.Application.html">Gtk.Application</link></p></item>
<item><p><link href="http://developer.gnome.org/gtk3/stable/GtkApplicationWindow.html">Gtk.ApplicationWindow</link></p></item>
<item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Grid.html">Gtk.Grid</link></p></item>
<item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Image.html">Gtk.Image</link></p></item>
<item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Label.html">Gtk.Label</link></p></item>
<item><p><link href="http://www.roojs.org/seed/gir-1.2-gtk-3.0/gjs/Gtk.Switch.html">Gtk.Switch</link></p></item>
</list>
</section>
</page>
|