2011-05-10 16:29:52 +00:00
|
|
|
#!/usr/bin/env gjs
|
|
|
|
|
|
|
|
const Gdk = imports.gi.Gdk;
|
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const GLib = imports.gi.GLib;
|
|
|
|
const Gtk = imports.gi.Gtk;
|
|
|
|
|
|
|
|
function do_action(action, parameter) {
|
|
|
|
print ("Action '" + action.name + "' invoked");
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_action_param(action, parameter) {
|
|
|
|
print ("Action '" + action.name + "' invoked with parameter " + parameter.print(true));
|
|
|
|
}
|
|
|
|
|
2011-05-15 16:55:23 +00:00
|
|
|
function do_action_toggle(action) {
|
|
|
|
action.set_state(GLib.Variant.new('b', !action.state.deep_unpack()));
|
|
|
|
print ("Toggled");
|
|
|
|
}
|
|
|
|
|
2011-05-10 16:29:52 +00:00
|
|
|
function do_action_state_change(action) {
|
2011-12-08 20:53:03 +00:00
|
|
|
print ("Action '" + action.name + "' has now state " + action.state.print(true));
|
2011-05-10 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
Gtk.init(null, null);
|
2012-04-06 18:38:27 +00:00
|
|
|
Gdk.set_program_class('test-gjsgapp');
|
2011-05-10 16:29:52 +00:00
|
|
|
|
|
|
|
let app = new Gtk.Application({ application_id: 'org.gnome.Shell.GtkApplicationTest' });
|
|
|
|
app.connect('activate', function() {
|
|
|
|
print ("Activated");
|
|
|
|
});
|
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'one' });
|
2011-05-10 16:29:52 +00:00
|
|
|
action.connect('activate', do_action);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'two' });
|
2011-05-10 16:29:52 +00:00
|
|
|
action.connect('activate', do_action);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'toggle', state: GLib.Variant.new('b', false) });
|
2011-05-15 16:55:23 +00:00
|
|
|
action.connect('activate', do_action_toggle);
|
2011-05-10 16:29:52 +00:00
|
|
|
action.connect('notify::state', do_action_state_change);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'disable', enabled: false });
|
2011-05-10 16:29:52 +00:00
|
|
|
action.set_enabled(false);
|
|
|
|
action.connect('activate', do_action);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'parameter-int', parameter_type: GLib.VariantType.new('u') });
|
2011-05-10 16:29:52 +00:00
|
|
|
action.connect('activate', do_action_param);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let action = new Gio.SimpleAction({ name: 'parameter-string', parameter_type: GLib.VariantType.new('s') });
|
2011-05-10 16:29:52 +00:00
|
|
|
action.connect('activate', do_action_param);
|
2011-12-02 21:15:14 +00:00
|
|
|
app.add_action(action);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
|
|
|
let menu = new Gio.Menu();
|
2011-12-08 20:53:03 +00:00
|
|
|
menu.append('An action', 'app.one');
|
2011-05-10 16:29:52 +00:00
|
|
|
|
|
|
|
let section = new Gio.Menu();
|
2011-12-08 20:53:03 +00:00
|
|
|
section.append('Another action', 'app.two');
|
|
|
|
section.append('Same as above', 'app.two');
|
2011-05-10 16:29:52 +00:00
|
|
|
menu.append_section(null, section);
|
|
|
|
|
|
|
|
// another section, to check separators
|
|
|
|
section = new Gio.Menu();
|
2011-12-08 20:53:03 +00:00
|
|
|
section.append('Checkbox', 'app.toggle');
|
|
|
|
section.append('Disabled', 'app.disable');
|
2011-12-21 22:11:40 +00:00
|
|
|
menu.append_section('Subsection', section);
|
2011-05-10 16:29:52 +00:00
|
|
|
|
|
|
|
// empty sections or submenus should be invisible
|
|
|
|
menu.append_section('Empty section', new Gio.Menu());
|
|
|
|
menu.append_submenu('Empty submenu', new Gio.Menu());
|
|
|
|
|
|
|
|
let submenu = new Gio.Menu();
|
2011-12-08 20:53:03 +00:00
|
|
|
submenu.append('Open c:\\', 'app.parameter-string::c:\\');
|
|
|
|
submenu.append('Open /home', 'app.parameter-string::/home');
|
2011-05-10 16:29:52 +00:00
|
|
|
menu.append_submenu('Recent files', submenu);
|
|
|
|
|
|
|
|
let item = Gio.MenuItem.new('Say 42', null);
|
2011-12-08 20:53:03 +00:00
|
|
|
item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 42));
|
2011-05-10 16:29:52 +00:00
|
|
|
menu.append_item(item);
|
|
|
|
|
|
|
|
let item = Gio.MenuItem.new('Say 43', null);
|
2011-12-08 20:53:03 +00:00
|
|
|
item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 43));
|
2011-05-10 16:29:52 +00:00
|
|
|
menu.append_item(item);
|
|
|
|
|
2011-12-02 21:15:14 +00:00
|
|
|
let window = null;
|
2011-05-10 16:29:52 +00:00
|
|
|
|
2011-12-08 20:53:03 +00:00
|
|
|
app.connect_after('startup', function(app) {
|
2012-04-06 18:38:27 +00:00
|
|
|
app.set_app_menu(menu);
|
|
|
|
|
2011-12-08 20:53:03 +00:00
|
|
|
window = new Gtk.ApplicationWindow({ title: "Test Application", application: app });
|
|
|
|
});
|
2011-12-02 21:15:14 +00:00
|
|
|
app.connect('activate', function(app) {
|
2011-05-10 16:29:52 +00:00
|
|
|
window.present();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.run(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|