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: dragging an event scrolls the hours

Yashwanth Kamireddi6625fc2b9c0d
diff --git a/.uiverify/calendar-dragscroll.mjs b/.uiverify/calendar-dragscroll.mjs
new file mode 100644
index 0000000..f863128
--- /dev/null
+++ b/.uiverify/calendar-dragscroll.mjs
@@ -0,0 +1,80 @@
+// Dragging an event in Week to an hour out of sight, as in Calendar: held near the bottom of
+// the hours they scroll, and the hours scrolled past — by the edge or by the wheel — count as
+// hours moved.
+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: 700 } });
+await ctx.addInitScript(installStub, { screen: "library", services: SERVICES, twoFactor: null, calendarFixture: true });
+const page = await ctx.newPage();
+await page.goto(BASE, { waitUntil: "networkidle" });
+await openScreen(page, "Calendar");
+await page.keyboard.press("Control+2");
+const scroller = page.locator(".cx-scroll");
+await scroller.waitFor();
+let failures = 0;
+const ok = (p, m) => { console.log(`${p ? "PASS" : "FAIL"} ${m}`); if (!p) failures++; };
+const saves = () => page.evaluate(() => window.__eventSaves ?? []);
+const ten = await page.evaluate(() => { const d = new Date(); d.setHours(10, 0, 0, 0); return d.getTime() / 1000; });
+// Design review, 10 to 11, at the top of the hours in view.
+await scroller.evaluate((el) => { el.scrollTop = 9 * 48; });
+const review = page.locator(".cx-block", { hasText: "Design review" });
+
+// The wheel while it is held: two hours scrolled are two hours moved.
+let at = await review.boundingBox();
+await page.mouse.move(at.x + at.width / 2, at.y + 20);
+await page.mouse.down();
+await page.mouse.move(at.x + at.width / 2, at.y + 24, { steps: 2 });
+await scroller.evaluate((el) => { el.scrollTop += 2 * 48; });
+await page.waitForFunction(() => {
+ const block = [...document.querySelectorAll(".cx-block")].find((x) => x.textContent.includes("Design review"));
+ return block && parseFloat(block.style.top) >= 12 * 48;
+}, null, { timeout: 2000 }).catch(() => {});
+await page.mouse.up();
+await page.waitForFunction(() => (window.__eventSaves ?? []).length >= 1, null, { timeout: 3000 }).catch(() => {});
+let s = await saves();
+ok(s.length === 1 && s[0].guid === "e1" && s[0].startsAt === ten + 2 * 3600, `scrolled two hours while held, it moves two hours ${JSON.stringify(s.map((x) => (x.startsAt - ten) / 3600))}`);
+
+// Held at the bottom edge, the hours scroll and the event goes with them.
+await page.reload({ waitUntil: "networkidle" });
+await openScreen(page, "Calendar");
+await scroller.waitFor();
+await scroller.evaluate((el) => { el.scrollTop = 9 * 48; });
+const box = await scroller.boundingBox();
+at = await review.boundingBox();
+const top = await scroller.evaluate((el) => el.scrollTop);
+await page.mouse.move(at.x + at.width / 2, at.y + 20);
+await page.mouse.down();
+await page.mouse.move(at.x + at.width / 2, at.y + 40, { steps: 3 });
+await page.mouse.move(at.x + at.width / 2, box.y + box.height - 6, { steps: 6 });
+await page.waitForFunction((t) => document.querySelector(".cx-scroll").scrollTop > t + 3 * 48, top, { timeout: 4000 }).catch(() => {});
+const scrolledTo = await scroller.evaluate((el) => el.scrollTop);
+ok(scrolledTo > top + 3 * 48, `the hours scroll while it is held at the edge [${Math.round(scrolledTo - top)}px]`);
+await page.mouse.up();
+await page.waitForFunction(() => (window.__eventSaves ?? []).length >= 1, null, { timeout: 3000 }).catch(() => {});
+s = await saves();
+// Where it was let go: the pointer's place in the hours, less where it was taken from.
+const expected = Math.round(((box.y + box.height - 6 + scrolledTo) - (at.y + 20 + top)) / 48 * 4) / 4;
+const got = s[0] ? (s[0].startsAt - ten) / 3600 : null;
+ok(s.length === 1 && got !== null && got >= 3 && Math.abs(got - expected) <= 0.25, `it lands where it was let go [${got} h, expected ~${expected} h]`);
+
+// A press on an event near the edge, the hand not quite still, scrolls nothing.
+await page.reload({ waitUntil: "networkidle" });
+await openScreen(page, "Calendar");
+await scroller.waitFor();
+// Design review's top half at the bottom edge of the hours.
+await scroller.evaluate((el) => { el.scrollTop = 10 * 48 + 30 - el.clientHeight; });
+const still = await scroller.evaluate((el) => el.scrollTop);
+at = await review.boundingBox();
+await page.mouse.move(at.x + at.width / 2, at.y + 10);
+await page.mouse.down();
+await page.mouse.move(at.x + at.width / 2 + 1, at.y + 11);
+await page.waitForTimeout(300);
+await page.mouse.up();
+ok(await scroller.evaluate((el) => el.scrollTop) === still, "a press held still near the edge scrolls nothing");
+await page.keyboard.press("Escape");
+
+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 91f38ea..e02a24b 100644
--- a/src/screens/calendar/CalendarView.tsx
+++ b/src/screens/calendar/CalendarView.tsx
@@ -2297,6 +2297,8 @@ type Drag =
/** `anchor` is the quarter hour pressed: the drag runs from it up or down, as in Calendar. */
| { kind: "create"; day: Date; column: number; anchor: number; from: number; to: number }
/** `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 }
/** 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. */
@@ -2375,6 +2377,8 @@ const TimeGrid = memo(function TimeGrid({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
+ const scrolled = () => scroller.current?.scrollTop ?? 0;
+
/** Minutes since midnight under the pointer, in the column it is over. */
const where = (clientX: number, clientY: number) => {
const el = columnsEl.current;
@@ -2423,7 +2427,7 @@ const TimeGrid = memo(function TimeGrid({
} else if (current.kind === "resize") {
// Never shorter than a quarter hour, as Calendar holds it.
const length = (current.event.endsAt - current.event.startsAt) / 60;
- const by = snap(((clientY - current.startY) / HOUR) * 60);
+ const by = snap(((clientY + scrolled() - current.startY) / HOUR) * 60);
next = { ...current, dMinutes: current.top ? Math.min(length - 15, by) : Math.max(15 - length, by) };
if (next.dMinutes === current.dMinutes) return;
} else if (current.kind === "days") {
@@ -2439,7 +2443,7 @@ const TimeGrid = memo(function TimeGrid({
// 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 - current.startY) / HOUR) * 60) };
+ next = { ...current, dx, dMinutes: snap(((clientY + scrolled() - current.startY) / HOUR) * 60) };
if (next.dx === current.dx && next.dMinutes === current.dMinutes) return;
}
setDragNow(next);
@@ -2491,10 +2495,41 @@ const TimeGrid = memo(function TimeGrid({
const live = useActiveRef();
useEffect(() => {
if (!dragging) return undefined;
+ // Held near the top or bottom of the hours, they scroll, as Calendar's do — faster the
+ // nearer the edge — so an event can be carried to an hour out of sight. The pointer still,
+ // the drag follows the hours scrolling under it.
+ let last: { x: number; y: number } | null = null;
+ // Only once the pointer has really gone somewhere: a press on an event near the edge, the
+ // hand not quite still, had scrolled it away under the click.
+ let first: { x: number; y: number } | null = null;
+ let travelled = false;
+ let frame = 0;
+ const edge = () => {
+ frame = 0;
+ const el = scroller.current;
+ if (!el || !last || !dragRef.current || dragRef.current.kind === "days") return;
+ const r = el.getBoundingClientRect();
+ const ZONE_PX = 32;
+ const into = last.y < r.top + ZONE_PX ? last.y - (r.top + ZONE_PX) : last.y > r.bottom - ZONE_PX ? last.y - (r.bottom - ZONE_PX) : 0;
+ if (!into) return;
+ const before = el.scrollTop;
+ el.scrollTop += Math.max(-16, Math.min(16, into / 2));
+ if (el.scrollTop === before) return;
+ handlers.current.follow(last.x, last.y);
+ frame = requestAnimationFrame(edge);
+ };
const move = (e: PointerEvent) => {
- if (e.buttons === 0) handlers.current.finish(e.clientX, e.clientY);
- else handlers.current.follow(e.clientX, e.clientY);
+ if (e.buttons === 0) return handlers.current.finish(e.clientX, e.clientY);
+ last = { x: e.clientX, y: e.clientY };
+ first ??= last;
+ travelled ||= Math.hypot(last.x - first.x, last.y - first.y) > 6;
+ handlers.current.follow(e.clientX, e.clientY);
+ if (travelled && !frame) frame = requestAnimationFrame(edge);
};
+ // Scrolled by the wheel while held: followed as if the pointer had moved over the hours.
+ const scrolledBy = () => last && handlers.current.follow(last.x, last.y);
+ const hours = scroller.current;
+ hours?.addEventListener("scroll", scrolledBy, { passive: true });
const up = (e: PointerEvent | MouseEvent) => handlers.current.finish(e.clientX, e.clientY);
const drop = () => setDragNow(null);
const key = (e: KeyboardEvent) => live.current && e.key === "Escape" && drop();
@@ -2509,6 +2544,8 @@ const TimeGrid = memo(function TimeGrid({
window.addEventListener("dragstart", drop);
window.addEventListener("dragend", drop);
return () => {
+ cancelAnimationFrame(frame);
+ hours?.removeEventListener("scroll", scrolledBy);
window.removeEventListener("dragstart", drop);
window.removeEventListener("dragend", drop);
window.removeEventListener("pointermove", move);
@@ -2719,7 +2756,7 @@ const TimeGrid = memo(function TimeGrid({
e.stopPropagation();
e.preventDefault();
if (canWrite(p.event.calendarGuid)) {
- setDragNow({ kind: "move", event: p.event, column, startX: e.clientX, startY: e.clientY, dx: 0, dMinutes: 0 });
+ setDragNow({ kind: "move", event: p.event, column, startX: e.clientX, startY: e.clientY + scrolled(), dx: 0, dMinutes: 0 });
} else {
onOpen(p.event, { x: e.clientX, y: e.clientY });
}
@@ -2742,7 +2779,7 @@ const TimeGrid = memo(function TimeGrid({
if (e.button !== 0) return;
e.stopPropagation();
e.preventDefault();
- setDragNow({ kind: "resize", event: p.event, startY: e.clientY, dMinutes: 0, top: true });
+ setDragNow({ kind: "resize", event: p.event, startY: e.clientY + scrolled(), dMinutes: 0, top: true });
}}
/>
)}
@@ -2754,7 +2791,7 @@ const TimeGrid = memo(function TimeGrid({
if (e.button !== 0) return;
e.stopPropagation();
e.preventDefault();
- setDragNow({ kind: "resize", event: p.event, startY: e.clientY, dMinutes: 0 });
+ setDragNow({ kind: "resize", event: p.event, startY: e.clientY + scrolled(), dMinutes: 0 });
}}
/>
)}