2011-05-10 12:29:52 -04:00
|
|
|
#!/usr/bin/env gjs
|
2012-03-30 10:44:36 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-05-10 12:29:52 -04:00
|
|
|
|
|
|
|
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 12:55:23 -04:00
|
|
|
function do_action_toggle(action) {
|
|
|
|
action.set_state(GLib.Variant.new('b', !action.state.deep_unpack()));
|
|
|
|
print ("Toggled");
|
|
|
|
}
|
|
|
|
|
2011-05-10 12:29:52 -04:00
|
|
|
function do_action_state_change(action) {
|
2011-12-08 15:53:03 -05:00
|
|
|
print ("Action '" + action.name + "' has now state " + action.state.print(true));
|
2011-05-10 12:29:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
2014-04-22 18:37:22 -04:00
|
|
|
Gtk.init(null);
|
2012-04-06 14:38:27 -04:00
|
|
|
Gdk.set_program_class('test-gjsgapp');
|
2011-05-10 12:29:52 -04:00
|
|
|
|
|
|
|
let app = new Gtk.Application({ application_id: 'org.gnome.Shell.GtkApplicationTest' });
|
|
|
|
app.connect('activate', function() {
|
2012-03-30 10:44:36 -04:00
|
|
|
print ("Activated");
|
2011-05-10 12:29:52 -04:00
|
|
|
});
|
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'one' });
|
2011-05-10 12:29:52 -04:00
|
|
|
action.connect('activate', do_action);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'two' });
|
2011-05-10 12:29:52 -04:00
|
|
|
action.connect('activate', do_action);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'toggle', state: GLib.Variant.new('b', false) });
|
2011-05-15 12:55:23 -04:00
|
|
|
action.connect('activate', do_action_toggle);
|
2011-05-10 12:29:52 -04:00
|
|
|
action.connect('notify::state', do_action_state_change);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'disable', enabled: false });
|
2011-05-10 12:29:52 -04:00
|
|
|
action.set_enabled(false);
|
|
|
|
action.connect('activate', do_action);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'parameter-int', parameter_type: GLib.VariantType.new('u') });
|
2011-05-10 12:29:52 -04:00
|
|
|
action.connect('activate', do_action_param);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let action = new Gio.SimpleAction({ name: 'parameter-string', parameter_type: GLib.VariantType.new('s') });
|
2011-05-10 12:29:52 -04:00
|
|
|
action.connect('activate', do_action_param);
|
2011-12-02 16:15:14 -05:00
|
|
|
app.add_action(action);
|
2011-05-10 12:29:52 -04:00
|
|
|
|
|
|
|
let menu = new Gio.Menu();
|
2011-12-08 15:53:03 -05:00
|
|
|
menu.append('An action', 'app.one');
|
2011-05-10 12:29:52 -04:00
|
|
|
|
|
|
|
let section = new Gio.Menu();
|
2011-12-08 15:53:03 -05:00
|
|
|
section.append('Another action', 'app.two');
|
|
|
|
section.append('Same as above', 'app.two');
|
2011-05-10 12:29:52 -04:00
|
|
|
menu.append_section(null, section);
|
|
|
|
|
|
|
|
// another section, to check separators
|
|
|
|
section = new Gio.Menu();
|
2011-12-08 15:53:03 -05:00
|
|
|
section.append('Checkbox', 'app.toggle');
|
|
|
|
section.append('Disabled', 'app.disable');
|
2015-02-27 09:01:04 -05:00
|
|
|
section.append('Missing Action', 'app.no-action');
|
2011-12-21 17:11:40 -05:00
|
|
|
menu.append_section('Subsection', section);
|
2011-05-10 12:29:52 -04: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 15:53:03 -05:00
|
|
|
submenu.append('Open c:\\', 'app.parameter-string::c:\\');
|
|
|
|
submenu.append('Open /home', 'app.parameter-string::/home');
|
2011-05-10 12:29:52 -04:00
|
|
|
menu.append_submenu('Recent files', submenu);
|
|
|
|
|
|
|
|
let item = Gio.MenuItem.new('Say 42', null);
|
2011-12-08 15:53:03 -05:00
|
|
|
item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 42));
|
2011-05-10 12:29:52 -04:00
|
|
|
menu.append_item(item);
|
|
|
|
|
|
|
|
let item = Gio.MenuItem.new('Say 43', null);
|
2011-12-08 15:53:03 -05:00
|
|
|
item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 43));
|
2011-05-10 12:29:52 -04:00
|
|
|
menu.append_item(item);
|
|
|
|
|
2011-12-02 16:15:14 -05:00
|
|
|
let window = null;
|
2011-05-10 12:29:52 -04:00
|
|
|
|
2011-12-08 15:53:03 -05:00
|
|
|
app.connect_after('startup', function(app) {
|
2012-03-30 10:44:36 -04:00
|
|
|
app.set_app_menu(menu);
|
|
|
|
window = new Gtk.ApplicationWindow({ title: "Test Application", application: app });
|
2011-12-08 15:53:03 -05:00
|
|
|
});
|
2011-12-02 16:15:14 -05:00
|
|
|
app.connect('activate', function(app) {
|
2012-03-30 10:44:36 -04:00
|
|
|
window.present();
|
2011-05-10 12:29:52 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
app.run(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|