summaryrefslogtreecommitdiff
path: root/src/library/BackgroundProgressBar.vala
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff.email>2023-06-14 20:36:37 +0200
committerJörg Frings-Fürst <debian@jff.email>2023-06-14 20:36:37 +0200
commitbb80d3feebdc9acc52e3f4ad24084d8425f043a2 (patch)
tree2084a84c39f159c6aea254775dc0880d52579d45 /src/library/BackgroundProgressBar.vala
parentb26ff0798252a1a8072dd2c7a67f6205de9fde11 (diff)
parent31804433d72460cbe0a39f9f8ea5e76058d84cda (diff)
Merge branch 'feature/upstream' into develop
Diffstat (limited to 'src/library/BackgroundProgressBar.vala')
-rw-r--r--src/library/BackgroundProgressBar.vala109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/library/BackgroundProgressBar.vala b/src/library/BackgroundProgressBar.vala
new file mode 100644
index 0000000..8ad7185
--- /dev/null
+++ b/src/library/BackgroundProgressBar.vala
@@ -0,0 +1,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;
+ }
+}