Skip to content

Commit 453def8

Browse files
committed
Bangle.js: setUI/etc now don't draw a 'back' icon if there is no widget bar (or it's hidden)
Also tidy up setUI touch handling - now we have stopEventPropagation and prependListener it's easier
1 parent f1e5e6f commit 453def8

File tree

5 files changed

+75
-65
lines changed

5 files changed

+75
-65
lines changed

ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
micro:bit2: Ensure we don't initialise the I2C1 peripheral (we use software I2C internally)
2727
Unlock functionCode early in function execution to reduce locks needed during recursion (#2616)
2828
nRF52: Improve software timer accuracy by keeping timer running while executing jstUtilTimerInterruptHandler (fix #2620)
29+
Bangle.js: setUI/etc now don't draw a 'back' icon if there is no widget bar (or it's hidden)
2930

3031
2v25 : ESP32C3: Get analogRead working correctly
3132
Graphics: Adjust image alignment when rotating images to avoid cropping (fix #2535)

libs/js/banglejs/Bangle_setUI_F18.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
mode = options.mode;
66
if (!mode) throw new Error("Missing mode in setUI({...})");
77
}
8-
var redraw = true;
9-
if (global.WIDGETS && WIDGETS.back) {
10-
redraw = false;
11-
WIDGETS.back.remove(mode && options.back);
8+
var hadBackWidget = false;
9+
if (global.WIDGETS && WIDGETS.back) {
10+
hadBackWidget = true; // if we had a back widget already, don't redraw at the end
11+
WIDGETS.back.remove(options.back); // only redraw when removing if we don't have options.back
1212
}
1313
if (Bangle.btnWatches) {
1414
Bangle.btnWatches.forEach(clearWatch);
@@ -92,29 +92,38 @@
9292
if (options.redraw) // handler for redrawing the UI
9393
Bangle.uiRedraw = options.redraw;
9494
if (options.back) {
95-
var touchHandler = (z) => {
96-
if (z==1) options.back();
97-
};
98-
Bangle.on("touch", touchHandler);
9995
var btnWatch;
100-
if (Bangle.btnWatches===undefined) // only add back button handler if there's no existing watch on BTN1
96+
// only add back button handler if there's no existing watch on BTN1
97+
if (Bangle.btnWatches===undefined)
10198
btnWatch = setWatch(function() {
10299
btnWatch = undefined;
103100
options.back();
104101
}, BTN3, {edge:"rising"});
105-
WIDGETS = Object.assign({back:{
106-
area:"tl", width:24,
107-
draw:e=>g.reset().setColor("#f00").drawImage(atob("GBiBAAAYAAH/gAf/4A//8B//+D///D///H/P/n+H/n8P/n4f/vwAP/wAP34f/n8P/n+H/n/P/j///D///B//+A//8Af/4AH/gAAYAA=="),e.x,e.y),
108-
remove:(noclear)=>{
109-
var w = WIDGETS.back;
110-
if (w.area!="tl") noclear=true; // area="" is set by widget_utils.hide, so avoid drawing
111-
if (btnWatch) clearWatch(btnWatch);
112-
Bangle.removeListener("touch", touchHandler);
113-
if (!noclear) g.reset().clearRect({x:w.x, y:w.y, w:24,h:24});
114-
delete WIDGETS.back;
115-
if (!noclear) Bangle.drawWidgets();
116-
}
117-
}},global.WIDGETS);
118-
if (redraw) Bangle.drawWidgets();
102+
// if we have widgets loaded *and* visible at the top, add a back widget (see #3788)
103+
if (global.WIDGETS && Bangle.appRect.y) {
104+
// add our own touch handler for touching in the left
105+
var touchHandler = function(z) {
106+
if (z==1) {
107+
E.stopEventPropagation(); // stop subsequent touch handlers from being called
108+
options.back();
109+
}
110+
};
111+
Bangle.prependListener("touch", touchHandler);
112+
// add widget - 'remove' function will remove the widgets
113+
WIDGETS.back = {
114+
area:"tl", width:24,
115+
draw:e=>g.reset().setColor("#f00").drawImage(atob("GBiBAAAYAAH/gAf/4A//8B//+D///D///H/P/n+H/n8P/n4f/vwAP/wAP34f/n8P/n+H/n/P/j///D///B//+A//8Af/4AH/gAAYAA=="),e.x,e.y),
116+
remove:function(noclear){
117+
var w = WIDGETS.back;
118+
if (w.area!="tl") noclear=true; // area="" is set by widget_utils.hide, so avoid drawing
119+
if (btnWatch) clearWatch(btnWatch);
120+
Bangle.removeListener("touch", touchHandler);
121+
if (!noclear) g.reset().clearRect({x:w.x, y:w.y, w:24,h:24});
122+
delete WIDGETS.back;
123+
if (!noclear) Bangle.drawWidgets();
124+
}
125+
};
126+
if (!hadBackWidget) Bangle.drawWidgets();
127+
}
119128
}
120129
})
46 Bytes
Binary file not shown.

libs/js/banglejs/Bangle_setUI_Q3.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
mode = options.mode;
66
if (!mode) throw new Error("Missing mode in setUI({...})");
77
}
8-
var redraw = true;
9-
if (global.WIDGETS && WIDGETS.back) {
10-
redraw = false;
11-
WIDGETS.back.remove(mode && options.back);
8+
var hadBackWidget = false;
9+
if (global.WIDGETS && WIDGETS.back) {
10+
hadBackWidget = true; // if we had a back widget already, don't redraw at the end
11+
WIDGETS.back.remove(options.back); // only redraw when removing if we don't have options.back
1212
}
1313
if (Bangle.btnWatches) {
1414
Bangle.btnWatches.forEach(clearWatch);
@@ -52,6 +52,7 @@
5252
};
5353
Bangle.on('drag',Bangle.dragHandler);
5454
Bangle.touchHandler = d => {b();cb();};
55+
Bangle.on("touch", Bangle.touchHandler);
5556
Bangle.btnWatches = [
5657
setWatch(function() { b();cb(); }, BTN1, {repeat:1, edge:"rising"}),
5758
];
@@ -69,6 +70,7 @@
6970
};
7071
Bangle.on('drag',Bangle.dragHandler);
7172
Bangle.touchHandler = d => {b();cb();};
73+
Bangle.on("touch", Bangle.touchHandler);
7274
Bangle.btnWatches = [
7375
setWatch(function() { b();cb(); }, BTN1, {repeat:1, edge:"rising"}),
7476
];
@@ -83,6 +85,7 @@
8385
if (e.x < 120) return;
8486
b();cb((e.y > 88) ? 1 : -1);
8587
};
88+
Bangle.on("touch", Bangle.touchHandler);
8689
Bangle.btnWatches = [
8790
setWatch(Bangle.showLauncher, BTN1, {repeat:1,edge:"rising"})
8891
];
@@ -95,8 +98,10 @@
9598
} else
9699
throw new Error("Unknown UI mode "+E.toJS(mode));
97100
if (options.clock) Bangle.CLOCK=1;
98-
if (options.touch)
99-
Bangle.touchHandler = options.touch;
101+
if (options.touch) {
102+
Bangle.touchHandler = options.touch;
103+
Bangle.on("touch", Bangle.touchHandler);
104+
}
100105
if (options.drag) {
101106
Bangle.dragHandler = options.drag;
102107
Bangle.on("drag", Bangle.dragHandler);
@@ -113,44 +118,39 @@
113118
if (options.redraw) // handler for redrawing the UI
114119
Bangle.uiRedraw = options.redraw;
115120
if (options.back) {
116-
var touchHandler = (_,e) => {
117-
if (e.y<36 && e.x<48) {
118-
e.handled = true;
119-
E.stopEventPropagation();
121+
var btnWatch;
122+
// only add back button handler if there's no existing watch on BTN1
123+
if (Bangle.btnWatches===undefined)
124+
btnWatch = setWatch(function() {
125+
btnWatch = undefined;
120126
options.back();
121-
}
122-
};
123-
Bangle.on("touch", touchHandler);
124-
// If a touch handler was needed for setUI, add it - but ignore touches if they've already gone to the 'back' handler
125-
if (Bangle.touchHandler) {
126-
var uiTouchHandler = Bangle.touchHandler;
127-
Bangle.touchHandler = (_,e) => {
128-
if (!e.handled) uiTouchHandler(_,e);
127+
}, BTN1, {edge:"rising"});
128+
// if we have widgets loaded *and* visible at the top, add a back widget (see #3788)
129+
if (global.WIDGETS && Bangle.appRect.y) {
130+
// add our own touch handler for touching in the top left
131+
var touchHandler = function(_,e) {
132+
if (e.y<36 && e.x<48) {
133+
e.handled = true;
134+
E.stopEventPropagation(); // stop subsequent touch handlers from being called
135+
options.back();
136+
}
129137
};
130-
Bangle.on("touch", Bangle.touchHandler);
138+
Bangle.prependListener("touch", touchHandler);
139+
// add widget - 'remove' function will remove the widgets
140+
WIDGETS.back = {
141+
area:"tl", width:24,
142+
draw:e=>g.reset().setColor("#f00").drawImage(atob("GBiBAAAYAAH/gAf/4A//8B//+D///D///H/P/n+H/n8P/n4f/vwAP/wAP34f/n8P/n+H/n/P/j///D///B//+A//8Af/4AH/gAAYAA=="),e.x,e.y),
143+
remove:function(noclear){
144+
var w = WIDGETS.back;
145+
if (w.area!="tl") noclear=true; // area="" is set by widget_utils.hide, so avoid drawing
146+
if (btnWatch) clearWatch(btnWatch);
147+
Bangle.removeListener("touch", touchHandler);
148+
if (!noclear) g.reset().clearRect({x:w.x, y:w.y, w:24,h:24});
149+
delete WIDGETS.back;
150+
if (!noclear) Bangle.drawWidgets();
151+
}
152+
};
153+
if (!hadBackWidget) Bangle.drawWidgets();
131154
}
132-
var btnWatch;
133-
if (Bangle.btnWatches===undefined) // only add back button handler if there's no existing watch on BTN1
134-
btnWatch = setWatch(function() {
135-
btnWatch = undefined;
136-
options.back();
137-
}, BTN1, {edge:"rising"});
138-
WIDGETS = Object.assign({back:{
139-
area:"tl", width:24,
140-
draw:e=>g.reset().setColor("#f00").drawImage(atob("GBiBAAAYAAH/gAf/4A//8B//+D///D///H/P/n+H/n8P/n4f/vwAP/wAP34f/n8P/n+H/n/P/j///D///B//+A//8Af/4AH/gAAYAA=="),e.x,e.y),
141-
remove:(noclear)=>{
142-
var w = WIDGETS.back;
143-
if (w.area!="tl") noclear=true; // area="" is set by widget_utils.hide, so avoid drawing
144-
if (btnWatch) clearWatch(btnWatch);
145-
Bangle.removeListener("touch", touchHandler);
146-
if (!noclear) g.reset().clearRect({x:w.x, y:w.y, w:24,h:24});
147-
delete WIDGETS.back;
148-
if (!noclear) Bangle.drawWidgets();
149-
}
150-
}},global.WIDGETS);
151-
if (redraw) Bangle.drawWidgets();
152-
} else { // If a touch handler was needed for setUI, add it
153-
if (Bangle.touchHandler)
154-
Bangle.on("touch", Bangle.touchHandler);
155155
}
156156
})
-18 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)