Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Calendar gets its four scopes, and they are real views
Yashwanth Kamireddifbbaae179e8d
diff --git a/.uiverify/assert.mjs b/.uiverify/assert.mjsindex 6152f27..0ca9865 100644--- a/.uiverify/assert.mjs+++ b/.uiverify/assert.mjs@@ -69,5 +69,49 @@ const collapsed = toggle?.includes("Show") && count === 11;const selectsBuilt = selected?.trim() === "Calendar";console.log(collapsed ? "PASS collapsed by default" : "FAIL not collapsed by default");console.log(selectsBuilt ? "PASS opens on a built screen" : `FAIL opened on ${selected}`);++// ── Calendar's four scopes each render their own view ─────────────────────────────────+// A scope that only changed a label would pass a screenshot; this checks it changed the view.+let failures = 0;+{+ const page = await ctx.newPage();+ await page.goto("http://localhost:4173", { waitUntil: "networkidle" });+ await page.getByRole("button", { name: "Calendar" }).click();+ await page.waitForSelector(".cal-grid");++ const scopes = [+ ["Day", ".cal-hours.is-day"],+ ["Week", ".cal-hours"],+ ["Year", ".cal-year"],+ ["Month", ".cal-grid"],+ ];+ for (const [label, selector] of scopes) {+ await page.getByRole("button", { name: label, exact: true }).click();+ await page.waitForSelector(selector, { timeout: 4000 });+ const shown = await page.locator(selector).count();+ console.log(shown > 0 ? `PASS ${label} renders ${selector}` : `FAIL ${label} did not render ${selector}`);+ if (shown === 0) failures++;+ }++ // Week shows seven day columns; Day shows one. A label change alone would not.+ await page.getByRole("button", { name: "Week", exact: true }).click();+ await page.waitForSelector(".cal-hours");+ const week = await page.locator(".cal-col-head").count();+ console.log(week === 7 ? "PASS Week shows seven days" : `FAIL Week showed ${week} columns`);+ if (week !== 7) failures++;++ await page.getByRole("button", { name: "Day", exact: true }).click();+ const day = await page.locator(".cal-col-head").count();+ console.log(day === 1 ? "PASS Day shows one" : `FAIL Day showed ${day} columns`);+ if (day !== 1) failures++;++ await page.getByRole("button", { name: "Year", exact: true }).click();+ await page.waitForSelector(".cal-year");+ const months = await page.locator(".cal-mini").count();+ console.log(months === 12 ? "PASS Year shows twelve months" : `FAIL Year showed ${months} months`);+ if (months !== 12) failures++;+ await page.close();+}+await b.close();-process.exitCode = collapsed && selectsBuilt ? 0 : 1;+process.exitCode = collapsed && selectsBuilt && failures === 0 ? 0 : 1;diff --git a/.uiverify/keyboard.mjs b/.uiverify/keyboard.mjsindex 87d893e..18c1117 100644--- a/.uiverify/keyboard.mjs+++ b/.uiverify/keyboard.mjs@@ -182,6 +182,10 @@ const focused = (page) =>await page.getByRole("button", { name: "Calendar" }).click();await page.waitForSelector(".cal-grid");+ // Calendar opens on the month, which is the view the grid checks below belong to.+ const scope = await page.locator('.segment[aria-pressed="true"]').innerText();+ check("Calendar opens on the month", scope === "Month", scope);+const stops = await page.evaluate(() => document.querySelectorAll('.cal-grid button[tabindex="0"]').length);diff --git a/src/screens/CalendarView.tsx b/src/screens/CalendarView.tsxindex c9e41c2..2402922 100644--- a/src/screens/CalendarView.tsx+++ b/src/screens/CalendarView.tsx@@ -1,4 +1,4 @@-import { useCallback, useEffect, useMemo, useRef, useState } from "react";+import { Fragment, useCallback, useEffect, useMemo, useRef, useState } from "react";import { api, messageOf, type Calendar, type CalendarEvent, type Reminder } from "../lib/ipc";import { useSync } from "../lib/sync";import { Picker } from "../components/Picker";@@ -84,6 +84,68 @@ function draftFrom(event: CalendarEvent): EventDraft {};}+/** The rows of a day or week grid. Apple shows the full twenty-four. */+const HOURS = Array.from({ length: 24 }, (_, i) => i);++/** How tall one hour is in the day and week grids. */+const HOUR_HEIGHT = 44;++/**+ * Where a timed event sits on the hour grid.+ *+ * The grid has a header row, an all-day row and then one row per hour, so an event is+ * placed against the grid's own tracks rather than by measuring pixels — that way it stays+ * put when the window resizes.+ */+function placement(+ event: CalendarEvent,+ day: Date,+ column: number+): React.CSSProperties {+ const dayStart = new Date(day.getFullYear(), day.getMonth(), day.getDate()).getTime() / 1000;+ const from = Math.max(event.startsAt, dayStart);+ const to = Math.min(event.endsAt, dayStart + 86_400);+ const minutesIn = (from - dayStart) / 60;+ // A zero-length event still has to be readable, so nothing is thinner than a quarter hour.+ const minutesLong = Math.max((to - from) / 60, 15);+ return {+ gridColumn: column + 2,+ // Rows: 1 is the column head, 2 is all-day, so hour zero starts at 3.+ gridRow: "3 / -1",+ marginTop: `${(minutesIn / 60) * HOUR_HEIGHT}px`,+ height: `${(minutesLong / 60) * HOUR_HEIGHT}px`,+ };+}++/** Apple's four scopes, in Apple's order. */+const VIEWS = [+ { key: "day", label: "Day" },+ { key: "week", label: "Week" },+ { key: "month", label: "Month" },+ { key: "year", label: "Year" },+] as const;++type View = (typeof VIEWS)[number]["key"];++const midnight = (d: Date) => new Date(d.getFullYear(), d.getMonth(), d.getDate());++/** The Sunday on or before `d`, which is where Apple starts a week. */+const startOfWeek = (d: Date) => {+ const start = midnight(d);+ start.setDate(start.getDate() - start.getDay());+ return start;+};++/** Move a date by one unit of `view`. */+function step(from: Date, view: View, by: number): Date {+ const d = new Date(from);+ if (view === "day") d.setDate(d.getDate() + by);+ else if (view === "week") d.setDate(d.getDate() + by * 7);+ else if (view === "month") d.setMonth(d.getMonth() + by);+ else d.setFullYear(d.getFullYear() + by);+ return d;+}+/** The 42 days a month grid shows: the weeks containing the first and last of the month. */function gridDays(anchor: Date): Date[] {const first = new Date(anchor.getFullYear(), anchor.getMonth(), 1);@@ -109,18 +171,41 @@ export function CalendarView() {const { revision } = useSync();/** The day whose contents the inspector shows. Defaults to today. */const [selected, setSelected] = useState<Date>(() => new Date(today));+ /** Day, week, month or year — the scope everything else follows. */+ const [view, setView] = useState<View>("month");/** The event being written, or null when the inspector is just listing. */const [draft, setDraft] = useState<EventDraft | null>(null);const [busy, setBusy] = useState(false);const days = useMemo(() => gridDays(anchor), [anchor]);- const range = useMemo(- () => ({- from: startOfDay(days[0]),- to: startOfDay(days[41]) + 86_400,- }),- [days]- );++ /** The seven days of the week `anchor` falls in. */+ const weekDays = useMemo(() => {+ const start = startOfWeek(anchor);+ return Array.from({ length: 7 }, (_, i) => {+ const d = new Date(start);+ d.setDate(start.getDate() + i);+ return d;+ });+ }, [anchor]);++ // Each view asks Apple for exactly what it shows, so a day view does not fetch a month+ // and a year view does not fetch it twelve times over.+ const range = useMemo(() => {+ if (view === "day") {+ return { from: startOfDay(anchor), to: startOfDay(anchor) + 86_400 };+ }+ if (view === "week") {+ return { from: startOfDay(weekDays[0]), to: startOfDay(weekDays[6]) + 86_400 };+ }+ if (view === "year") {+ return {+ from: startOfDay(new Date(anchor.getFullYear(), 0, 1)),+ to: startOfDay(new Date(anchor.getFullYear(), 11, 31)) + 86_400,+ };+ }+ return { from: startOfDay(days[0]), to: startOfDay(days[41]) + 86_400 };+ }, [view, anchor, days, weekDays]);const read = useCallback(async () => {// Apple's Calendar shows reminders beside events, which for most people is most of@@ -309,10 +394,40 @@ export function CalendarView() {[write]);- const shift = (months: number) =>- setAnchor((a) => new Date(a.getFullYear(), a.getMonth() + months, 1));+ // The arrows move by whatever is on screen: a day in Day, a week in Week, and so on.+ const shift = (by: number) =>+ setAnchor((a) => {+ const to = step(a, view, by);+ // In Day and Week the thing being looked at is also the thing selected.+ if (view === "day" || view === "week") setSelected(to);+ return to;+ });++ /** What the title says, which is different in every view — as it is in Calendar. */+ const title = useMemo(() => {+ if (view === "day") {+ return anchor.toLocaleDateString(undefined, {+ weekday: "long",+ day: "numeric",+ month: "long",+ year: "numeric",+ });+ }+ if (view === "week") {+ const [first, last] = [weekDays[0], weekDays[6]];+ const sameMonth = first.getMonth() === last.getMonth();+ const left = first.toLocaleDateString(undefined, { day: "numeric", month: "short" });+ const right = last.toLocaleDateString(+ undefined,+ sameMonth ? { day: "numeric" } : { day: "numeric", month: "short" }+ );+ return `${left} – ${right} ${last.getFullYear()}`;+ }+ if (view === "year") return String(anchor.getFullYear());+ return anchor.toLocaleDateString(undefined, { month: "long", year: "numeric" });+ }, [view, anchor, weekDays]);- const monthLabel = anchor.toLocaleDateString(undefined, { month: "long", year: "numeric" });+ const monthLabel = title;return (<div className="cal">@@ -329,12 +444,33 @@ export function CalendarView() {<button className="chev" onClick={() => shift(1)} aria-label="Next month">›</button></div><div className="cal-actions">- {fetchingMonth && <span className="badge">Fetching this month…</span>}+ {fetchingMonth && <span className="badge">Fetching…</span>}+ {/* Calendar's own scope control, in Calendar's own order. */}+ <div className="segmented" role="group" aria-label="Calendar view">+ {VIEWS.map((v) => (+ <button+ key={v.key}+ type="button"+ className="segment"+ aria-pressed={view === v.key}+ onClick={() => {+ setView(v.key);+ setDraft(null);+ // Switching scope keeps you where you were looking.+ if (v.key === "day" || v.key === "week") setAnchor(new Date(selected));+ }}+ >+ {v.label}+ </button>+ ))}+ </div></div></header>{error && <p className="error" role="alert">{error}</p>}+ {view === "month" && (+ <><div className="cal-weekdays" aria-hidden="true">{weekdayNames().map((name) => (<span key={name}>{name}</span>@@ -452,6 +588,184 @@ export function CalendarView() {))}</div>+ </>+ )}++ {view === "year" && (+ <div className="cal-year" role="grid" aria-label={`${anchor.getFullYear()} calendar`}>+ {Array.from({ length: 12 }, (_, month) => {+ const first = new Date(anchor.getFullYear(), month, 1);+ const cells = gridDays(first);+ return (+ <button+ key={month}+ type="button"+ className="cal-mini"+ aria-label={first.toLocaleDateString(undefined, { month: "long", year: "numeric" })}+ onClick={() => {+ setAnchor(first);+ setView("month");+ }}+ >+ <span className="cal-mini-name">+ {first.toLocaleDateString(undefined, { month: "long" })}+ </span>+ <span className="cal-mini-grid" aria-hidden="true">+ {weekdayNames().map((name) => (+ <span className="cal-mini-weekday" key={name}>+ {name.slice(0, 1)}+ </span>+ ))}+ {cells.map((day) => {+ const outside = day.getMonth() !== month;+ const busyDay = (byDay.get(day.toDateString()) ?? []).length > 0;+ return (+ <span+ key={day.toISOString()}+ className={`cal-mini-day${outside ? " outside" : ""}${+ sameDay(day, today) ? " is-today" : ""+ }${busyDay && !outside ? " has-items" : ""}`}+ >+ {day.getDate()}+ </span>+ );+ })}+ </span>+ </button>+ );+ })}+ </div>+ )}++ {(view === "day" || view === "week") && (+ <div className="cal-hours-wrap">+ <div+ className={`cal-hours${view === "day" ? " is-day" : ""}`}+ role="grid"+ aria-label={`${title} schedule`}+ >+ <span className="cal-hours-corner" aria-hidden="true" />+ {(view === "day" ? [anchor] : weekDays).map((day) => (+ <button+ key={`head-${day.toISOString()}`}+ type="button"+ className={`cal-col-head${sameDay(day, today) ? " is-today" : ""}${+ sameDay(day, selected) ? " is-selected" : ""+ }`}+ onClick={() => {+ setSelected(day);+ setDraft(null);+ }}+ >+ <span className="cal-col-weekday">+ {day.toLocaleDateString(undefined, { weekday: "short" })}+ </span>+ <span className="cal-col-daynum">{day.getDate()}</span>+ </button>+ ))}++ {/* All-day events sit above the hours, as they do in Calendar. */}+ <span className="cal-allday-label" aria-hidden="true">+ all-day+ </span>+ {(view === "day" ? [anchor] : weekDays).map((day) => (+ <div className="cal-allday" key={`allday-${day.toISOString()}`}>+ {(byDay.get(day.toDateString()) ?? [])+ .filter((e) => e.kind === "event" && e.event.allDay)+ .map((entry) =>+ entry.kind === "event" ? (+ <button+ key={entry.event.guid}+ type="button"+ className="chip all-day"+ style={{+ background: colours.get(entry.event.calendarGuid) || "var(--gold)",+ }}+ onClick={() => {+ setSelected(day);+ setDraft(draftFrom(entry.event));+ }}+ >+ <span className="chip-title">{entry.event.title}</span>+ </button>+ ) : null+ )}+ </div>+ ))}++ {HOURS.map((hour) => (+ <Fragment key={hour}>+ <span className="cal-hour-label">+ {hour === 0+ ? ""+ : new Date(2024, 0, 1, hour).toLocaleTimeString(undefined, {+ hour: "numeric",+ })}+ </span>+ {(view === "day" ? [anchor] : weekDays).map((day) => (+ <button+ key={`${day.toISOString()}-${hour}`}+ type="button"+ className="cal-hour-cell"+ aria-label={`${day.toLocaleDateString(undefined, {+ weekday: "long",+ day: "numeric",+ month: "long",+ })} at ${hour}:00`}+ onClick={() => {+ setSelected(day);+ const at = new Date(+ day.getFullYear(),+ day.getMonth(),+ day.getDate(),+ hour+ );+ const startsAt = Math.floor(at.getTime() / 1000);+ setDraft({+ ...blankEvent(day, calendars[0]?.guid ?? ""),+ startsAt,+ endsAt: startsAt + 3600,+ });+ }}+ />+ ))}+ </Fragment>+ ))}++ {/* Timed events, laid over the hour grid where they actually fall. */}+ {(view === "day" ? [anchor] : weekDays).map((day, column) =>+ (byDay.get(day.toDateString()) ?? [])+ .filter((e) => e.kind === "event" && !e.event.allDay)+ .map((entry) =>+ entry.kind === "event" ? (+ <button+ key={`timed-${entry.event.guid}-${column}`}+ type="button"+ className="cal-timed"+ style={placement(entry.event, day, column)}+ onClick={() => {+ setSelected(day);+ setDraft(draftFrom(entry.event));+ }}+ >+ <span+ className="cal-timed-bar"+ style={{+ background: colours.get(entry.event.calendarGuid) || "var(--gold)",+ }}+ />+ <span className="cal-timed-text">+ <span className="cal-timed-title">{entry.event.title}</span>+ <span className="cal-timed-when">{clockTime(entry.event.startsAt)}</span>+ </span>+ </button>+ ) : null+ )+ )}+ </div>+ </div>+ )}+<sectionclassName="cal-day-panel"aria-label={`${selected.toLocaleDateString(undefined, { weekday: "long", day: "numeric", month: "long" })}`}diff --git a/src/styles/app.css b/src/styles/app.cssindex 6629260..51c1a77 100644--- a/src/styles/app.css+++ b/src/styles/app.css@@ -1619,3 +1619,249 @@ button.cal-day-item:hover { background: color-mix(in srgb, var(--sheet) 92%, var}.picker-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; }++/* ── Scope control ──────────────────────────────────────────────────────────────────────+ Calendar's Day / Week / Month / Year, as a segmented control: one recessed track with a+ raised cap over the active segment, which is the macOS shape. */+.segmented {+ display: inline-flex;+ padding: 2px;+ gap: 2px;+ border-radius: var(--r-field);+ background: color-mix(in srgb, var(--text) 7%, transparent);+}++.segment {+ border: 0;+ background: none;+ font: inherit;+ font-size: var(--t-11);+ font-weight: 510;+ color: var(--text-2);+ padding: 3px 10px;+ border-radius: 5px;+ cursor: pointer;+ transition: background 140ms var(--ease), color 140ms var(--ease);+}++.segment:hover { color: var(--text); }++.segment[aria-pressed="true"] {+ background: var(--sheet);+ color: var(--text);+ font-weight: 590;+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.14), 0 0 0 0.5px var(--glass-edge);+}++.segment:focus-visible { outline: 3px solid var(--gold-ring); outline-offset: 1px; }++/* ── Day and week ───────────────────────────────────────────────────────────────────────+ An hour grid: a gutter of times down the left, a column per day, all-day events in their+ own row above the hours. Events are placed on the grid's tracks rather than by pixel, so+ they stay put when the window changes size. */+.cal-hours-wrap {+ flex: 1;+ min-height: 0;+ overflow-y: auto;+ border: 1px solid var(--separator);+ border-radius: var(--r-plateau);+ background: var(--bg);+}++.cal-hours {+ display: grid;+ grid-template-columns: 52px repeat(7, minmax(0, 1fr));+ grid-auto-rows: 44px;+ /* The head and the all-day row size to their contents; the hours are fixed. */+ grid-template-rows: auto auto;+ position: relative;+}++.cal-hours.is-day { grid-template-columns: 52px minmax(0, 1fr); }++.cal-hours-corner,+.cal-allday-label {+ position: sticky;+ left: 0;+ background: var(--bg);+ border-right: 1px solid var(--separator);+}++.cal-allday-label {+ font-size: var(--t-11);+ color: var(--text-3);+ padding: 4px 6px;+ text-align: right;+ border-bottom: 1px solid var(--separator);+}++.cal-col-head {+ position: sticky;+ top: 0;+ z-index: 3;+ display: flex;+ flex-direction: column;+ align-items: center;+ gap: 1px;+ padding: 6px 4px 5px;+ border: 0;+ border-left: 1px solid var(--separator);+ background: var(--bg);+ font: inherit;+ color: var(--text-2);+ cursor: pointer;+}++.cal-col-weekday { font-size: var(--t-11); text-transform: uppercase; letter-spacing: 0.4px; }++.cal-col-daynum {+ font-size: var(--t-17);+ font-weight: 590;+ color: var(--text);+ font-variant-numeric: tabular-nums;+ min-width: 26px;+ height: 26px;+ line-height: 26px;+ border-radius: var(--r-pill);+ text-align: center;+}++.cal-col-head.is-today .cal-col-daynum { background: var(--gold); color: #fff; }+.cal-col-head.is-selected { box-shadow: inset 0 -2px 0 var(--gold); }+.cal-col-head:focus-visible { outline: 2px solid var(--gold); outline-offset: -2px; }++.cal-allday {+ display: flex;+ flex-direction: column;+ gap: 2px;+ min-height: 22px;+ padding: 2px 3px;+ border-left: 1px solid var(--separator);+ border-bottom: 1px solid var(--separator);+}++.cal-hour-label {+ position: sticky;+ left: 0;+ background: var(--bg);+ border-right: 1px solid var(--separator);+ font-size: var(--t-11);+ color: var(--text-3);+ text-align: right;+ padding: 0 6px;+ /* Sits against the line it names, as a ruler does. */+ transform: translateY(-7px);+ font-variant-numeric: tabular-nums;+}++.cal-hour-cell {+ border: 0;+ border-left: 1px solid var(--separator);+ border-top: 1px solid var(--separator);+ background: none;+ padding: 0;+ cursor: pointer;+}++.cal-hour-cell:hover { background: color-mix(in srgb, var(--text) 4%, transparent); }+.cal-hour-cell:focus-visible { outline: 2px solid var(--gold); outline-offset: -2px; }++.cal-timed {+ display: flex;+ align-items: stretch;+ gap: 5px;+ align-self: start;+ z-index: 2;+ margin-inline: 3px;+ padding: 2px 4px 2px 0;+ border: 0;+ border-radius: 5px;+ background: var(--sheet);+ box-shadow: 0 1px 2px rgba(0, 0, 0, 0.16), 0 0 0 0.5px var(--glass-edge);+ font: inherit;+ text-align: left;+ overflow: hidden;+ cursor: pointer;+}++.cal-timed-bar { flex: none; width: 3px; border-radius: var(--r-pill); }++.cal-timed-text {+ display: flex;+ flex-direction: column;+ min-width: 0;+ line-height: 1.25;+ padding-top: 1px;+}++.cal-timed-title {+ font-size: var(--t-11);+ font-weight: 550;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}++.cal-timed-when { font-size: var(--t-11); color: var(--text-2); }++/* ── Year ───────────────────────────────────────────────────────────────────────────────+ Twelve months at a glance, each one clickable. A day with something on it is marked, so+ the year reads as a heat map of where the work is. */+.cal-year {+ flex: 1;+ min-height: 0;+ overflow-y: auto;+ display: grid;+ grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));+ gap: 14px;+ padding: 4px 2px;+}++.cal-mini {+ display: flex;+ flex-direction: column;+ gap: 5px;+ border: 0;+ background: none;+ font: inherit;+ color: inherit;+ text-align: left;+ padding: 6px;+ border-radius: var(--r-plateau);+ cursor: pointer;+}++.cal-mini:hover { background: color-mix(in srgb, var(--text) 4%, transparent); }+.cal-mini:focus-visible { outline: 3px solid var(--gold-ring); outline-offset: -1px; }++.cal-mini-name {+ font-size: var(--t-13);+ font-weight: 600;+ color: var(--gold-strong);+}++.cal-mini-grid {+ display: grid;+ grid-template-columns: repeat(7, 1fr);+ gap: 1px 0;+}++.cal-mini-weekday {+ font-size: 9px;+ color: var(--text-3);+ text-align: center;+ padding-bottom: 2px;+}++.cal-mini-day {+ font-size: var(--t-11);+ text-align: center;+ line-height: 18px;+ border-radius: var(--r-pill);+ font-variant-numeric: tabular-nums;+}++.cal-mini-day.outside { color: transparent; }+.cal-mini-day.is-today { background: var(--gold); color: #fff; font-weight: 600; }+.cal-mini-day.has-items { font-weight: 620; color: var(--gold-strong); }+.cal-mini-day.is-today.has-items { color: #fff; }