BangleApps/apps/backswipe/boot.js

62 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2023-01-20 07:32:08 +00:00
(function () {
var DEFAULTS = {
mode: 0,
apps: [],
};
var settings = require("Storage").readJSON("backswipe.json", 1) || DEFAULTS;
2023-01-20 08:32:11 +00:00
2023-01-20 07:32:08 +00:00
// Overrride the default setUI method, so we can save the back button callback
var setUI = Bangle.setUI;
Bangle.setUI = function (mode, cb) {
var options = {};
if ("object"==typeof mode) {
options = mode;
}
2023-01-20 08:32:11 +00:00
var currentFile = global.__FILE__ || "";
2023-02-04 21:36:16 +00:00
if (global.BACK) delete global.BACK;
2023-02-07 18:40:07 +00:00
if (options && options.back && enabledForApp(currentFile)) {
2023-01-20 07:32:08 +00:00
global.BACK = options.back;
}
setUI(mode, cb);
};
function countHandlers(eventType) {
if (Bangle["#on"+eventType] === undefined) {
return 0;
} else if (Bangle["#on"+eventType] instanceof Array) {
return Bangle["#on"+eventType].filter(x=>x).length;
} else if (Bangle["#on"+eventType] !== undefined) {
return 1;
}
}
2023-02-05 13:43:43 +00:00
function goBack(lr, _) {
2023-01-20 07:32:08 +00:00
// if it is a left to right swipe
if (lr === 1) {
// if we're in an app that has a back button, run the callback for it
if (global.BACK && countHandlers("swipe")<=settings.standardNumSwipeHandlers && countHandlers("drag")<=settings.standardNumDragHandlers) {
2023-01-20 07:32:08 +00:00
global.BACK();
E.stopEventPropagation();
2023-01-20 07:32:08 +00:00
}
}
}
// Check if the back button should be enabled for the current app
// app is the src file of the app
2023-01-20 08:32:11 +00:00
function enabledForApp(app) {
if (!settings) return true;
if (settings.mode === 0) {
return !(settings.apps.filter((a) => (a.src===app)||(a.files&&a.files.includes(app))).length > 0); // The `a.src===app` and `a.files&&...` checks are for backwards compatibility. Otherwise only `a.files.includes(app)` is needed.
} else if (settings.mode === 1) {
return settings.apps.filter((a) => (a.src===app)||(a.files&&a.files.includes(app))).length > 0;
2023-01-20 08:32:11 +00:00
} else {
return settings.mode === 2 ? true : false;
2023-01-20 08:32:11 +00:00
}
}
2023-01-20 07:32:08 +00:00
// Listen to left to right swipe
Bangle.prependListener("swipe", goBack);
2023-01-20 07:32:08 +00:00
})();