aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/clock.lua
blob: 10f1dd8b730021503aa6db1ef76986d563979696 (plain)
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
56
-- Clock: hour stacked over minute, weekday, slashed date.
--
-- Shape follows idea2.png; every colour comes from the palette.

local card = require 'lib.card'

local M = {}

function M.draw(cr, rect, colors)
  local inner = card.card(cr, rect, colors)

  -- Numeral size is derived from the cell, not fixed, so the same widget fills
  -- a 1x2 cell on either monitor instead of needing a per-screen constant.
  -- Two stacked numerals plus the date block: allow 40% of the height each.
  local size = math.min(inner.h * 0.40, inner.w * 0.95)

  card.font(cr, card.FONT_HEAVY, size, false)
  card.rgba(cr, colors.body)

  -- Baselines. Cairo's y is the baseline, so the first sits one cap-height
  -- down; 0.78 of the font size approximates cap height for Noto Sans and
  -- avoids measuring every frame.
  local x = inner.x
  local y1 = inner.y + size * 0.78
  -- Tight leading, as in the mockup: the numerals nearly touch.
  local y2 = y1 + size * 0.92

  card.text(cr, x, y1, os.date('%H'))
  card.text(cr, x, y2, os.date('%M'))

  -- Weekday, small caps.
  card.font(cr, card.FONT_UI, 15, true)
  card.rgba(cr, colors.body)
  local wy = y2 + 34
  card.text(cr, x, wy, os.date('%A'):upper())

  -- Date as "16 / SEP / 2026". The slashes take the dimmer label colour so the
  -- numerals read first, which is what gives the mockup's date line its rhythm.
  card.font(cr, card.FONT_UI, 14, true)
  local dy = wy + 24
  local parts = {
    { os.date('%d'), colors.value },
    { ' / ', colors.label },
    { os.date('%b'):upper(), colors.value },
    { ' / ', colors.label },
    { os.date('%Y'), colors.value },
  }
  local dx = x
  for _, p in ipairs(parts) do
    card.rgba(cr, p[2])
    card.text(cr, dx, dy, p[1])
    dx = dx + card.measure(cr, p[1])
  end
end

return M