Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/clock/Time.qml

Raw
import QtQuick
import Quickshell

Item {
    id: root

    readonly property date now: clock.date
    // Seconds only while the clock popup shows them (the registry sets this on
    // opening and dismissal); otherwise the bar's minutes need one tick a minute.
    property bool secondsShown: false
    readonly property string compact: formatClock(now).display
    // English and lowercase on every host locale, like every shell label.
    readonly property var weekdays: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
    readonly property var monthNames: ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]

    function pad(value) {
        return value < 10 ? `0${value}` : `${value}`;
    }

    function formatClock(value) {
        const hour = value.getHours();
        const minute = value.getMinutes();
        const second = value.getSeconds();
        return {
            "hour": hour,
            "minute": minute,
            "second": second,
            "display": `${pad(hour)}:${pad(minute)}`,
            "statusDisplay": `${pad(hour)}:${pad(minute)}:${pad(second)}`
        };
    }

    function formatDate(value) {
        const year = value.getFullYear();
        const month = value.getMonth() + 1;
        const day = value.getDate();
        const weekday = weekdays[value.getDay()];
        const monthName = monthNames[month - 1];
        return {
            "year": year,
            "month": month,
            "day": day,
            "weekday": weekday,
            "monthName": monthName,
            "isoDay": isoDay(value),
            "line": `${weekday} ${day} ${monthName}`
        };
    }

    function isoDay(value) {
        return `${value.getFullYear()}-${pad(value.getMonth() + 1)}-${pad(value.getDate())}`;
    }

    // Monday-first six-week grid; selectedDay counts as today.
    function monthGrid(year, month, selectedDay) {
        const first = new Date(year, month - 1, 1);
        const mondayOffset = (first.getDay() + 6) % 7;
        const cursor = new Date(first.getFullYear(), first.getMonth(), first.getDate() - mondayOffset);
        const todayIso = `${year}-${pad(month)}-${pad(selectedDay)}`;
        const weeks = [];
        for (let week = 0; week < 6; week++) {
            const days = [];
            for (let day = 0; day < 7; day++) {
                const current = new Date(cursor.getTime());
                const inMonth = current.getMonth() === month - 1;
                const currentDay = current.getDate();
                const currentIso = isoDay(current);
                days.push({
                    "iso": currentIso,
                    "day": currentDay,
                    "inMonth": inMonth,
                    "selected": inMonth && currentDay === selectedDay,
                    "today": currentIso === todayIso
                });
                cursor.setDate(cursor.getDate() + 1);
            }
            weeks.push(days);
        }
        return {
            "year": year,
            "month": month,
            "selectedDay": selectedDay,
            "weekStartsOn": "Mon",
            "weeks": weeks
        };
    }

    SystemClock {
        id: clock

        precision: root.secondsShown ? SystemClock.Seconds : SystemClock.Minutes
    }
}