Skip to content

Commit

Permalink
Make compatible with top and bottom widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
whenlit committed Sep 7, 2024
1 parent 4bdf4a0 commit 490b165
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 46 deletions.
3 changes: 2 additions & 1 deletion apps/dutchclock/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
0.15: Fix midnight better
0.16: Fix midnight decisively
0.17: Get loadWidgets back in the right place
0.18: Move setUI and loadWidgets to initialize function
0.18: Move setUI and loadWidgets to initialize function
0.19: Make compatible with top and bottom widgets
8 changes: 6 additions & 2 deletions apps/dutchclock/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Dutch Clock
This clock shows the time, in words, the way a Dutch person would tell you what time it is. Useful when learning Dutch or pretending to know Dutch.
This clock shows the time, in words, the way a Dutch person might respond when asked what time it is. Useful when learning Dutch and/or pretending to know Dutch.

Dedicated my wife, who will sometimes insist I tell her exactly what time it says on the watch and not just an approximation.
Dedicated to my wife, who will sometimes insist I tell her exactly what time it says on the watch and not just an approximation.

## Options
- Three modes:
Expand All @@ -12,6 +12,10 @@ Dedicated my wife, who will sometimes insist I tell her exactly what time it say
- Option to show digital time at the bottom (off by default)
- Option to show the date at the bottom (on by default)

The app respects top and bottom widgets, but it gets a bit crowded when you add the time/date and you also have bottom widgets turned on.

When you turn widgets off, you can still see the top widgets by swiping down from the top.

## Screenshots
![](screenshotbangle1-2.png)
![](screenshotbangle2.png)
Expand Down
79 changes: 37 additions & 42 deletions apps/dutchclock/app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// Load libraries
const storage = require("Storage");
const locale = require('locale');
const widget_utils = require('widget_utils');

const SCREEN_WIDTH = g.getWidth();
const SCREEN_HEIGHT = g.getHeight();

const TOP_SPACING = 5;
// Define constants
const WIDGETS_HEIGHT = 20;

Check warning on line 7 in apps/dutchclock/app.js

View workflow job for this annotation

GitHub Actions / build

'WIDGETS_HEIGHT' is assigned a value but never used
const DATETIME_SPACING_HEIGHT = 5;
const TIME_HEIGHT = 10;
const DATE_HEIGHT = 10;
const BOTTOM_SPACING = 5;

const TEXT_WIDTH = SCREEN_WIDTH - 2;
const TIME_HEIGHT = 8;
const DATE_HEIGHT = 8;
const BOTTOM_SPACING = 2;

const MINS_IN_HOUR = 60;
const MINS_IN_DAY = 24 * MINS_IN_HOUR;
Expand All @@ -30,29 +26,39 @@ const settings = Object.assign(
storage.readJSON(SETTINGS_FILE, true) || {}
);

const maxFontSize = SCREEN_HEIGHT
- TOP_SPACING
- (settings.showWidgets ? WIDGETS_HEIGHT : 0)
- (settings.showDate || settings.showTime ? DATETIME_SPACING_HEIGHT : 0)
- (settings.showDate ? DATE_HEIGHT : 0)
- (settings.showTime ? TIME_HEIGHT : 0);

const X = SCREEN_WIDTH / 2;
const Y = SCREEN_HEIGHT / 2
+ TOP_SPACING / 2
+ (settings.showWidgets ? WIDGETS_HEIGHT / 2 : 0)
- (settings.showDate || settings.showTime ? DATETIME_SPACING_HEIGHT / 2 : 0)
- (settings.showDate ? DATE_HEIGHT / 2 : 0)
- (settings.showTime ? TIME_HEIGHT / 2 : 0);

// Define global variables
const textBox = {};
let date, mins;

// Define functions
function initialize() {
// Reset the state of the graphics library
g.clear(true);

// Tell Bangle this is a clock
Bangle.setUI("clock");

// Load widgets
Bangle.loadWidgets();

// Show widgets, or not
if (settings.showWidgets) {
Bangle.drawWidgets();
} else {
widget_utils.swipeOn();
}

const dateTimeHeight = (settings.showDate || settings.showTime ? DATETIME_SPACING_HEIGHT : 0)
+ (settings.showDate ? DATE_HEIGHT : 0)
+ (settings.showTime ? TIME_HEIGHT : 0);

Object.assign(textBox, {
x: Bangle.appRect.x + Bangle.appRect.w / 2,
y: Bangle.appRect.y + (Bangle.appRect.h - dateTimeHeight) / 2,
w: Bangle.appRect.w - 2,
h: Bangle.appRect.h - dateTimeHeight
});

// draw immediately at first
tick();

Expand All @@ -78,51 +84,39 @@ function tick() {
mins = m;
draw();
}

if (!settings.showWidgets) {
widget_utils.hide();
}
}

function draw() {
// work out how to display the current time
const timeLines = getTimeLines(mins);
const bottomLines = getBottomLines();

// Reset the state of the graphics library
g.clear(true);
g.reset().clearRect(Bangle.appRect);

// draw the current time (4x size 7 segment)
setFont(timeLines);

g.setFontAlign(0,0); // align center top
g.drawString(timeLines.join("\n"), X, Y, false);
g.drawString(timeLines.join("\n"), textBox.x, textBox.y, false);

if (bottomLines.length) {
// draw the time and/or date, in a normal font
g.setFont("6x8");
g.setFontAlign(0,1); // align center bottom
// pad the date - this clears the background if the date were to change length
g.drawString(bottomLines.join('\n'), SCREEN_WIDTH/2, SCREEN_HEIGHT - BOTTOM_SPACING, false);
}


if (settings.showWidgets) {
Bangle.drawWidgets();
} else {
widget_utils.hide();
g.drawString(bottomLines.join('\n'), Bangle.appRect.w / 2, Bangle.appRect.y2 - BOTTOM_SPACING, false);
}
}

function setFont(timeLines) {
const size = maxFontSize / timeLines.length;
const size = textBox.h / timeLines.length;

g.setFont("Vector", size);

let width = g.stringWidth(timeLines.join('\n'));

if (width > TEXT_WIDTH) {
g.setFont("Vector", Math.floor(size * (TEXT_WIDTH / width)));
if (width > textBox.w) {
g.setFont("Vector", Math.floor(size * (textBox.w / width)));
}
}

Expand Down Expand Up @@ -263,4 +257,5 @@ function roundTo(x) {
return n => Math.round(n / x) * x;
}

// Let's go
initialize();
2 changes: 1 addition & 1 deletion apps/dutchclock/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Dutch Clock",
"shortName":"Dutch Clock",
"icon": "app.png",
"version":"0.18",
"version":"0.19",
"description": "A clock that displays the time the way a Dutch person would respond when asked what time it is.",
"type": "clock",
"tags": "clock,dutch,text",
Expand Down

0 comments on commit 490b165

Please sign in to comment.