1 module w95.clock;
2 
3 import fluid;
4 import raylib;
5 import std.datetime;
6 
7 import w95.theme;
8 import w95.image_button;
9 
10 @safe:
11 
12 alias clock = simpleConstructor!Clock;
13 
14 /// Displays a Windows 95 toolbar clock.
15 class Clock : Label {
16 
17     public {
18 
19         immutable TimeZone timezone;
20 
21     }
22 
23     private {
24 
25         int hour;
26         int minute;
27 
28     }
29 
30     /// Create a clock, and set the timezone
31     this(immutable TimeZone tz = LocalTime()) {
32 
33         super("12:00 AM");
34         this.timezone = tz;
35         updateClock();
36 
37     }
38 
39     override void drawImpl(Rectangle outer, Rectangle inner) {
40 
41         // Place the text in the center
42         const size = text.size;
43         inner = Rectangle(
44             (inner.center - size / 2).tupleof,
45             size.tupleof,    
46         );
47 
48         super.drawImpl(outer, inner);
49         updateClock();
50 
51     }
52 
53     /// Update the time.
54     private void updateClock() {
55 
56         import std.format : format;
57 
58         const time = std.datetime.Clock.currTime(timezone);
59 
60         const newHour = time.hour;
61         const newMinute = time.minute;
62 
63         // Do nothing if nothing has changed
64         if (newHour == hour && newMinute == minute) return;
65 
66         // AM time
67         const period = newHour < 12 ? "AM" : "PM";
68         const displayHour = newHour % 12 == 0
69             ? 12
70             : newHour % 12;
71 
72         text = format!"%s:%02d %s"(displayHour, newMinute, period);
73 
74     }
75 
76 }