|
1 | | -if (!require("clock_info").loadCount) { // don't load if a clock_info was already loaded |
2 | | - // Load the clock infos |
3 | | - let clockInfoItems = clock_info.load(); |
| 1 | +(() => { |
| 2 | + if (!require("clock_info").loadCount) { // don't load if a clock_info was already loaded |
| 3 | + //WIDGETS = {}; |
| 4 | + const clock_info = require("clock_info"); |
4 | 5 |
|
5 | | - let clockInfoMenu = clock_info.addInteractive(clockInfoItems, { |
6 | | - app: "widclkinfo", |
7 | | - // Add the dimensions we're rendering to here - these are used to detect taps on the clock info area |
8 | | - x: 0, |
9 | | - y: -24, // TODO how know if offscreen to start? |
10 | | - w: 72, |
11 | | - h: 24, |
12 | | - // You can add other information here you want to be passed into 'options' in 'draw' |
13 | | - // This function draws the info |
14 | | - draw: (itm, info, options) => { |
15 | | - // itm: the item containing name/hasRange/etc |
16 | | - // info: data returned from itm.get() containing text/img/etc |
17 | | - // options: options passed into addInteractive |
18 | | - clockInfoInfo = info; |
19 | | - clockInfoMenu.y = options.y; |
20 | | - if (WIDGETS["clkinfo"] && clockInfoMenu.y > -24) { |
21 | | - WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
| 6 | + WIDGETS["clkinfo"] = { |
| 7 | + area: "tl", |
| 8 | + width: 0, //this.clockInfoMenu.w, |
| 9 | + init: function() { |
| 10 | + this.width = this.clockInfoMenu.w; |
| 11 | + delete this.init; |
| 12 | + return this; |
| 13 | + }, |
| 14 | + clockInfoInfo: undefined, // defined during clockInfoMenu.draw() |
| 15 | + clockInfoMenu: clock_info.addInteractive(clock_info.load(), { |
| 16 | + app: "widclkinfo", |
| 17 | + // Add the dimensions we're rendering to here - these are used to detect taps on the clock info area |
| 18 | + x: 0, |
| 19 | + y: 0, // TODO how know if offscreen to start? |
| 20 | + w: 72, |
| 21 | + h: 24, |
| 22 | + // You can add other information here you want to be passed into 'options' in 'draw' |
| 23 | + // This function draws the info |
| 24 | + draw: (itm, info, options) => { |
| 25 | + // itm: the item containing name/hasRange/etc |
| 26 | + // info: data returned from itm.get() containing text/img/etc |
| 27 | + // options: options passed into addInteractive |
| 28 | + var wi = WIDGETS["clkinfo"]; |
| 29 | + wi.clockInfoInfo = info; |
| 30 | + wi.clockInfoMenu.y = options.y; |
| 31 | + if (WIDGETS["clkinfo"] && wi.clockInfoMenu.y > -24) { |
| 32 | + WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
| 33 | + } |
22 | 34 | } |
23 | | - } |
24 | | - }); |
25 | | - |
26 | | - let clockInfoInfo; // when clockInfoMenu.draw is called we set this up |
27 | | - let draw = function(e) { |
28 | | - clockInfoMenu.x = e.x; |
29 | | - var o = clockInfoMenu; |
30 | | - // Clear the background |
31 | | - g.reset(); |
32 | | - // indicate focus |
33 | | - if (clockInfoMenu.focus) { |
34 | | - g.setColor("#f00"); |
35 | | - } |
36 | | - g.clearRect(o.x, o.y, o.x + o.w - 1, o.y + o.h - 1); |
37 | | - |
38 | | - if (clockInfoInfo) { |
39 | | - var x = o.x; |
40 | | - if (clockInfoInfo.img) { |
41 | | - g.drawImage(clockInfoInfo.img, x, o.y); // draw the image |
42 | | - x += 24; |
| 35 | + }), |
| 36 | + draw: function(e) { |
| 37 | + this.clockInfoMenu.x = e.x; |
| 38 | + var o = this.clockInfoMenu; |
| 39 | + g.reset(); |
| 40 | + // indicate focus |
| 41 | + if (this.clockInfoMenu.focus) { |
| 42 | + g.setColor("#f00"); |
| 43 | + } |
| 44 | + g.clearRect(o.x, o.y, o.x + o.w - 1, o.y + o.h - 1); |
| 45 | + if (this.clockInfoInfo) { |
| 46 | + var x = o.x; |
| 47 | + if (this.clockInfoInfo.img) { |
| 48 | + g.drawImage(this.clockInfoInfo.img, x, o.y); // draw the image |
| 49 | + x += 24; |
| 50 | + } |
| 51 | + var availableWidth = o.x + this.clockInfoMenu.w - (x + 2); |
| 52 | + g.setFont("6x8:2").setFontAlign(-1, 0); |
| 53 | + if (g.stringWidth(this.clockInfoInfo.text) > availableWidth) |
| 54 | + g.setFont("6x8:1x2"); |
| 55 | + g.drawString(this.clockInfoInfo.text, x + 2, o.y + 12); // draw the text |
43 | 56 | } |
44 | | - var availableWidth = o.x + clockInfoMenu.w - (x + 2); |
45 | | - g.setFont("6x8:2").setFontAlign(-1, 0); |
46 | | - if (g.stringWidth(clockInfoInfo.text) > availableWidth) |
47 | | - g.setFont("6x8:1x2"); |
48 | | - g.drawString(clockInfoInfo.text, x + 2, o.y + 12); // draw the text |
49 | 57 | } |
50 | | - }; |
51 | | - |
52 | | - // The actual widget we're displaying |
53 | | - WIDGETS["clkinfo"] = { |
54 | | - area: "tl", |
55 | | - width: clockInfoMenu.w, |
56 | | - draw: draw |
57 | | - }; |
| 58 | + }.init(); |
58 | 59 |
|
59 | 60 | Bangle.on("widgets-start-show", () => { |
60 | | - clockInfoMenu.y = 0; |
61 | | - if (WIDGETS["clkinfo"]) { |
62 | | - WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
| 61 | + var wi = WIDGETS["clkinfo"]; |
| 62 | + if (wi) { |
| 63 | + wi.clockInfoMenu.y = 0; |
| 64 | + wi.draw(wi); |
63 | 65 | } |
64 | | - }) |
| 66 | + }); |
65 | 67 |
|
66 | 68 | Bangle.on("widgets-shown", () => { |
67 | | - clockInfoMenu.y = 0; |
68 | | - if (WIDGETS["clkinfo"]) { |
69 | | - WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
| 69 | + var wi = WIDGETS["clkinfo"]; |
| 70 | + if (wi) { |
| 71 | + wi.clockInfoMenu.y = 0; |
| 72 | + wi.draw(wi); |
70 | 73 | } |
71 | 74 | }); |
72 | 75 |
|
73 | 76 | Bangle.on("widgets-start-hide", () => { |
74 | | - clockInfoMenu.y = -24; |
75 | | - if (WIDGETS["clkinfo"]) { |
76 | | - WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
77 | | - } |
78 | | - if (clockInfoMenu.focus) { |
79 | | - clockInfoMenu.blur(); |
| 77 | + var wi = WIDGETS["clkinfo"]; |
| 78 | + if (wi) { |
| 79 | + if (wi.clockInfoMenu.focus) wi.clockInfoMenu.blur(); |
| 80 | + wi.clockInfoMenu.y = -24; |
| 81 | + wi.draw(wi); |
80 | 82 | } |
81 | 83 | }); |
82 | 84 |
|
83 | 85 | Bangle.on("widgets-hidden", () => { |
84 | | - clockInfoMenu.y = -24; |
85 | | - if (WIDGETS["clkinfo"]) { |
86 | | - WIDGETS["clkinfo"].draw(WIDGETS["clkinfo"]); |
87 | | - } |
88 | | - // check here too in case widget_utils.hide() is called |
89 | | - if (clockInfoMenu.focus) { |
90 | | - clockInfoMenu.blur(); |
| 86 | + var wi = WIDGETS["clkinfo"]; |
| 87 | + if (wi) { |
| 88 | + if (wi.clockInfoMenu.focus) wi.clockInfoMenu.blur(); |
| 89 | + wi.clockInfoMenu.y = -24; |
| 90 | + wi.draw(wi); |
91 | 91 | } |
92 | 92 | }); |
93 | | -} |
| 93 | +}})() |
| 94 | + |
| 95 | +/* |
| 96 | +const widget_utils = require("widget_utils"); |
| 97 | +g.clear(); |
| 98 | +Bangle.loadWidgets(); |
| 99 | +widget_utils.swipeOn(); |
| 100 | +Bangle.drawWidgets(); |
| 101 | +*/ |
0 commit comments