|
1 | 1 | { |
2 | 2 | // 1. Module dependencies and initial configurations |
3 | 3 | let background = require("clockbg"); |
| 4 | + background.load(); // reload if we fast loaded into here |
4 | 5 | let storage = require("Storage"); |
5 | 6 | let locale = require("locale"); |
6 | 7 | let widgets = require("widget_utils"); |
|
27 | 28 | let touchHandler = function(zone, e) { |
28 | 29 | let boxTouched = false; |
29 | 30 | let touchedBox = null; |
30 | | - |
| 31 | + |
31 | 32 | for (let boxKey in boxes) { |
32 | 33 | if (touchInText(e, boxes[boxKey])) { |
33 | 34 | touchedBox = boxKey; |
34 | 35 | boxTouched = true; |
35 | 36 | break; |
36 | 37 | } |
37 | 38 | } |
38 | | - |
| 39 | + |
39 | 40 | if (boxTouched) { |
40 | 41 | // Toggle the selected state of the touched box |
41 | 42 | boxes[touchedBox].selected = !boxes[touchedBox].selected; |
42 | | - |
| 43 | + |
43 | 44 | // Update isDragging based on whether any box is selected |
44 | 45 | isDragging = Object.values(boxes).some(box => box.selected); |
45 | | - |
| 46 | + |
46 | 47 | if (isDragging) { |
47 | 48 | widgets.hide(); |
48 | 49 | } else { |
|
52 | 53 | // If tapped outside any box, deselect all boxes |
53 | 54 | deselectAllBoxes(); |
54 | 55 | } |
55 | | - |
| 56 | + |
56 | 57 | // Always redraw after a touch event |
57 | 58 | draw(); |
58 | | - |
| 59 | + |
59 | 60 | // Handle double tap for saving |
60 | 61 | if (!boxTouched && !isDragging) { |
61 | 62 | if (doubleTapTimer) { |
|
69 | 70 | displaySaveIcon(); |
70 | 71 | return; |
71 | 72 | } |
72 | | - |
| 73 | + |
73 | 74 | doubleTapTimer = setTimeout(() => { |
74 | 75 | doubleTapTimer = null; |
75 | 76 | }, 500); |
76 | 77 | } |
77 | 78 | }; |
78 | | - |
| 79 | + |
79 | 80 | let dragHandler = function(e) { |
80 | 81 | if (!isDragging) return; |
81 | | - |
| 82 | + |
82 | 83 | // Stop propagation of the drag event to prevent other handlers |
83 | 84 | E.stopEventPropagation(); |
84 | | - |
| 85 | + |
85 | 86 | for (let key in boxes) { |
86 | 87 | if (boxes[key].selected) { |
87 | 88 | let boxItem = boxes[key]; |
88 | 89 | calcBoxSize(boxItem); |
89 | 90 | let newX = boxItem.pos.x + e.dx; |
90 | 91 | let newY = boxItem.pos.y + e.dy; |
91 | | - |
| 92 | + |
92 | 93 | if (newX - boxItem.cachedSize.width / 2 >= 0 && |
93 | 94 | newX + boxItem.cachedSize.width / 2 <= w && |
94 | 95 | newY - boxItem.cachedSize.height / 2 >= 0 && |
|
98 | 99 | } |
99 | 100 | } |
100 | 101 | } |
101 | | - |
| 102 | + |
102 | 103 | draw(); |
103 | 104 | }; |
104 | | - |
| 105 | + |
105 | 106 | let stepHandler = function(up) { |
106 | 107 | if (boxes.step && !isDragging) { |
107 | 108 | boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps); |
108 | 109 | boxes.step.cachedSize = null; |
109 | 110 | draw(); |
110 | 111 | } |
111 | 112 | }; |
112 | | - |
| 113 | + |
113 | 114 | let lockHandler = function(isLocked) { |
114 | 115 | if (isLocked) { |
115 | 116 | deselectAllBoxes(); |
|
211 | 212 | const day = date.getDate(); |
212 | 213 | const month = shortMonth ? locale.month(date, 1) : locale.month(date, 0); |
213 | 214 | const year = date.getFullYear(); |
214 | | - |
| 215 | + |
215 | 216 | const getSuffix = (day) => { |
216 | 217 | if (day >= 11 && day <= 13) return 'th'; |
217 | 218 | const lastDigit = day % 10; |
|
222 | 223 | default: return 'th'; |
223 | 224 | } |
224 | 225 | }; |
225 | | - |
| 226 | + |
226 | 227 | const dayStr = disableSuffix ? day : `${day}${getSuffix(day)}`; |
227 | 228 | return `${month} ${dayStr}${short ? '' : `, ${year}`}`; // not including year for short version |
228 | 229 | }; |
|
323 | 324 |
|
324 | 325 | let draw = function() { |
325 | 326 | g.clear(); |
326 | | - |
| 327 | + |
327 | 328 | // Always draw backgrounds full screen |
328 | 329 | if (bgImage) { // Check for bg in boxclk config |
329 | 330 | g.drawImage(bgImage, 0, 0); |
330 | 331 | } else { // Otherwise use clockbg module |
331 | 332 | background.fillRect(0, 0, g.getWidth(), g.getHeight()); |
332 | 333 | } |
333 | | - |
| 334 | + |
334 | 335 | if (!isDragging) { |
335 | 336 | updateBoxData(); |
336 | 337 | } |
337 | | - |
| 338 | + |
338 | 339 | for (let boxKey in boxes) { |
339 | 340 | let boxItem = boxes[boxKey]; |
340 | | - |
| 341 | + |
341 | 342 | // Set font and alignment for each box individually |
342 | 343 | g.setFont(boxItem.font, boxItem.fontSize); |
343 | 344 | g.setFontAlign(0, 0); |
344 | | - |
| 345 | + |
345 | 346 | calcBoxSize(boxItem); |
346 | | - |
| 347 | + |
347 | 348 | const pos = calcBoxPos(boxItem); |
348 | | - |
| 349 | + |
349 | 350 | if (boxItem.selected) { |
350 | 351 | g.setColor(boxItem.border); |
351 | 352 | g.drawRect(pos.x1, pos.y1, pos.x2, pos.y2); |
352 | 353 | } |
353 | | - |
| 354 | + |
354 | 355 | g.drawString( |
355 | 356 | boxItem, |
356 | 357 | boxItem.string, |
357 | 358 | boxItem.pos.x + boxItem.xOffset, |
358 | 359 | boxItem.pos.y + boxItem.yOffset |
359 | 360 | ); |
360 | 361 | } |
361 | | - |
| 362 | + |
362 | 363 | if (!isDragging) { |
363 | 364 | if (drawTimeout) clearTimeout(drawTimeout); |
364 | 365 | let updateInterval = boxes.time && !isBool(boxes.time.short, true) ? 1000 : 60000 - (Date.now() % 60000); |
|
382 | 383 | if (boxItem.cachedSize) { |
383 | 384 | return boxItem.cachedSize; |
384 | 385 | } |
385 | | - |
| 386 | + |
386 | 387 | g.setFont(boxItem.font, boxItem.fontSize); |
387 | 388 | g.setFontAlign(0, 0); |
388 | | - |
| 389 | + |
389 | 390 | let strWidth = g.stringWidth(boxItem.string) + 2 * boxItem.outline; |
390 | 391 | let fontHeight = g.getFontHeight() + 2 * boxItem.outline; |
391 | 392 | let totalWidth = strWidth + 2 * boxItem.xPadding; |
392 | 393 | let totalHeight = fontHeight + 2 * boxItem.yPadding; |
393 | | - |
| 394 | + |
394 | 395 | boxItem.cachedSize = { |
395 | 396 | width: totalWidth, |
396 | 397 | height: totalHeight |
397 | 398 | }; |
398 | | - |
| 399 | + |
399 | 400 | return boxItem.cachedSize; |
400 | 401 | }; |
401 | 402 |
|
|
424 | 425 | Bangle.on('lock', lockHandler); |
425 | 426 | Bangle.on('touch', touchHandler); |
426 | 427 | Bangle.on('drag', dragHandler); |
427 | | - |
| 428 | + |
428 | 429 | if (boxes.step) { |
429 | 430 | boxes.step.string = formatStr(boxes.step, Bangle.getHealthStatus("day").steps); |
430 | 431 | Bangle.on('step', stepHandler); |
431 | 432 | } |
432 | | - |
| 433 | + |
433 | 434 | if (boxes.batt) { |
434 | 435 | boxes.batt.lastLevel = E.getBattery(); |
435 | 436 | boxes.batt.string = formatStr(boxes.batt, boxes.batt.lastLevel); |
436 | 437 | boxes.batt.lastUpdate = Date.now(); |
437 | 438 | } |
438 | | - |
| 439 | + |
439 | 440 | Bangle.setUI({ |
440 | 441 | mode: "clock", |
441 | 442 | remove: function() { |
|
448 | 449 | } |
449 | 450 | if (drawTimeout) clearTimeout(drawTimeout); |
450 | 451 | drawTimeout = undefined; |
| 452 | + background.unload(); // free memory from background |
451 | 453 | delete Graphics.prototype.setFontBrunoAce; |
452 | 454 | // Restore original drawString function (no outlines) |
453 | 455 | g.drawString = g_drawString; |
454 | 456 | restoreSetColor(); |
455 | 457 | widgets.show(); |
456 | 458 | } |
457 | 459 | }); |
458 | | - |
| 460 | + |
459 | 461 | loadCustomFont(); |
460 | 462 | draw(); |
461 | 463 | }; |
|
0 commit comments