1/**2 * The shared decision for atomic Switchers.3 *4 * CSS lays a Switcher out as one row or one column. It cannot compare the5 * row's intrinsic requirement with the space the Switcher's slot provides, so6 * this module makes that one comparison and publishes the result as an7 * attribute. It reads no geometry itself: the browser reports both numbers8 * through a single ResizeObserver, and the decision is written inside the9 * observer callback, before the frame paints.10 *11 * The module knows nothing about how a Switcher is rendered. A registration12 * names the two boxes that carry its numbers; `primitives.ts` builds them for13 * the controlled Composition tree.14 *15 * Dependencies between Switchers are carried by the browser, not by a tree16 * walk here. Within a frame, ResizeObserver delivers in rounds, and after a17 * round it redelivers only targets deeper than the shallowest target that18 * round delivered, for any observer in the document. That places two19 * obligations on tree depth:20 *21 * - A nested Switcher's slot is deeper than the slot and requirement of every22 * enclosing Switcher (see {@linkSwitcherRegistration.slot}).23 * - Any other observer of a box that a published arrangement can resize24 * watches from deeper than that Switcher's slot and requirement. Otherwise25 * its notification waits a frame and the browser reports a loop error.26 */2728exporttypeArrangement="row"|"column";2930/** Where the decision is published. CSS may still derive a row from the host31 * (see `primitives.css`), so read `flex-direction` for the effective mode. */32exportconstarrangementAttribute="data-axiomatic-arrangement";3334/**35 * A row that exactly fits is a row. Engines keep layout sizes in fixed-point36 * units (1/64px in Blink and WebKit, 1/60px in Gecko), so two boxes laid out37 * from the same content can report sizes one unit apart. The tolerance is38 * four Blink units: above that noise, and far below a device pixel.39 */40exportconstfitTolerance=1/16;4142/** The whole decision: a function of two numbers and no history. */43exportfunctionarrangementFor(requirement:number, slot:number):Arrangement {44returnrequirement<=slot+fitTolerance?"row":"column";45}4647exportinterfaceSwitcherRegistration {48/** Receives {@linkarrangementAttribute}. */49readonlyswitcher:Element;50/**51 * Its inline size is the space available to the row. The published52 * arrangement must not be able to resize this box in a host that imposes a53 * width, and it must sit deeper in the tree than the slot and requirement54 * boxes of every enclosing registered Switcher, so that an ancestor's55 * decision reaches it in the same frame.56 */57readonlyslot:Element;58/**59 * Its inline size is the row requirement, laid out independently of every60 * Switcher's current arrangement. Its block size is non-zero whenever it is61 * rendered, so a 0×0 report means "not rendered" rather than "needs no room".62 */63readonlyrequirement:Element;64}6566/** What one observer delivery concluded for one Switcher. */67exportinterfaceSwitcherDecision {68readonlyswitcher:Element;69readonlyslot:number;70/** `undefined` while the requirement box is not rendered. */71readonlyrequirement:number|undefined;72/** `undefined` when the previous arrangement was kept for lack of a requirement. */73readonlyarrangement:Arrangement|undefined;74readonlywritten:boolean;75}7677interfaceMeasured {78readonlyregistration:SwitcherRegistration;79/** Starts at zero, as an unreported ResizeObserver size does. */80slot:number;81requirement:number|undefined;82}8384exportclassSwitcherCoordinator {85/** Diagnostic hook for fixtures: receives every delivery's conclusions. */86trace: ((decisions:readonlySwitcherDecision[]) =>void) |undefined;8788readonly#observer:ResizeObserver;89// Registration owners must unregister: native observation retains targets.90// `size` exposes outstanding registrations for lifecycle qualification.91readonly#measured=newWeakMap<Element, Measured>();92#size=0;9394constructor(Observer:typeofResizeObserver) {95this.#observer=newObserver((entries) =>this.#receive(entries));96 }9798/** Registrations not yet unregistered. */99getsize():number {100returnthis.#size;101 }102103/** Starts deciding for a Switcher. Call the returned function to stop. */104register(registration:SwitcherRegistration): () =>void {105const { slot, requirement } =registration;106if (this.#measured.has(slot) ||this.#measured.has(requirement))107thrownewError("This Switcher is already registered");108constmeasured:Measured= {109registration,110slot: 0,111requirement: undefined,112 };113this.#measured.set(slot, measured);114this.#measured.set(requirement, measured);115this.#observer.observe(slot);116this.#observer.observe(requirement);117this.#size++;118letregistered=true;119return () => {120if (!registered) return;121registered=false;122this.#observer.unobserve(slot);123this.#observer.unobserve(requirement);124this.#measured.delete(slot);125this.#measured.delete(requirement);126this.#size--;127 };128 }129130 #receive(entries:readonlyResizeObserverEntry[]) {131// Record every delivered number before deciding, so the result cannot132// depend on the order of entries within a delivery.133constaffected=newSet<Measured>();134for (constentryofentries) {135constmeasured=this.#measured.get(entry.target);136constsize=entry.contentBoxSize[0];137if (!measured||!size) continue;138if (entry.target===measured.registration.slot) {139measured.slot=size.inlineSize;140 } else {141constrendered=size.inlineSize>0||size.blockSize>0;142measured.requirement=rendered?size.inlineSize:undefined;143 }144affected.add(measured);145 }146constdecisions:SwitcherDecision[] = [];147for (const { registration, slot, requirement } ofaffected) {148const { switcher } =registration;149// Without a rendered requirement there is nothing to compare. Keeping150// the last arrangement lets a hidden Switcher reappear undisturbed.151constarrangement=152requirement===undefined153?undefined154:arrangementFor(requirement, slot);155constwritten=156arrangement!==undefined&&157switcher.getAttribute(arrangementAttribute) !==arrangement;158if (written) switcher.setAttribute(arrangementAttribute, arrangement);159decisions.push({ switcher, slot, requirement, arrangement, written });160 }161this.trace?.(decisions);162 }163}164165constcoordinators=newWeakMap<Document, SwitcherCoordinator>();166167/** The one coordinator, and so the one ResizeObserver, for a document. It168 * serves Switchers in the document and in any of its shadow roots. */169exportfunctionswitcherCoordinator(document:Document):SwitcherCoordinator {170letcoordinator=coordinators.get(document);171if (!coordinator) {172constview=document.defaultView;173if (!view) thrownewError("Switchers need a document with a window");174coordinator=newSwitcherCoordinator(view.ResizeObserver);175coordinators.set(document, coordinator);176 }177returncoordinator;178}