Revise event handlers
parent
69d12f3114
commit
4bf23b4fbb
|
|
@ -23,84 +23,100 @@
|
||||||
let h = g.getHeight();
|
let h = g.getHeight();
|
||||||
let drawTimeout;
|
let drawTimeout;
|
||||||
|
|
||||||
// 3. Touch and drag handlers
|
// 3. Event handlers
|
||||||
let touchHandler = function(zone, e) {
|
let eventHandlers = {
|
||||||
let boxTouched = false;
|
touchHandler: function(zone, e) {
|
||||||
let touchedBox = null;
|
let boxTouched = false;
|
||||||
|
let touchedBox = null;
|
||||||
for (let boxKey in boxes) {
|
|
||||||
if (touchInText(e, boxes[boxKey])) {
|
for (let boxKey in boxes) {
|
||||||
touchedBox = boxKey;
|
if (touchInText(e, boxes[boxKey])) {
|
||||||
boxTouched = true;
|
touchedBox = boxKey;
|
||||||
break;
|
boxTouched = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
if (boxTouched) {
|
||||||
if (boxTouched) {
|
|
||||||
// Toggle the selected state of the touched box
|
// Toggle the selected state of the touched box
|
||||||
boxes[touchedBox].selected = !boxes[touchedBox].selected;
|
boxes[touchedBox].selected = !boxes[touchedBox].selected;
|
||||||
|
|
||||||
// Update isDragging based on whether any box is selected
|
// Update isDragging based on whether any box is selected
|
||||||
isDragging = Object.values(boxes).some(box => box.selected);
|
isDragging = Object.values(boxes).some(box => box.selected);
|
||||||
|
|
||||||
if (isDragging) {
|
if (isDragging) {
|
||||||
widgets.hide();
|
widgets.hide();
|
||||||
|
} else {
|
||||||
|
deselectAllBoxes();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// If tapped outside any box, deselect all boxes
|
||||||
deselectAllBoxes();
|
deselectAllBoxes();
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// If tapped outside any box, deselect all boxes
|
|
||||||
deselectAllBoxes();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Always redraw after a touch event
|
// Always redraw after a touch event
|
||||||
draw();
|
draw();
|
||||||
|
|
||||||
// Handle double tap for saving
|
// Handle double tap for saving
|
||||||
if (!boxTouched && !isDragging) {
|
if (!boxTouched && !isDragging) {
|
||||||
if (doubleTapTimer) {
|
if (doubleTapTimer) {
|
||||||
clearTimeout(doubleTapTimer);
|
clearTimeout(doubleTapTimer);
|
||||||
doubleTapTimer = null;
|
doubleTapTimer = null;
|
||||||
for (let boxKey in boxes) {
|
for (let boxKey in boxes) {
|
||||||
boxesConfig[boxKey].boxPos.x = (boxes[boxKey].pos.x / w).toFixed(3);
|
boxesConfig[boxKey].boxPos.x = (boxes[boxKey].pos.x / w).toFixed(3);
|
||||||
boxesConfig[boxKey].boxPos.y = (boxes[boxKey].pos.y / h).toFixed(3);
|
boxesConfig[boxKey].boxPos.y = (boxes[boxKey].pos.y / h).toFixed(3);
|
||||||
|
}
|
||||||
|
storage.write(fileName, JSON.stringify(boxesConfig));
|
||||||
|
displaySaveIcon();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
storage.write(fileName, JSON.stringify(boxesConfig));
|
|
||||||
displaySaveIcon();
|
doubleTapTimer = setTimeout(() => {
|
||||||
return;
|
doubleTapTimer = null;
|
||||||
|
}, 500);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
doubleTapTimer = setTimeout(() => {
|
|
||||||
doubleTapTimer = null;
|
dragHandler: function(e) {
|
||||||
}, 500);
|
if (!isDragging) return;
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let dragHandler = function(e) {
|
|
||||||
// Check if any box is being dragged
|
|
||||||
if (!isDragging) return;
|
|
||||||
|
|
||||||
// Stop propagation of the drag event to prevent other handlers
|
// Stop propagation of the drag event to prevent other handlers
|
||||||
E.stopEventPropagation();
|
E.stopEventPropagation();
|
||||||
|
|
||||||
for (let key in boxes) {
|
for (let key in boxes) {
|
||||||
if (boxes[key].selected) {
|
if (boxes[key].selected) {
|
||||||
let boxItem = boxes[key];
|
let boxItem = boxes[key];
|
||||||
calcBoxSize(boxItem);
|
calcBoxSize(boxItem);
|
||||||
let newX = boxItem.pos.x + e.dx;
|
let newX = boxItem.pos.x + e.dx;
|
||||||
let newY = boxItem.pos.y + e.dy;
|
let newY = boxItem.pos.y + e.dy;
|
||||||
|
|
||||||
if (newX - boxItem.cachedSize.width / 2 >= 0 &&
|
if (newX - boxItem.cachedSize.width / 2 >= 0 &&
|
||||||
newX + boxItem.cachedSize.width / 2 <= w &&
|
newX + boxItem.cachedSize.width / 2 <= w &&
|
||||||
newY - boxItem.cachedSize.height / 2 >= 0 &&
|
newY - boxItem.cachedSize.height / 2 >= 0 &&
|
||||||
newY + boxItem.cachedSize.height / 2 <= h) {
|
newY + boxItem.cachedSize.height / 2 <= h) {
|
||||||
boxItem.pos.x = newX;
|
boxItem.pos.x = newX;
|
||||||
boxItem.pos.y = newY;
|
boxItem.pos.y = newY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
draw();
|
||||||
|
},
|
||||||
|
|
||||||
|
stepHandler: function(up) {
|
||||||
|
if (boxes.step && !isDragging) {
|
||||||
|
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
|
||||||
|
boxes.step.cachedSize = null;
|
||||||
|
draw();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
lockHandler: function(isLocked) {
|
||||||
|
if (isLocked) {
|
||||||
|
deselectAllBoxes();
|
||||||
|
draw();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
draw();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 4. Font loading function
|
// 4. Font loading function
|
||||||
|
|
@ -396,35 +412,31 @@
|
||||||
|
|
||||||
// 10. Setup function to configure event handlers
|
// 10. Setup function to configure event handlers
|
||||||
let setup = function() {
|
let setup = function() {
|
||||||
Bangle.on('lock', function(isLocked) {
|
Bangle.on('lock', eventHandlers.lockHandler);
|
||||||
if (isLocked) {
|
Bangle.on('touch', eventHandlers.touchHandler);
|
||||||
// Screen is about to lock, deselect all boxes
|
Bangle.on('drag', eventHandlers.dragHandler);
|
||||||
deselectAllBoxes();
|
|
||||||
// Redraw to reflect changes
|
|
||||||
draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
Bangle.on('touch', touchHandler);
|
|
||||||
Bangle.on('drag', dragHandler);
|
|
||||||
|
|
||||||
if (boxes.step) {
|
if (boxes.step) {
|
||||||
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
|
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
|
||||||
|
Bangle.on('step', eventHandlers.stepHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (boxes.batt) {
|
if (boxes.batt) {
|
||||||
boxes.batt.lastLevel = E.getBattery();
|
boxes.batt.lastLevel = E.getBattery();
|
||||||
boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel);
|
boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel);
|
||||||
boxes.batt.lastUpdate = Date.now();
|
boxes.batt.lastUpdate = Date.now();
|
||||||
}
|
}
|
||||||
|
|
||||||
Bangle.setUI({
|
Bangle.setUI({
|
||||||
mode: "clock",
|
mode: "clock",
|
||||||
remove: function() {
|
remove: function() {
|
||||||
// Remove event handlers, stop draw timer, remove custom font
|
// Remove event handlers, stop draw timer, remove custom font
|
||||||
Bangle.removeListener('touch', touchHandler);
|
Bangle.removeListener('touch', eventHandlers.touchHandler);
|
||||||
Bangle.removeListener('drag', dragHandler);
|
Bangle.removeListener('drag', eventHandlers.dragHandler);
|
||||||
Bangle.removeListener('step');
|
Bangle.removeListener('lock', eventHandlers.lockHandler);
|
||||||
Bangle.removeAllListeners('lock');
|
if (boxes.step) {
|
||||||
|
Bangle.removeListener('step', eventHandlers.stepHandler);
|
||||||
|
}
|
||||||
if (drawTimeout) clearTimeout(drawTimeout);
|
if (drawTimeout) clearTimeout(drawTimeout);
|
||||||
drawTimeout = undefined;
|
drawTimeout = undefined;
|
||||||
delete Graphics.prototype.setFontBrunoAce;
|
delete Graphics.prototype.setFontBrunoAce;
|
||||||
|
|
@ -434,6 +446,7 @@
|
||||||
widgets.show();
|
widgets.show();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
loadCustomFont();
|
loadCustomFont();
|
||||||
draw();
|
draw();
|
||||||
};
|
};
|
||||||
|
|
@ -443,13 +456,4 @@
|
||||||
widgets.swipeOn();
|
widgets.swipeOn();
|
||||||
modSetColor();
|
modSetColor();
|
||||||
setup();
|
setup();
|
||||||
|
|
||||||
// Event listener for real-time step updates
|
|
||||||
Bangle.on('step', function(up) {
|
|
||||||
if (boxes.step && !isDragging) {
|
|
||||||
boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps);
|
|
||||||
boxes.step.cachedSize = null;
|
|
||||||
draw();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue