Files
paseo/packages/app/src/hooks/pointer-capabilities.ts
T
Michael ThomasandMohamed Boudra e5352c9890 fix: size controls by the primary pointer, not any touch screen (#9909)
* feat(desktop): size controls by the primary pointer, not any touch screen

The desktop app gave touch-sized controls to every machine with a touch
screen, including Windows laptops driven by a trackpad. In Electron, ask
whether a mouse-like primary pointer drives the UI instead: Chromium
reports a fine primary pointer for those laptops and a coarse one for a
tablet with no mouse or trackpad attached.

Browsers and native keep the touch-screen check, so an iPad keeps touch
sizes with a trackpad attached. Swipe gestures still follow the
touch-screen capability.

Refs #9892

* feat(app): size controls by the primary pointer on every platform

Replace the Electron-only check with one rule everywhere: touch-sized
when touch is the primary input, `(pointer: coarse)`, rather than when a
touch screen exists at all. WebKit keeps touch primary on an iPad with a
trackpad attached, so iPad Safari stays touch-sized; Chromium reports a
fine primary pointer for a touch-screen laptop driven by its trackpad, in
the browser and the desktop app. Native is always touch-sized.

`hasTouchScreen()` and its `maxTouchPoints` fallback had no other caller
and are removed. Swipe gestures still follow the touch-screen capability.

Refs #9892

* test(app): exercise pointer sizing through the media source

* test(app): cover touch laptops with a primary pointer

---------

Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
2026-11-17 10:48:34 +07:00

95 lines
3.7 KiB
TypeScript

import { useSyncExternalStore } from "react";
import { isWeb } from "@/constants/platform";
// A touch tablet running the desktop layout is still `isWeb === false || true`
// depending on the build, so neither platform gate answers "can this user
// hover and point precisely" or "can this user touch the screen". Only the
// media queries do, and the same queries notify when the answer changes
// (detachable keyboards, tablet mode, devtools touch emulation).
const FINE_POINTER_QUERY = "(hover: hover) and (pointer: fine)";
const TOUCH_SCREEN_QUERY = "(any-pointer: coarse)";
const TOUCH_PRIMARY_POINTER_QUERY = "(pointer: coarse)";
export interface PointerCapabilities {
/** A mouse-like pointer drives the UI. */
finePointer: boolean;
/** A touch screen is available, possibly next to a mouse on hybrid devices. */
touchScreen: boolean;
}
type MatchMedia = (query: string) => MediaQueryList;
export interface PointerCapabilitiesSource {
getSnapshot: () => PointerCapabilities;
subscribe: (onChange: () => void) => () => void;
hasTouchPrimaryPointer: () => boolean;
}
export function createPointerCapabilitiesSource(
matchMedia: MatchMedia | null,
): PointerCapabilitiesSource {
const finePointerQuery = matchMedia?.(FINE_POINTER_QUERY) ?? null;
const touchScreenQuery = matchMedia?.(TOUCH_SCREEN_QUERY) ?? null;
const touchPrimaryPointerQuery = matchMedia?.(TOUCH_PRIMARY_POINTER_QUERY) ?? null;
let snapshot: PointerCapabilities = { finePointer: false, touchScreen: false };
return {
hasTouchPrimaryPointer: () => touchPrimaryPointerQuery?.matches ?? false,
getSnapshot() {
const finePointer = finePointerQuery?.matches ?? false;
const touchScreen = touchScreenQuery?.matches ?? false;
if (snapshot.finePointer !== finePointer || snapshot.touchScreen !== touchScreen) {
snapshot = { finePointer, touchScreen };
}
return snapshot;
},
subscribe(onChange) {
finePointerQuery?.addEventListener("change", onChange);
touchScreenQuery?.addEventListener("change", onChange);
return () => {
finePointerQuery?.removeEventListener("change", onChange);
touchScreenQuery?.removeEventListener("change", onChange);
};
},
};
}
function browserMatchMedia(): MatchMedia | null {
if (typeof window === "undefined" || typeof window.matchMedia !== "function") return null;
return (query) => window.matchMedia(query);
}
// Native builds run on iOS and Android touch screens. A paired mouse or pen is
// filtered per gesture event by its pointer type, not by this capability.
const NATIVE_POINTER_CAPABILITIES: PointerCapabilities = { finePointer: false, touchScreen: true };
const nativeSource: PointerCapabilitiesSource = {
getSnapshot: () => NATIVE_POINTER_CAPABILITIES,
subscribe: () => () => {},
hasTouchPrimaryPointer: () => true,
};
const source = isWeb ? createPointerCapabilitiesSource(browserMatchMedia()) : nativeSource;
const SERVER_SNAPSHOT: PointerCapabilities = { finePointer: false, touchScreen: false };
function getServerSnapshot(): PointerCapabilities {
return SERVER_SNAPSHOT;
}
export function usePointerCapabilities(): PointerCapabilities {
return useSyncExternalStore(source.subscribe, source.getSnapshot, getServerSnapshot);
}
/** True when a mouse-like pointer drives the UI, read at call time. */
export function hasFinePointer(): boolean {
return source.getSnapshot().finePointer;
}
/**
* True when touch is the primary input, read at call time. iPadOS keeps touch primary with a
* trackpad attached; a laptop with a touch screen that its trackpad drives reports a fine primary
* pointer.
*/
export function hasTouchPrimaryPointer(): boolean {
return source.hasTouchPrimaryPointer();
}