Skip to content

Commit 2a706a2

Browse files
author
thyttan
committed
Merge branch 'swipeinv' into swipeinv-modify-on
2 parents 04023e8 + 9e7bee6 commit 2a706a2

File tree

4 files changed

+93
-21
lines changed

4 files changed

+93
-21
lines changed

apps/swipeinv/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
0.01: New app!
22
0.02: Minor code improvements
3+
0.03: (WIP) Add inversion of drag events. Add per app, per direction setting.
4+
Refactor to hijack and modify the events instead of the handling of them.
5+
This should add compatibility with most apps, even if Bangle.setUI is not
6+
used.

apps/swipeinv/boot.js

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,46 @@
44
apps: []
55
}, require("Storage").readJSON("swipeinv.json", true) || {});
66

7-
if (settings.global || settings.apps.length > 0) {
8-
const setURIOrig = Bangle.setUI;
9-
Bangle.setUI = (mode, callback) => {
10-
if (typeof mode === "object" && mode.swipe) {
11-
if (settings.global ^ settings.apps.includes(global.__FILE__)) {
12-
const origSwipeCb = mode.swipe;
13-
mode.swipe = (dirLR, dirUD) => origSwipeCb(dirLR*-1, dirUD*-1);
7+
let getAppIdFromSrc = ()=> {
8+
if (!global.__FILE__ || global.__FILE__===".bootcde") {
9+
return require("Storage").readJSON("setting.json",true).clock
10+
} else {return global.__FILE__.split(".")[0];}
11+
}
12+
13+
setTimeout(() => { // Timeout so we prepend listeners late, hopefully after all other listerners were added.
14+
if (settings.global || Object.keys(settings.apps).length > 0) {
15+
16+
let swipeInverter = (dirLR, dirUD, obj) => {
17+
if (settings.global ^ Object.keys(settings.apps).includes(getAppIdFromSrc())) {
18+
if (!(obj && obj.inverted)) {
19+
E.stopEventPropagation();
20+
obj = Object.assign({inverted:true}, obj);
21+
22+
if (settings.global ^ (settings.apps[getAppIdFromSrc()]&&settings.apps[getAppIdFromSrc()].swipeH)) {dirLR *= -1;}
23+
if (settings.global ^ (settings.apps[getAppIdFromSrc()]&&settings.apps[getAppIdFromSrc()].swipeV)) {dirUD *= -1;}
24+
25+
Bangle.emit("swipe", dirLR, dirUD, obj)
26+
}
1427
}
1528
}
16-
return setURIOrig(mode, callback);
17-
};
18-
}
29+
30+
let dragInverter = (e) => {
31+
if (settings.global ^ Object.keys(settings.apps).includes(getAppIdFromSrc())) {
32+
if (!e.inverted) {
33+
E.stopEventPropagation();
34+
e.inverted = true;
35+
36+
if (settings.global ^ (settings.apps[getAppIdFromSrc()]&&settings.apps[getAppIdFromSrc()].dragH)) {e.dx *= -1;}
37+
if (settings.global ^ (settings.apps[getAppIdFromSrc()]&&settings.apps[getAppIdFromSrc()].dragV)) {e.dy *= -1;}
38+
39+
Bangle.emit("drag", e);
40+
}
41+
}
42+
}
43+
44+
Bangle.prependListener("swipe", swipeInverter);
45+
Bangle.prependListener("drag", dragInverter);
46+
47+
}
48+
}, 0)
1949
}

apps/swipeinv/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Swipe inversion",
44
"shortName":"Swipe inv.",
55
"icon": "app.png",
6-
"version": "0.02",
6+
"version": "0.03",
77
"description": "Inverts swipe direction globally or per app, see settings. If global inversion is enabled, you can unselect the inversion per app and vice versa.",
88
"readme":"README.md",
99
"type": "bootloader",

apps/swipeinv/settings.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,45 @@
33
// Load settings
44
const settings = Object.assign({
55
global: false,
6-
apps: []
6+
apps: {}
77
}, require("Storage").readJSON(FILE, true) || {});
88

99
function writeSettings() {
1010
require('Storage').writeJSON(FILE, settings);
1111
}
1212

13+
// Convert settings when coming from ver < 4
14+
function convertAppsArrayToObject() {
15+
if (Array.isArray(settings.apps)) {
16+
let newObject = {};
17+
settings.apps.forEach((source)=>{
18+
let idExtractedFromSource = source.split(".")[0];
19+
newObject[idExtractedFromSource] = {};
20+
})
21+
settings.apps = newObject;
22+
}
23+
}
24+
convertAppsArrayToObject();
25+
1326
function appMenu() {
1427
let menu = {
1528
"" : { "title" : /*LANG*/"Swipe inversion apps" },
1629
"< Back" : () => { writeSettings(); mainMenu();}
1730
};
18-
require("Storage").list(/\.info$/).map(app=>require("Storage").readJSON(app,1)).filter(app => app.type === "app" || !app.type).sort((a,b) => {
31+
require("Storage").list(/\.info$/).map(app=>require("Storage").readJSON(app,1)).filter(app => app.type==="app" || app.type==="clock" || app.type==="launch" || !app.type).sort((a,b) => {
1932
if (a.name<b.name) return -1;
2033
if (a.name>b.name) return 1;
2134
return 0;
2235
}).forEach(app => {
2336
menu[app.name] = {
24-
value: settings.apps.includes(app.src),
37+
value: Object.keys(settings.apps).includes(app.id),
2538
onchange: v => {
2639
if (v) {
27-
settings.apps.push(app.src);
40+
if (!settings.apps[app.id] || Object.keys(settings.apps[app.id]).length===0) {
41+
settings.apps[app.id] = {"name":app.name, "swipeH":true, "swipeV":true, "dragH":true, "dragV":true};
42+
}
2843
} else {
29-
const idx = settings.apps.indexOf(app.src);
30-
if (idx !== -1) {
31-
settings.apps.splice(idx, 1);
32-
}
44+
if (settings.apps[app.id]) {delete settings.apps[app.id];}
3345
}
3446
}
3547
};
@@ -38,7 +50,7 @@
3850
}
3951

4052
function mainMenu() {
41-
E.showMenu({
53+
let menu = {
4254
"" : { "title" : /*LANG*/"Swipe inversion" },
4355
"< Back" : () => back(),
4456

@@ -50,8 +62,34 @@
5062
}
5163
},
5264

53-
/*LANG*/'Select apps': () => appMenu()
65+
/*LANG*/'Select apps': () => appMenu(),
66+
};
67+
68+
Object.keys(settings.apps).forEach((appID) => {
69+
// Create a sub menu and show it.
70+
let subMenu = {
71+
"" : { "title" : /*LANG*/"Tune"+" "+appID },
72+
"< Back" : () => { writeSettings(); mainMenu();}
73+
}
74+
let subMenuEntries = [{name:"Swipe Horizontal", id:"swipeH"}, {name:"Swipe Vertical", id:"swipeV"}, {name:"Drag Horizontal", id:"dragH"}, {name:"Drag Vertical", id:"dragV"}];
75+
subMenuEntries.forEach((setting)=>{
76+
if (!Object.keys(settings.apps).includes(appID)) {settings.apps[appID] = {"swipeH":true, "swipeV":true, "dragH":true, "dragV":true}}
77+
subMenu[setting.name] = {
78+
value: settings.apps[appID][setting.id],
79+
onchange: v => {
80+
if (v) {
81+
settings.apps[appID][setting.id] = true;
82+
} else {
83+
settings.apps[appID][setting.id] = false;
84+
}
85+
}
86+
};
87+
})
88+
89+
menu[settings.apps[appID].name] = ()=>E.showMenu(subMenu);
5490
});
91+
92+
E.showMenu(menu);
5593
}
5694

5795
mainMenu();

0 commit comments

Comments
 (0)