Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/clock/ClockPanel.qml

Raw
import QtQuick
import "../.."
import "caldav.js" as Ical

Column {
    id: root

    required property Theme theme
    readonly property Flickable scrollView: agendaFlick
    required property Time timeService
    required property CalDav caldavService

    readonly property date localNow: root.timeService.now
    // Bound to the day, not the second: the grid is rebuilt at midnight only.
    readonly property var days: {
        const [year, month, day] = todayIso.split("-").map(Number);
        return [].concat(...root.timeService.monthGrid(year, month, day).weeks);
    }

    signal configureRequested
    // The card fits the local calendar, agenda and Config link; the entry
    // height caps it (FeatureEntry.qml).
    readonly property real naturalHeight: agendaFrame.y + agendaCol.height + configure.height + spacing
    readonly property bool loading: root.caldavService.status === "loading"
    readonly property bool offline: root.caldavService.stale || root.caldavService.status === "offline"
    // The agenda's sync state in words: offline data names its last good sync;
    // a first load shows as pending, a background refresh of shown data does not.
    readonly property string syncNotice: {
        if (root.offline) {
            const last = root.caldavService.lastSyncMs;
            return (root.loading ? "retrying" : "calendar offline") + " · " + (last > 0 ? "last sync " + root.syncStamp(new Date(last)) : "never synced");
        }
        return root.loading && root.caldavEvents.length === 0 ? "loading events" : "";
    }

    function syncStamp(value: date): string {
        const time = root.timeService.formatClock(value).display;
        return root.timeService.isoDay(value) === root.todayIso ? time : root.timeService.formatDate(value).line + " " + time;
    }

    // grid sizing: big square day cells, grid centered in the popup
    readonly property int gridSpacing: 6
    readonly property int cellW: 48
    readonly property int cellH: 48
    readonly property real pitchX: cellW + gridSpacing
    readonly property real pitchY: cellH + gridSpacing
    readonly property real gridW: cellW * 7 + gridSpacing * 6

    // iso "YYYY-MM-DD" -> array of single-day markers {color, allDay}.
    // Multi-day events (coveredDates>1) are drawn as bands, not markers.
    readonly property var caldavEvents: root.caldavService.events
    readonly property var eventsByDate: {
        const map = {};
        const evs = caldavEvents;
        for (let i = 0; i < evs.length; i++) {
            const ev = evs[i];
            const dates = Ical.coveredDates(ev);
            if (dates.length !== 1)
                continue;
            const iso = dates[0];
            (map[iso] || (map[iso] = [])).push({
                "color": ev.color,
                "allDay": ev.allDay || ev.kind === "task"
            });
        }
        // all-day pills before timed dots within each day
        for (const k in map)
            map[k].sort(function (a, b) {
                return (b.allDay ? 1 : 0) - (a.allDay ? 1 : 0);
            });
        return map;
    }

    // multi-day events as week-split band segments from the grid's first cell
    readonly property var bandLayout: Ical.buildSegments(caldavEvents, root.days[0].iso)

    readonly property string todayIso: root.timeService.isoDay(root.localNow)
    // today's events + tasks, sorted: all-day/untimed first, then by start time
    readonly property var todayItems: {
        const evs = caldavEvents;
        const out = [];
        for (let i = 0; i < evs.length; i++) {
            if (Ical.coveredDates(evs[i]).indexOf(todayIso) >= 0)
                out.push(evs[i]);
        }
        out.sort(function (a, b) {
            const ta = (a.allDay || a.startMs == null) ? -1 : a.startMs;
            const tb = (b.allDay || b.startMs == null) ? -1 : b.startMs;
            return ta - tb;
        });
        return out;
    }

    spacing: 8

    NuguText {
        width: parent ? parent.width : 0
        horizontalAlignment: Text.AlignHCenter
        theme: root.theme
        variant: "title"
        text: root.timeService.formatClock(root.timeService.now).statusDisplay
    }

    NuguText {
        width: parent ? parent.width : 0
        horizontalAlignment: Text.AlignHCenter
        theme: root.theme
        variant: "meta"
        text: root.timeService.formatDate(root.timeService.now).line
    }

    // centered calendar block: weekday header + month grid + bands
    Item {
        width: parent ? parent.width : 0
        height: calStack.height

        Column {
            id: calStack

            anchors.horizontalCenter: parent.horizontalCenter
            spacing: root.gridSpacing

            // single-letter weekday header, Monday-first; weekend dimmed at legible contrast
            Row {
                spacing: root.gridSpacing

                Repeater {
                    model: ["m", "t", "w", "t", "f", "s", "s"]

                    Item {
                        width: root.cellW
                        height: hdr.height

                        NuguText {
                            id: hdr

                            anchors.horizontalCenter: parent.horizontalCenter
                            theme: root.theme
                            variant: "meta"
                            text: modelData
                            color: root.theme ? (index >= 5 ? root.theme.ui.minor : root.theme.ui.normal) : "gray"
                        }
                    }
                }
            }

            Item {
                width: monthGrid.width
                height: monthGrid.height

                Grid {
                    id: monthGrid

                    objectName: "calendar-grid"
                    columns: 7
                    spacing: root.gridSpacing

                    Repeater {
                        model: root.days

                        Item {
                            id: cell

                            readonly property var markers: root.eventsByDate[modelData.iso] || []

                            width: root.cellW
                            height: root.cellH

                            // today: filled accent circle behind the number
                            Rectangle {
                                visible: !!modelData.today
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.verticalCenterOffset: -5
                                width: 34
                                height: 34
                                radius: width / 2
                                color: root.theme ? root.theme.ui.accent : "transparent"
                            }

                            NuguText {
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.verticalCenter: parent.verticalCenter
                                anchors.verticalCenterOffset: -5
                                theme: root.theme
                                variant: "body"
                                text: modelData.day ? String(modelData.day) : ""
                                // adjacent-month days subdued, still above 4.5:1 on the card
                                color: !root.theme ? "white" : (modelData.today ? root.theme.content.backdrop : (modelData.inMonth ? root.theme.content.normal : root.theme.ui.minor))
                            }

                            // marker strip: all-day pills + timed dots, max 3 then "+"
                            Row {
                                anchors.horizontalCenter: parent.horizontalCenter
                                anchors.bottom: parent.bottom
                                anchors.bottomMargin: 7
                                spacing: 3

                                Repeater {
                                    model: Math.min(cell.markers.length, 3)

                                    Rectangle {
                                        readonly property var m: cell.markers[index]
                                        readonly property bool overflow: index === 2 && cell.markers.length > 3

                                        width: overflow ? 5 : (m.allDay ? 8 : 4)
                                        height: 3
                                        radius: height / 2
                                        color: overflow ? (root.theme ? root.theme.content.minor : "gray") : m.color
                                    }
                                }
                            }
                        }
                    }
                }

                // multi-day bands sit behind day numbers and dots.
                // pitch derives from the live cell size; bands sit near the
                // cell bottom, stacking upward per lane.
                Item {
                    anchors.fill: monthGrid
                    z: -1

                    Repeater {
                        model: root.bandLayout.segments

                        Rectangle {
                            readonly property var seg: modelData

                            x: seg.colStart * root.pitchX
                            y: seg.row * root.pitchY + root.cellH - 17 - seg.lane * 9
                            width: (seg.colEnd - seg.colStart + 1) * root.pitchX - root.gridSpacing
                            height: 6
                            color: Qt.alpha(seg.color, 0.9)
                            topLeftRadius: seg.roundLeft ? 3 : 0
                            bottomLeftRadius: seg.roundLeft ? 3 : 0
                            topRightRadius: seg.roundRight ? 3 : 0
                            bottomRightRadius: seg.roundRight ? 3 : 0
                        }
                    }
                }
            }
        }
    }

    // today's agenda: events + tasks, ○ for open tasks, ✓ for done; centered
    Item {
        id: agendaFrame
        width: parent ? parent.width : 0
        // A tight popup still shows 70 px of a longer agenda.
        height: Math.max(Math.min(70, agendaCol.height), parent ? parent.height - y - configure.height - root.spacing : agendaCol.height)
        clip: true

        Flickable {
            id: agendaFlick

            anchors.fill: parent
            contentWidth: root.gridW
            contentHeight: agendaCol.height
            interactive: contentHeight > height
            boundsBehavior: Flickable.StopAtBounds

            Column {
                id: agendaCol

                x: Math.round((agendaFrame.width - width) / 2)
                width: root.gridW
                spacing: 4

                // offline or first load, in words, with a retry while offline
                Row {
                    visible: root.syncNotice.length > 0
                    spacing: 8

                    NuguText {
                        anchors.verticalCenter: parent.verticalCenter
                        theme: root.theme
                        variant: "meta"
                        text: root.syncNotice
                        color: root.offline && root.theme ? root.theme.warning : (root.theme ? root.theme.ui.normal : "white")
                    }

                    PopupButton {
                        visible: root.offline
                        enabled: !root.loading
                        theme: root.theme
                        height: 28
                        width: implicitWidth
                        text: "retry"
                        onClicked: root.caldavService.refresh()
                    }
                }

                Repeater {
                    model: root.todayItems

                    Row {
                        spacing: 6

                        // centred on the first line of a wrapped summary
                        Rectangle {
                            y: Math.round((summary.implicitHeight / Math.max(1, summary.lineCount) - height) / 2)
                            width: 6
                            height: 6
                            radius: width / 2
                            color: modelData.color
                        }

                        NuguText {
                            id: summary

                            width: root.gridW - 12
                            wrapMode: Text.Wrap
                            maximumLineCount: 2
                            theme: root.theme
                            variant: "body"
                            userText: true
                            text: {
                                const ev = modelData;
                                const mark = ev.kind === "task" ? (ev.done ? "✓ " : "○ ") : "";
                                const time = !ev.allDay && ev.startMs != null ? root.timeService.formatClock(new Date(ev.startMs)).display + " " : "";
                                return time + mark + (ev.summary || "untitled");
                            }
                        }
                    }
                }

                NuguText {
                    visible: root.todayItems.length === 0 && root.syncNotice.length === 0
                    theme: root.theme
                    variant: "meta"
                    text: "no events today"
                }
            }
        }

        Rectangle {
            visible: agendaFlick.interactive
            anchors.right: parent.right
            anchors.rightMargin: 2
            y: agendaFlick.contentY / Math.max(1, agendaFlick.contentHeight) * parent.height
            width: 3
            height: Math.max(18, parent.height * Math.min(1, parent.height / Math.max(parent.height, agendaFlick.contentHeight)))
            radius: width / 2
            color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.55) : "white"
        }
    }

    PopupButton {
        id: configure
        theme: root.theme
        text: "configure calendar"
        onClicked: root.configureRequested()
    }
}