summaryrefslogtreecommitdiff
path: root/src/library/BackgroundProgressBar.vala
blob: 8ad718533b826680f325c72e8a43799f9942718b (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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

internal class BackgroundProgressBar : Gtk.ProgressBar {
    public enum Priority {
        NONE = 0,
        STARTUP_SCAN = 35,
        REALTIME_UPDATE = 40,
        REALTIME_IMPORT = 50,
        METADATA_WRITER = 30
    }

    public bool should_be_visible { get; private set; default = false; }

#if UNITY_SUPPORT
    // UnityProgressBar: init
    private UnityProgressBar uniprobar = UnityProgressBar.get_instance();
#endif

    private const int PULSE_MSEC = 250;

    public BackgroundProgressBar() {
        Object(show_text: true);
    }

    private Priority current_priority = Priority.NONE;
    private uint pulse_id = 0;

    public void start(string label, Priority priority) {
        if (priority < current_priority)
            return;

        stop(priority, false);

        current_priority = priority;
        set_text(label);
        pulse();
        should_be_visible = true;
        pulse_id = Timeout.add(PULSE_MSEC, on_pulse_timeout);
    }

    public void stop(Priority priority, bool clear) {
        if (priority < current_priority)
            return;

        if (pulse_id != 0) {
            Source.remove(pulse_id);
            pulse_id = 0;
        }

        if (clear)
            this.clear(priority);
    }

    public bool update(string label, Priority priority, double count, double total) {
        if (priority < current_priority)
            return false;

        stop(priority, false);

        if (count <= 0.0 || total <= 0.0 || count >= total) {
            clear(priority);

            return false;
        }

        current_priority = priority;

        double fraction = count / total;
        set_fraction(fraction);
        set_text(_("%s (%d%%)").printf(label, (int) (fraction * 100.0)));
        should_be_visible = true;

#if UNITY_SUPPORT
        // UnityProgressBar: try to draw & set progress
        uniprobar.set_visible(true);
        uniprobar.set_progress(fraction);
#endif

        return true;
    }

    public void clear(Priority priority) {
        if (priority < current_priority)
            return;

        stop(priority, false);

        current_priority = 0;

        set_fraction(0.0);
        set_text("");
        should_be_visible = false;

#if UNITY_SUPPORT
        // UnityProgressBar: reset
        uniprobar.reset();
#endif
    }

    private bool on_pulse_timeout() {
        pulse();

        return true;
    }
}