summaryrefslogtreecommitdiffstats
path: root/tests/interactive/gapplication.js
blob: ec38b806f31d17561c271fca108cfc61583a7713 (plain)
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
#!/usr/bin/env gjs
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

imports.gi.versions = { Gdk: '3.0', Gtk: '3.0' };
const { Gdk, Gio, GLib, Gtk } = imports.gi;

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));
}

function do_action_toggle(action) {
    action.set_state(GLib.Variant.new('b', !action.state.deep_unpack()));
    print ("Toggled");
}

function do_action_state_change(action) {
    print ("Action '" + action.name + "' has now state " + action.state.print(true));
}

function main() {
    Gtk.init(null);
    Gdk.set_program_class('test-gjsgapp');

    let app = new Gtk.Application({ application_id: 'org.gnome.Shell.GtkApplicationTest' });
    app.connect('activate', () => {
        print ("Activated");
    });

    let action = new Gio.SimpleAction({ name: 'one' });
    action.connect('activate', do_action);
    app.add_action(action);

    action = new Gio.SimpleAction({ name: 'two' });
    action.connect('activate', do_action);
    app.add_action(action);

    action = new Gio.SimpleAction({ name: 'toggle', state: GLib.Variant.new('b', false) });
    action.connect('activate', do_action_toggle);
    action.connect('notify::state', do_action_state_change);
    app.add_action(action);

    action = new Gio.SimpleAction({ name: 'disable', enabled: false });
    action.set_enabled(false);
    action.connect('activate', do_action);
    app.add_action(action);

    action = new Gio.SimpleAction({ name: 'parameter-int', parameter_type: GLib.VariantType.new('u') });
    action.connect('activate', do_action_param);
    app.add_action(action);

    action = new Gio.SimpleAction({ name: 'parameter-string', parameter_type: GLib.VariantType.new('s') });
    action.connect('activate', do_action_param);
    app.add_action(action);

    let menu = new Gio.Menu();
    menu.append('An action', 'app.one');

    let section = new Gio.Menu();
    section.append('Another action', 'app.two');
    section.append('Same as above', 'app.two');
    menu.append_section(null, section);

    // another section, to check separators
    section = new Gio.Menu();
    section.append('Checkbox', 'app.toggle');
    section.append('Disabled', 'app.disable');
    section.append('Missing Action', 'app.no-action');
    menu.append_section('Subsection', section);

    // 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();
    submenu.append('Open c:\\', 'app.parameter-string::c:\\');
    submenu.append('Open /home', 'app.parameter-string::/home');
    menu.append_submenu('Recent files', submenu);

    let item = Gio.MenuItem.new('Say 42', null);
    item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 42));
    menu.append_item(item);

    item = Gio.MenuItem.new('Say 43', null);
    item.set_action_and_target_value('app.parameter-int', GLib.Variant.new('u', 43));
    menu.append_item(item);

    let window = null;

    app.connect_after('startup', app => {
        app.set_app_menu(menu);
        window = new Gtk.ApplicationWindow({ title: "Test Application", application: app });
    });
    app.connect('activate', app => {
        window.present();
    });

    app.run(null);
}

main();