Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Calendar: carry an event between the all-day row and the hours

Yashwanth Kamireddi308aafedd568
diff --git a/.uiverify/calendar-allday-drag.mjs b/.uiverify/calendar-allday-drag.mjs
new file mode 100644
index 0000000..ca82409
--- /dev/null
+++ b/.uiverify/calendar-allday-drag.mjs
@@ -0,0 +1,139 @@
+// Carried between the all-day row and the hours, as in Calendar on the Mac: a timed event let
+// go over the all-day row becomes all-day on that day; an all-day event let go over the hours
+// becomes an hour long from where it was let go. Drawn where it would land while held, saved at
+// once, and Ctrl+Z puts it back as it was. Escape while held leaves it where it was.
+import { engine } from "./engine.mjs";
+import { SERVICES, installStub, openScreen } from "./stub.mjs";
+const BASE = process.env.BASE ?? "http://localhost:4173";
+const b = await engine.launch();
+const ctx = await b.newContext({ viewport: { width: 1300, height: 850 } });
+await ctx.addInitScript(installStub, {
+ screen: "library", services: SERVICES, twoFactor: null, calendarFixture: true,
+ extraEvents: [
+ // Today, all day: from this midnight (9 AM less nine hours) to the next.
+ { guid: "ad1", title: "Offsite", hour: -9, length: 24, allDay: true },
+ // Today from 11 to noon.
+ { guid: "t1", title: "Budget talk", hour: 2, length: 1 },
+ ],
+});
+// Apple slow to answer, as on a slow network: what is drawn meanwhile is the app's own.
+await ctx.addInitScript(() => {
+ const wrap = () => {
+ const t = window.__TAURI_INTERNALS__;
+ if (!t || t.__slow) return;
+ const inner = t.invoke;
+ t.invoke = async (cmd, args) => {
+ if (cmd === "event_save") await new Promise((r) => setTimeout(r, 1200));
+ return inner(cmd, args);
+ };
+ t.__slow = true;
+ };
+ wrap(); document.addEventListener("DOMContentLoaded", wrap);
+});
+const page = await ctx.newPage();
+page.on("pageerror", (e) => { console.log(`FAIL page error: ${e.message}`); process.exitCode = 1; });
+await page.goto(BASE, { waitUntil: "networkidle" });
+await openScreen(page, "Calendar");
+await page.keyboard.press("Control+2");
+await page.locator(".cx-scroll").waitFor();
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+const saves = () => page.evaluate(() => window.__eventSaves ?? []);
+const savesReach = (n) => page.waitForFunction((n) => (window.__eventSaves ?? []).length >= n, n, { timeout: 3000 }).catch(() => {});
+const midnightIn = (n) => page.evaluate((n) => { const d = new Date(); d.setHours(0, 0, 0, 0); d.setDate(d.getDate() + n); return d.getTime() / 1000; }, n);
+const clockIn = (n, h) => page.evaluate(([n, h]) => { const d = new Date(); d.setHours(h, 0, 0, 0); d.setDate(d.getDate() + n); return d.getTime() / 1000; }, [n, h]);
+await page.locator(".cx-scroll").evaluate((el) => { el.scrollTop = 8 * 48; });
+
+const today = await page.evaluate(() => [...document.querySelectorAll(".cal-col-head")].findIndex((h) => h.classList.contains("is-today")));
+const by = today === 6 ? -1 : 1;
+const lanes = page.locator(".cx-allday-lanes");
+const row = await lanes.boundingBox();
+const colWidth = row.width / 7;
+
+// 1. The timed event carried up into the all-day row, and a day on.
+const block = page.locator(".cx-block", { hasText: "Budget talk" });
+const from = await block.boundingBox();
+await page.mouse.move(from.x + 20, from.y + 10);
+await page.mouse.down();
+await page.mouse.move(from.x + 20, from.y - 40, { steps: 3 });
+await page.mouse.move(from.x + 20 + by * colWidth, row.y + row.height - 6, { steps: 6 });
+const carried = page.locator(".cx-allday .cx-bar-event.is-carried", { hasText: "Budget talk" });
+await carried.waitFor({ timeout: 2000 }).catch(() => {});
+ok(await carried.isVisible(), "held over the all-day row, it is drawn there");
+const carriedAt = await carried.boundingBox();
+const dayLeft = (await lanes.boundingBox()).x + colWidth * (today + by);
+ok(carriedAt && Math.abs(carriedAt.x - dayLeft) < 6, `on the day it is over [${carriedAt && Math.round(carriedAt.x - dayLeft)}]`);
+ok(!(await block.isVisible()), "and not in the hours meanwhile");
+const scrolledBefore = await page.locator(".cx-scroll").evaluate((el) => el.scrollTop);
+await page.waitForTimeout(500);
+ok(await page.locator(".cx-scroll").evaluate((el) => el.scrollTop) === scrolledBefore, "held there, the hours do not scroll under it");
+await page.mouse.up();
+// Drawn as all-day at once, before Apple has answered.
+const asBar = page.locator(".cx-allday .cx-bar-event:not(.is-carried)", { hasText: "Budget talk" });
+await asBar.waitFor({ timeout: 1000 }).catch(() => {});
+ok(await asBar.isVisible() && (await page.locator(".cx-block", { hasText: "Budget talk" }).count()) === 0, "let go, it is an all-day event at once");
+await savesReach(1);
+let s = await saves();
+const start = await midnightIn(0);
+ok(s.length === 1 && s[0].guid === "t1" && s[0].allDay === true && s[0].startsAt === (await midnightIn(by)) && s[0].endsAt === (await midnightIn(by + 1)),
+ `saved all-day on that day ${JSON.stringify(s[0] && { g: s[0].guid, a: s[0].allDay, d: (s[0].startsAt - start) / 3600, len: (s[0].endsAt - s[0].startsAt) / 3600 })}`);
+ok(await page.getByRole("dialog").count() === 0, "nothing opens");
+
+// Ctrl+Z: back in the hours, as it was.
+await page.keyboard.press("Control+z");
+await savesReach(2);
+s = await saves();
+ok(s.length === 2 && s[1].guid === "t1" && s[1].allDay === false && s[1].startsAt === (await clockIn(0, 11)) && s[1].endsAt === (await clockIn(0, 12)),
+ `Ctrl+Z puts it back in the hours ${JSON.stringify(s[1] && { g: s[1].guid, a: s[1].allDay, h: (s[1].startsAt - start) / 3600 })}`);
+await page.locator(".cx-block", { hasText: "Budget talk" }).waitFor({ timeout: 2000 }).catch(() => {});
+ok(await page.locator(".cx-block", { hasText: "Budget talk" }).isVisible(), "and drawn there again");
+
+// 2. The all-day bar carried down into the hours, a day on, at 2 PM.
+const bar = page.locator(".cx-allday .cx-bar-event", { hasText: "Offsite" });
+const at = await bar.boundingBox();
+const columns = await page.locator(".cx-columns").boundingBox();
+const twoPm = columns.y + 14 * 48 + 6;
+ok(twoPm < 850, `2 PM is on screen [${Math.round(twoPm)}]`);
+await page.mouse.move(at.x + 10, at.y + at.height / 2);
+await page.mouse.down();
+await page.mouse.move(at.x + 10, at.y + 40, { steps: 3 });
+await page.mouse.move(columns.x + colWidth * (today + by) + colWidth / 2, twoPm, { steps: 6 });
+const hour = page.locator(".cx-column .cx-block.is-carried", { hasText: "Offsite" });
+await hour.waitFor({ timeout: 2000 }).catch(() => {});
+ok(await hour.isVisible(), "held over the hours, it is drawn there as an hour");
+const hourAt = await hour.boundingBox();
+ok(hourAt && Math.abs(hourAt.y - (columns.y + 14 * 48)) < 3 && Math.abs(hourAt.height - 46) < 3,
+ `from the quarter hour it is over, an hour long [${hourAt && Math.round(hourAt.y - columns.y)}, ${hourAt && Math.round(hourAt.height)}]`);
+ok(!(await bar.isVisible()), "and not in the all-day row meanwhile");
+await page.mouse.up();
+const asBlock = page.locator(".cx-block:not(.is-carried)", { hasText: "Offsite" });
+await asBlock.waitFor({ timeout: 1000 }).catch(() => {});
+ok(await asBlock.isVisible() && (await page.locator(".cx-allday .cx-bar-event", { hasText: "Offsite" }).count()) === 0, "let go, it is an hour in the day at once");
+await savesReach(3);
+s = await saves();
+ok(s.length === 3 && s[2].guid === "ad1" && s[2].allDay === false && s[2].startsAt === (await clockIn(by, 14)) && s[2].endsAt === (await clockIn(by, 15)),
+ `saved as an hour from 2 PM that day ${JSON.stringify(s[2] && { g: s[2].guid, a: s[2].allDay, h: (s[2].startsAt - start) / 3600, len: (s[2].endsAt - s[2].startsAt) / 3600 })}`);
+ok(await page.getByRole("dialog").count() === 0, "nothing opens");
+
+await page.keyboard.press("Control+z");
+await savesReach(4);
+s = await saves();
+ok(s.length === 4 && s[3].guid === "ad1" && s[3].allDay === true && s[3].startsAt === start && s[3].endsAt === (await midnightIn(1)),
+ `Ctrl+Z makes it all-day again, on its day ${JSON.stringify(s[3] && { g: s[3].guid, a: s[3].allDay, d: (s[3].startsAt - start) / 3600 })}`);
+await page.locator(".cx-allday .cx-bar-event", { hasText: "Offsite" }).waitFor({ timeout: 2000 }).catch(() => {});
+ok(await page.locator(".cx-allday .cx-bar-event", { hasText: "Offsite" }).isVisible(), "and drawn in the all-day row again");
+
+// 3. Escape while held over the all-day row: nothing moves, nothing is saved.
+const again = await page.locator(".cx-block", { hasText: "Budget talk" }).boundingBox();
+await page.mouse.move(again.x + 20, again.y + 10);
+await page.mouse.down();
+await page.mouse.move(again.x + 20, row.y + row.height - 6, { steps: 6 });
+await page.keyboard.press("Escape");
+await page.mouse.up();
+await page.waitForTimeout(300);
+ok((await saves()).length === 4 && await page.locator(".cx-block", { hasText: "Budget talk" }).isVisible()
+ && (await page.locator(".cx-bar-event.is-carried").count()) === 0, "Escape leaves it where it was");
+
+await b.close();
+console.log(failures ? `${failures} failed` : "all passed");
+process.exitCode = failures ? 1 : 0;
diff --git a/src/screens/calendar/CalendarView.tsx b/src/screens/calendar/CalendarView.tsx
index 70dd280..e78e3ef 100644
--- a/src/screens/calendar/CalendarView.tsx
+++ b/src/screens/calendar/CalendarView.tsx
@@ -953,7 +953,10 @@ export function CalendarView() {
if (gone) await restore(gone);
}, [undo, restore]);
const moveEvent = useCallback(
- (e: CalendarEvent, startsAt: number, endsAt: number) => void save({ ...draftOf(e), startsAt, endsAt }),
+ // `allDay` when it was carried between the all-day row and the hours: Undo puts it back as
+ // it was, all-day or not, as for any change.
+ (e: CalendarEvent, startsAt: number, endsAt: number, allDay = e.allDay) =>
+ void save({ ...draftOf(e), startsAt, endsAt, allDay }),
[save]
);
/** Dragged to another day in Month: the same clock time, that many days on. */
@@ -2321,14 +2324,18 @@ type Drag =
/** `column` is the day the event was taken from, so it can be carried only to days shown. */
/** `startY` is where it was pressed in the grid's own height (the pointer plus the scroll),
* so the hours scrolled past while it is held count as hours moved. */
- | { kind: "move"; event: CalendarEvent; column: number; startX: number; startY: number; dx: number; dMinutes: number }
+ /** `allDay` while it is held over the all-day row: let go there, it becomes all-day on the
+ * day it is over, as in Calendar. */
+ | { kind: "move"; event: CalendarEvent; column: number; startX: number; startY: number; dx: number; dMinutes: number; allDay?: boolean }
/** Dragging an event's bottom edge: its end moves, its start stays — or, by its top edge
* (`top`), its start moves and its end stays. */
| { kind: "resize"; event: CalendarEvent; startY: number; dMinutes: number; top?: boolean }
/** A bar in the all-day row carried to other days: `from` and `to` are its first and last
* columns shown, and `dx` how many days it has been carried — or, by one of its ends
- * (`edge`), how many days that end has been stretched. */
- | { kind: "days"; event: CalendarEvent; from: number; to: number; startX: number; dx: number; edge?: "start" | "end" };
+ * (`edge`), how many days that end has been stretched. `into` while the whole bar is held
+ * over the hours: the day and quarter hour it would start at, let go there — an hour long,
+ * no longer all-day, as in Calendar. */
+ | { kind: "days"; event: CalendarEvent; from: number; to: number; startX: number; dx: number; edge?: "start" | "end"; into?: { column: number; minutes: number } };
const TimeGrid = memo(function TimeGrid({
days,
@@ -2354,7 +2361,8 @@ const TimeGrid = memo(function TimeGrid({
canWrite: (calendarGuid: string) => boolean;
onOpen: (e: CalendarEvent, at: Anchor) => void;
onCreate: (startsAt: number, endsAt: number, allDay: boolean, at: Anchor) => void;
- onMove: (e: CalendarEvent, startsAt: number, endsAt: number) => void;
+ /** `allDay` given when the event was carried into the all-day row or out of it. */
+ onMove: (e: CalendarEvent, startsAt: number, endsAt: number, allDay?: boolean) => void;
onPickDay: (d: Date) => void;
/** A new event being named in the popover, held on the grid where it was made — in the
* all-day row when it is all-day. */
@@ -2396,7 +2404,14 @@ const TimeGrid = memo(function TimeGrid({
// eslint-disable-next-line react-hooks/exhaustive-deps
[reminders, daysKey]
);
- const lanes = Math.max(1, eventLanes + Math.max(0, ...dayReminders.map((l) => l.length)));
+ const reminderLanes = Math.max(0, ...dayReminders.map((l) => l.length));
+ // A timed event held over the all-day row is drawn there, in a lane of its own below the
+ // rest, so it covers nothing while it is carried.
+ const carriedUp = drag?.kind === "move" && drag.allDay ? drag : null;
+ const lanes = Math.max(1, eventLanes + reminderLanes + (carriedUp ? 1 : 0));
+ /** An all-day bar held down over the hours, and where it would start. */
+ const carriedDown = drag?.kind === "days" && drag.into ? drag : null;
+ const alldayEl = useRef<HTMLDivElement | null>(null);
// Opened, it shows the working day rather than midnight — as Calendar does. Only when it
// opens: stepping to the next week or day keeps the hours scrolled to, as Calendar keeps
@@ -2475,16 +2490,24 @@ const TimeGrid = memo(function TimeGrid({
: current.edge === "start"
? Math.min(current.to - current.from, Math.max(-current.from, want))
: Math.min(days.length - 1 - current.from, Math.max(-current.from, want));
- if (dx === current.dx) return;
- next = { ...current, dx };
+ // The whole bar held down over the hours: the quarter hour under the pointer.
+ const hours = scroller.current?.getBoundingClientRect();
+ const into = !current.edge && hours && clientY > hours.top
+ ? { column: at.column, minutes: Math.min(Math.floor(at.minutes / 15) * 15, 23 * 60) }
+ : undefined;
+ if (dx === current.dx && into?.column === current.into?.column && into?.minutes === current.into?.minutes) return;
+ next = { ...current, dx, into };
} else {
const el = columnsEl.current;
const width = el ? el.getBoundingClientRect().width / days.length : 1;
// Carried only as far as the days on screen: past them it went nowhere visible and
// landed on a day no one could see.
const dx = Math.min(days.length - 1 - current.column, Math.max(-current.column, Math.round((clientX - current.startX) / width)));
- next = { ...current, dx, dMinutes: snap(((clientY + scrolled() - current.startY) / HOUR) * 60) };
- if (next.dx === current.dx && next.dMinutes === current.dMinutes) return;
+ // Held up over the all-day row, it would become all-day there.
+ const row = alldayEl.current?.getBoundingClientRect();
+ const allDay = !!row && clientY < row.bottom;
+ next = { ...current, dx, dMinutes: allDay ? current.dMinutes : snap(((clientY + scrolled() - current.startY) / HOUR) * 60), allDay };
+ if (next.dx === current.dx && next.dMinutes === current.dMinutes && next.allDay === !!current.allDay) return;
}
setDragNow(next);
};
@@ -2499,6 +2522,12 @@ const TimeGrid = memo(function TimeGrid({
if (done.to - done.from > 15) {
onCreate(onClockOf(done.day, done.from), onClockOf(done.day, done.to), false, besideColumn(done.column, clientY));
}
+ } else if (done.kind === "days" && done.into) {
+ // Let go over the hours: an hour from the quarter hour it was let go at, on that day.
+ swallowClick.current = true;
+ window.setTimeout(() => (swallowClick.current = false), 0);
+ const day = days[done.into.column];
+ onMove(done.event, onClockOf(day, done.into.minutes), onClockOf(day, done.into.minutes + 60), false);
} else if (done.kind === "days") {
// Let go where it was taken, it was a click: the bar's own click opens it.
if (done.dx !== 0) {
@@ -2517,6 +2546,10 @@ const TimeGrid = memo(function TimeGrid({
if (done.top) onMove(done.event, done.event.startsAt + done.dMinutes * 60, done.event.endsAt);
else onMove(done.event, done.event.startsAt, done.event.endsAt + done.dMinutes * 60);
}
+ } else if (done.allDay) {
+ // Let go over the all-day row: all-day on the day it is over.
+ const day = midnight(days[done.column + done.dx]);
+ onMove(done.event, seconds(day), seconds(addDays(day, 1)), true);
} else if (done.dx !== 0 || done.dMinutes !== 0) {
const shift = done.dMinutes * 60;
const moved = (t: number) => {
@@ -2552,7 +2585,9 @@ const TimeGrid = memo(function TimeGrid({
const edge = () => {
frame = 0;
const el = scroller.current;
- if (!el || !last || !dragRef.current || dragRef.current.kind === "days") return;
+ // Held over the all-day row, it is going there, not to an earlier hour.
+ const held = dragRef.current;
+ if (!el || !last || !held || held.kind === "days" || (held.kind === "move" && held.allDay)) return;
const r = el.getBoundingClientRect();
const ZONE_PX = 28;
// Toward the edge it was carried to, never the other way: an event taken near the top
@@ -2685,6 +2720,7 @@ const TimeGrid = memo(function TimeGrid({
<span className="cx-gutter cx-allday-label">all-day</span>
<div
className="cx-allday-lanes"
+ ref={alldayEl}
// A double-click on a day's free part of the row makes an all-day event on it, as in
// Calendar.
onDoubleClick={(e) => {
@@ -2729,7 +2765,7 @@ const TimeGrid = memo(function TimeGrid({
e.preventDefault();
onMenu(s.event, { x: e.clientX, y: e.clientY });
}}
- className={`cx-bar-event${s.continuesLeft ? " from-before" : ""}${s.continuesRight ? " to-after" : ""}${picked === s.event.guid ? " is-picked" : ""}${carried ? " is-moving" : ""}`}
+ className={`cx-bar-event${s.continuesLeft ? " from-before" : ""}${s.continuesRight ? " to-after" : ""}${picked === s.event.guid ? " is-picked" : ""}${carried ? " is-moving" : ""}${carried?.into ? " is-away" : ""}`}
style={
{
"--cal": tint(calendars.get(s.event.calendarGuid)),
@@ -2739,8 +2775,9 @@ const TimeGrid = memo(function TimeGrid({
} as React.CSSProperties
}
// Carried to other days, as a bar in Month is; held still, a click opens it.
+ // In Day too: there is no other day to carry it to, but there are the hours.
onPointerDown={(e) => {
- if (e.button !== 0 || days.length < 2 || !mine) return;
+ if (e.button !== 0 || !mine) return;
e.preventDefault();
setDragNow({ kind: "days", event: s.event, from: s.from, to: s.to, startX: e.clientX, dx: 0 });
}}
@@ -2775,6 +2812,21 @@ const TimeGrid = memo(function TimeGrid({
<span className="cx-reminder-title">{r.title}</span>
</span>
)))}
+ {carriedUp && (
+ <span
+ className="cx-bar-event is-moving is-carried"
+ style={
+ {
+ "--cal": tint(calendars.get(carriedUp.event.calendarGuid)),
+ left: `calc(${((carriedUp.column + carriedUp.dx) / days.length) * 100}% + 2px)`,
+ width: `calc(${100 / days.length}% - 4px)`,
+ top: `${4 + (eventLanes + reminderLanes) * LANE}px`,
+ } as React.CSSProperties
+ }
+ >
+ {carriedUp.event.title}
+ </span>
+ )}
</div>
</div>
@@ -2827,7 +2879,7 @@ const TimeGrid = memo(function TimeGrid({
<button
key={p.event.guid}
data-event={p.event.guid}
- className={`cx-block${moving || sizing ? " is-moving" : ""}${p.length < 45 ? " is-short" : ""}${picked === p.event.guid ? " is-picked" : ""}`}
+ className={`cx-block${moving || sizing ? " is-moving" : ""}${moving?.allDay ? " is-away" : ""}${p.length < 45 ? " is-short" : ""}${picked === p.event.guid ? " is-picked" : ""}`}
onContextMenu={(e) => {
e.preventDefault();
onMenu(p.event, { x: e.clientX, y: e.clientY });
@@ -2921,6 +2973,22 @@ const TimeGrid = memo(function TimeGrid({
<span className="cx-block-time">{clock(pending.startsAt)}</span>
</span>
)}
+ {/* An all-day bar held over the hours: the hour it would become. */}
+ {carriedDown?.into?.column === column && (
+ <span
+ className="cx-block is-moving is-carried"
+ style={
+ {
+ "--cal": tint(calendars.get(carriedDown.event.calendarGuid)),
+ top: `${(carriedDown.into.minutes / 60) * HOUR}px`,
+ height: `${HOUR - 2}px`,
+ } as React.CSSProperties
+ }
+ >
+ <span className="cx-block-title">{carriedDown.event.title}</span>
+ <span className="cx-block-time">{clock(onClockOf(d, carriedDown.into.minutes))}</span>
+ </span>
+ )}
{/* Drawn once the drag is one, never for a press held still: a
quarter-hour sliver that release then discards reads as a bug. */}
{drag?.kind === "create" && drag.column === column && drag.to - drag.from > 15 && (
diff --git a/src/styles/calendar.css b/src/styles/calendar.css
index 69d729f..8865aef 100644
--- a/src/styles/calendar.css
+++ b/src/styles/calendar.css
@@ -442,6 +442,11 @@
.cx-block:focus-visible { outline: var(--focus-ring); outline-offset: 0; }
.cx-block.is-moving { z-index: 3; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28); opacity: 0.94; }
.cx-bar-event.is-moving { z-index: 3; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.24); cursor: grabbing; }
+/* Carried between the all-day row and the hours, as in Calendar: drawn where it would land,
+ and not where it was. */
+.cx-block.is-away, .cx-bar-event.is-away { visibility: hidden; }
+.cx-block.is-carried { left: 0; right: 3px; width: auto; pointer-events: none; }
+.cx-bar-event.is-carried { display: flex; align-items: center; pointer-events: none; }
.cx-bar-grip { position: absolute; top: 0; bottom: 0; width: 6px; cursor: ew-resize; }
.cx-bar-grip.is-start { left: 0; }
.cx-bar-grip.is-end { right: 0; }