summaryrefslogtreecommitdiff
path: root/test/smoke-tests/back-forward.js
blob: 9c4f9b20cc51b67a684a3829f531fd50e2d3e08d (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2013 Endless Mobile, Inc.

const Lang = imports.lang;
const Endless = imports.gi.Endless;
const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
const GObject = imports.gi.GObject;

const TEST_APPLICATION_ID = 'com.endlessm.example.test-back-forward';

const CSS_STYLES = ' \
    .count { \
        font-size: 36px; \
        padding: 10px; \
        color: black; \
        background-color: white; \
        border-radius: 0; \
        transition: border-radius 500ms, background-color 500ms; \
    } \
    .current { \
        border-radius: 20px; \
        background-color: purple; \
    }';

const Page = new Lang.Class({
    Name: 'Page',
    Extends: Gtk.Grid,
    Properties: {
        'current': GObject.ParamSpec.int('current', '', '',
            GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE,
            0, 4, 0)
    },

    _init: function (props) {
        this._current = 0;
        this._frames = null;

        props = props || {};
        props.orientation = Gtk.Orientation.HORIZONTAL;
        this.parent(props);

        this._frames = [0, 1, 2, 3, 4].map(function (item, index) {
            let frame = new Gtk.Frame({
                margin: 5
            });
            let label = new Gtk.Label({
                label: index.toString()
            });
            frame.add(label);
            frame.get_style_context().add_class('count');
            this.add(frame);
            return frame;
        }, this);
    },

    get current() {
        return this._current;
    },

    set current(value) {
        if(this._frames)
            this._frames[this._current].get_style_context().remove_class('current');
        this._current = value;
        if(this._frames)
            this._frames[this._current].get_style_context().add_class('current');
        this.notify('current');
    }
});

const TestApplication = new Lang.Class ({
    Name: 'TestApplication',
    Extends: Endless.Application,

    vfunc_startup: function () {
        this.parent();

        this._win = new Endless.Window({
            application: this
        });
        let page = new Page({
            halign: Gtk.Align.CENTER,
            valign: Gtk.Align.CENTER,
            expand: true,
        });
        let nav = new Endless.TopbarNavButton();
        this._win.page_manager.add(page, {
            left_topbar_widget: nav
        });

        let provider = new Gtk.CssProvider();
        provider.load_from_data(CSS_STYLES);
        Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), provider,
            Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

        nav.back_button.connect('clicked', function () {
            page.current--;
        });
        nav.forward_button.connect('clicked', function () {
            page.current++;
        });
        page.connect('notify::current', function () {
            nav.back_button.sensitive = true;
            nav.forward_button.sensitive = true;
            if (page.current === 0)
                nav.back_button.sensitive = false;
            if (page.current === 4)
                nav.forward_button.sensitive = false;
        });

        page.current = 0;
    },

    vfunc_activate: function () {
        this._win.show_all();

        this.parent();
    }
});

let app = new TestApplication({
    application_id: TEST_APPLICATION_ID,
    flags: 0
});
app.run(ARGV);