~andreafeletto/levee

ref: e0ba69e3fef33bbfbfd5207ad1c1546fb25cebc5 levee/src/clock.zig -rw-r--r-- 1.8 KiB
e0ba69e3Andrea Feletto add clock 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const std = @import("std");

const datetime = @import("datetime").datetime;
const pixman = @import("pixman");
const fcft = @import("fcft");

const Wayland = @import("wayland.zig").Wayland;
const State = @import("main.zig").State;

pub const Clock = struct {
    thread: ?*std.Thread = null,
    stop: bool = false,

    pub fn render(self: *const Clock, pix: *pixman.Image, state: *State) !void {
        const width: i16 = 100;
        const height = @intCast(i16, state.wayland.height);
        const offset = @divFloor(@intCast(i16, state.wayland.width) - width, 2);

        var now = datetime.Datetime.now();
        const str = try now.formatHttp(state.allocator);
        std.log.info("{s}", .{ str });

        // const outer = [_]pixman.Rectangle16{
        //     .{ .x = offset, .y = 0, .width = width, .height = height },
        // };
        // _ = pixman.Image.fillRectangles(.over, pix, state.foreground, 1, &outer);

        var x = offset;
        for (str) |char, i| {
            var color = pixman.Image.createSolidFill(state.foreground).?;
            const glyph = try fcft.Glyph.rasterize(state.font, char, .default);
            // x += @intCast(i16, glyph.advance.x);
            x += 14;
            //const y = @divFloor(height - @intCast(i16, glyph.height), 2);
            const y = 10;
            std.log.info("{d}, {d}", .{ x, y });
            pixman.Image.composite32(
                .over, color, glyph.pix, pix,
                0, 0, 0, 0, x, y,
                glyph.width, glyph.height
            );
        }
    }

    pub fn start(self: *Clock, state: *State) !void {
        self.thread = try std.Thread.spawn(repeatDraw, state);
    }
};

fn repeatDraw(state: *State) void {
    while (!state.clock.stop) {
        state.draw(state);
        std.time.sleep(1 * std.time.ns_per_s);
    }
}