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
Notes: the arrows draw the two rows they move between, not the whole list
Yashwanth Kamireddi6eb88a67b874
diff --git a/src/lib/chosen.ts b/src/lib/chosen.tsnew file mode 100644index 0000000..002028d--- /dev/null+++ b/src/lib/chosen.ts@@ -0,0 +1,62 @@+/**+ * Which rows of a long list are selected, told to each row on its own.+ *+ * Handed down as a prop, the selection changed the props of the list itself at every arrow+ * press, so the list was drawn again — every one of thousands of rows made anew and compared,+ * to find the two that changed. Here the list is handed one object that never changes, each+ * row asks it whether it is selected, and a change wakes only the rows it moved between.+ */+import { useCallback, useLayoutEffect, useState, useSyncExternalStore } from "react";++export class Chosen {+ private keys = new Set<string>();+ /** The keys rows were last told: a change is told against these. */+ private told = new Set<string>();+ private listeners = new Map<string, Set<() => void>>();++ has(key: string): boolean {+ return this.keys.has(key);+ }++ /** The selection as of the render in progress, read by rows drawn in it. */+ hold(next: Iterable<string>) {+ this.keys = new Set(next);+ }++ /** Wake the rows whose part in the selection changed since they were last told. */+ tell() {+ const changed: string[] = [];+ for (const k of this.keys) if (!this.told.has(k)) changed.push(k);+ for (const k of this.told) if (!this.keys.has(k)) changed.push(k);+ this.told = new Set(this.keys);+ for (const k of changed) this.listeners.get(k)?.forEach((wake) => wake());+ }++ subscribe(key: string, wake: () => void): () => void {+ let set = this.listeners.get(key);+ if (!set) this.listeners.set(key, (set = new Set()));+ set.add(wake);+ return () => {+ set.delete(wake);+ if (set.size === 0) this.listeners.delete(key);+ };+ }+}++/**+ * One `Chosen` for the life of the list, holding `keys` — the selected rows, as this render+ * has them. Rows drawn in this render read it at once; the rows not drawn are woken once it+ * commits, before the frame is painted.+ */+export function useChosen(keys: Iterable<string>): Chosen {+ const [chosen] = useState(() => new Chosen());+ chosen.hold(keys);+ useLayoutEffect(() => chosen.tell());+ return chosen;+}++/** Whether the row `key` is selected, drawn again only when that changes. */+export function useIsChosen(chosen: Chosen, key: string): boolean {+ const subscribe = useCallback((wake: () => void) => chosen.subscribe(key, wake), [chosen, key]);+ return useSyncExternalStore(subscribe, () => chosen.has(key));+}diff --git a/src/screens/NotesView.tsx b/src/screens/NotesView.tsxindex 1df1233..d749995 100644--- a/src/screens/NotesView.tsx+++ b/src/screens/NotesView.tsx@@ -30,6 +30,7 @@ import { useArriving, useRevision, useShownRevision } from "../lib/sync";import { Picker } from "../components/Picker";import { holdTyping } from "../lib/updates";import { keepSame, same } from "../lib/same";+import { useChosen, useIsChosen, type Chosen } from "../lib/chosen";import { useArrivals } from "../lib/arrival";import { foundRanges, queryWords } from "./notes/found";@@ -96,20 +97,22 @@ function fitFolders(aside: HTMLElement | null): number | null {}/**- * The list of notes, drawn again only when the notes or the open one change. Inline in the- * screen, every key typed in a note — and every move of the cursor — drew every row again,- * which in an account of thousands was felt in the typing.+ * The list of notes, drawn again only when the notes change. Inline in the screen, every key+ * typed in a note — and every move of the cursor — drew every row again, which in an account+ * of thousands was felt in the typing. The open note is not one of its props either: each row+ * asks `chosen` itself (lib/chosen), so the arrows draw the two rows they move between.*/const NoteRows = memo(function NoteRows({sections,- openGuid,+ chosen,rowsRef,onPick,found,pictures,}: {sections: Section[];- openGuid: string | null;+ /** The open note, as each row asks it. */+ chosen: Chosen;rowsRef: React.MutableRefObject<HTMLDivElement | null>;onPick: (note: Note, e: React.MouseEvent) => void;/** The words being searched for, marked where they fall in each row. */@@ -127,7 +130,7 @@ const NoteRows = memo(function NoteRows({<NoteRowkey={note.guid}note={note}- selected={openGuid === note.guid}+ chosen={chosen}picture={pictures[note.guid] ?? null}onPick={onPick}found={found}@@ -150,17 +153,18 @@ const NoteRows = memo(function NoteRows({*/const NoteRow = memo(function NoteRow({note,- selected,+ chosen,picture,onPick,found,}: {note: Note;- selected: boolean;+ chosen: Chosen;picture: string | null;onPick: (note: Note, e: React.MouseEvent) => void;found: string[];}) {+ const selected = useIsChosen(chosen, note.guid);return (<buttonclassName="note-row"@@ -1320,6 +1324,8 @@ export function NotesView() {const pickRef = useRef(pick);pickRef.current = pick;const pickRow = useCallback((note: Note, e: React.MouseEvent) => pickRef.current(note, e), []);+ /** The open note, told to the rows of the list one by one (lib/chosen). */+ const chosenNote = useChosen(openGuid ? [openGuid] : []);// The notes either side of the open one, read ahead once it has opened (see `ahead`): behind// every save already on its way, and dropped if the library changed meanwhile.@@ -1595,7 +1601,7 @@ export function NotesView() {{/* One list box of notes, a group per heading: the open note is its selected option.The heading is drawn for the eye; the group's name says it to a screen reader. */}- <NoteRows sections={sections} openGuid={openGuid} rowsRef={rowsRef} onPick={pickRow} found={foundWords} pictures={pictures} />+ <NoteRows sections={sections} chosen={chosenNote} rowsRef={rowsRef} onPick={pickRow} found={foundWords} pictures={pictures} />{loaded && notes.length === 0 && !error && !(arriving && !searching) && (searching ? (