1<?xml version="1.0" encoding="utf-8"?> 2<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="gmenu.js" xml:lang="gl"> 3 <info> 4 <title type="text">GMenu (JavaScript)</title> 5 <link type="guide" xref="beginner.js#menu-combo-toolbar"/> 6 <revision version="0.1" date="2012-04-07" status="draft"/> 7 8 <credit type="author copyright"> 9 <name>Tiffany Antopolski</name> 10 <email its:translate="no">tiffany.antopolski@gmail.com</email> 11 <years>2012</years> 12 </credit> 13 14 <desc>A simple implementation of GMenuModel</desc> 15 16 <mal:credit xmlns:mal="http://projectmallard.org/1.0/" type="translator copyright"> 17 <mal:name>Fran Dieguez</mal:name> 18 <mal:email>frandieguez@gnome.org</mal:email> 19 <mal:years>2012-2013.</mal:years> 20 </mal:credit> 21 </info> 22 23 <title>GMenu</title> 24 <media type="image" mime="image/png" src="media/gmenu.js.png"/> 25 <p>A GtkApplication with a simple GMenu and SimpleActions</p> 26 27 <code mime="application/javascript" style="numbered">#!/usr/bin/gjs 28 29imports.gi.versions.Gtk = '3.0'; 30 31const Gio = imports.gi.Gio; 32const GLib = imports.gi.GLib; 33const Gtk = imports.gi.Gtk; 34 35class Application { 36 37 //create the application 38 constructor() { 39 this.application = new Gtk.Application ({ 40 application_id: 'org.example.myapp', 41 flags: Gio.ApplicationFlags.FLAGS_NONE 42 }); 43 44 //connect to 'activate' and 'startup' signals to the callback functions 45 this.application.connect('activate', this._onActivate.bind(this)); 46 this.application.connect('startup', this._onStartup.bind(this)); 47 } 48 49 //create the UI (in this case it's just the ApplicationWindow 50 _buildUI() { 51 this._window = new Gtk.ApplicationWindow ({ application: this.application, 52 window_position: Gtk.WindowPosition.CENTER, 53 title: "Welcome to GNOME" }); 54 55 //uncommenting the line below will change the window size 56 //this._window.set_default_size(600, 400); 57 58 //show the window and all child widgets (none in this case) 59 this._window.show_all(); 60 } 61 62 _showNew() { 63 print("This doesn't do anything. It is only a demonstration."); 64 } 65 66 _showAbout() { 67 print("No AboutDialog here. This is only a demonstration."); 68 } 69 70 //create the menu items and connect the signals to the callback functions. 71 _initMenus() { 72 let menu = new Gio.Menu(); 73 menu.append("New",'app.new'); 74 menu.append("About", 'app.about'); 75 menu.append("Quit",'app.quit'); 76 this.application.set_app_menu(menu); 77 78 let newAction = new Gio.SimpleAction ({ name: 'new' }); 79 newAction.connect('activate', () => { this._showNew(); }); 80 this.application.add_action(newAction); 81 82 let aboutAction = new Gio.SimpleAction ({ name: 'about' }); 83 aboutAction.connect('activate', () => { this._showAbout(); }); 84 this.application.add_action(aboutAction); 85 86 let quitAction = new Gio.SimpleAction ({ name: 'quit' }); 87 quitAction.connect('activate', () => { this._window.destroy(); }); 88 this.application.add_action(quitAction); 89 } 90 91 //callback function for 'activate' signal 92 _onActivate() { 93 this._window.present(); 94 } 95 96 //callback function for 'startup' signal 97 _onStartup() { 98 //You must call _initMenus() before calling _buildUI(). 99 this._initMenus(); 100 this._buildUI(); 101 } 102}; 103 104//run the application 105let app = new Application (); 106app.application.run (ARGV); 107</code> 108<p>Neste exemplo empregaremos o seguinte:</p> 109<list> 110 <item><p><link href="http://developer.gnome.org/gio/unstable/GMenu.html">GMenu</link></p></item> 111 <item><p><link href="http://developer.gnome.org/gio/stable/GSimpleAction.html">GSimpleAction</link></p></item> 112</list> 113</page> 114