Alexandra Betouni 4ee21ada05 feat(desktop) Added image function in Style
Introduced Style.svg() Style.png() Style.emoji() and
Style.icon() in Style.qml. Those should be used to
set the source in Images instead of using relative
paths. Usage:
Image {
   source: Style.svg("check)
   ....

Also moved all Singletons inside a new "utils"
folder and made it a QML module, to use
import utils 1.0 instead of relative paths

Closes #3678
2021-09-28 15:28:00 -04:00

124 lines
3.0 KiB
QML

pragma Singleton
import QtQuick 2.0
Item {
id: backpressure
property var _timers: ({})
property int _nextId: 0
function setTimeout(owner, timeout, callback) {
var tid = ++_nextId;
var cleanup = function() {
if (tid !== null) {
clearTimeout(tid);
}
}
if (!owner) {
owner = backpressure;
}
owner.Component.destruction.connect(cleanup);
var obj = Qt.createQmlObject('import QtQuick 2.0; Timer {running: false; repeat: false; interval: ' + timeout + '}', backpressure, "setTimeout");
obj.triggered.connect(function() {
callback();
obj.destroy();
owner.Component.destruction.disconnect(cleanup);
delete _timers[tid];
});
obj.running = true;
_timers[tid] = obj;
return tid;
}
function clearTimeout(timerId) {
if (!_timers.hasOwnProperty(timerId)) {
return;
}
var timer = _timers[timerId];
timer.stop();
timer.destroy();
delete _timers[timerId];
}
function oneInTime(owner, duration, callback) {
var pending = false;
var timerId = null;
return function() {
if (pending) {
return;
}
pending = true;
var args = arguments;
callback.apply(null, args);
timerId = setTimeout(owner, duration , function() {
pending = false;
}, duration);
}
}
function promisedOneInTime(owner, callback) {
var q = priv.loadPromiseLib();
var promise = null;
return function() {
var args = arguments;
if (promise !== null) {
return q.rejected();
}
promise = q.promise(function(fulfill, reject) {
fulfill(callback.apply(null, args));
});
promise.then(function() {
promise = null;
}, function() {
promise = null;
});
return promise;
};
}
function debounce(owner, duration, callback) {
var timerId = null;
return function() {
var args = arguments;
if (timerId !== null) {
clearTimeout(timerId);
}
timerId = setTimeout(owner, duration, function() {
timerId = null;
callback.apply(null, args);
});
}
}
QtObject {
id: priv
property var q: null
function loadPromiseLib() {
if (q !== null) {
return q.q;
}
q = Qt.createQmlObject('import QtQuick 2.0; import QuickPromise 1.0; Item { property var q: Q;}', backpressure);
if (q.hasOwnProperty("qmlErrors")) {
console.error("Backpressure: Failed to load QuickPromise. Please check your installation.");
}
return q.q;
}
}
}