mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
chore(gpui): install runtime packages from release tarballs
Replace the vendored react-native-gpui tree with pinned tarballs from the getpaseo/react-native-gpui paseo-2 release for the four runtime packages and seven @react-native-gpui modules, and add a --local install path for testing against a local fork checkout.
This commit is contained in:
@@ -1,3 +1,2 @@
|
||||
vendor/react-native-gpui/** linguist-vendored
|
||||
packages/app/gpui/package-lock.json linguist-generated
|
||||
packages/app/gpui/desktop/Cargo.lock linguist-generated
|
||||
|
||||
@@ -100,8 +100,4 @@ app.json
|
||||
# Project-local Serena configuration and memories.
|
||||
.serena/
|
||||
|
||||
# Vendored experimental React Native desktop runtime.
|
||||
!vendor/
|
||||
!vendor/react-native-gpui/
|
||||
!vendor/react-native-gpui/**
|
||||
!packages/app/gpui/app.json
|
||||
|
||||
+1
-2
@@ -16,7 +16,6 @@
|
||||
"**/*.gen.ts",
|
||||
"**/*.gen.tsx",
|
||||
"**/*.icon/**",
|
||||
"packages/react-native-enriched-markdown/**",
|
||||
"vendor/**"
|
||||
"packages/react-native-enriched-markdown/**"
|
||||
]
|
||||
}
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
"options": {
|
||||
"typeAware": false
|
||||
},
|
||||
"ignorePatterns": [".dev/**", "packages/react-native-enriched-markdown/**", "vendor/**"],
|
||||
"ignorePatterns": [".dev/**", "packages/react-native-enriched-markdown/**"],
|
||||
"plugins": ["react", "react-perf", "unicorn", "typescript", "oxc", "import", "promise"],
|
||||
"jsPlugins": ["./scripts/oxlint/paseo-plugin.mjs"],
|
||||
"categories": {
|
||||
|
||||
+11
-23
@@ -96,7 +96,7 @@ iOS, Android, web, and macOS all run from the same app code. This target does to
|
||||
behind the interface the app already uses.
|
||||
- **Fix the renderer, not the React code.** When something looks or behaves
|
||||
wrong only on GPUI, such as the tab track, the same React code is correct on
|
||||
every other platform. The defect is in `vendor/react-native-gpui`; fix it there.
|
||||
every other platform. The defect is in [getpaseo/react-native-gpui](https://github.com/getpaseo/react-native-gpui); fix it there.
|
||||
|
||||
## Approach and renderer choice
|
||||
|
||||
@@ -136,38 +136,26 @@ controlled CPU, memory, startup, or frame-time results against these alternative
|
||||
|
||||
| Area | Why it is here |
|
||||
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `vendor/react-native-gpui/` | Editable, pinned runtime source. This accounts for roughly 130,000 lines of the diff. GitHub marks it as vendored; inspect [UPSTREAM](../../../vendor/react-native-gpui/UPSTREAM) for provenance and the local patches. |
|
||||
| npm and Cargo lockfiles | Reproduce the experiment's dependencies. Roughly 15,000 generated lines, collapsed on GitHub. |
|
||||
| `packages/app/gpui/package.json` and `package-lock.json` | Pinned tarballs from a [getpaseo/react-native-gpui](https://github.com/getpaseo/react-native-gpui/releases) release (`paseo-*`) for `@react-native-gpui/{cli,codegen,core,hermes}` and the seven `@react-native-gpui/*` modules. |
|
||||
| `packages/app/gpui/desktop/Cargo.lock` | Pins Rust crates for the desktop shell (`../node_modules/@react-native-gpui/core/ReactGpui/...` path dependencies). |
|
||||
| `desktop/`, `index.js`, `routes.js`, Metro/Babel config, root launcher | Register the full app, select the native runtime, compile native services, and bundle the actual route graph. The explicit route context prevents an apparently successful build with no app routes. |
|
||||
| `adapters/` | Desktop implementations of npm packages used by the app, plus explicit unavailable-capability implementations for mobile packages. Limited ports, not complete replacements for Expo/Unistyles. App-module overrides live next to the shared files as `.gpui.ts` / `.gpui.tsx`. |
|
||||
| Shared app edits | One capability gate for alpha-mask shimmer. GPUI shows the logo and labels without that effect. Existing web/iOS/Android paths keep their behavior. |
|
||||
| Native tests and this guide | Make the working slice reproducible and give contributors bounded follow-up work. |
|
||||
|
||||
Upstream examples, its tester/testing package, website, unused fonts/vector-icon
|
||||
ports, starter navigation dependencies, sample icon, and unused nested Cargo config
|
||||
are omitted. Build outputs, native binaries, downloaded SDKs, logs, screenshots,
|
||||
and the exploratory size-measurement copy stay out of Git.
|
||||
### Testing changes against a local fork checkout
|
||||
|
||||
The retained runtime still includes its build machinery, platform backends,
|
||||
headers, and tests. Arbitrarily deleting those to reduce line count makes an
|
||||
upstream refresh harder. Vendoring can later be replaced by a pinned dependency
|
||||
once the local patches are upstreamed and distribution is reproducible.
|
||||
To build Paseo against a local checkout of `getpaseo/react-native-gpui` instead of the pinned release:
|
||||
|
||||
### Editing the runtime
|
||||
```sh
|
||||
npm run gpui:install -- --local /path/to/react-native-gpui
|
||||
```
|
||||
|
||||
Edit `vendor/react-native-gpui`, then run `npm run gpui:install` before rebuilding.
|
||||
npm copies local packages rather than symlinking them; the install command
|
||||
refreshes those copies even when their versions have not changed.
|
||||
|
||||
There are two local renderer changes:
|
||||
|
||||
- `node_view.rs`: a `pointerEvents="box-none"` view must not install its own hit
|
||||
area. Before the patch, Paseo's empty portal overlay intercepted sidebar clicks.
|
||||
- `StatusBar.desktop.js`: desktop windows do not have the mobile OS status bar.
|
||||
You can also pass the path via `PASEO_GPUI_LOCAL=/path/to/react-native-gpui npm run gpui:install`.
|
||||
This installs locked dependencies and the seven `@react-native-gpui/*` modules from the release, then packs `@react-native-gpui/{cli,codegen,core,hermes}` from the local checkout into `packages/app/gpui/node_modules/@react-native-gpui/` without modifying `package.json` or `package-lock.json`. Re-run the same command after editing the fork, then rebuild (`npm run build:gpui` or `npm run dev:gpui`). Run `npm run gpui:install` without `--local` to restore the release packages.
|
||||
|
||||
GPUI remains a pinned Cargo git dependency. Upstream tooling downloads Hermes
|
||||
and React Native prebuilt libraries. The snapshot is not an offline or fully
|
||||
source-built distribution, and release packaging has not been verified.
|
||||
and builds the C++ runtime when needed; release packaging has not been verified across all platforms.
|
||||
|
||||
## Working slice and limits
|
||||
|
||||
|
||||
Generated
+404
-369
File diff suppressed because it is too large
Load Diff
@@ -10,16 +10,16 @@
|
||||
"@formatjs/intl-numberformat": "^9.5.0",
|
||||
"@formatjs/intl-pluralrules": "^6.4.0",
|
||||
"@formatjs/intl-segmenter": "^12.3.0",
|
||||
"@react-native-gpui/codegen": "file:../../../vendor/react-native-gpui/packages/codegen",
|
||||
"@react-native-gpui/core": "file:../../../vendor/react-native-gpui/packages/core",
|
||||
"@react-native-gpui/fs": "file:../../../vendor/react-native-gpui/modules/fs",
|
||||
"@react-native-gpui/gesture-handler": "file:../../../vendor/react-native-gpui/modules/gesture-handler",
|
||||
"@react-native-gpui/hermes": "file:../../../vendor/react-native-gpui/packages/hermes",
|
||||
"@react-native-gpui/reanimated": "file:../../../vendor/react-native-gpui/modules/reanimated",
|
||||
"@react-native-gpui/safe-area-context": "file:../../../vendor/react-native-gpui/modules/safe-area-context",
|
||||
"@react-native-gpui/screens": "file:../../../vendor/react-native-gpui/modules/screens",
|
||||
"@react-native-gpui/svg": "file:../../../vendor/react-native-gpui/modules/svg",
|
||||
"@react-native-gpui/worklets": "file:../../../vendor/react-native-gpui/modules/worklets",
|
||||
"@react-native-gpui/codegen": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-codegen-0.87.0-alpha.2.tgz",
|
||||
"@react-native-gpui/core": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-core-0.87.0-alpha.5.tgz",
|
||||
"@react-native-gpui/fs": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-fs-0.0.1-alpha.1.tgz",
|
||||
"@react-native-gpui/gesture-handler": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-gesture-handler-3.3.0-alpha.1.tgz",
|
||||
"@react-native-gpui/hermes": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-hermes-0.87.0-alpha.1.tgz",
|
||||
"@react-native-gpui/reanimated": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-reanimated-4.7.0-alpha.1.tgz",
|
||||
"@react-native-gpui/safe-area-context": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-safe-area-context-5.10.0-alpha.0.tgz",
|
||||
"@react-native-gpui/screens": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-screens-4.28.0-alpha.2.tgz",
|
||||
"@react-native-gpui/svg": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-svg-15.15.0-alpha.1.tgz",
|
||||
"@react-native-gpui/worklets": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-worklets-0.13.0-alpha.1.tgz",
|
||||
"@react-native/assets-registry": "^0.87.1",
|
||||
"react": "19.2.3",
|
||||
"react-native": "0.87.1",
|
||||
@@ -32,7 +32,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@react-native-community/cli": "20.2.0",
|
||||
"@react-native-gpui/cli": "file:../../../vendor/react-native-gpui/packages/cli",
|
||||
"@react-native-gpui/cli": "https://github.com/getpaseo/react-native-gpui/releases/download/paseo-2/react-native-gpui-cli-0.87.0-alpha.5.tgz",
|
||||
"@react-native/babel-preset": "0.87.1",
|
||||
"@react-native/metro-config": "0.87.1",
|
||||
"typescript": "^5.8.3"
|
||||
|
||||
+63
-17
@@ -1,14 +1,17 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { cpSync, mkdirSync, readFileSync } from "node:fs";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const root = fileURLToPath(new URL("..", import.meta.url));
|
||||
const project = path.join(root, "packages/app/gpui");
|
||||
const action = process.argv[2] ?? "run";
|
||||
const extra = process.argv.slice(3);
|
||||
const artifacts = path.join(root, ".artifacts/gpui");
|
||||
const localRuntimePackages = ["cli", "codegen", "core", "hermes"];
|
||||
mkdirSync(artifacts, { recursive: true });
|
||||
|
||||
function run(command, args, env = {}) {
|
||||
const result = spawnSync(command, args, {
|
||||
cwd: project,
|
||||
@@ -23,21 +26,64 @@ function run(command, args, env = {}) {
|
||||
if (result.error) throw result.error;
|
||||
if (result.status !== 0) process.exit(result.status ?? 1);
|
||||
}
|
||||
if (action === "install") {
|
||||
run("npm", ["install", "--workspaces=false"]);
|
||||
// npm copies file: packages. Refresh vendored source even when its version
|
||||
// did not change, so edits to the native renderer reach the next Cargo build.
|
||||
const manifest = JSON.parse(readFileSync(path.join(project, "package.json"), "utf8"));
|
||||
for (const [name, spec] of Object.entries({
|
||||
...manifest.dependencies,
|
||||
...manifest.devDependencies,
|
||||
})) {
|
||||
if (spec.startsWith("file:"))
|
||||
cpSync(path.resolve(project, spec.slice(5)), path.join(project, "node_modules", name), {
|
||||
recursive: true,
|
||||
preserveTimestamps: true,
|
||||
});
|
||||
|
||||
function parseLocalInstallArg(args) {
|
||||
let localRepo = process.env.PASEO_GPUI_LOCAL?.trim() || "";
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const arg = args[i];
|
||||
if (arg === "--local") {
|
||||
localRepo = args[++i] ?? "";
|
||||
if (!localRepo) throw new Error("--local requires a path to a react-native-gpui checkout");
|
||||
} else if (arg.startsWith("--local=")) {
|
||||
localRepo = arg.slice("--local=".length);
|
||||
} else {
|
||||
throw new Error(`Unknown gpui:install argument: ${arg}`);
|
||||
}
|
||||
}
|
||||
return localRepo ? path.resolve(localRepo) : null;
|
||||
}
|
||||
|
||||
function installLocalRuntime(localRepo) {
|
||||
for (const name of localRuntimePackages) {
|
||||
const manifestPath = path.join(localRepo, "packages", name, "package.json");
|
||||
if (!existsSync(manifestPath)) {
|
||||
throw new Error(`Missing local package manifest: ${manifestPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
const stage = mkdtempSync(path.join(os.tmpdir(), "paseo-gpui-local-"));
|
||||
try {
|
||||
for (const name of localRuntimePackages) {
|
||||
const pkgDir = path.join(localRepo, "packages", name);
|
||||
const output = execFileSync("npm", ["pack", "--json", "--pack-destination", stage], {
|
||||
cwd: pkgDir,
|
||||
encoding: "utf8",
|
||||
shell: process.platform === "win32",
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const [packed] = JSON.parse(output);
|
||||
if (!packed?.filename) {
|
||||
throw new Error(`npm pack produced no tarball for local package ${name}`);
|
||||
}
|
||||
const destDir = path.join(project, "node_modules", "@react-native-gpui", name);
|
||||
rmSync(destDir, { recursive: true, force: true });
|
||||
mkdirSync(destDir, { recursive: true });
|
||||
execFileSync(
|
||||
"tar",
|
||||
["-xzf", path.join(stage, packed.filename), "--strip-components=1", "-C", destDir],
|
||||
{ stdio: "inherit" },
|
||||
);
|
||||
process.stdout.write(`Installed local @react-native-gpui/${name} from ${pkgDir}\n`);
|
||||
}
|
||||
} finally {
|
||||
rmSync(stage, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
if (action === "install") {
|
||||
const localRepo = parseLocalInstallArg(extra);
|
||||
run("npm", ["ci", "--workspaces=false"]);
|
||||
if (localRepo) installLocalRuntime(localRepo);
|
||||
} else if (action === "bundle") {
|
||||
run("node", [
|
||||
"node_modules/react-native/cli.js",
|
||||
|
||||
Vendored
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
Vendored
-59
@@ -1,59 +0,0 @@
|
||||
Repository: https://github.com/streetwriters/react-native-gpui
|
||||
Revision: ad502d80d828fed59c468eb15a90b1fa03d21125
|
||||
Modules repository: https://github.com/streetwriters/react-native-gpui-modules
|
||||
Modules revision: 7435b7f4dfdaa001f10099214ccbb23fe81f9c51
|
||||
License: MIT; see LICENSE and the licenses retained within packages.
|
||||
|
||||
Snapshot: packages/{cli,codegen,core,hermes} from react-native-gpui, and
|
||||
modules/{fs,gesture-handler,reanimated,safe-area-context,screens,svg,worklets}
|
||||
from react-native-gpui-modules (where upstream moved modules/ in 897fe925).
|
||||
Examples, tester, testing package, website, unused modules, and unused
|
||||
fonts/vector-icons ports are not included. Upstream tests and build sources
|
||||
within retained packages stay with the snapshot so contributors can inspect
|
||||
and modify the renderer.
|
||||
|
||||
Local patches (all other retained files match the revisions above):
|
||||
- packages/core/src-gpui/Libraries/Components/Pressable/Pressable.desktop.js:
|
||||
supply hovered to style and children render props; upstream now carries the
|
||||
GpuiJsEngine.h W3C hover flag and metro.config.js self-import bypass.
|
||||
native.test.ts covers callbacks, render props, and nested row hover using
|
||||
appearance-regression.fixture.js.
|
||||
- packages/cli/src/package-desktop.js: ad-hoc sign macOS packages so their
|
||||
bundle resources are sealed, with templates/adhoc.entitlements.plist allowing
|
||||
bundled ad-hoc dylibs under hardened runtime; covered by package-desktop.test.js.
|
||||
- packages/cli/src/commands.js: preserve Cargo's generated dSYM symlink so
|
||||
subsequent builds can replace it; dereference when copying symbols into the
|
||||
distribution instead of linking back into the build tree. Covered by
|
||||
commands.test.js.
|
||||
- packages/core/ReactCxxPlatform/react/io/WebSocketModule.{cpp,h}: release
|
||||
failed and closed connections on the JS thread. Keeping terminal clients
|
||||
exhausts file descriptors during daemon retries; deleting them on the IO
|
||||
thread would join that same thread. websocket.native.test.ts exercises the
|
||||
real RN API against a loopback peer. ReactGpui/rn-gpui-sys/build.rs tracks
|
||||
these sources so incremental builds include the fix.
|
||||
- packages/core/ReactGpui/rn-gpui-sys/build.rs: pass Cargo's
|
||||
MACOSX_DEPLOYMENT_TARGET to CMake. Otherwise source builds default to the
|
||||
installed SDK's version even when the app targets an older macOS release.
|
||||
Also exports `RN_GPUI_LOOM_RENDERER_BUILT` so `store.rs` knows whether Loom
|
||||
or Fabric owns `topLayout`.
|
||||
- packages/core/src-gpui/Libraries/NativeComponent/BaseViewConfig.desktop.ts,
|
||||
packages/core/ReactGpui/rn-gpui-sys/src/lib.rs,
|
||||
packages/core/ReactGpui/rn-gpui-sys/cpp/{MountBatchBuilder,LoomViewProps}.cpp,
|
||||
packages/core/ReactGpui/rn-gpui/src/{bootstrap,view_props,store,host}.rs:
|
||||
restore standard React Native `onLayout` behavior across all components
|
||||
(Text/Paragraph, TextInput, Image, View, ScrollView, etc.) instead of
|
||||
hardcoding `topLayout` to `View` and scroll containers. Add `onLayout: true`
|
||||
to desktop `validAttributes` so JS forwards the listener flag; let Fabric's
|
||||
`ShadowTree::emitLayoutEvents` emit `topLayout` without double-dispatching
|
||||
in `store.rs` (selected explicitly via `LayoutEventOwner` at store
|
||||
construction from `rn_gpui_sys::LOOM_RENDERER`), and propagate `on_layout`
|
||||
into `ViewPropsFlat` so Loom dispatches `topLayout` for any laid-out node
|
||||
with `on_layout` set. Covered by `native.test.ts` and `store.rs` unit tests.
|
||||
|
||||
(Upstream now includes box-none pointer pass-through, uniform rounded border
|
||||
decoration, GpuiJsEngine.h hover flag, metro.config.js self-import bypass, and
|
||||
StatusBar.desktop.js.)
|
||||
|
||||
GPUI itself remains a pinned Cargo git dependency. Hermes/React Native prebuilt
|
||||
libraries are downloaded by upstream build tooling; this is not an offline or
|
||||
fully source-built distribution.
|
||||
@@ -1,17 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/fs` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Honour `RN_GPUI_APP_DATA_DIR`: when set and non-empty, `paths.appData`, `paths.appCache` and `paths.appLogs` become `$RN_GPUI_APP_DATA_DIR/<id>`, `.../cache/<id>` and `.../logs/<id>`, so several copies of an app can run with separate data.
|
||||
|
||||
## 0.0.1-alpha.1 - 2026-11-12
|
||||
|
||||
- Add `upload(url, path, options)`, which streams a file, or a range of one, as an HTTP request body in small blocks, reports progress, honours an `AbortSignal` and resolves with the response status, headers and the start of its body.
|
||||
- Add `download(url, path, options)`, which streams an HTTP response to a file through `<path>.part`, reports progress, honours an `AbortSignal` and rejects on a non-2xx status.
|
||||
|
||||
## 0.0.1-alpha.0 - 2026-11-01
|
||||
|
||||
- First release.
|
||||
- File system access for desktop apps.
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-41
@@ -1,41 +0,0 @@
|
||||
# @react-native-gpui/fs
|
||||
|
||||
Our own module: a file system API for desktop apps, shaped like Node's `node:fs`, with file dialogs and the operating system's well-known folders.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
npm install @react-native-gpui/fs
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import fs, { dialog, paths } from '@react-native-gpui/fs';
|
||||
|
||||
const text = fs.readFileSync(`${paths.documents}/notes.txt`, 'utf8');
|
||||
await fs.promises.writeFile(
|
||||
`${paths.appData}/state.json`,
|
||||
JSON.stringify(state),
|
||||
);
|
||||
|
||||
const [file] = await dialog.open({
|
||||
filters: [{ name: 'Text', extensions: ['txt'] }],
|
||||
});
|
||||
```
|
||||
|
||||
The default export has the synchronous, callback and promise forms of the common `node:fs` calls, with Node's encodings, `Stats`, `Dirent` and error codes. The module runs on its own thread, so slow files never block JavaScript or the interface.
|
||||
|
||||
## Separate app data
|
||||
|
||||
Set `RN_GPUI_APP_DATA_DIR` to a non-empty folder to run several copies of an app side by side with separate data, without overriding `HOME`. `paths.appData` becomes `$RN_GPUI_APP_DATA_DIR/<id>`, `paths.appCache` `$RN_GPUI_APP_DATA_DIR/cache/<id>` and `paths.appLogs` `$RN_GPUI_APP_DATA_DIR/logs/<id>`; the other paths are unchanged.
|
||||
|
||||
## Documentation
|
||||
|
||||
[File system](docs/file-system.md)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,29 +0,0 @@
|
||||
[package]
|
||||
name = "rn-gpui-fs"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rn-gpui-module = { version = "0.1" }
|
||||
# Dialogs with file-type filters and a default name on all three platforms, using
|
||||
# the XDG portal on Linux so no GTK is required.
|
||||
rfd = "0.15"
|
||||
# The standard cross-platform watcher.
|
||||
notify = "6"
|
||||
# Well-known folders, which differ per platform in ways not worth re-deriving.
|
||||
dirs = "5"
|
||||
# `utimes` sets both timestamps of an existing file; `std` cannot.
|
||||
filetime = "0.2"
|
||||
# `mkdtemp` with Node's retry-on-collision behaviour.
|
||||
tempfile = "3"
|
||||
# `chmod` by descriptor, and the errno constants the error mapping reads.
|
||||
libc = "0.2"
|
||||
# `realpath` without the `\\?\` prefix on Windows.
|
||||
dunce = "1"
|
||||
# Streams a download; rustls with the system certificate store, as the upload module does.
|
||||
ureq = { version = "3", default-features = false, features = ["rustls", "platform-verifier"] }
|
||||
# Hands a download's result back to the async method.
|
||||
futures = "0.3"
|
||||
|
||||
[build-dependencies]
|
||||
rn-gpui-module-build = { version = "0.1" }
|
||||
@@ -1,3 +0,0 @@
|
||||
fn main() {
|
||||
rn_gpui_module_build::generate(&["../src/NativeFs.ts"]);
|
||||
}
|
||||
@@ -1,394 +0,0 @@
|
||||
//! The native pickers, Finder/Explorer reveal, and "open with the default app".
|
||||
//!
|
||||
//! `rfd` owns the dialogs, so the platform differences (AppKit panels, the
|
||||
//! Windows common dialogs, the XDG portal on Linux) stay out of this crate. All
|
||||
//! of `rfd`'s dialog entry points are blocking, which is acceptable here: the
|
||||
//! module has a thread of its own (`THREAD = Own`), so a panel left open blocks
|
||||
//! neither JS nor a frame.
|
||||
//!
|
||||
//! # Open question: dialogs on macOS and the main thread
|
||||
//!
|
||||
//! The design flags that macOS may require the panel to be created on the main
|
||||
//! thread, and that this has to be confirmed under GPUI's run loop before it can
|
||||
//! be called done. What `rfd` 0.15's blocking API actually does on macOS is
|
||||
//! dispatch the panel onto the main queue when the process already has a running
|
||||
//! `NSApplication`, run it inline when the caller is already on the main thread,
|
||||
//! and panic when neither holds. This module is on its own thread, so the first
|
||||
//! branch is the one that applies, and the call should hop to the main thread and
|
||||
//! block this module's thread until the panel closes.
|
||||
//!
|
||||
//! That is the theory, and it is **not yet verified** under GPUI's run loop. The
|
||||
//! spike is: run the app, open a dialog from JS, and check that the panel
|
||||
//! appears, that no main-thread warning is logged, that the module thread
|
||||
//! resumes when it closes, and that the app does not deadlock while the panel is
|
||||
//! up. If any of that fails, the fallback is the one the design names: a second
|
||||
//! module, `GpuiFsDialogs`, with `THREAD = Main`, holding only the two pickers;
|
||||
//! the rest of this module stays off the main thread. Attaching the panel to the
|
||||
//! calling window (a sheet on macOS) is a further step that needs a host
|
||||
//! function exposing the active window's raw handle for `rfd`'s `set_parent`,
|
||||
//! which does not exist yet either.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
use crate::errors;
|
||||
use crate::{DialogFilter, OpenDialogOptions, SaveDialogOptions};
|
||||
|
||||
/// The open picker. An empty list means the user cancelled, which is what the JS
|
||||
/// layer expects; a cancelled dialog is not a failure.
|
||||
pub fn show_open(options: OpenDialogOptions) -> Result<Vec<String>, ModuleError> {
|
||||
let dialog = configured(
|
||||
options.title.as_deref(),
|
||||
options.default_path.as_deref(),
|
||||
&options.filters,
|
||||
);
|
||||
|
||||
let picked = if options.directory {
|
||||
// A folder picker cannot return files, and `pick_folder` selects a
|
||||
// single folder — rfd's blocking folder call has no multi-select mode.
|
||||
// So `multiple` is honoured only as far as the platform allows: a
|
||||
// caller asking for `{ directory: true, multiple: true }` gets at most
|
||||
// one path, which is better than refusing the call outright.
|
||||
dialog.pick_folder().into_iter().collect()
|
||||
} else if options.multiple {
|
||||
dialog.pick_files().unwrap_or_default()
|
||||
} else {
|
||||
dialog.pick_file().into_iter().collect()
|
||||
};
|
||||
|
||||
Ok(utf8_paths(picked))
|
||||
}
|
||||
|
||||
/// The save picker. `None` means the user cancelled; no file is created here
|
||||
/// either way, the caller decides what to write.
|
||||
pub fn show_save(options: SaveDialogOptions) -> Result<Option<String>, ModuleError> {
|
||||
let (_, from_path) = split_default_path(options.default_path.as_deref());
|
||||
// `defaultName` wins over the name implied by `defaultPath`, but a path
|
||||
// ending in a file name is still the only hint a caller may give.
|
||||
let name = options
|
||||
.default_name
|
||||
.filter(|name| !name.trim().is_empty())
|
||||
.or(from_path);
|
||||
|
||||
let mut dialog = configured(
|
||||
options.title.as_deref(),
|
||||
options.default_path.as_deref(),
|
||||
&options.filters,
|
||||
);
|
||||
if let Some(name) = name {
|
||||
dialog = dialog.set_file_name(name);
|
||||
}
|
||||
|
||||
let picked = dialog.save_file();
|
||||
// The picker never writes, so a chosen name the bridge cannot represent is
|
||||
// reported as "nothing chosen" rather than as an error the user has no way
|
||||
// to act on; the alternative is a file the caller would try to write at a
|
||||
// path JS could not have named.
|
||||
Ok(picked.as_deref().and_then(utf8_path))
|
||||
}
|
||||
|
||||
/// Shows `path` in the platform's file manager, selected where the platform has
|
||||
/// a verb for it.
|
||||
pub fn reveal(path: String) -> Result<(), ModuleError> {
|
||||
ensure_exists(&path)?;
|
||||
run(reveal_command(Path::new(&path)), &path)
|
||||
}
|
||||
|
||||
/// Opens `path` with whatever the platform associates with it.
|
||||
pub fn open_external(path: String) -> Result<(), ModuleError> {
|
||||
ensure_exists(&path)?;
|
||||
run(open_external_command(Path::new(&path)), &path)
|
||||
}
|
||||
|
||||
/// A dialog carrying everything the three calls configure the same way: title,
|
||||
/// starting directory, and the extension filters.
|
||||
fn configured(
|
||||
title: Option<&str>,
|
||||
default_path: Option<&str>,
|
||||
filters: &[DialogFilter],
|
||||
) -> rfd::FileDialog {
|
||||
let mut dialog = rfd::FileDialog::new();
|
||||
if let Some(title) = title {
|
||||
dialog = dialog.set_title(title);
|
||||
}
|
||||
if let Some(directory) = split_default_path(default_path).0 {
|
||||
dialog = dialog.set_directory(directory);
|
||||
}
|
||||
for filter in filters {
|
||||
// An empty extension list carries no information, and on some backends
|
||||
// a filter is applied as an allow-list, which would hide every file.
|
||||
if !filter.extensions.is_empty() {
|
||||
dialog = dialog.add_filter(filter.name.as_str(), &filter.extensions);
|
||||
}
|
||||
}
|
||||
dialog
|
||||
}
|
||||
|
||||
/// Splits `defaultPath` into the directory a picker should open in, and the file
|
||||
/// name to prefill when the path names a file rather than a folder.
|
||||
///
|
||||
/// The two pickers need different halves: the open dialog only starts in the
|
||||
/// directory, the save dialog also offers the name.
|
||||
fn split_default_path(default_path: Option<&str>) -> (Option<PathBuf>, Option<String>) {
|
||||
let Some(raw) = default_path.map(str::trim).filter(|path| !path.is_empty()) else {
|
||||
return (None, None);
|
||||
};
|
||||
let path = Path::new(raw);
|
||||
|
||||
let is_directory = match std::fs::metadata(path) {
|
||||
Ok(metadata) => metadata.is_dir(),
|
||||
// A save dialog is usually given a path that does not exist yet, so fall
|
||||
// back to the spelling: a trailing separator promises a directory, and
|
||||
// anything else is a proposed file name whose parent is the start.
|
||||
Err(_) => ends_with_separator(raw),
|
||||
};
|
||||
|
||||
if is_directory {
|
||||
(Some(path.to_path_buf()), None)
|
||||
} else {
|
||||
(
|
||||
path.parent()
|
||||
.filter(|parent| !parent.as_os_str().is_empty())
|
||||
.map(Path::to_path_buf),
|
||||
path.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.map(str::to_string),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a path is spelled as a directory on any of the platforms this module
|
||||
/// runs on, so a Windows path still splits correctly when read elsewhere.
|
||||
fn ends_with_separator(path: &str) -> bool {
|
||||
path.ends_with('/') || path.ends_with('\\')
|
||||
}
|
||||
|
||||
/// The picked paths as strings, dropping any the bridge cannot represent.
|
||||
///
|
||||
/// A Unix file name need not be UTF-8, but JS can only hold strings. Failing the
|
||||
/// whole selection because one of several files has an unusual name would throw
|
||||
/// away the files the user can use, so the others are returned instead.
|
||||
fn utf8_paths(paths: Vec<PathBuf>) -> Vec<String> {
|
||||
paths
|
||||
.into_iter()
|
||||
.filter_map(|path| utf8_path(&path))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn utf8_path(path: &Path) -> Option<String> {
|
||||
path.to_str().map(str::to_string)
|
||||
}
|
||||
|
||||
/// Fails with the same code `stat` would before a platform command can turn the
|
||||
/// problem into an opaque non-zero exit.
|
||||
fn ensure_exists(path: &str) -> Result<(), ModuleError> {
|
||||
// `symlink_metadata` rather than `metadata`: a dangling symlink is still a
|
||||
// row in Finder or Explorer, so revealing it is meaningful.
|
||||
std::fs::symlink_metadata(path)
|
||||
.map(|_| ())
|
||||
.map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
/// A command line, kept separate from running it so the platform branches can be
|
||||
/// asserted on without opening anything.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
struct CommandSpec {
|
||||
program: &'static str,
|
||||
args: Vec<String>,
|
||||
}
|
||||
|
||||
/// The command that reveals `path` in the platform's file manager.
|
||||
fn reveal_command(path: &Path) -> CommandSpec {
|
||||
let shown = path.to_string_lossy().into_owned();
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
// `open -R` is the only thing that selects the item rather than opening
|
||||
// its containing folder.
|
||||
CommandSpec {
|
||||
program: "open",
|
||||
args: vec!["-R".to_string(), shown],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// `/select,<path>` is a single argument, comma included.
|
||||
//
|
||||
// Note: explorer.exe is known to return exit code 1 even when it does
|
||||
// open the window, which the non-zero check below would report as a
|
||||
// failure. That needs a check on a Windows machine; if it does bite,
|
||||
// this branch should treat a successful spawn as success instead of
|
||||
// reading the exit code.
|
||||
CommandSpec {
|
||||
program: "explorer",
|
||||
args: vec![format!("/select,{shown}")],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
{
|
||||
// `xdg-open` has no portable "select this item" verb, so the containing
|
||||
// folder is the closest equivalent. A path with no parent is its own
|
||||
// directory and is opened as it is.
|
||||
let folder = path.parent().unwrap_or(path);
|
||||
CommandSpec {
|
||||
program: "xdg-open",
|
||||
args: vec![folder.to_string_lossy().into_owned()],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The command that opens `path` with the platform's default application.
|
||||
fn open_external_command(path: &Path) -> CommandSpec {
|
||||
let shown = path.to_string_lossy().into_owned();
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
CommandSpec {
|
||||
program: "open",
|
||||
args: vec![shown],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
// `start` reads its first quoted argument as the window title, so the
|
||||
// empty string is required for a path containing spaces to survive.
|
||||
CommandSpec {
|
||||
program: "cmd",
|
||||
args: vec!["/C".to_string(), "start".to_string(), String::new(), shown],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
{
|
||||
CommandSpec {
|
||||
program: "xdg-open",
|
||||
args: vec![shown],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs a command and turns a refusal into a Node-shaped error.
|
||||
///
|
||||
/// `Command` is used without a shell, so a path is one argument no matter what
|
||||
/// characters it contains; quoting and escaping are the platform's problem, not
|
||||
/// a shell's.
|
||||
fn run(spec: CommandSpec, path: &str) -> Result<(), ModuleError> {
|
||||
let status = Command::new(spec.program)
|
||||
.args(&spec.args)
|
||||
.status()
|
||||
// The program itself could not be started (missing, or not executable),
|
||||
// which is an I/O failure on the path the caller asked about.
|
||||
.map_err(|error| errors::from_io(error, path))?;
|
||||
|
||||
if status.success() {
|
||||
return Ok(());
|
||||
}
|
||||
Err(errors::invalid(
|
||||
"EIO",
|
||||
format!(
|
||||
"EIO: i/o error, {path} ({} exited with {status})",
|
||||
spec.program
|
||||
),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_default_path_that_names_a_file_splits_into_parent_and_name() {
|
||||
let file = std::env::temp_dir().join("rn-gpui-fs-dialog-default.txt");
|
||||
let (directory, name) = split_default_path(Some(&file.to_string_lossy()));
|
||||
assert_eq!(directory.as_deref(), Some(std::env::temp_dir().as_path()));
|
||||
assert_eq!(name.as_deref(), Some("rn-gpui-fs-dialog-default.txt"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_default_path_that_names_an_existing_directory_stays_a_directory() {
|
||||
let directory = std::env::temp_dir();
|
||||
let (found, name) = split_default_path(Some(&directory.to_string_lossy()));
|
||||
assert_eq!(found, Some(directory));
|
||||
assert_eq!(name, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bare_file_name_has_no_directory_to_open_in() {
|
||||
let (directory, name) = split_default_path(Some("rn-gpui-fs-report.csv"));
|
||||
assert_eq!(directory, None);
|
||||
assert_eq!(name.as_deref(), Some("rn-gpui-fs-report.csv"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_trailing_separator_marks_a_path_that_does_not_exist_as_a_directory() {
|
||||
let missing = std::env::temp_dir().join("rn-gpui-fs-not-created");
|
||||
let spelled = format!("{}/", missing.to_string_lossy());
|
||||
let (directory, name) = split_default_path(Some(&spelled));
|
||||
assert_eq!(directory, Some(missing));
|
||||
assert_eq!(name, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_default_path_means_no_directory_and_no_name() {
|
||||
assert_eq!(split_default_path(None), (None, None));
|
||||
assert_eq!(split_default_path(Some(" ")), (None, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reveal_targets_the_platform_file_manager() {
|
||||
let spec = reveal_command(Path::new("/tmp/example.txt"));
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
assert_eq!(spec.program, "open");
|
||||
assert_eq!(spec.args, ["-R", "/tmp/example.txt"]);
|
||||
}
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
{
|
||||
assert_eq!(spec.program, "xdg-open");
|
||||
// Only the folder, because xdg-open cannot select an item in it.
|
||||
assert_eq!(spec.args, ["/tmp"]);
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
assert_eq!(spec.program, "explorer");
|
||||
assert_eq!(spec.args, ["/select,/tmp/example.txt"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_external_hands_the_path_to_the_default_application() {
|
||||
let spec = open_external_command(Path::new("/tmp/example.txt"));
|
||||
assert_eq!(
|
||||
spec.args.last().map(String::as_str),
|
||||
Some("/tmp/example.txt")
|
||||
);
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
assert_eq!(spec.program, "open");
|
||||
assert_eq!(spec.args.len(), 1);
|
||||
}
|
||||
#[cfg(not(any(target_os = "macos", target_os = "windows")))]
|
||||
assert_eq!(spec.program, "xdg-open");
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
assert_eq!(spec.program, "cmd");
|
||||
// `/C start "" <path>`: the empty argument is the title `start`
|
||||
// would otherwise take the path for.
|
||||
assert_eq!(&spec.args[..3], ["/C", "start", ""]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_path_with_spaces_is_still_a_single_argument() {
|
||||
let spec = open_external_command(Path::new("/tmp/two words.txt"));
|
||||
assert_eq!(
|
||||
spec.args.last().map(String::as_str),
|
||||
Some("/tmp/two words.txt")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,363 +0,0 @@
|
||||
//! Listing, creating and removing directories, and `mkdtemp`.
|
||||
//!
|
||||
//! `readdir` returns every entry as a plain `{name, kind, parentPath}` object,
|
||||
//! which the JS layer turns into `Dirent` instances; there is no per-directory
|
||||
//! cursor to hold open because the whole listing crosses the bridge at once.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
use crate::errors;
|
||||
use crate::module::kind;
|
||||
use crate::DirEntry;
|
||||
|
||||
pub async fn readdir(path: String, recursive: bool) -> Result<Vec<DirEntry>, ModuleError> {
|
||||
readdir_sync(path, recursive)
|
||||
}
|
||||
|
||||
pub async fn mkdir(path: String, recursive: bool, mode: f64) -> Result<(), ModuleError> {
|
||||
create_dir(&path, recursive, mode)
|
||||
}
|
||||
|
||||
pub async fn rm(path: String, recursive: bool, force: bool) -> Result<(), ModuleError> {
|
||||
remove(&path, recursive, force)
|
||||
}
|
||||
|
||||
pub async fn mkdtemp(prefix: String) -> Result<String, ModuleError> {
|
||||
let directory = tempfile::Builder::new()
|
||||
.prefix(&prefix)
|
||||
.tempdir_in(std::env::temp_dir())
|
||||
.map_err(|error| errors::from_io(error, &prefix))?;
|
||||
// The caller asked for a directory to keep, so it must outlive this frame;
|
||||
// dropping the guard would delete it again.
|
||||
Ok(path_string(directory.keep()))
|
||||
}
|
||||
|
||||
pub fn readdir_sync(path: String, recursive: bool) -> Result<Vec<DirEntry>, ModuleError> {
|
||||
let entries = list(Path::new(&path))?;
|
||||
if !recursive {
|
||||
return Ok(entries);
|
||||
}
|
||||
let mut collected = entries;
|
||||
// Depth-first, appending each level as it is reached. Iterative rather than
|
||||
// recursive: a deep tree should cost heap, not stack.
|
||||
let mut pending: Vec<PathBuf> = collected
|
||||
.iter()
|
||||
.filter(|entry| entry.kind == kind::DIRECTORY)
|
||||
.map(|entry| Path::new(&entry.parent_path).join(&entry.name))
|
||||
.collect();
|
||||
while let Some(directory) = pending.pop() {
|
||||
let children = list(&directory)?;
|
||||
pending.extend(
|
||||
children
|
||||
.iter()
|
||||
.filter(|entry| entry.kind == kind::DIRECTORY)
|
||||
.map(|entry| Path::new(&entry.parent_path).join(&entry.name)),
|
||||
);
|
||||
collected.extend(children);
|
||||
}
|
||||
Ok(collected)
|
||||
}
|
||||
|
||||
pub fn mkdir_sync(path: String, recursive: bool, mode: f64) -> Result<bool, ModuleError> {
|
||||
create_dir(&path, recursive, mode)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn rm_sync(path: String, recursive: bool, force: bool) -> Result<bool, ModuleError> {
|
||||
remove(&path, recursive, force)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// The direct children of `path`, in the order the file system lists them.
|
||||
///
|
||||
/// `DirEntry::file_type` does not follow symbolic links, so a link is reported
|
||||
/// as a link rather than as whatever it points at.
|
||||
fn list(path: &Path) -> Result<Vec<DirEntry>, ModuleError> {
|
||||
let name = path_string(path.to_path_buf());
|
||||
let iterator = fs::read_dir(path).map_err(|error| errors::from_io(error, &name))?;
|
||||
let mut entries = Vec::new();
|
||||
for entry in iterator {
|
||||
let entry = entry.map_err(|error| errors::from_io(error, &name))?;
|
||||
let file_type = entry
|
||||
.file_type()
|
||||
.map_err(|error| errors::from_io(error, &name))?;
|
||||
entries.push(DirEntry {
|
||||
name: entry.file_name().to_string_lossy().into_owned(),
|
||||
kind: entry_kind(&file_type),
|
||||
parent_path: name.clone(),
|
||||
});
|
||||
}
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
fn entry_kind(file_type: &fs::FileType) -> f64 {
|
||||
if file_type.is_file() {
|
||||
kind::FILE
|
||||
} else if file_type.is_dir() {
|
||||
kind::DIRECTORY
|
||||
} else if file_type.is_symlink() {
|
||||
kind::SYMLINK
|
||||
} else {
|
||||
kind::OTHER
|
||||
}
|
||||
}
|
||||
|
||||
fn create_dir(path: &str, recursive: bool, mode: f64) -> Result<(), ModuleError> {
|
||||
if recursive {
|
||||
// `create_dir_all` succeeds on an existing directory, which is what
|
||||
// Node's `{recursive: true}` promises.
|
||||
return fs::create_dir_all(path).map_err(|error| errors::from_io(error, path));
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::DirBuilderExt as _;
|
||||
let mut builder = fs::DirBuilder::new();
|
||||
builder.mode(crate::module::mode(mode));
|
||||
builder
|
||||
.create(path)
|
||||
.map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
// Windows has no mode for a new directory, and `create_dir` would reject
|
||||
// the `DirBuilder` extension that only exists on Unix.
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = mode;
|
||||
fs::create_dir(path).map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
}
|
||||
|
||||
fn remove(path: &str, recursive: bool, force: bool) -> Result<(), ModuleError> {
|
||||
// `symlink_metadata`, so a link is removed as a link instead of acting on
|
||||
// its target.
|
||||
let metadata = match fs::symlink_metadata(path) {
|
||||
Ok(metadata) => metadata,
|
||||
Err(error) if force && errors::code_for(&error) == "ENOENT" => return Ok(()),
|
||||
Err(error) => return Err(errors::from_io(error, path)),
|
||||
};
|
||||
if recursive && metadata.is_dir() {
|
||||
return fs::remove_dir_all(path).map_err(|error| errors::from_io(error, path));
|
||||
}
|
||||
if metadata.is_dir() {
|
||||
// `remove_dir` fails with ENOTEMPTY on Unix and a folder-not-empty error
|
||||
// on Windows, which the mapping turns into the same code; removing a
|
||||
// file with it reports what Node calls ENOTDIR.
|
||||
fs::remove_dir(path).map_err(|error| errors::from_io(error, path))
|
||||
} else {
|
||||
fs::remove_file(path).map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
}
|
||||
|
||||
fn path_string(path: PathBuf) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// The async wrappers never suspend, so polling once with a no-op waker is
|
||||
/// enough and keeps the tests independent of a runtime.
|
||||
fn block_on<F: std::future::Future>(future: F) -> F::Output {
|
||||
let mut future = std::pin::pin!(future);
|
||||
let waker = std::task::Waker::noop();
|
||||
let mut context = std::task::Context::from_waker(waker);
|
||||
// One poll, no loop: these helpers do their work inline, so a `Pending`
|
||||
// would mean the helper is not synchronous after all, which the test
|
||||
// should fail loudly on rather than spin.
|
||||
match future.as_mut().poll(&mut context) {
|
||||
std::task::Poll::Ready(value) => value,
|
||||
std::task::Poll::Pending => panic!("the wrappers are synchronous"),
|
||||
}
|
||||
}
|
||||
|
||||
fn tempdir() -> TempDir {
|
||||
TempDir::new().expect("a temporary directory")
|
||||
}
|
||||
|
||||
fn text(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// The listed paths, with separators normalised to `/`.
|
||||
///
|
||||
/// The entries themselves carry the platform separator, which on Windows is
|
||||
/// `\`; building the expected path with `/` and comparing would test the
|
||||
/// separator rather than the listing.
|
||||
fn names(entries: &[DirEntry]) -> Vec<String> {
|
||||
let mut names: Vec<String> = entries
|
||||
.iter()
|
||||
.map(|entry| format!("{}/{}", entry.parent_path, entry.name).replace('\\', "/"))
|
||||
.collect();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
|
||||
fn tree(root: &Path) {
|
||||
fs::create_dir_all(root.join("nested").join("deep")).expect("nested");
|
||||
fs::write(root.join("top.txt"), b"top").expect("seed");
|
||||
fs::write(root.join("nested").join("middle.txt"), b"middle").expect("seed");
|
||||
fs::write(root.join("nested").join("deep").join("leaf.txt"), b"leaf").expect("seed");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readdir_lists_direct_children_only() {
|
||||
let dir = tempdir();
|
||||
tree(dir.path());
|
||||
|
||||
let entries = readdir_sync(text(dir.path()), false).expect("readdir");
|
||||
// `names` normalises separators, so the expected paths are built from a
|
||||
// normalised root rather than the platform one.
|
||||
let root = text(dir.path()).replace('\\', "/");
|
||||
assert_eq!(
|
||||
names(&entries),
|
||||
vec![format!("{root}/nested"), format!("{root}/top.txt")]
|
||||
);
|
||||
|
||||
let top = entries
|
||||
.iter()
|
||||
.find(|entry| entry.name == "top.txt")
|
||||
.expect("top.txt");
|
||||
assert_eq!(top.kind, kind::FILE);
|
||||
assert_eq!(top.parent_path, text(dir.path()));
|
||||
let nested = entries
|
||||
.iter()
|
||||
.find(|entry| entry.name == "nested")
|
||||
.expect("nested");
|
||||
assert_eq!(nested.kind, kind::DIRECTORY);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readdir_recursive_reaches_every_descendant() {
|
||||
let dir = tempdir();
|
||||
tree(dir.path());
|
||||
|
||||
let entries = block_on(readdir(text(dir.path()), true)).expect("readdir");
|
||||
// Two entries per directory, three directories deep.
|
||||
assert_eq!(entries.len(), 5);
|
||||
let listed = names(&entries);
|
||||
let root = text(dir.path()).replace('\\', "/");
|
||||
assert!(listed.contains(&format!("{root}/nested/deep/leaf.txt")));
|
||||
assert!(listed.contains(&format!("{root}/nested/deep")));
|
||||
|
||||
let deep = entries
|
||||
.iter()
|
||||
.find(|entry| entry.name == "deep")
|
||||
.expect("deep");
|
||||
assert_eq!(
|
||||
deep.parent_path,
|
||||
text(&dir.path().join("nested")),
|
||||
"parentPath is the containing directory"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn readdir_reports_a_link_as_a_link() {
|
||||
let dir = tempdir();
|
||||
let target = dir.path().join("target.txt");
|
||||
fs::write(&target, b"x").expect("seed");
|
||||
std::os::unix::fs::symlink(&target, dir.path().join("link.txt")).expect("symlink");
|
||||
|
||||
let entries = readdir_sync(text(dir.path()), false).expect("readdir");
|
||||
let link = entries
|
||||
.iter()
|
||||
.find(|entry| entry.name == "link.txt")
|
||||
.expect("link.txt");
|
||||
assert_eq!(link.kind, kind::SYMLINK);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mkdir_with_recursive_creates_the_chain_and_accepts_an_existing_dir() {
|
||||
let dir = tempdir();
|
||||
let nested = dir.path().join("a").join("b").join("c");
|
||||
|
||||
block_on(mkdir(text(&nested), true, f64::from(0o755u32))).expect("mkdir");
|
||||
assert!(nested.is_dir());
|
||||
|
||||
let again = mkdir_sync(text(&nested), true, f64::from(0o755u32)).expect("mkdir again");
|
||||
assert!(again, "recursive mkdir succeeds when the path exists");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mkdir_without_recursive_fails_with_eexist() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("single");
|
||||
assert!(mkdir_sync(text(&path), false, f64::from(0o755u32)).expect("mkdir"));
|
||||
assert!(path.is_dir());
|
||||
|
||||
let error =
|
||||
block_on(mkdir(text(&path), false, f64::from(0o755u32))).expect_err("mkdir twice");
|
||||
assert_eq!(error.code.as_deref(), Some("EEXIST"));
|
||||
|
||||
let missing_parent = text(&dir.path().join("no").join("such").join("dir"));
|
||||
let error =
|
||||
mkdir_sync(missing_parent, false, f64::from(0o755u32)).expect_err("missing parent");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rm_force_ignores_a_missing_path() {
|
||||
let dir = tempdir();
|
||||
let missing = text(&dir.path().join("absent"));
|
||||
|
||||
block_on(rm(missing.clone(), false, true)).expect("rm force");
|
||||
assert!(rm_sync(missing.clone(), true, true).expect("rm sync force"));
|
||||
|
||||
let error = block_on(rm(missing, false, false)).expect_err("rm without force");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rm_removes_a_file_and_a_tree() {
|
||||
let dir = tempdir();
|
||||
let file = dir.path().join("one.txt");
|
||||
fs::write(&file, b"x").expect("seed");
|
||||
block_on(rm(text(&file), false, false)).expect("rm file");
|
||||
assert!(!file.exists());
|
||||
|
||||
let root = dir.path().join("tree");
|
||||
tree(&root);
|
||||
assert!(rm_sync(text(&root), true, false).expect("rm tree"));
|
||||
assert!(!root.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rm_without_recursive_refuses_a_non_empty_directory() {
|
||||
let dir = tempdir();
|
||||
let root = dir.path().join("tree");
|
||||
tree(&root);
|
||||
|
||||
let error = block_on(rm(text(&root), false, false)).expect_err("non-empty");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOTEMPTY"));
|
||||
assert!(root.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mkdtemp_creates_a_random_directory_with_the_prefix() {
|
||||
let created = block_on(mkdtemp("rn-gpui-fs-".to_string())).expect("mkdtemp");
|
||||
let path = Path::new(&created);
|
||||
let name = path
|
||||
.file_name()
|
||||
.expect("the returned path names a directory")
|
||||
.to_string_lossy()
|
||||
.into_owned();
|
||||
|
||||
assert!(name.starts_with("rn-gpui-fs-"), "name was {name}");
|
||||
assert!(name.len() > "rn-gpui-fs-".len(), "the rest is random");
|
||||
assert!(path.is_dir());
|
||||
assert_eq!(path.parent(), Some(std::env::temp_dir().as_path()));
|
||||
|
||||
fs::remove_dir_all(path).expect("clean up");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn readdir_of_a_missing_directory_is_enoent() {
|
||||
let dir = tempdir();
|
||||
let error = readdir_sync(text(&dir.path().join("absent")), false).expect_err("readdir");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
}
|
||||
@@ -1,329 +0,0 @@
|
||||
//! `download`: an HTTP body streamed to a file.
|
||||
//!
|
||||
//! A transfer is two threads. The network thread makes the request and reads the
|
||||
//! body in chunks; the coordinator receives them, writes `<path>.part` and renames
|
||||
//! it onto `path` when the body is complete. Splitting them is what makes a cancel
|
||||
//! prompt: the coordinator polls the cancel flag between messages, so it does not
|
||||
//! wait on a socket that has gone quiet, and the network thread simply stops the
|
||||
//! next time it tries to hand over a chunk.
|
||||
//!
|
||||
//! The module's own thread is only waited on through a channel, so a slow download
|
||||
//! never blocks other file calls.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::mpsc::{self, RecvTimeoutError, SyncSender};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
use ureq::config::Config;
|
||||
use ureq::tls::{RootCerts, TlsConfig};
|
||||
use ureq::Agent;
|
||||
|
||||
use crate::{
|
||||
errors, DownloadHeader, DownloadOptions, DownloadProgress, DownloadResult, RNFsModule,
|
||||
};
|
||||
|
||||
const CHUNK: usize = 64 * 1024;
|
||||
const PROGRESS_INTERVAL: Duration = Duration::from_millis(100);
|
||||
const POLL: Duration = Duration::from_millis(25);
|
||||
/// How much of a failed response's body is kept for the error.
|
||||
const ERROR_BODY_LIMIT: u64 = 2048;
|
||||
|
||||
pub type Downloads = Mutex<HashMap<u32, Arc<AtomicBool>>>;
|
||||
|
||||
enum Message {
|
||||
Head {
|
||||
status: u16,
|
||||
headers: Vec<DownloadHeader>,
|
||||
total: Option<u64>,
|
||||
},
|
||||
Rejected {
|
||||
status: u16,
|
||||
headers: Vec<DownloadHeader>,
|
||||
body: String,
|
||||
},
|
||||
Chunk(Vec<u8>),
|
||||
End,
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
fn agent() -> &'static Agent {
|
||||
static AGENT: OnceLock<Agent> = OnceLock::new();
|
||||
AGENT.get_or_init(|| {
|
||||
let config = Config::builder()
|
||||
.http_status_as_error(false)
|
||||
.timeout_connect(Some(Duration::from_secs(30)))
|
||||
.tls_config(
|
||||
TlsConfig::builder()
|
||||
.root_certs(RootCerts::PlatformVerifier)
|
||||
.build(),
|
||||
)
|
||||
.build();
|
||||
Agent::new_with_config(config)
|
||||
})
|
||||
}
|
||||
|
||||
fn aborted() -> ModuleError {
|
||||
ModuleError::with_code("ABORT_ERR", "The download was aborted")
|
||||
}
|
||||
|
||||
fn failed(message: impl Into<String>) -> ModuleError {
|
||||
ModuleError::with_code("ERR_DOWNLOAD", message.into())
|
||||
}
|
||||
|
||||
fn id_of(value: f64) -> u32 {
|
||||
value as u32
|
||||
}
|
||||
|
||||
pub fn cancel(module: &RNFsModule, id: f64) -> bool {
|
||||
match module.downloads.lock().expect("downloads").get(&id_of(id)) {
|
||||
Some(stop) => {
|
||||
stop.store(true, Ordering::SeqCst);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch(
|
||||
url: &str,
|
||||
method: &str,
|
||||
headers: &[DownloadHeader],
|
||||
sender: &SyncSender<Message>,
|
||||
stop: &AtomicBool,
|
||||
) {
|
||||
let result = (|| -> Result<(), String> {
|
||||
let mut builder = ureq::http::Request::builder().method(method).uri(url);
|
||||
for header in headers {
|
||||
builder = builder.header(header.name.as_str(), header.value.as_str());
|
||||
}
|
||||
let request = builder.body(()).map_err(|e| e.to_string())?;
|
||||
let mut response = agent().run(request).map_err(|e| e.to_string())?;
|
||||
let status = response.status().as_u16();
|
||||
let mut listed: Vec<DownloadHeader> = Vec::new();
|
||||
for (name, value) in response.headers() {
|
||||
let value = String::from_utf8_lossy(value.as_bytes()).into_owned();
|
||||
match listed.iter_mut().find(|h| h.name == name.as_str()) {
|
||||
Some(existing) => {
|
||||
existing.value.push_str(", ");
|
||||
existing.value.push_str(&value);
|
||||
}
|
||||
None => listed.push(DownloadHeader {
|
||||
name: name.as_str().to_string(),
|
||||
value,
|
||||
}),
|
||||
}
|
||||
}
|
||||
let total = response
|
||||
.headers()
|
||||
.get("content-length")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|v| v.parse::<u64>().ok());
|
||||
let mut reader = response.body_mut().as_reader();
|
||||
if !(200..300).contains(&status) {
|
||||
let mut body = Vec::new();
|
||||
let _ = reader
|
||||
.by_ref()
|
||||
.take(ERROR_BODY_LIMIT)
|
||||
.read_to_end(&mut body);
|
||||
let _ = sender.send(Message::Rejected {
|
||||
status,
|
||||
headers: listed,
|
||||
body: String::from_utf8_lossy(&body).into_owned(),
|
||||
});
|
||||
return Ok(());
|
||||
}
|
||||
if sender
|
||||
.send(Message::Head {
|
||||
status,
|
||||
headers: listed,
|
||||
total,
|
||||
})
|
||||
.is_err()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
loop {
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
return Ok(());
|
||||
}
|
||||
let mut chunk = vec![0u8; CHUNK];
|
||||
let read = reader.read(&mut chunk).map_err(|e| e.to_string())?;
|
||||
if read == 0 {
|
||||
let _ = sender.send(Message::End);
|
||||
return Ok(());
|
||||
}
|
||||
chunk.truncate(read);
|
||||
if sender.send(Message::Chunk(chunk)).is_err() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
})();
|
||||
if let Err(message) = result {
|
||||
let _ = sender.send(Message::Failed(message));
|
||||
}
|
||||
}
|
||||
|
||||
fn transfer(
|
||||
module_events: &crate::NativeFsEvents,
|
||||
id: u32,
|
||||
url: String,
|
||||
dest: PathBuf,
|
||||
method: String,
|
||||
headers: Vec<DownloadHeader>,
|
||||
stop: Arc<AtomicBool>,
|
||||
) -> Result<DownloadResult, ModuleError> {
|
||||
let mut partial = dest.clone().into_os_string();
|
||||
partial.push(".part");
|
||||
let partial = PathBuf::from(partial);
|
||||
let shown = dest.display().to_string();
|
||||
|
||||
let (sender, receiver) = mpsc::sync_channel::<Message>(8);
|
||||
{
|
||||
let stop = stop.clone();
|
||||
std::thread::spawn(move || fetch(&url, &method, &headers, &sender, &stop));
|
||||
}
|
||||
|
||||
let mut file: Option<File> = None;
|
||||
let mut head: Option<(u16, Vec<DownloadHeader>, Option<u64>)> = None;
|
||||
let mut received: u64 = 0;
|
||||
let mut last_progress = Instant::now() - PROGRESS_INTERVAL;
|
||||
let emit = |received: u64, total: Option<u64>| {
|
||||
module_events.on_download_progress(DownloadProgress {
|
||||
id: f64::from(id),
|
||||
received: received as f64,
|
||||
total: total.map_or(-1.0, |t| t as f64),
|
||||
});
|
||||
};
|
||||
|
||||
let outcome: Result<DownloadResult, ModuleError> = loop {
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
break Err(aborted());
|
||||
}
|
||||
let message = match receiver.recv_timeout(POLL) {
|
||||
Ok(message) => message,
|
||||
Err(RecvTimeoutError::Timeout) => continue,
|
||||
Err(RecvTimeoutError::Disconnected) => break Err(failed("the connection closed")),
|
||||
};
|
||||
match message {
|
||||
Message::Rejected {
|
||||
status,
|
||||
headers,
|
||||
body,
|
||||
} => {
|
||||
break Ok(DownloadResult {
|
||||
status: f64::from(status),
|
||||
headers,
|
||||
bytes: 0.0,
|
||||
body: Some(body),
|
||||
});
|
||||
}
|
||||
Message::Head {
|
||||
status,
|
||||
headers,
|
||||
total,
|
||||
} => {
|
||||
match File::create(&partial) {
|
||||
Ok(created) => file = Some(created),
|
||||
Err(e) => break Err(errors::from_io(e, &shown)),
|
||||
}
|
||||
head = Some((status, headers, total));
|
||||
emit(0, total);
|
||||
}
|
||||
Message::Chunk(bytes) => {
|
||||
let Some(target) = file.as_mut() else {
|
||||
break Err(failed("data arrived before the response"));
|
||||
};
|
||||
if let Err(e) = target.write_all(&bytes) {
|
||||
break Err(errors::from_io(e, &shown));
|
||||
}
|
||||
received += bytes.len() as u64;
|
||||
if last_progress.elapsed() >= PROGRESS_INTERVAL {
|
||||
last_progress = Instant::now();
|
||||
emit(received, head.as_ref().and_then(|h| h.2));
|
||||
}
|
||||
}
|
||||
Message::End => {
|
||||
let Some((status, headers, total)) = head.take() else {
|
||||
break Err(failed("the response had no head"));
|
||||
};
|
||||
if let Some(expected) = total {
|
||||
if received != expected {
|
||||
break Err(failed(format!(
|
||||
"the connection closed after {received} of {expected} bytes"
|
||||
)));
|
||||
}
|
||||
}
|
||||
if let Some(mut target) = file.take() {
|
||||
if let Err(e) = target.flush() {
|
||||
break Err(errors::from_io(e, &shown));
|
||||
}
|
||||
}
|
||||
if let Err(e) = std::fs::rename(&partial, &dest) {
|
||||
break Err(errors::from_io(e, &shown));
|
||||
}
|
||||
emit(received, total);
|
||||
break Ok(DownloadResult {
|
||||
status: f64::from(status),
|
||||
headers,
|
||||
bytes: received as f64,
|
||||
body: None,
|
||||
});
|
||||
}
|
||||
Message::Failed(message) => break Err(failed(message)),
|
||||
}
|
||||
};
|
||||
|
||||
drop(receiver);
|
||||
drop(file);
|
||||
let kept = matches!(&outcome, Ok(result) if result.body.is_none());
|
||||
if !kept {
|
||||
let _ = std::fs::remove_file(&partial);
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
pub async fn download(
|
||||
module: &RNFsModule,
|
||||
url: String,
|
||||
path: String,
|
||||
options: DownloadOptions,
|
||||
) -> Result<DownloadResult, ModuleError> {
|
||||
let id = id_of(options.id);
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
{
|
||||
let mut running = module.downloads.lock().expect("downloads");
|
||||
if running.contains_key(&id) {
|
||||
return Err(failed(format!("download {id} is already running")));
|
||||
}
|
||||
running.insert(id, stop.clone());
|
||||
}
|
||||
let (sender, receiver) = futures::channel::oneshot::channel();
|
||||
let events = module.events.clone();
|
||||
let method = if options.method.is_empty() {
|
||||
"GET".to_string()
|
||||
} else {
|
||||
options.method
|
||||
};
|
||||
let flag = stop.clone();
|
||||
std::thread::spawn(move || {
|
||||
let result = transfer(
|
||||
&events,
|
||||
id,
|
||||
url,
|
||||
PathBuf::from(path),
|
||||
method,
|
||||
options.headers,
|
||||
flag,
|
||||
);
|
||||
let _ = sender.send(result);
|
||||
});
|
||||
let result = receiver.await;
|
||||
module.downloads.lock().expect("downloads").remove(&id);
|
||||
result.unwrap_or_else(|_| Err(failed("the download stopped unexpectedly")))
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
//! Node-compatible error mapping.
|
||||
//!
|
||||
//! Every failure a caller sees has to look like the one Node would have raised for
|
||||
//! the same call: a `code` such as `ENOENT`, and a message in Node's format.
|
||||
//! `std::io::Error` gives a kind and, on Unix, a raw errno; neither is a Node code,
|
||||
//! so the translation lives here once rather than at each call site.
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
/// The Node code for an `std::io::Error`.
|
||||
///
|
||||
/// The raw OS error is consulted before the kind because several distinct errnos
|
||||
/// share a kind: `EEXIST` and `ENOTEMPTY` are both "already exists"-ish, and
|
||||
/// `EACCES` versus `EPERM` matter to a caller deciding whether to try again.
|
||||
pub fn code_for(error: &std::io::Error) -> &'static str {
|
||||
#[cfg(unix)]
|
||||
if let Some(errno) = error.raw_os_error() {
|
||||
let code = match errno {
|
||||
libc::ENOENT => "ENOENT",
|
||||
libc::EACCES => "EACCES",
|
||||
libc::EPERM => "EPERM",
|
||||
libc::EEXIST => "EEXIST",
|
||||
libc::ENOTDIR => "ENOTDIR",
|
||||
libc::EISDIR => "EISDIR",
|
||||
libc::ENOTEMPTY => "ENOTEMPTY",
|
||||
libc::EINVAL => "EINVAL",
|
||||
libc::ELOOP => "ELOOP",
|
||||
libc::ENOSPC => "ENOSPC",
|
||||
libc::EROFS => "EROFS",
|
||||
libc::EMFILE => "EMFILE",
|
||||
libc::ENAMETOOLONG => "ENAMETOOLONG",
|
||||
// Windows' share-violation has no Unix equivalent; callers treat it
|
||||
// as a permission problem, which is what Node's `EPERM` means there.
|
||||
_ => return code_for_kind(error.kind()),
|
||||
};
|
||||
return code;
|
||||
}
|
||||
// Windows exposes `raw_os_error` too, but its values are Win32 codes rather
|
||||
// than errnos, so the kind is the better signal there, with the few Win32
|
||||
// codes whose kind is not enough named below.
|
||||
#[cfg(not(unix))]
|
||||
if let Some(code) = win32_code(error.raw_os_error()) {
|
||||
return code;
|
||||
}
|
||||
code_for_kind(error.kind())
|
||||
}
|
||||
|
||||
/// The Node code for a Win32 error whose `ErrorKind` is too coarse.
|
||||
///
|
||||
/// `ERROR_PRIVILEGE_NOT_HELD` (1314) has no kind of its own, so it would be `EIO`.
|
||||
/// Creating a symbolic link without the privilege is the case that hits it, and
|
||||
/// libuv, which Node is built on, reports it as `EPERM`.
|
||||
#[cfg(not(unix))]
|
||||
fn win32_code(raw: Option<i32>) -> Option<&'static str> {
|
||||
match raw {
|
||||
Some(1314) => Some("EPERM"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn code_for_kind(kind: std::io::ErrorKind) -> &'static str {
|
||||
use std::io::ErrorKind;
|
||||
match kind {
|
||||
ErrorKind::NotFound => "ENOENT",
|
||||
ErrorKind::PermissionDenied => "EACCES",
|
||||
ErrorKind::AlreadyExists => "EEXIST",
|
||||
ErrorKind::InvalidInput => "EINVAL",
|
||||
ErrorKind::InvalidData => "EINVAL",
|
||||
ErrorKind::WriteZero => "ENOSPC",
|
||||
ErrorKind::StorageFull => "ENOSPC",
|
||||
ErrorKind::ReadOnlyFilesystem => "EROFS",
|
||||
ErrorKind::DirectoryNotEmpty => "ENOTEMPTY",
|
||||
ErrorKind::IsADirectory => "EISDIR",
|
||||
ErrorKind::NotADirectory => "ENOTDIR",
|
||||
_ => "EIO",
|
||||
}
|
||||
}
|
||||
|
||||
/// The human half of a Node error message, by code.
|
||||
///
|
||||
/// Node's own table, because the tests compare against messages and because these
|
||||
/// strings are what a developer recognises when a path is wrong.
|
||||
fn description_for(code: &str) -> &'static str {
|
||||
match code {
|
||||
"ENOENT" => "no such file or directory",
|
||||
"EACCES" => "permission denied",
|
||||
"EPERM" => "operation not permitted",
|
||||
"EEXIST" => "file already exists",
|
||||
"ENOTDIR" => "not a directory",
|
||||
"EISDIR" => "illegal operation on a directory",
|
||||
"ENOTEMPTY" => "directory not empty",
|
||||
"EINVAL" => "invalid argument",
|
||||
"ELOOP" => "too many symbolic links encountered",
|
||||
"ENOSPC" => "no space left on device",
|
||||
"EROFS" => "read-only file system",
|
||||
"EMFILE" => "too many open files",
|
||||
"ENAMETOOLONG" => "name too long",
|
||||
_ => "i/o error",
|
||||
}
|
||||
}
|
||||
|
||||
/// An `std::io::Error` as the `ModuleError` JS should receive.
|
||||
///
|
||||
/// `syscall` and `path` are attached by the JS layer, which knows the names the
|
||||
/// caller used; the native side would have to guess between `open`, `lstat` and
|
||||
/// the rest for what is already a single `stat` call.
|
||||
pub fn from_io(error: std::io::Error, path: &str) -> ModuleError {
|
||||
let code = code_for(&error);
|
||||
ModuleError::with_code(code, format!("{code}: {}, {path}", description_for(code)))
|
||||
}
|
||||
|
||||
/// A failure that has no `std::io::Error`, such as a closed file descriptor.
|
||||
pub fn invalid(code: &str, message: impl Into<String>) -> ModuleError {
|
||||
ModuleError::with_code(code, message)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_is_enoent_with_nodes_message() {
|
||||
let error = std::io::Error::new(std::io::ErrorKind::NotFound, "gone");
|
||||
let mapped = from_io(error, "/nope");
|
||||
assert_eq!(mapped.code.as_deref(), Some("ENOENT"));
|
||||
assert_eq!(mapped.message, "ENOENT: no such file or directory, /nope");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_denied_file_is_eacces() {
|
||||
let error = std::io::Error::new(std::io::ErrorKind::PermissionDenied, "nope");
|
||||
assert_eq!(from_io(error, "/x").code.as_deref(), Some("EACCES"));
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn a_missing_symlink_privilege_is_eperm_not_eio() {
|
||||
// ERROR_PRIVILEGE_NOT_HELD: what `symlink_dir` returns without developer
|
||||
// mode, and what Node reports as EPERM.
|
||||
let error = std::io::Error::from_raw_os_error(1314);
|
||||
assert_eq!(from_io(error, "C:\\link").code.as_deref(), Some("EPERM"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unknown_failure_still_has_a_code() {
|
||||
let error = std::io::Error::new(std::io::ErrorKind::Other, "odd");
|
||||
assert_eq!(from_io(error, "/x").code.as_deref(), Some("EIO"));
|
||||
}
|
||||
}
|
||||
@@ -1,640 +0,0 @@
|
||||
//! Reading, writing, copying, truncating, permissions and times.
|
||||
//!
|
||||
//! The async forms are thin wrappers over the sync ones: `std::fs` is blocking
|
||||
//! either way, and this module has a thread of its own, so the only difference
|
||||
//! is which thread the bridge calls the function on.
|
||||
//!
|
||||
//! Every failure goes through `errors::from_io`, so JS receives a Node code and
|
||||
//! Node's message rather than an `std::io::Error` kind.
|
||||
|
||||
use std::fs;
|
||||
use std::io::Write as _;
|
||||
use std::path::Path;
|
||||
|
||||
use rn_gpui_module::{ArrayBuffer, ModuleError};
|
||||
|
||||
use crate::errors;
|
||||
use crate::module::{kind, millis, open_options, system_time};
|
||||
use crate::StatResult;
|
||||
|
||||
pub async fn read_file(path: String) -> Result<ArrayBuffer, ModuleError> {
|
||||
read_file_sync(path)
|
||||
}
|
||||
|
||||
pub async fn read_text_file(path: String) -> Result<String, ModuleError> {
|
||||
read_text_file_sync(path)
|
||||
}
|
||||
|
||||
pub async fn write_file(
|
||||
path: String,
|
||||
data: ArrayBuffer,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<(), ModuleError> {
|
||||
write_bytes(&path, data.as_slice(), flags, mode)
|
||||
}
|
||||
|
||||
pub async fn write_text_file(
|
||||
path: String,
|
||||
data: String,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<(), ModuleError> {
|
||||
write_bytes(&path, data.as_bytes(), flags, mode)
|
||||
}
|
||||
|
||||
pub async fn stat(path: String, follow_symlinks: bool) -> Result<StatResult, ModuleError> {
|
||||
stat_of(&path, follow_symlinks)
|
||||
}
|
||||
|
||||
pub async fn rename(from: String, to: String) -> Result<(), ModuleError> {
|
||||
fs::rename(&from, &to).map_err(|error| errors::from_io(error, &from))
|
||||
}
|
||||
|
||||
pub async fn copy(from: String, to: String, recursive: bool) -> Result<(), ModuleError> {
|
||||
copy_path(&from, &to, recursive)
|
||||
}
|
||||
|
||||
pub async fn truncate(path: String, length: f64) -> Result<(), ModuleError> {
|
||||
let length = crate::module::length(length)?;
|
||||
let file = fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.open(&path)
|
||||
.map_err(|error| errors::from_io(error, &path))?;
|
||||
file.set_len(length as u64)
|
||||
.map_err(|error| errors::from_io(error, &path))
|
||||
}
|
||||
|
||||
pub async fn utimes(path: String, atime_ms: f64, mtime_ms: f64) -> Result<(), ModuleError> {
|
||||
let atime = filetime::FileTime::from_system_time(system_time(atime_ms));
|
||||
let mtime = filetime::FileTime::from_system_time(system_time(mtime_ms));
|
||||
filetime::set_file_times(&path, atime, mtime).map_err(|error| errors::from_io(error, &path))
|
||||
}
|
||||
|
||||
pub async fn chmod(path: String, mode: f64) -> Result<(), ModuleError> {
|
||||
set_mode(&path, mode)
|
||||
}
|
||||
|
||||
pub fn read_file_sync(path: String) -> Result<ArrayBuffer, ModuleError> {
|
||||
fs::read(&path)
|
||||
.map(ArrayBuffer::from)
|
||||
.map_err(|error| errors::from_io(error, &path))
|
||||
}
|
||||
|
||||
pub fn read_text_file_sync(path: String) -> Result<String, ModuleError> {
|
||||
let bytes = fs::read(&path).map_err(|error| errors::from_io(error, &path))?;
|
||||
// Node decodes with replacement characters instead of rejecting a file that
|
||||
// is not valid UTF-8, so reading a mis-encoded file still yields text.
|
||||
Ok(String::from_utf8_lossy(&bytes).into_owned())
|
||||
}
|
||||
|
||||
pub fn write_file_sync(
|
||||
path: String,
|
||||
data: ArrayBuffer,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<bool, ModuleError> {
|
||||
write_bytes(&path, data.as_slice(), flags, mode)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn write_text_file_sync(
|
||||
path: String,
|
||||
data: String,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<bool, ModuleError> {
|
||||
write_bytes(&path, data.as_bytes(), flags, mode)?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// `Ok(None)` for a path that is not there: that is what `existsSync` asks, and
|
||||
/// only a missing path means "no". A permission failure is still an error.
|
||||
pub fn stat_sync(path: String, follow_symlinks: bool) -> Result<Option<StatResult>, ModuleError> {
|
||||
match stat_of(&path, follow_symlinks) {
|
||||
Ok(result) => Ok(Some(result)),
|
||||
Err(error) if error.code.as_deref() == Some("ENOENT") => Ok(None),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exists_sync(path: String) -> Result<bool, ModuleError> {
|
||||
Ok(matches!(stat_sync(path, true), Ok(Some(_))))
|
||||
}
|
||||
|
||||
pub fn rename_sync(from: String, to: String) -> Result<bool, ModuleError> {
|
||||
fs::rename(&from, &to).map_err(|error| errors::from_io(error, &from))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
pub fn copy_file_sync(from: String, to: String) -> Result<bool, ModuleError> {
|
||||
fs::copy(&from, &to).map_err(|error| errors::from_io(error, &from))?;
|
||||
Ok(true)
|
||||
}
|
||||
|
||||
/// The one place a write happens, so bytes and text share the flag and mode
|
||||
/// translation exactly.
|
||||
fn write_bytes(path: &str, data: &[u8], flags: f64, mode: f64) -> Result<(), ModuleError> {
|
||||
let mut file = open_options(flags, crate::module::mode(mode))
|
||||
.open(path)
|
||||
.map_err(|error| errors::from_io(error, path))?;
|
||||
file.write_all(data)
|
||||
.map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
fn stat_of(path: &str, follow_symlinks: bool) -> Result<StatResult, ModuleError> {
|
||||
let metadata = if follow_symlinks {
|
||||
fs::metadata(path)
|
||||
} else {
|
||||
fs::symlink_metadata(path)
|
||||
}
|
||||
.map_err(|error| errors::from_io(error, path))?;
|
||||
Ok(stat_result(&metadata))
|
||||
}
|
||||
|
||||
/// The generated `StatResult`, filled from `std::fs::Metadata`.
|
||||
///
|
||||
/// Node's `Stats` names the Unix inode fields even on Windows, where they have
|
||||
/// no meaning; zero is what Node itself reports there.
|
||||
fn stat_result(metadata: &fs::Metadata) -> StatResult {
|
||||
let file_type = metadata.file_type();
|
||||
let kind = if file_type.is_file() {
|
||||
kind::FILE
|
||||
} else if file_type.is_dir() {
|
||||
kind::DIRECTORY
|
||||
} else if file_type.is_symlink() {
|
||||
kind::SYMLINK
|
||||
} else {
|
||||
kind::OTHER
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
let (mode, dev, ino, nlink, uid, gid) = {
|
||||
use std::os::unix::fs::MetadataExt as _;
|
||||
(
|
||||
metadata.mode() as f64,
|
||||
metadata.dev() as f64,
|
||||
metadata.ino() as f64,
|
||||
metadata.nlink() as f64,
|
||||
metadata.uid() as f64,
|
||||
metadata.gid() as f64,
|
||||
)
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let (mode, dev, ino, nlink, uid, gid) = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
// `ctime` is the inode change time on Unix; Windows has no such thing, and
|
||||
// its creation time is the closest equivalent.
|
||||
#[cfg(unix)]
|
||||
let ctime_ms = {
|
||||
use std::os::unix::fs::MetadataExt as _;
|
||||
metadata.ctime() as f64 * 1000.0 + metadata.ctime_nsec() as f64 / 1_000_000.0
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let ctime_ms = metadata
|
||||
.created()
|
||||
.map(millis)
|
||||
.unwrap_or_else(|_| metadata.modified().map(millis).unwrap_or(0.0));
|
||||
|
||||
// A file system that cannot report creation gives 0, as Node does, rather
|
||||
// than pretending the modification time is the birth time.
|
||||
let birthtime_ms = metadata.created().map(millis).unwrap_or(0.0);
|
||||
|
||||
StatResult {
|
||||
size: metadata.len() as f64,
|
||||
mode,
|
||||
kind,
|
||||
atime_ms: metadata.accessed().map(millis).unwrap_or(0.0),
|
||||
mtime_ms: metadata.modified().map(millis).unwrap_or(0.0),
|
||||
ctime_ms,
|
||||
birthtime_ms,
|
||||
dev,
|
||||
ino,
|
||||
nlink,
|
||||
uid,
|
||||
gid,
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_path(from: &str, to: &str, recursive: bool) -> Result<(), ModuleError> {
|
||||
let metadata = fs::symlink_metadata(from).map_err(|error| errors::from_io(error, from))?;
|
||||
if !metadata.is_dir() {
|
||||
fs::copy(from, to).map_err(|error| errors::from_io(error, from))?;
|
||||
return Ok(());
|
||||
}
|
||||
if !recursive {
|
||||
return Err(errors::invalid(
|
||||
"EISDIR",
|
||||
format!("EISDIR: illegal operation on a directory, {from}"),
|
||||
));
|
||||
}
|
||||
copy_tree(Path::new(from), Path::new(to))
|
||||
}
|
||||
|
||||
/// Refuse to copy a directory into itself or a directory beneath it.
|
||||
///
|
||||
/// The destination may not exist yet, so the comparison is against its nearest
|
||||
/// existing ancestor: `a` into `a/b/c` has to be caught before `a/b/c` exists,
|
||||
/// which is the whole point. Both sides are canonicalised so a symlinked path
|
||||
/// cannot slip past the check.
|
||||
///
|
||||
/// Node's code and wording are reproduced rather than invented; `cpSync` reports
|
||||
/// `ERR_FS_CP_EINVAL` with `Cannot copy /tmp/cpa/ to a subdirectory of self
|
||||
/// /tmp/cpa/sub`, and callers match on both.
|
||||
fn refuse_copy_into_self(from: &Path, to: &Path) -> Result<(), ModuleError> {
|
||||
let resolve = |path: &Path| -> Option<std::path::PathBuf> {
|
||||
let mut existing = path.to_path_buf();
|
||||
let mut tail = Vec::new();
|
||||
loop {
|
||||
if let Ok(canonical) = existing.canonicalize() {
|
||||
return Some(
|
||||
tail.iter()
|
||||
.rev()
|
||||
.fold(canonical, |base, part| base.join(part)),
|
||||
);
|
||||
}
|
||||
let name = existing.file_name().map(|name| name.to_owned())?;
|
||||
tail.push(name);
|
||||
if !existing.pop() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
};
|
||||
let Some(source) = resolve(from) else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(destination) = resolve(to) else {
|
||||
return Ok(());
|
||||
};
|
||||
if destination == source || destination.starts_with(&source) {
|
||||
return Err(errors::invalid(
|
||||
"ERR_FS_CP_EINVAL",
|
||||
format!(
|
||||
"Cannot copy {} to a subdirectory of self {}",
|
||||
from.display(),
|
||||
to.display()
|
||||
),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_tree(from: &Path, to: &Path) -> Result<(), ModuleError> {
|
||||
let target = to.to_string_lossy().into_owned();
|
||||
// Checked before anything is created: copying a directory into its own
|
||||
// subtree would otherwise recurse forever, and the tree it built on the way
|
||||
// down would have to be cleaned up by the caller.
|
||||
refuse_copy_into_self(from, to)?;
|
||||
fs::create_dir_all(to).map_err(|error| errors::from_io(error, &target))?;
|
||||
let children = fs::read_dir(from).map_err(|error| errors::from_io(error, &source(from)))?;
|
||||
for child in children {
|
||||
let child = child.map_err(|error| errors::from_io(error, &source(from)))?;
|
||||
let path = child.path();
|
||||
let destination = to.join(child.file_name());
|
||||
// `file_type` does not follow links, which keeps a link cycle in the
|
||||
// source tree from turning into infinite recursion.
|
||||
let file_type = child
|
||||
.file_type()
|
||||
.map_err(|error| errors::from_io(error, &source(&path)))?;
|
||||
if file_type.is_dir() {
|
||||
copy_tree(&path, &destination)?;
|
||||
} else if file_type.is_symlink() {
|
||||
copy_symlink(&path, &destination)?;
|
||||
} else {
|
||||
fs::copy(&path, &destination)
|
||||
.map_err(|error| errors::from_io(error, &source(&path)))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn copy_symlink(from: &Path, to: &Path) -> Result<(), ModuleError> {
|
||||
let target = fs::read_link(from).map_err(|error| errors::from_io(error, &source(from)))?;
|
||||
std::os::unix::fs::symlink(&target, to).map_err(|error| errors::from_io(error, &source(from)))
|
||||
}
|
||||
|
||||
/// Windows needs a privilege to create a symbolic link, so there the link is
|
||||
/// followed and its target copied, which is what a caller copying a tree wants
|
||||
/// to end up with anyway.
|
||||
#[cfg(not(unix))]
|
||||
fn copy_symlink(from: &Path, to: &Path) -> Result<(), ModuleError> {
|
||||
fs::copy(from, to)
|
||||
.map(|_| ())
|
||||
.map_err(|error| errors::from_io(error, &source(from)))
|
||||
}
|
||||
|
||||
fn source(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn set_mode(path: &str, mode: f64) -> Result<(), ModuleError> {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let bits = if mode.is_finite() && mode >= 0.0 {
|
||||
mode as u32 & 0o7777
|
||||
} else {
|
||||
0o666
|
||||
};
|
||||
fs::set_permissions(path, fs::Permissions::from_mode(bits))
|
||||
.map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
/// Windows has no permission bits. The closest equivalent is the read-only
|
||||
/// attribute, so a mode without a write bit marks the file read-only and any
|
||||
/// other mode clears it; nothing else about the mode survives.
|
||||
#[cfg(not(unix))]
|
||||
fn set_mode(path: &str, mode: f64) -> Result<(), ModuleError> {
|
||||
let metadata = fs::metadata(path).map_err(|error| errors::from_io(error, path))?;
|
||||
let mut permissions = metadata.permissions();
|
||||
permissions.set_readonly(mode.is_finite() && mode > 0.0 && mode as u32 & 0o200 == 0);
|
||||
fs::set_permissions(path, permissions).map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::module::flags;
|
||||
use tempfile::TempDir;
|
||||
|
||||
/// The async wrappers never actually suspend (they call `std::fs` directly),
|
||||
/// so a single-poll executor is enough and keeps the tests free of a runtime.
|
||||
fn block_on<F: std::future::Future>(future: F) -> F::Output {
|
||||
let mut future = std::pin::pin!(future);
|
||||
let waker = std::task::Waker::noop();
|
||||
let mut context = std::task::Context::from_waker(waker);
|
||||
// One poll, no loop: these helpers do their work inline, so a `Pending`
|
||||
// would mean the helper is not synchronous after all, which the test
|
||||
// should fail loudly on rather than spin.
|
||||
match future.as_mut().poll(&mut context) {
|
||||
std::task::Poll::Ready(value) => value,
|
||||
std::task::Poll::Pending => panic!("the wrappers are synchronous"),
|
||||
}
|
||||
}
|
||||
|
||||
fn tempdir() -> TempDir {
|
||||
TempDir::new().expect("a temporary directory")
|
||||
}
|
||||
|
||||
fn text(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// `'w'` in Node's table.
|
||||
fn write_flags() -> f64 {
|
||||
f64::from(flags::WRONLY | flags::CREAT | flags::TRUNC)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn text_round_trip() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("note.txt");
|
||||
|
||||
block_on(write_text_file(
|
||||
text(&path),
|
||||
"hello".to_string(),
|
||||
write_flags(),
|
||||
0.0,
|
||||
))
|
||||
.expect("write");
|
||||
assert_eq!(
|
||||
block_on(read_text_file(text(&path))).expect("read"),
|
||||
"hello"
|
||||
);
|
||||
assert_eq!(
|
||||
read_text_file_sync(text(&path)).expect("read sync"),
|
||||
"hello"
|
||||
);
|
||||
|
||||
assert!(
|
||||
write_text_file_sync(text(&path), "again".to_string(), write_flags(), 0.0)
|
||||
.expect("write sync")
|
||||
);
|
||||
assert_eq!(fs::read_to_string(&path).expect("on disk"), "again");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bytes_round_trip() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("bytes.bin");
|
||||
let bytes = ArrayBuffer::from(vec![0u8, 1, 254, 255]);
|
||||
|
||||
block_on(write_file(text(&path), bytes.clone(), write_flags(), 0.0)).expect("write");
|
||||
assert_eq!(
|
||||
block_on(read_file(text(&path))).expect("read").as_slice(),
|
||||
&[0u8, 1, 254, 255]
|
||||
);
|
||||
assert_eq!(
|
||||
read_file_sync(text(&path)).expect("read sync").into_vec(),
|
||||
vec![0u8, 1, 254, 255]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_append_flag_keeps_the_existing_bytes() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("log.txt");
|
||||
fs::write(&path, b"one").expect("seed");
|
||||
|
||||
block_on(write_text_file(
|
||||
text(&path),
|
||||
"two".to_string(),
|
||||
f64::from(flags::WRONLY | flags::CREAT | flags::APPEND),
|
||||
0.0,
|
||||
))
|
||||
.expect("append");
|
||||
|
||||
assert_eq!(fs::read_to_string(&path).expect("on disk"), "onetwo");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stat_reports_kind_and_size() {
|
||||
let dir = tempdir();
|
||||
let file = dir.path().join("data.bin");
|
||||
fs::write(&file, b"12345").expect("seed");
|
||||
|
||||
let stats = block_on(stat(text(&file), true)).expect("stat");
|
||||
assert_eq!(stats.kind, kind::FILE);
|
||||
assert_eq!(stats.size, 5.0);
|
||||
assert!(stats.mtime_ms > 0.0);
|
||||
#[cfg(unix)]
|
||||
assert_ne!(
|
||||
stats.mode as u32 & 0o777,
|
||||
0,
|
||||
"the mode bits come from MetadataExt"
|
||||
);
|
||||
|
||||
let directory = block_on(stat(text(dir.path()), true)).expect("stat dir");
|
||||
assert_eq!(directory.kind, kind::DIRECTORY);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn stat_follows_links_only_when_asked() {
|
||||
let dir = tempdir();
|
||||
let target = dir.path().join("target.txt");
|
||||
fs::write(&target, b"x").expect("seed");
|
||||
let link = dir.path().join("link.txt");
|
||||
std::os::unix::fs::symlink(&target, &link).expect("symlink");
|
||||
|
||||
assert_eq!(
|
||||
block_on(stat(text(&link), false)).expect("lstat").kind,
|
||||
kind::SYMLINK
|
||||
);
|
||||
assert_eq!(
|
||||
block_on(stat(text(&link), true)).expect("stat").kind,
|
||||
kind::FILE
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stat_sync_is_none_for_a_missing_path() {
|
||||
let dir = tempdir();
|
||||
let missing = text(&dir.path().join("absent.txt"));
|
||||
assert!(stat_sync(missing, true).expect("stat").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn exists_sync_is_false_for_a_missing_path() {
|
||||
let dir = tempdir();
|
||||
assert!(!exists_sync(text(&dir.path().join("absent.txt"))).expect("exists"));
|
||||
assert!(exists_sync(text(dir.path())).expect("exists"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_file_is_enoent() {
|
||||
let dir = tempdir();
|
||||
let missing = text(&dir.path().join("gone.txt"));
|
||||
|
||||
let error = block_on(read_text_file(missing.clone())).expect_err("read");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
assert!(error
|
||||
.message
|
||||
.starts_with("ENOENT: no such file or directory"));
|
||||
|
||||
let error = read_file_sync(missing).expect_err("read sync");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rename_moves_a_file() {
|
||||
let dir = tempdir();
|
||||
let from = dir.path().join("a.txt");
|
||||
let to = dir.path().join("b.txt");
|
||||
fs::write(&from, b"a").expect("seed");
|
||||
|
||||
block_on(rename(text(&from), text(&to))).expect("rename");
|
||||
assert!(!from.exists());
|
||||
assert_eq!(fs::read_to_string(&to).expect("on disk"), "a");
|
||||
|
||||
assert!(rename_sync(text(&to), text(&from)).expect("rename sync"));
|
||||
assert!(from.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_copies_a_file() {
|
||||
let dir = tempdir();
|
||||
let from = dir.path().join("one.txt");
|
||||
fs::write(&from, b"copy me").expect("seed");
|
||||
let to = dir.path().join("two.txt");
|
||||
|
||||
block_on(copy(text(&from), text(&to), false)).expect("copy");
|
||||
assert_eq!(fs::read_to_string(&to).expect("on disk"), "copy me");
|
||||
|
||||
let third = dir.path().join("three.txt");
|
||||
assert!(copy_file_sync(text(&from), text(&third)).expect("copy sync"));
|
||||
assert_eq!(fs::read_to_string(&third).expect("on disk"), "copy me");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copy_copies_a_directory_tree_when_recursive() {
|
||||
let dir = tempdir();
|
||||
let from = dir.path().join("tree");
|
||||
fs::create_dir_all(from.join("nested")).expect("tree");
|
||||
fs::write(from.join("top.txt"), b"top").expect("seed");
|
||||
fs::write(from.join("nested").join("deep.txt"), b"deep").expect("seed");
|
||||
let to = dir.path().join("copy");
|
||||
|
||||
block_on(copy(text(&from), text(&to), true)).expect("copy tree");
|
||||
assert_eq!(
|
||||
fs::read_to_string(to.join("nested").join("deep.txt")).expect("on disk"),
|
||||
"deep"
|
||||
);
|
||||
|
||||
let error = block_on(copy(text(&from), text(&dir.path().join("nope")), false))
|
||||
.expect_err("a directory without recursive");
|
||||
assert_eq!(error.code.as_deref(), Some("EISDIR"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copying_a_directory_into_itself_is_refused() {
|
||||
let dir = tempdir();
|
||||
let from = dir.path().join("tree");
|
||||
fs::create_dir_all(from.join("sub")).expect("tree");
|
||||
fs::write(from.join("top.txt"), b"top").expect("seed");
|
||||
|
||||
// Into a directory beneath itself. This used to recurse until the path
|
||||
// got too long, and left whatever it had built on the way down.
|
||||
let into_self = from.join("sub");
|
||||
let error = block_on(copy(text(&from), text(&into_self), true))
|
||||
.expect_err("copy into a subdirectory of self");
|
||||
assert_eq!(error.code.as_deref(), Some("ERR_FS_CP_EINVAL"));
|
||||
assert!(
|
||||
error.message.starts_with("Cannot copy"),
|
||||
"{}",
|
||||
error.message
|
||||
);
|
||||
// The check runs before anything is created, so the destination is still
|
||||
// just the empty directory it was.
|
||||
assert_eq!(
|
||||
fs::read_dir(&into_self).expect("still there").count(),
|
||||
0,
|
||||
"nothing may be created when the copy is refused"
|
||||
);
|
||||
|
||||
// The directory itself is the same case.
|
||||
let error = block_on(copy(text(&from), text(&from), true)).expect_err("copy onto itself");
|
||||
assert_eq!(error.code.as_deref(), Some("ERR_FS_CP_EINVAL"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate_changes_the_length() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("long.txt");
|
||||
fs::write(&path, b"0123456789").expect("seed");
|
||||
|
||||
block_on(truncate(text(&path), 4.0)).expect("truncate");
|
||||
assert_eq!(fs::read_to_string(&path).expect("on disk"), "0123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn utimes_sets_both_times() {
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("stamp.txt");
|
||||
fs::write(&path, b"x").expect("seed");
|
||||
|
||||
block_on(utimes(
|
||||
text(&path),
|
||||
1_000_000_000_000.0,
|
||||
1_100_000_000_000.0,
|
||||
))
|
||||
.expect("utimes");
|
||||
|
||||
let stats = block_on(stat(text(&path), true)).expect("stat");
|
||||
assert_eq!(stats.mtime_ms, 1_100_000_000_000.0);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn chmod_sets_the_permission_bits() {
|
||||
use std::os::unix::fs::PermissionsExt as _;
|
||||
let dir = tempdir();
|
||||
let path = dir.path().join("locked.txt");
|
||||
fs::write(&path, b"x").expect("seed");
|
||||
|
||||
block_on(chmod(text(&path), f64::from(0o640u32))).expect("chmod");
|
||||
|
||||
let mode = fs::metadata(&path).expect("metadata").permissions().mode() & 0o777;
|
||||
assert_eq!(mode, 0o640);
|
||||
}
|
||||
}
|
||||
@@ -1,697 +0,0 @@
|
||||
//! Open descriptors: `open`, `read`, `write`, `fstat`, `fsync`, `ftruncate`
|
||||
//! and `close`.
|
||||
//!
|
||||
//! A descriptor is an id from the module's table plus a clone of the file that
|
||||
//! shares its offset, which is what a descriptor is to the OS. The table owns
|
||||
//! the original, so dropping the module closes every handle; ids are never
|
||||
//! reused, so a stale `fd` that JS kept across a `close` fails with `EBADF`
|
||||
//! instead of reaching an unrelated file.
|
||||
//!
|
||||
//! These functions are synchronous even though the trait methods around them are
|
||||
//! `async`: the module has a thread of its own, so a blocking call here never
|
||||
//! blocks JS or the UI.
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{Read as _, Seek as _, Write as _};
|
||||
|
||||
use rn_gpui_module::{ArrayBuffer, ModuleError};
|
||||
|
||||
use crate::errors;
|
||||
use crate::module::{bad_fd, descriptor, flags, kind, millis, open_options};
|
||||
use crate::StatResult;
|
||||
|
||||
pub fn open(
|
||||
module: &crate::RNFsModule,
|
||||
path: String,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<f64, ModuleError> {
|
||||
// The table owns the file; everything the caller asks of it goes through
|
||||
// `module.file(fd)`, which hands back a clone sharing the same offset.
|
||||
let file = open_options(flags, crate::module::mode(mode))
|
||||
.open(&path)
|
||||
.map_err(|error| errors::from_io(error, &path))?;
|
||||
|
||||
let id = module.take_fd();
|
||||
// `RDONLY` is zero, so "writable" is the presence of any write bit rather
|
||||
// than the absence of a read bit.
|
||||
let writable = (flags as i32) & (flags::WRONLY | flags::RDWR) != 0;
|
||||
module
|
||||
.files
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
.insert(id, (file, writable));
|
||||
Ok(f64::from(id))
|
||||
}
|
||||
|
||||
pub fn read(
|
||||
module: &crate::RNFsModule,
|
||||
fd: f64,
|
||||
length: f64,
|
||||
position: f64,
|
||||
) -> Result<ArrayBuffer, ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let length = crate::module::length(length)?;
|
||||
let mut file = module.file(fd)?;
|
||||
|
||||
// Nothing larger than the file can be read, so the buffer is capped at what
|
||||
// remains rather than at what was asked for. A caller that passes a huge
|
||||
// `length` — which `readToEnd` used to do deliberately — would otherwise ask
|
||||
// for that many bytes up front and get whatever the allocator did with it.
|
||||
let cap = match offset(position)? {
|
||||
Some(start) => file
|
||||
.metadata()
|
||||
.ok()
|
||||
.and_then(|meta| meta.len().checked_sub(start))
|
||||
.map_or(length, |remaining| length.min(remaining as usize)),
|
||||
None => file
|
||||
.metadata()
|
||||
.ok()
|
||||
.and_then(|meta| {
|
||||
// A cursor read starts wherever the handle is, so the remainder
|
||||
// is measured from there rather than from the start.
|
||||
let current = file.stream_position().ok()?;
|
||||
meta.len().checked_sub(current)
|
||||
})
|
||||
.map_or(length, |remaining| length.min(remaining as usize)),
|
||||
};
|
||||
let mut buffer = vec![0u8; cap];
|
||||
let filled = match offset(position)? {
|
||||
Some(start) => read_at(&mut file, &mut buffer, start, id)?,
|
||||
None => read_cursor(&mut file, &mut buffer, id)?,
|
||||
};
|
||||
// A read that stopped at end of file is a short read, not a failure, so the
|
||||
// buffer is shrunk to what the OS actually produced rather than padded.
|
||||
buffer.truncate(filled);
|
||||
Ok(ArrayBuffer::from(buffer))
|
||||
}
|
||||
|
||||
pub fn write(
|
||||
module: &crate::RNFsModule,
|
||||
fd: f64,
|
||||
data: ArrayBuffer,
|
||||
position: f64,
|
||||
) -> Result<f64, ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let (mut file, writable) = module.file_and_writability(fd)?;
|
||||
// Checked from the flags the handle was opened with, not from what the OS
|
||||
// says: a write to a read-only handle is `EBADF` in Node, `EACCES` on
|
||||
// Windows and `EBADF` on Unix.
|
||||
if !writable {
|
||||
return Err(bad_fd(id));
|
||||
}
|
||||
let bytes = data.as_slice();
|
||||
if bytes.is_empty() {
|
||||
return Ok(0.0);
|
||||
}
|
||||
|
||||
// Read before the borrow below: `positioned_write` needs `&mut File` so the
|
||||
// Windows branch can seek, and a match guard borrowing the file would overlap.
|
||||
let append_only = appends(&file);
|
||||
let written = match offset(position)? {
|
||||
// An append-mode descriptor appends whatever offset the caller passes:
|
||||
// a cursor write does that because the file was opened with `O_APPEND`
|
||||
// (or `FILE_APPEND_DATA`), and Linux also appends for a positioned
|
||||
// write. Ignoring the offset keeps the platforms alike and is what
|
||||
// callers of `appendFile` expect.
|
||||
Some(start) if !append_only => write_at(&mut file, bytes, start, id)?,
|
||||
_ => write_cursor(&mut file, bytes, id)?,
|
||||
};
|
||||
Ok(written as f64)
|
||||
}
|
||||
|
||||
pub fn fstat(module: &crate::RNFsModule, fd: f64) -> Result<StatResult, ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let file = module.file(fd)?;
|
||||
let metadata = file.metadata().map_err(|error| io_error(error, id))?;
|
||||
Ok(stat_result(&metadata))
|
||||
}
|
||||
|
||||
pub fn fsync(module: &crate::RNFsModule, fd: f64) -> Result<(), ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let file = module.file(fd)?;
|
||||
file.sync_all().map_err(|error| io_error(error, id))
|
||||
}
|
||||
|
||||
pub fn ftruncate(module: &crate::RNFsModule, fd: f64, length: f64) -> Result<(), ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let length = crate::module::length(length)?;
|
||||
let file = module.file(fd)?;
|
||||
file.set_len(length as u64)
|
||||
.map_err(|error| io_error(error, id))
|
||||
}
|
||||
|
||||
pub fn close(module: &crate::RNFsModule, fd: f64) -> Result<(), ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let mut files = module
|
||||
.files
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner());
|
||||
// Removing rather than merely dropping the entry is what makes a second
|
||||
// close report `EBADF`: the id is gone, and it is never handed out again.
|
||||
files.remove(&id).ok_or_else(|| bad_fd(id))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A failure from an operation on a descriptor.
|
||||
///
|
||||
/// The table may still know an id the kernel has already rejected — a write to a
|
||||
/// read-only descriptor is the usual way — and Node reports that as `EBADF`, not
|
||||
/// as a generic I/O error. Everything else goes through the shared mapping, with
|
||||
/// the descriptor standing in for the path a Node message names.
|
||||
fn io_error(error: std::io::Error, id: u32) -> ModuleError {
|
||||
#[cfg(unix)]
|
||||
if error.raw_os_error() == Some(libc::EBADF) {
|
||||
return bad_fd(id);
|
||||
}
|
||||
errors::from_io(error, &id.to_string())
|
||||
}
|
||||
|
||||
/// The byte offset a `position` argument names, or `None` for "the current
|
||||
/// offset", which is what a negative position means to `node:fs`.
|
||||
fn offset(position: f64) -> Result<Option<u64>, ModuleError> {
|
||||
if !position.is_finite() {
|
||||
return Err(errors::invalid(
|
||||
"EINVAL",
|
||||
format!("EINVAL: invalid argument, position {position}"),
|
||||
));
|
||||
}
|
||||
if position < 0.0 {
|
||||
return Ok(None);
|
||||
}
|
||||
Ok(Some(position as u64))
|
||||
}
|
||||
|
||||
/// Whether the descriptor was opened for append.
|
||||
///
|
||||
/// The table stores only the `File`, so the bit is read back from the descriptor
|
||||
/// itself with `F_GETFL`: that reports the flag the kernel actually holds, which
|
||||
/// is more reliable than tracking a second copy beside the table.
|
||||
#[cfg(unix)]
|
||||
fn appends(file: &File) -> bool {
|
||||
use std::os::unix::io::AsRawFd as _;
|
||||
// SAFETY: `file` owns a valid descriptor for the duration of the call; `F_GETFL` takes no argument.
|
||||
let flags = unsafe { libc::fcntl(file.as_raw_fd(), libc::F_GETFL) };
|
||||
flags >= 0 && flags & libc::O_APPEND != 0
|
||||
}
|
||||
|
||||
/// Windows keeps the append bit inside the handle's granted access, with no
|
||||
/// portable way to read it back, so the answer is "no" here. Cursor writes still
|
||||
/// append — `append(true)` opens with `FILE_APPEND_DATA`, which does that in the
|
||||
/// kernel — and a write with an explicit offset lands at that offset, which is
|
||||
/// what a caller using `pwrite` on Windows would expect.
|
||||
#[cfg(not(unix))]
|
||||
fn appends(_file: &File) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn read_at(file: &mut File, buffer: &mut [u8], offset: u64, id: u32) -> Result<usize, ModuleError> {
|
||||
let mut filled = 0;
|
||||
while filled < buffer.len() {
|
||||
match positioned_read(file, &mut buffer[filled..], offset + filled as u64) {
|
||||
Ok(0) => break,
|
||||
Ok(count) => filled += count,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(io_error(error, id)),
|
||||
}
|
||||
}
|
||||
Ok(filled)
|
||||
}
|
||||
|
||||
fn read_cursor(file: &mut File, buffer: &mut [u8], id: u32) -> Result<usize, ModuleError> {
|
||||
let mut filled = 0;
|
||||
while filled < buffer.len() {
|
||||
match file.read(&mut buffer[filled..]) {
|
||||
Ok(0) => break,
|
||||
Ok(count) => filled += count,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(io_error(error, id)),
|
||||
}
|
||||
}
|
||||
Ok(filled)
|
||||
}
|
||||
|
||||
fn write_at(file: &mut File, bytes: &[u8], offset: u64, id: u32) -> Result<usize, ModuleError> {
|
||||
let mut written = 0;
|
||||
while written < bytes.len() {
|
||||
match positioned_write(file, &bytes[written..], offset + written as u64) {
|
||||
// A regular file does not write zero bytes for a non-empty buffer;
|
||||
// treating it as `EIO` keeps the loop from spinning forever.
|
||||
Ok(0) => return Err(errors::invalid("EIO", format!("EIO: i/o error, {id}"))),
|
||||
Ok(count) => written += count,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return Err(io_error(error, id)),
|
||||
}
|
||||
}
|
||||
Ok(written)
|
||||
}
|
||||
|
||||
fn write_cursor(file: &mut File, bytes: &[u8], id: u32) -> Result<usize, ModuleError> {
|
||||
file.write_all(bytes).map_err(|error| io_error(error, id))?;
|
||||
Ok(bytes.len())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn positioned_read(file: &mut File, buffer: &mut [u8], offset: u64) -> std::io::Result<usize> {
|
||||
use std::os::unix::fs::FileExt as _;
|
||||
// `read_at` takes `&self`, but the call sites hold `&mut File` so the
|
||||
// Windows branch — which has to seek — shares one signature.
|
||||
(&*file).read_at(buffer, offset)
|
||||
}
|
||||
|
||||
/// Windows' positional read, with the cursor left where it was.
|
||||
///
|
||||
/// `FileExt::seek_read` is documented as not affecting the cursor, but on a
|
||||
/// synchronous handle — which is what `File::open` gives — the underlying
|
||||
/// `ReadFile` with an `OVERLAPPED` offset keeps the file position as a side
|
||||
/// effect. Node's positional forms leave the descriptor alone, and so does the
|
||||
/// Unix branch above, so the position is saved and restored here. Without this a
|
||||
/// positioned read moved the cursor, and a following cursor read started from the
|
||||
/// wrong place.
|
||||
#[cfg(windows)]
|
||||
fn positioned_read(file: &mut File, buffer: &mut [u8], offset: u64) -> std::io::Result<usize> {
|
||||
use std::io::Seek as _;
|
||||
use std::os::windows::fs::FileExt as _;
|
||||
let saved = file.stream_position();
|
||||
let result = file.seek_read(buffer, offset);
|
||||
if let Ok(position) = saved {
|
||||
let _ = file.seek(std::io::SeekFrom::Start(position));
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn positioned_write(file: &mut File, bytes: &[u8], offset: u64) -> std::io::Result<usize> {
|
||||
use std::os::unix::fs::FileExt as _;
|
||||
(&*file).write_at(bytes, offset)
|
||||
}
|
||||
|
||||
/// Windows' positional write, with the cursor left where it was. Same reason as
|
||||
/// `positioned_read` above.
|
||||
#[cfg(windows)]
|
||||
fn positioned_write(file: &mut File, bytes: &[u8], offset: u64) -> std::io::Result<usize> {
|
||||
use std::io::Seek as _;
|
||||
use std::os::windows::fs::FileExt as _;
|
||||
let saved = file.stream_position();
|
||||
let result = file.seek_write(bytes, offset);
|
||||
if let Ok(position) = saved {
|
||||
let _ = file.seek(std::io::SeekFrom::Start(position));
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// The generated `StatResult`, filled from `std::fs::Metadata`.
|
||||
///
|
||||
/// `files::stat` builds the same shape for a path. The two are deliberately
|
||||
/// separate: `files` and `handles` are independent, and twenty duplicated lines
|
||||
/// cost less than one file reaching into the other's internals.
|
||||
fn stat_result(metadata: &std::fs::Metadata) -> StatResult {
|
||||
let file_type = metadata.file_type();
|
||||
let kind = if file_type.is_file() {
|
||||
kind::FILE
|
||||
} else if file_type.is_dir() {
|
||||
kind::DIRECTORY
|
||||
} else if file_type.is_symlink() {
|
||||
kind::SYMLINK
|
||||
} else {
|
||||
kind::OTHER
|
||||
};
|
||||
|
||||
// Node's `Stats` names the Unix inode fields even on Windows, where they
|
||||
// have no meaning; zero is what Node reports there too.
|
||||
#[cfg(unix)]
|
||||
let (mode, dev, ino, nlink, uid, gid) = {
|
||||
use std::os::unix::fs::MetadataExt as _;
|
||||
(
|
||||
metadata.mode() as f64,
|
||||
metadata.dev() as f64,
|
||||
metadata.ino() as f64,
|
||||
metadata.nlink() as f64,
|
||||
metadata.uid() as f64,
|
||||
metadata.gid() as f64,
|
||||
)
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let (mode, dev, ino, nlink, uid, gid) = (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
// `ctime` is the inode change time on Unix; Windows has no equivalent, and
|
||||
// its creation time is the closest thing it has.
|
||||
#[cfg(unix)]
|
||||
let ctime_ms = {
|
||||
use std::os::unix::fs::MetadataExt as _;
|
||||
metadata.ctime() as f64 * 1000.0 + metadata.ctime_nsec() as f64 / 1_000_000.0
|
||||
};
|
||||
#[cfg(not(unix))]
|
||||
let ctime_ms = metadata
|
||||
.created()
|
||||
.map(millis)
|
||||
.unwrap_or_else(|_| metadata.modified().map(millis).unwrap_or(0.0));
|
||||
|
||||
// A file system that cannot report creation gives 0, as Node does, rather
|
||||
// than pretending the modification time is the birth time.
|
||||
StatResult {
|
||||
size: metadata.len() as f64,
|
||||
mode,
|
||||
kind,
|
||||
atime_ms: metadata.accessed().map(millis).unwrap_or(0.0),
|
||||
mtime_ms: metadata.modified().map(millis).unwrap_or(0.0),
|
||||
ctime_ms,
|
||||
birthtime_ms: metadata.created().map(millis).unwrap_or(0.0),
|
||||
dev,
|
||||
ino,
|
||||
nlink,
|
||||
uid,
|
||||
gid,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use rn_gpui_module::{EventEmitter, ModuleInstance, Value};
|
||||
use tempfile::TempDir;
|
||||
|
||||
use super::*;
|
||||
use crate::native_fs_module;
|
||||
|
||||
/// The async trait methods never actually suspend — they call these helpers
|
||||
/// directly — so a single-poll executor drives the generated glue without a
|
||||
/// runtime.
|
||||
fn block_on<F: std::future::Future>(future: F) -> F::Output {
|
||||
let mut future = std::pin::pin!(future);
|
||||
let waker = std::task::Waker::noop();
|
||||
let mut context = std::task::Context::from_waker(waker);
|
||||
loop {
|
||||
match future.as_mut().poll(&mut context) {
|
||||
std::task::Poll::Ready(value) => return value,
|
||||
std::task::Poll::Pending => std::thread::yield_now(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `NativeFsEvents` has a private emitter and no constructor, so a test
|
||||
/// cannot call `RNFsModule::create` itself. Building the instance through the
|
||||
/// descriptor the bridge uses is the supported way in, and it exercises the
|
||||
/// generated glue as a bonus.
|
||||
fn module() -> Arc<dyn ModuleInstance> {
|
||||
(native_fs_module::<crate::RNFsModule>().create)(EventEmitter::new(1))
|
||||
}
|
||||
|
||||
fn index(name: &str) -> usize {
|
||||
native_fs_module::<crate::RNFsModule>()
|
||||
.methods
|
||||
.iter()
|
||||
.position(|method| method.name == name)
|
||||
.unwrap_or_else(|| panic!("the spec has no method {name}"))
|
||||
}
|
||||
|
||||
fn call(
|
||||
module: &Arc<dyn ModuleInstance>,
|
||||
name: &str,
|
||||
args: Vec<Value>,
|
||||
) -> Result<Value, ModuleError> {
|
||||
block_on(module.clone().call_promise(index(name), args)?)
|
||||
}
|
||||
|
||||
fn text(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
fn open_fd(module: &Arc<dyn ModuleInstance>, path: &Path, flags_value: i32) -> f64 {
|
||||
let value = call(
|
||||
module,
|
||||
"open",
|
||||
vec![
|
||||
Value::String(text(path)),
|
||||
Value::Number(f64::from(flags_value)),
|
||||
Value::Number(0.0),
|
||||
],
|
||||
)
|
||||
.expect("open");
|
||||
match value {
|
||||
Value::Number(fd) => fd,
|
||||
other => panic!("expected a descriptor, got {}", other.type_name()),
|
||||
}
|
||||
}
|
||||
|
||||
fn read_bytes(
|
||||
module: &Arc<dyn ModuleInstance>,
|
||||
fd: f64,
|
||||
length: f64,
|
||||
position: f64,
|
||||
) -> Result<Vec<u8>, ModuleError> {
|
||||
match call(
|
||||
module,
|
||||
"read",
|
||||
vec![
|
||||
Value::Number(fd),
|
||||
Value::Number(length),
|
||||
Value::Number(position),
|
||||
],
|
||||
)? {
|
||||
Value::Buffer(bytes) => Ok(bytes),
|
||||
other => panic!("expected bytes, got {}", other.type_name()),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_bytes(
|
||||
module: &Arc<dyn ModuleInstance>,
|
||||
fd: f64,
|
||||
bytes: &[u8],
|
||||
position: f64,
|
||||
) -> Result<f64, ModuleError> {
|
||||
match call(
|
||||
module,
|
||||
"write",
|
||||
vec![
|
||||
Value::Number(fd),
|
||||
Value::Buffer(bytes.to_vec()),
|
||||
Value::Number(position),
|
||||
],
|
||||
)? {
|
||||
Value::Number(written) => Ok(written),
|
||||
other => panic!("expected a count, got {}", other.type_name()),
|
||||
}
|
||||
}
|
||||
|
||||
fn stat_size(module: &Arc<dyn ModuleInstance>, fd: f64) -> f64 {
|
||||
let value = call(module, "fstat", vec![Value::Number(fd)]).expect("fstat");
|
||||
let Value::Object(fields) = value else {
|
||||
panic!("expected a stat object");
|
||||
};
|
||||
fields
|
||||
.iter()
|
||||
.find(|(name, _)| name == "size")
|
||||
.and_then(|(_, value)| match value {
|
||||
Value::Number(size) => Some(*size),
|
||||
_ => None,
|
||||
})
|
||||
.expect("a size field")
|
||||
}
|
||||
|
||||
fn close_fd(module: &Arc<dyn ModuleInstance>, fd: f64) -> Result<Value, ModuleError> {
|
||||
call(module, "close", vec![Value::Number(fd)])
|
||||
}
|
||||
|
||||
/// Open a fresh file containing `contents` and hand back its descriptor.
|
||||
fn opened(module: &Arc<dyn ModuleInstance>, dir: &TempDir, contents: &str) -> (String, f64) {
|
||||
let path = dir.path().join("data.txt");
|
||||
std::fs::write(&path, contents).expect("write the fixture file");
|
||||
let fd = open_fd(module, &path, flags::RDWR);
|
||||
(text(&path), fd)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_handle_reads_back_what_it_wrote() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let path = dir.path().join("note.txt");
|
||||
let module = module();
|
||||
|
||||
let fd = open_fd(&module, &path, flags::WRONLY | flags::CREAT | flags::TRUNC);
|
||||
assert_eq!(
|
||||
write_bytes(&module, fd, b"hello", -1.0).expect("write"),
|
||||
5.0
|
||||
);
|
||||
assert_eq!(stat_size(&module, fd), 5.0);
|
||||
close_fd(&module, fd).expect("close");
|
||||
|
||||
let fd = open_fd(&module, &path, flags::RDONLY);
|
||||
assert_eq!(read_bytes(&module, fd, 5.0, -1.0).expect("read"), b"hello");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_positioned_read_leaves_the_cursor_alone() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abcdef");
|
||||
|
||||
assert_eq!(read_bytes(&module, fd, 2.0, 0.0).expect("read"), b"ab");
|
||||
// The cursor never moved, so a cursor read starts at the beginning.
|
||||
assert_eq!(read_bytes(&module, fd, 2.0, -1.0).expect("read"), b"ab");
|
||||
assert_eq!(read_bytes(&module, fd, 2.0, -1.0).expect("read"), b"cd");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_positioned_write_only_touches_its_range() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (path, fd) = opened(&module, &dir, "abcdef");
|
||||
|
||||
assert_eq!(write_bytes(&module, fd, b"XY", 2.0).expect("write"), 2.0);
|
||||
assert_eq!(stat_size(&module, fd), 6.0);
|
||||
assert_eq!(std::fs::read(&path).expect("on disk"), b"abXYef");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reading_past_the_end_is_a_short_read_not_an_error() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abc");
|
||||
|
||||
// Two bytes asked for, one left: the buffer holds exactly one byte.
|
||||
assert_eq!(read_bytes(&module, fd, 2.0, 2.0).expect("read"), b"c");
|
||||
// Nothing left at all: an empty buffer, still a success.
|
||||
assert_eq!(read_bytes(&module, fd, 8.0, 10.0).expect("read"), b"");
|
||||
assert_eq!(read_bytes(&module, fd, 8.0, -1.0).expect("read"), b"abc");
|
||||
assert_eq!(read_bytes(&module, fd, 8.0, -1.0).expect("read"), b"");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_huge_length_only_allocates_what_the_file_holds() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "0123456789");
|
||||
|
||||
// The buffer is capped at what remains, so this returns rather than
|
||||
// trying to allocate four gigabytes. Finishing at all is the check: an
|
||||
// allocation of `u32::MAX` bytes would abort the test process instead.
|
||||
let bytes = read_bytes(&module, fd, u32::MAX as f64, 0.0).expect("read");
|
||||
assert_eq!(bytes, b"0123456789");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn appending_ignores_the_position() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let path = dir.path().join("log.txt");
|
||||
std::fs::write(&path, b"ab").expect("write the fixture file");
|
||||
let module = module();
|
||||
|
||||
let fd = open_fd(&module, &path, flags::APPEND | flags::WRONLY | flags::CREAT);
|
||||
write_bytes(&module, fd, b"cd", -1.0).expect("append");
|
||||
// An append-mode descriptor appends even when an offset is passed, which
|
||||
// is what the kernel does on Linux and what `appendFile` means.
|
||||
write_bytes(&module, fd, b"ef", 0.0).expect("append");
|
||||
assert_eq!(stat_size(&module, fd), 6.0);
|
||||
close_fd(&module, fd).expect("close");
|
||||
assert_eq!(std::fs::read(&path).expect("on disk"), b"abcdef");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncating_by_descriptor_changes_the_size() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abcdef");
|
||||
|
||||
call(
|
||||
&module,
|
||||
"ftruncate",
|
||||
vec![Value::Number(fd), Value::Number(3.0)],
|
||||
)
|
||||
.expect("ftruncate");
|
||||
assert_eq!(stat_size(&module, fd), 3.0);
|
||||
assert_eq!(read_bytes(&module, fd, 16.0, 0.0).expect("read"), b"abc");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn syncing_a_handle_succeeds() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abc");
|
||||
write_bytes(&module, fd, b"d", 3.0).expect("write");
|
||||
call(&module, "fsync", vec![Value::Number(fd)]).expect("fsync");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_close_is_ebadf() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abc");
|
||||
|
||||
close_fd(&module, fd).expect("the first close");
|
||||
let error = close_fd(&module, fd).expect_err("the second close");
|
||||
assert_eq!(error.code.as_deref(), Some("EBADF"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_closed_descriptor_is_ebadf_for_every_op() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abc");
|
||||
close_fd(&module, fd).expect("close");
|
||||
|
||||
let error = read_bytes(&module, fd, 1.0, -1.0).expect_err("read");
|
||||
assert_eq!(error.code.as_deref(), Some("EBADF"));
|
||||
let error = write_bytes(&module, fd, b"x", -1.0).expect_err("write");
|
||||
assert_eq!(error.code.as_deref(), Some("EBADF"));
|
||||
let error = call(&module, "fstat", vec![Value::Number(fd)]).expect_err("fstat");
|
||||
assert_eq!(error.code.as_deref(), Some("EBADF"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_a_missing_file_is_enoent() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let path = dir.path().join("gone.txt");
|
||||
let module = module();
|
||||
|
||||
let error = call(
|
||||
&module,
|
||||
"open",
|
||||
vec![
|
||||
Value::String(text(&path)),
|
||||
Value::Number(f64::from(flags::RDONLY)),
|
||||
Value::Number(0.0),
|
||||
],
|
||||
)
|
||||
.expect_err("open");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
/// A descriptor the table still knows but the kernel rejects: Node says
|
||||
/// `EBADF` here, and so must this module, rather than a generic `EIO`.
|
||||
#[test]
|
||||
fn writing_through_a_read_only_descriptor_is_ebadf() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let path = dir.path().join("data.txt");
|
||||
std::fs::write(&path, b"abc").expect("write the fixture file");
|
||||
let fd = open_fd(&module, &path, flags::RDONLY);
|
||||
|
||||
let error = write_bytes(&module, fd, b"x", -1.0).expect_err("write");
|
||||
assert_eq!(error.code.as_deref(), Some("EBADF"));
|
||||
// Reading is still fine, and the file is untouched.
|
||||
assert_eq!(read_bytes(&module, fd, 3.0, 0.0).expect("read"), b"abc");
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_non_finite_position_is_einval() {
|
||||
let dir = TempDir::new().expect("a temporary directory");
|
||||
let module = module();
|
||||
let (_, fd) = opened(&module, &dir, "abc");
|
||||
|
||||
let error = read_bytes(&module, fd, 1.0, f64::NAN).expect_err("read");
|
||||
assert_eq!(error.code.as_deref(), Some("EINVAL"));
|
||||
let error = write_bytes(&module, fd, b"x", f64::INFINITY).expect_err("write");
|
||||
assert_eq!(error.code.as_deref(), Some("EINVAL"));
|
||||
close_fd(&module, fd).expect("close");
|
||||
}
|
||||
}
|
||||
@@ -1,286 +0,0 @@
|
||||
//! `@react-native-gpui/fs`: a Node-shaped file API for the desktop app.
|
||||
//!
|
||||
//! The TypeScript spec in `../src/NativeFs.ts` is the contract; the generator
|
||||
//! turns it into the `NativeFsSpec` trait implemented here. That trait is the
|
||||
//! only thing the C++ bridge knows about, so the API surface is edited there and
|
||||
//! regenerated, never in this file.
|
||||
//!
|
||||
//! Rust needs a trait's methods in a single `impl` block, so this file holds that
|
||||
//! block and hands each method to a helper in the file that owns the concern:
|
||||
//!
|
||||
//! - `files` — reading, writing, copying, truncating, permissions and times
|
||||
//! - `dirs` — listing, creating, removing and `mkdtemp`
|
||||
//! - `handles` — open descriptors, positioned reads and writes
|
||||
//! - `links` — symbolic links, `realpath` and `access`
|
||||
//! - `paths` — the well-known folders, created on first read
|
||||
//! - `dialogs` — the pickers, `reveal` and `openExternal`
|
||||
//! - `watching` — the watcher behind `watch` and `unwatch`
|
||||
//!
|
||||
//! Keeping the bodies in those files is what makes the crate readable; keeping
|
||||
//! the `impl` here is what makes it compile.
|
||||
|
||||
rn_gpui_module::include_specs!();
|
||||
|
||||
mod dialogs;
|
||||
mod dirs;
|
||||
mod download;
|
||||
mod errors;
|
||||
mod files;
|
||||
mod handles;
|
||||
mod links;
|
||||
mod module;
|
||||
mod paths;
|
||||
mod upload;
|
||||
mod watching;
|
||||
|
||||
pub use module::RNFsModule;
|
||||
|
||||
use rn_gpui_module::{ArrayBuffer, ModuleError};
|
||||
|
||||
impl NativeFsSpec for RNFsModule {
|
||||
fn new(events: NativeFsEvents) -> Self {
|
||||
Self::create(events)
|
||||
}
|
||||
|
||||
async fn read_file(&self, path: String) -> Result<ArrayBuffer, ModuleError> {
|
||||
files::read_file(path).await
|
||||
}
|
||||
|
||||
async fn read_text_file(&self, path: String) -> Result<String, ModuleError> {
|
||||
files::read_text_file(path).await
|
||||
}
|
||||
|
||||
async fn write_file(
|
||||
&self,
|
||||
path: String,
|
||||
data: ArrayBuffer,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<(), ModuleError> {
|
||||
files::write_file(path, data, flags, mode).await
|
||||
}
|
||||
|
||||
async fn write_text_file(
|
||||
&self,
|
||||
path: String,
|
||||
data: String,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<(), ModuleError> {
|
||||
files::write_text_file(path, data, flags, mode).await
|
||||
}
|
||||
|
||||
async fn readdir(&self, path: String, recursive: bool) -> Result<Vec<DirEntry>, ModuleError> {
|
||||
dirs::readdir(path, recursive).await
|
||||
}
|
||||
|
||||
async fn stat(&self, path: String, follow_symlinks: bool) -> Result<StatResult, ModuleError> {
|
||||
files::stat(path, follow_symlinks).await
|
||||
}
|
||||
|
||||
async fn mkdir(&self, path: String, recursive: bool, mode: f64) -> Result<(), ModuleError> {
|
||||
dirs::mkdir(path, recursive, mode).await
|
||||
}
|
||||
|
||||
async fn rm(&self, path: String, recursive: bool, force: bool) -> Result<(), ModuleError> {
|
||||
dirs::rm(path, recursive, force).await
|
||||
}
|
||||
|
||||
async fn rename(&self, from: String, to: String) -> Result<(), ModuleError> {
|
||||
files::rename(from, to).await
|
||||
}
|
||||
|
||||
async fn copy(&self, from: String, to: String, recursive: bool) -> Result<(), ModuleError> {
|
||||
files::copy(from, to, recursive).await
|
||||
}
|
||||
|
||||
async fn access(&self, path: String, mode: f64) -> Result<(), ModuleError> {
|
||||
links::access(path, mode)
|
||||
}
|
||||
|
||||
async fn realpath(&self, path: String) -> Result<String, ModuleError> {
|
||||
links::realpath(path)
|
||||
}
|
||||
|
||||
async fn readlink(&self, path: String) -> Result<String, ModuleError> {
|
||||
links::readlink(path)
|
||||
}
|
||||
|
||||
async fn symlink(
|
||||
&self,
|
||||
target: String,
|
||||
path: String,
|
||||
kind: Option<String>,
|
||||
) -> Result<(), ModuleError> {
|
||||
links::symlink(target, path, kind.as_deref())
|
||||
}
|
||||
|
||||
async fn truncate(&self, path: String, length: f64) -> Result<(), ModuleError> {
|
||||
files::truncate(path, length).await
|
||||
}
|
||||
|
||||
async fn utimes(&self, path: String, atime_ms: f64, mtime_ms: f64) -> Result<(), ModuleError> {
|
||||
files::utimes(path, atime_ms, mtime_ms).await
|
||||
}
|
||||
|
||||
async fn chmod(&self, path: String, mode: f64) -> Result<(), ModuleError> {
|
||||
files::chmod(path, mode).await
|
||||
}
|
||||
|
||||
async fn mkdtemp(&self, prefix: String) -> Result<String, ModuleError> {
|
||||
dirs::mkdtemp(prefix).await
|
||||
}
|
||||
|
||||
async fn open(&self, path: String, flags: f64, mode: f64) -> Result<f64, ModuleError> {
|
||||
handles::open(self, path, flags, mode)
|
||||
}
|
||||
|
||||
async fn read(&self, fd: f64, length: f64, position: f64) -> Result<ArrayBuffer, ModuleError> {
|
||||
handles::read(self, fd, length, position)
|
||||
}
|
||||
|
||||
async fn write(&self, fd: f64, data: ArrayBuffer, position: f64) -> Result<f64, ModuleError> {
|
||||
handles::write(self, fd, data, position)
|
||||
}
|
||||
|
||||
async fn fstat(&self, fd: f64) -> Result<StatResult, ModuleError> {
|
||||
handles::fstat(self, fd)
|
||||
}
|
||||
|
||||
async fn fsync(&self, fd: f64) -> Result<(), ModuleError> {
|
||||
handles::fsync(self, fd)
|
||||
}
|
||||
|
||||
async fn ftruncate(&self, fd: f64, length: f64) -> Result<(), ModuleError> {
|
||||
handles::ftruncate(self, fd, length)
|
||||
}
|
||||
|
||||
async fn close(&self, fd: f64) -> Result<(), ModuleError> {
|
||||
handles::close(self, fd)
|
||||
}
|
||||
|
||||
async fn show_open_dialog(
|
||||
&self,
|
||||
options: OpenDialogOptions,
|
||||
) -> Result<Vec<String>, ModuleError> {
|
||||
dialogs::show_open(options)
|
||||
}
|
||||
|
||||
async fn show_save_dialog(
|
||||
&self,
|
||||
options: SaveDialogOptions,
|
||||
) -> Result<Option<String>, ModuleError> {
|
||||
dialogs::show_save(options)
|
||||
}
|
||||
|
||||
async fn reveal(&self, path: String) -> Result<(), ModuleError> {
|
||||
dialogs::reveal(path)
|
||||
}
|
||||
|
||||
async fn open_external(&self, path: String) -> Result<(), ModuleError> {
|
||||
dialogs::open_external(path)
|
||||
}
|
||||
|
||||
async fn watch(&self, path: String, recursive: bool) -> Result<f64, ModuleError> {
|
||||
watching::watch(self, path, recursive)
|
||||
}
|
||||
|
||||
async fn download(
|
||||
&self,
|
||||
url: String,
|
||||
path: String,
|
||||
options: DownloadOptions,
|
||||
) -> Result<DownloadResult, ModuleError> {
|
||||
download::download(self, url, path, options).await
|
||||
}
|
||||
|
||||
async fn upload(
|
||||
&self,
|
||||
url: String,
|
||||
path: String,
|
||||
options: UploadOptions,
|
||||
) -> Result<UploadResult, ModuleError> {
|
||||
upload::upload(self, url, path, options).await
|
||||
}
|
||||
|
||||
fn cancel_upload(&self, id: f64) -> Result<bool, ModuleError> {
|
||||
Ok(upload::cancel(self, id))
|
||||
}
|
||||
|
||||
fn cancel_download(&self, id: f64) -> Result<bool, ModuleError> {
|
||||
Ok(download::cancel(self, id))
|
||||
}
|
||||
|
||||
fn unwatch(&self, id: f64) {
|
||||
watching::unwatch(self, id);
|
||||
}
|
||||
|
||||
fn read_file_sync(&self, path: String) -> Result<ArrayBuffer, ModuleError> {
|
||||
files::read_file_sync(path)
|
||||
}
|
||||
|
||||
fn read_text_file_sync(&self, path: String) -> Result<String, ModuleError> {
|
||||
files::read_text_file_sync(path)
|
||||
}
|
||||
|
||||
fn write_file_sync(
|
||||
&self,
|
||||
path: String,
|
||||
data: ArrayBuffer,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<bool, ModuleError> {
|
||||
files::write_file_sync(path, data, flags, mode)
|
||||
}
|
||||
|
||||
fn write_text_file_sync(
|
||||
&self,
|
||||
path: String,
|
||||
data: String,
|
||||
flags: f64,
|
||||
mode: f64,
|
||||
) -> Result<bool, ModuleError> {
|
||||
files::write_text_file_sync(path, data, flags, mode)
|
||||
}
|
||||
|
||||
fn stat_sync(
|
||||
&self,
|
||||
path: String,
|
||||
follow_symlinks: bool,
|
||||
) -> Result<Option<StatResult>, ModuleError> {
|
||||
files::stat_sync(path, follow_symlinks)
|
||||
}
|
||||
|
||||
fn exists_sync(&self, path: String) -> Result<bool, ModuleError> {
|
||||
files::exists_sync(path)
|
||||
}
|
||||
|
||||
fn readdir_sync(&self, path: String, recursive: bool) -> Result<Vec<DirEntry>, ModuleError> {
|
||||
dirs::readdir_sync(path, recursive)
|
||||
}
|
||||
|
||||
fn mkdir_sync(&self, path: String, recursive: bool, mode: f64) -> Result<bool, ModuleError> {
|
||||
dirs::mkdir_sync(path, recursive, mode)
|
||||
}
|
||||
|
||||
fn rm_sync(&self, path: String, recursive: bool, force: bool) -> Result<bool, ModuleError> {
|
||||
dirs::rm_sync(path, recursive, force)
|
||||
}
|
||||
|
||||
fn rename_sync(&self, from: String, to: String) -> Result<bool, ModuleError> {
|
||||
files::rename_sync(from, to)
|
||||
}
|
||||
|
||||
fn copy_file_sync(&self, from: String, to: String) -> Result<bool, ModuleError> {
|
||||
files::copy_file_sync(from, to)
|
||||
}
|
||||
|
||||
fn get_paths(&self) -> Result<KnownPaths, ModuleError> {
|
||||
paths::known_paths()
|
||||
}
|
||||
}
|
||||
|
||||
/// Called by the app's autolink shim, which keeps these modules linked in.
|
||||
pub fn rn_gpui_register() {
|
||||
rn_gpui_module::register(native_fs_module::<RNFsModule>());
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
//! Links and path checks: `access`, `realpath`, `readlink` and `symlink`.
|
||||
//!
|
||||
//! These are the calls whose exact meaning differs most between the platforms,
|
||||
//! so each one is written against the platform's own primitive rather than
|
||||
//! approximated from `std::fs` where that would change the answer.
|
||||
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
use crate::errors;
|
||||
use crate::module::access;
|
||||
|
||||
pub fn access(path: String, mode: f64) -> Result<(), ModuleError> {
|
||||
let mode = access_mode(mode)?;
|
||||
platform_access(&path, mode)
|
||||
}
|
||||
|
||||
pub fn realpath(path: String) -> Result<String, ModuleError> {
|
||||
let resolved = dunce::canonicalize(&path).map_err(|error| errors::from_io(error, &path))?;
|
||||
// A `String` crosses the bridge as UTF-8, so a path that is not valid UTF-8
|
||||
// has no faithful representation. Lossy-converting would hand JS a path it
|
||||
// cannot open, which is worse than failing: `EINVAL` says the argument could
|
||||
// not be represented rather than that the path does not exist.
|
||||
resolved
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.map_err(|unrepresentable| {
|
||||
errors::invalid(
|
||||
"EINVAL",
|
||||
format!(
|
||||
"EINVAL: invalid argument, path is not valid UTF-8, {}",
|
||||
Path::new(&unrepresentable).display()
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn readlink(path: String) -> Result<String, ModuleError> {
|
||||
let target = std::fs::read_link(&path).map_err(|error| errors::from_io(error, &path))?;
|
||||
// As with `realpath`: the target is returned exactly as the file system
|
||||
// stores it, and a target that is not UTF-8 cannot be a `String`.
|
||||
target
|
||||
.into_os_string()
|
||||
.into_string()
|
||||
.map_err(|unrepresentable| {
|
||||
errors::invalid(
|
||||
"EINVAL",
|
||||
format!(
|
||||
"EINVAL: invalid argument, link target is not valid UTF-8, {}",
|
||||
Path::new(&unrepresentable).display()
|
||||
),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
pub fn symlink(target: String, path: String, kind: Option<&str>) -> Result<(), ModuleError> {
|
||||
platform_symlink(&target, &path, kind)
|
||||
}
|
||||
|
||||
/// Validate the bitmask Node numbers, returning it unchanged.
|
||||
///
|
||||
/// The caller gets Node's own bits back, not `access(2)`'s: `libc` 0.2 defines
|
||||
/// `R_OK`, `W_OK` and `X_OK` only for Unix, so a Windows build that named them
|
||||
/// failed with "cannot find value `R_OK` in crate `libc`". The translation to
|
||||
/// `access(2)` happens in `platform_access`, which is already Unix-only, and the
|
||||
/// Windows path reads the same Node bits directly.
|
||||
///
|
||||
/// `F_OK` is 0: the mode is not a flag set, and asking for nothing means "does
|
||||
/// it exist".
|
||||
fn access_mode(mode: f64) -> Result<i32, ModuleError> {
|
||||
if !mode.is_finite() || mode < 0.0 || mode.fract() != 0.0 {
|
||||
return Err(errors::invalid(
|
||||
"EINVAL",
|
||||
format!("EINVAL: invalid argument, mode {mode}"),
|
||||
));
|
||||
}
|
||||
let requested = mode as i32;
|
||||
let known = access::F_OK | access::R_OK | access::W_OK | access::X_OK;
|
||||
if requested & !known != 0 {
|
||||
return Err(errors::invalid(
|
||||
"EINVAL",
|
||||
format!("EINVAL: invalid argument, mode {requested}"),
|
||||
));
|
||||
}
|
||||
Ok(requested)
|
||||
}
|
||||
|
||||
/// `access(2)` is the only way to get Node's semantics — it asks the kernel about
|
||||
/// *this* process's real uid and gid, including supplementary groups — so the
|
||||
/// Unix implementation calls it rather than inspecting the mode bits, which
|
||||
/// would give a different answer for a file the caller does not own.
|
||||
#[cfg(unix)]
|
||||
fn platform_access(path: &str, requested: i32) -> Result<(), ModuleError> {
|
||||
use std::ffi::CString;
|
||||
use std::os::unix::ffi::OsStrExt as _;
|
||||
|
||||
// `access(2)` uses the same values Node does for these three, but the
|
||||
// translation is spelled out so a platform whose constants differ still asks
|
||||
// the right question. This is the only place the `libc` constants are named.
|
||||
let mut mask = libc::F_OK;
|
||||
if requested & access::R_OK != 0 {
|
||||
mask |= libc::R_OK;
|
||||
}
|
||||
if requested & access::W_OK != 0 {
|
||||
mask |= libc::W_OK;
|
||||
}
|
||||
if requested & access::X_OK != 0 {
|
||||
mask |= libc::X_OK;
|
||||
}
|
||||
|
||||
// A path with an interior NUL cannot be passed to the C API at all; that is
|
||||
// an invalid argument rather than a file-system failure.
|
||||
let name = CString::new(std::ffi::OsStr::new(path).as_bytes()).map_err(|_| {
|
||||
errors::invalid(
|
||||
"EINVAL",
|
||||
format!("EINVAL: invalid argument, path contains a null byte, {path}"),
|
||||
)
|
||||
})?;
|
||||
// `access` reports failure by return value and sets `errno`, so the error is
|
||||
// built from `last_os_error` and then mapped like any other.
|
||||
// SAFETY: `name` is a NUL-terminated string that outlives the call.
|
||||
let result = unsafe { libc::access(name.as_ptr(), mask) };
|
||||
if result == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(errors::from_io(std::io::Error::last_os_error(), path))
|
||||
}
|
||||
}
|
||||
|
||||
/// Windows has no `access(2)`, so the checks are approximated from what the
|
||||
/// platform does offer: `F_OK` and `R_OK` are a `metadata()` call (the read bit
|
||||
/// is not expressible, and a file the process can stat is one it can normally
|
||||
/// read), `W_OK` tries to open the file for writing, and `X_OK` uses the
|
||||
/// executable extensions the shell recognises, because Windows has no execute
|
||||
/// bit to test.
|
||||
#[cfg(not(unix))]
|
||||
fn platform_access(path: &str, requested: i32) -> Result<(), ModuleError> {
|
||||
// `F_OK` asks only that the path resolves; `metadata` follows links, which
|
||||
// matches `access`'s "does the name lead to something".
|
||||
std::fs::metadata(path).map_err(|error| errors::from_io(error, path))?;
|
||||
if requested & access::W_OK != 0 {
|
||||
std::fs::OpenOptions::new()
|
||||
.write(true)
|
||||
.open(path)
|
||||
.map_err(|error| errors::from_io(error, path))?;
|
||||
}
|
||||
if requested & access::X_OK != 0 && !is_executable(Path::new(path)) {
|
||||
return Err(errors::invalid(
|
||||
"EACCES",
|
||||
format!("EACCES: permission denied, {path}"),
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The extensions `PATHEXT` names, which is how Windows decides a file is a
|
||||
/// program. A directory is executable for `access`'s purposes because the caller
|
||||
/// can `chdir` into it.
|
||||
#[cfg(not(unix))]
|
||||
fn is_executable(path: &Path) -> bool {
|
||||
if path.is_dir() {
|
||||
return true;
|
||||
}
|
||||
let extensions = std::env::var("PATHEXT").unwrap_or_else(|_| ".COM;.EXE;.BAT;.CMD".to_string());
|
||||
let Some(extension) = path.extension().and_then(|value| value.to_str()) else {
|
||||
return false;
|
||||
};
|
||||
let extension = format!(".{}", extension.to_ascii_uppercase());
|
||||
extensions
|
||||
.split(';')
|
||||
.any(|candidate| candidate.eq_ignore_ascii_case(&extension))
|
||||
}
|
||||
|
||||
/// Unix ignores the link kind: `symlink(2)` does not have one, and the target is
|
||||
/// resolved lazily on every traversal, so a link to a directory works without
|
||||
/// being declared as one.
|
||||
#[cfg(unix)]
|
||||
fn platform_symlink(target: &str, path: &str, kind: Option<&str>) -> Result<(), ModuleError> {
|
||||
let _ = kind;
|
||||
std::os::unix::fs::symlink(target, path).map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
/// Windows needs a different call for a file link and a directory link, and it
|
||||
/// needs the developer-mode privilege (or SeCreateSymbolicLinkPrivilege) for
|
||||
/// either. The design says to report the OS error rather than fall back to
|
||||
/// copying, so a caller without the privilege sees the real reason.
|
||||
///
|
||||
/// The kind decides which call, following Node's rules. It cannot be inferred
|
||||
/// from the target alone: a link may name a path that does not exist yet, and
|
||||
/// `metadata` would fail on exactly the case that matters. `'junction'` gets
|
||||
/// `symlink_dir` — a directory symlink rather than a true NTFS junction, which
|
||||
/// needs a different API and an absolute target; both are traversable, which is
|
||||
/// what a caller wants.
|
||||
#[cfg(not(unix))]
|
||||
fn platform_symlink(target: &str, path: &str, kind: Option<&str>) -> Result<(), ModuleError> {
|
||||
let directory = match kind {
|
||||
Some("dir") | Some("junction") => true,
|
||||
Some("file") => false,
|
||||
_ => {
|
||||
// Node's rule when no type is given: resolve the target relative to
|
||||
// the link's parent — the shell does the same — and take the answer
|
||||
// from what is there.
|
||||
let resolved = match Path::new(path).parent() {
|
||||
Some(parent) => parent.join(target),
|
||||
None => PathBuf::from(target),
|
||||
};
|
||||
resolved.is_dir()
|
||||
}
|
||||
};
|
||||
let result = if directory {
|
||||
std::os::windows::fs::symlink_dir(target, path)
|
||||
} else {
|
||||
std::os::windows::fs::symlink_file(target, path)
|
||||
};
|
||||
result.map_err(|error| errors::from_io(error, path))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn tempdir() -> TempDir {
|
||||
TempDir::new().expect("a temporary directory")
|
||||
}
|
||||
|
||||
fn text(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn access_finds_a_present_path_and_reports_a_missing_one() {
|
||||
let dir = tempdir();
|
||||
let present = dir.path().join("here.txt");
|
||||
std::fs::write(&present, b"x").expect("write the fixture file");
|
||||
let absent = dir.path().join("gone.txt");
|
||||
|
||||
access(text(&present), f64::from(access::F_OK)).expect("a present path exists");
|
||||
access(text(&present), f64::from(access::R_OK | access::W_OK))
|
||||
.expect("a file we just wrote is readable and writable");
|
||||
|
||||
let error = access(text(&absent), f64::from(access::F_OK)).expect_err("a missing path");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn access_rejects_a_mode_node_does_not_define() {
|
||||
let dir = tempdir();
|
||||
let error = access(text(dir.path()), 8.0).expect_err("an unknown bit");
|
||||
assert_eq!(error.code.as_deref(), Some("EINVAL"));
|
||||
}
|
||||
|
||||
/// `dunce::canonicalize` resolves the whole chain, so the result is the real
|
||||
/// file rather than the link. Windows is skipped: creating a symlink there
|
||||
/// needs developer mode, which a CI runner may not have.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn realpath_resolves_a_symlink() {
|
||||
let dir = tempdir();
|
||||
let file = dir.path().join("real.txt");
|
||||
std::fs::write(&file, b"x").expect("write the fixture file");
|
||||
let link = dir.path().join("link.txt");
|
||||
std::os::unix::fs::symlink(&file, &link).expect("create the link");
|
||||
|
||||
assert_eq!(
|
||||
realpath(text(&link)).expect("realpath"),
|
||||
realpath(text(&file)).expect("realpath")
|
||||
);
|
||||
assert_eq!(
|
||||
realpath(text(&file)).expect("realpath"),
|
||||
dunce::canonicalize(&file)
|
||||
.expect("canonical")
|
||||
.to_string_lossy()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realpath_of_a_missing_path_is_enoent() {
|
||||
let dir = tempdir();
|
||||
let error = realpath(text(&dir.path().join("gone"))).expect_err("missing");
|
||||
assert_eq!(error.code.as_deref(), Some("ENOENT"));
|
||||
}
|
||||
|
||||
/// The link is read, not followed: the target comes back as it was stored,
|
||||
/// relative or absolute.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn readlink_returns_the_stored_target() {
|
||||
let dir = tempdir();
|
||||
let link = dir.path().join("link.txt");
|
||||
std::os::unix::fs::symlink("some/relative/target", &link).expect("create the link");
|
||||
assert_eq!(
|
||||
readlink(text(&link)).expect("readlink"),
|
||||
"some/relative/target"
|
||||
);
|
||||
|
||||
let file = dir.path().join("real.txt");
|
||||
std::fs::write(&file, b"x").expect("write the fixture file");
|
||||
let error = readlink(text(&file)).expect_err("a plain file is not a link");
|
||||
assert_eq!(error.code.as_deref(), Some("EINVAL"));
|
||||
}
|
||||
|
||||
/// Windows is skipped because creating a symlink there needs developer mode;
|
||||
/// the OS error would be the correct result, not a test failure.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn symlink_round_trips_through_readlink() {
|
||||
let dir = tempdir();
|
||||
let target = dir.path().join("target.txt");
|
||||
std::fs::write(&target, b"x").expect("write the fixture file");
|
||||
let link = dir.path().join("link.txt");
|
||||
|
||||
symlink(text(&target), text(&link), None).expect("symlink");
|
||||
assert_eq!(readlink(text(&link)).expect("readlink"), text(&target));
|
||||
assert_eq!(std::fs::read(&link).expect("through the link"), b"x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn symlinking_onto_an_existing_path_is_eexist() {
|
||||
let dir = tempdir();
|
||||
let one = dir.path().join("one.txt");
|
||||
let two = dir.path().join("two.txt");
|
||||
std::fs::write(&one, b"1").expect("write");
|
||||
std::fs::write(&two, b"2").expect("write");
|
||||
|
||||
let error = symlink(text(&one), text(&two), None).expect_err("the name is taken");
|
||||
#[cfg(unix)]
|
||||
assert_eq!(error.code.as_deref(), Some("EEXIST"));
|
||||
// Windows reports the same collision, but through a Win32 code whose
|
||||
// kind is "already exists"; only the presence of an error is portable.
|
||||
#[cfg(not(unix))]
|
||||
assert!(error.code.is_some());
|
||||
}
|
||||
}
|
||||
@@ -1,192 +0,0 @@
|
||||
//! Shared state and helpers for the file system module.
|
||||
//!
|
||||
//! The trait's methods are split across sibling files by concern — `files`,
|
||||
//! `dirs`, `handles`, `paths`, `dialogs`, `watching` — because they are large and
|
||||
//! largely independent. Rust allows the trait to be implemented in several files
|
||||
//! of one crate, so each file owns its own `impl` block.
|
||||
//!
|
||||
//! Everything they share is here: the open file table, the flag translation, and
|
||||
//! the conversions the bridge's `f64` arguments need.
|
||||
|
||||
use std::fs::File;
|
||||
use std::sync::atomic::AtomicU32;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use rn_gpui_module::{ModuleError, ModuleThread};
|
||||
|
||||
use crate::NativeFsEvents;
|
||||
|
||||
use crate::errors;
|
||||
|
||||
/// Flags, as `node:fs` numbers them: Node's table, not the platform's, so these
|
||||
/// are translated rather than passed through to `OpenOptions`.
|
||||
pub mod flags {
|
||||
pub const RDONLY: i32 = 0;
|
||||
pub const WRONLY: i32 = 1;
|
||||
pub const RDWR: i32 = 2;
|
||||
pub const CREAT: i32 = 64;
|
||||
pub const EXCL: i32 = 128;
|
||||
pub const TRUNC: i32 = 512;
|
||||
pub const APPEND: i32 = 1024;
|
||||
}
|
||||
|
||||
/// Access modes for `access`, matching `node:fs.constants`.
|
||||
pub mod access {
|
||||
pub const F_OK: i32 = 0;
|
||||
pub const X_OK: i32 = 1;
|
||||
pub const W_OK: i32 = 2;
|
||||
pub const R_OK: i32 = 4;
|
||||
}
|
||||
|
||||
/// The kind numbers the JS layer turns into `Stats` predicates.
|
||||
pub mod kind {
|
||||
pub const FILE: f64 = 0.0;
|
||||
pub const DIRECTORY: f64 = 1.0;
|
||||
pub const SYMLINK: f64 = 2.0;
|
||||
pub const OTHER: f64 = 3.0;
|
||||
}
|
||||
|
||||
pub struct RNFsModule {
|
||||
pub events: NativeFsEvents,
|
||||
/// Open files, keyed by the number JS holds. The table belongs to the
|
||||
/// instance, so a reload closes every descriptor when it drops.
|
||||
///
|
||||
/// Each entry records whether the descriptor was opened writable. The OS
|
||||
/// reports a write to a read-only handle as `EBADF` on Unix and `EACCES` on
|
||||
/// Windows, and Node reports `EBADF` on both, so the answer is taken from what
|
||||
/// the handle was opened with rather than from the errno.
|
||||
pub files: Mutex<std::collections::HashMap<u32, (File, bool)>>,
|
||||
/// Never reused, so a stale `fd` from JS fails instead of closing an
|
||||
/// unrelated file.
|
||||
pub next_fd: AtomicU32,
|
||||
/// Live watchers, keyed the same way.
|
||||
pub watchers: Mutex<std::collections::HashMap<u32, crate::watching::Watcher>>,
|
||||
pub next_watcher: AtomicU32,
|
||||
/// Running downloads, keyed by the id JS chose, with the flag that stops each.
|
||||
pub downloads: crate::download::Downloads,
|
||||
pub uploads: crate::upload::Uploads,
|
||||
}
|
||||
|
||||
impl RNFsModule {
|
||||
pub fn create(events: NativeFsEvents) -> Self {
|
||||
Self {
|
||||
events,
|
||||
files: Mutex::new(std::collections::HashMap::new()),
|
||||
next_fd: AtomicU32::new(1),
|
||||
watchers: Mutex::new(std::collections::HashMap::new()),
|
||||
next_watcher: AtomicU32::new(1),
|
||||
downloads: Default::default(),
|
||||
uploads: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn take_fd(&self) -> u32 {
|
||||
self.next_fd
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
pub fn take_watcher_id(&self) -> u32 {
|
||||
self.next_watcher
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// A clone of an open file, sharing its offset — which is what a descriptor
|
||||
/// is. Holding the lock only for the lookup keeps a slow write from blocking
|
||||
/// every other call on this thread.
|
||||
pub fn file(&self, fd: f64) -> Result<File, ModuleError> {
|
||||
self.file_and_writability(fd).map(|(file, _)| file)
|
||||
}
|
||||
|
||||
/// The handle and whether it can be written to.
|
||||
pub fn file_and_writability(&self, fd: f64) -> Result<(File, bool), ModuleError> {
|
||||
let id = descriptor(fd)?;
|
||||
let files = self.files.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let (file, writable) = files.get(&id).ok_or_else(|| bad_fd(id))?;
|
||||
let cloned = file
|
||||
.try_clone()
|
||||
.map_err(|error| errors::from_io(error, &id.to_string()))?;
|
||||
Ok((cloned, *writable))
|
||||
}
|
||||
}
|
||||
|
||||
/// The error Node raises for a descriptor it does not know.
|
||||
pub fn bad_fd(id: u32) -> ModuleError {
|
||||
errors::invalid("EBADF", format!("EBADF: bad file descriptor, {id}"))
|
||||
}
|
||||
|
||||
/// A descriptor from the bridge. Anything but a non-negative integer is a
|
||||
/// programmer error, so it reports as a bad descriptor rather than a file error.
|
||||
pub fn descriptor(value: f64) -> Result<u32, ModuleError> {
|
||||
if value.is_finite() && value >= 0.0 && value.fract() == 0.0 && value <= u32::MAX as f64 {
|
||||
Ok(value as u32)
|
||||
} else {
|
||||
Err(bad_fd(0))
|
||||
}
|
||||
}
|
||||
|
||||
/// A byte length from the bridge.
|
||||
pub fn length(value: f64) -> Result<usize, ModuleError> {
|
||||
if value.is_finite() && value >= 0.0 && value.fract() == 0.0 && value <= u32::MAX as f64 {
|
||||
Ok(value as usize)
|
||||
} else {
|
||||
Err(errors::invalid(
|
||||
"EINVAL",
|
||||
format!("EINVAL: invalid argument, length {value}"),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// A permission bitmask, clamped to the bits `std::fs` accepts. Zero means the
|
||||
/// caller passed nothing, which Node treats as `0o666`.
|
||||
pub fn mode(value: f64) -> u32 {
|
||||
if value.is_finite() && value > 0.0 {
|
||||
value as u32 & 0o7777
|
||||
} else {
|
||||
0o666
|
||||
}
|
||||
}
|
||||
|
||||
/// `node:fs` numeric flags as `OpenOptions`.
|
||||
pub fn open_options(flags_value: f64, mode: u32) -> std::fs::OpenOptions {
|
||||
let flags_value = flags_value as i32;
|
||||
let mut options = std::fs::OpenOptions::new();
|
||||
// Bit tests, not `matches!`: `matches!(value, A | B)` is the or-pattern
|
||||
// `value == A || value == B`, which for Node's flags asks whether the whole
|
||||
// mask equals 0 or 2. `'w'` is `WRONLY | CREAT | TRUNC` (577), so that test
|
||||
// was false and every write opened read-only and failed `EINVAL`.
|
||||
// `RDONLY` is zero, so "read" is the absence of `WRONLY` rather than a bit.
|
||||
options
|
||||
.read(flags_value & flags::WRONLY == 0)
|
||||
.write(flags_value & (flags::WRONLY | flags::RDWR) != 0)
|
||||
.create(flags_value & flags::CREAT != 0)
|
||||
.create_new(flags_value & flags::EXCL != 0)
|
||||
.truncate(flags_value & flags::TRUNC != 0)
|
||||
.append(flags_value & flags::APPEND != 0);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::OpenOptionsExt as _;
|
||||
options.mode(mode);
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
let _ = mode;
|
||||
options
|
||||
}
|
||||
|
||||
/// Milliseconds since the epoch, as the bridge sends them.
|
||||
pub fn system_time(ms: f64) -> std::time::SystemTime {
|
||||
if ms.is_finite() && ms >= 0.0 {
|
||||
std::time::UNIX_EPOCH + std::time::Duration::from_millis(ms as u64)
|
||||
} else {
|
||||
std::time::UNIX_EPOCH
|
||||
}
|
||||
}
|
||||
|
||||
/// A `SystemTime` as milliseconds, which is what `StatResult` carries.
|
||||
pub fn millis(time: std::time::SystemTime) -> f64 {
|
||||
time.duration_since(std::time::UNIX_EPOCH)
|
||||
.map(|elapsed| elapsed.as_millis() as f64)
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
// The spec says `THREAD = Own`; this keeps the module compiled against that.
|
||||
const _: ModuleThread = ModuleThread::Own;
|
||||
@@ -1,271 +0,0 @@
|
||||
//! The well-known folders, as one `KnownPaths` object.
|
||||
//!
|
||||
//! `dirs` answers for the folders every platform has. The per-application three
|
||||
//! — `appData`, `appCache`, `appLogs` — are this module's own convention, because
|
||||
//! the host does not yet tell modules which app they belong to; see
|
||||
//! `app_id` below. All three are created on the first read, as the design asks,
|
||||
//! so a caller can write a log file without a `mkdir` first.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
use crate::KnownPaths;
|
||||
|
||||
/// The public folders plus the three per-application ones, created if needed.
|
||||
///
|
||||
/// Nothing here fails: a folder that does not exist is reported as the temp
|
||||
/// directory instead. The design treats these as conveniences, and a caller
|
||||
/// asking where to put a log file is better served by a working stand-in than by
|
||||
/// a thrown error it cannot recover from — the alternative is a crash on a
|
||||
/// system where, say, `Desktop` was never created.
|
||||
pub fn known_paths() -> Result<KnownPaths, ModuleError> {
|
||||
let temp = std::env::temp_dir();
|
||||
let id = app_id();
|
||||
|
||||
let root = std::env::var_os("RN_GPUI_APP_DATA_DIR");
|
||||
let (app_data, app_cache, app_logs) = app_folders(root.as_deref(), &id);
|
||||
|
||||
Ok(KnownPaths {
|
||||
home: fallback(dirs::home_dir(), &temp),
|
||||
documents: fallback(dirs::document_dir(), &temp),
|
||||
downloads: fallback(dirs::download_dir(), &temp),
|
||||
desktop: fallback(dirs::desktop_dir(), &temp),
|
||||
temp: path_string(&temp),
|
||||
app_data: ensure_directory(app_data, &temp),
|
||||
app_cache: ensure_directory(app_cache, &temp),
|
||||
app_logs: ensure_directory(app_logs, &temp),
|
||||
})
|
||||
}
|
||||
|
||||
/// The `appData`, `appCache` and `appLogs` folders for an app id.
|
||||
///
|
||||
/// A non-empty `RN_GPUI_APP_DATA_DIR` moves all three under that root so several
|
||||
/// copies of an app can run side by side with separate data without overriding
|
||||
/// `HOME`: `<root>/<id>`, `<root>/cache/<id>` and `<root>/logs/<id>`.
|
||||
fn app_folders(
|
||||
root: Option<&std::ffi::OsStr>,
|
||||
id: &str,
|
||||
) -> (Option<PathBuf>, Option<PathBuf>, Option<PathBuf>) {
|
||||
match root.filter(|root| !root.is_empty()).map(PathBuf::from) {
|
||||
Some(root) => (
|
||||
Some(root.join(id)),
|
||||
Some(root.join("cache").join(id)),
|
||||
Some(root.join("logs").join(id)),
|
||||
),
|
||||
None => (
|
||||
dirs::data_dir().map(|dir| dir.join(id)),
|
||||
dirs::cache_dir().map(|dir| dir.join(id)),
|
||||
logs_dir(id),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// A folder as a string, with the temp directory standing in when the platform
|
||||
/// cannot name it.
|
||||
fn fallback(found: Option<PathBuf>, temp: &Path) -> String {
|
||||
path_string(found.as_deref().unwrap_or(temp))
|
||||
}
|
||||
|
||||
/// A path as `KnownPaths` carries it. A non-UTF-8 path cannot be a JS string, so
|
||||
/// the lossy form is used rather than failing: it is still a path the platform
|
||||
/// will resolve, and the folder is only a suggestion to the caller.
|
||||
fn path_string(path: &Path) -> String {
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// Creates a directory that the returned path promises exists, falling back to
|
||||
/// the temp directory if it cannot be created.
|
||||
///
|
||||
/// A failure here means the platform gave no base directory at all, or that it
|
||||
/// exists but is not writable; temp is the one directory a process can normally
|
||||
/// count on, so the caller still gets somewhere to write.
|
||||
fn ensure_directory(found: Option<PathBuf>, temp: &Path) -> String {
|
||||
let Some(directory) = found else {
|
||||
return path_string(temp);
|
||||
};
|
||||
match std::fs::create_dir_all(&directory) {
|
||||
Ok(()) => path_string(&directory),
|
||||
Err(_) => path_string(temp),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where `appLogs` lives, which is the one app folder whose convention differs
|
||||
/// by platform: macOS keeps logs under `~/Library/Logs`, the others under the
|
||||
/// data directory. Both get the app id as their last component so two apps do
|
||||
/// not share a log folder.
|
||||
fn logs_dir(id: &str) -> Option<PathBuf> {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
dirs::home_dir().map(|home| home.join("Library").join("Logs").join(id))
|
||||
}
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
{
|
||||
dirs::data_dir().map(|dir| dir.join(id).join("logs"))
|
||||
}
|
||||
}
|
||||
|
||||
/// The id the three per-app folders are namespaced by.
|
||||
///
|
||||
/// This is a **placeholder**. The design wants these paths to be per-app and
|
||||
/// says the host already knows the id (`DesktopAppConfig::app_id`), but no host
|
||||
/// function exposes it to modules yet: `host_info().app_id` does not exist. Until
|
||||
/// that API lands, `RN_GPUI_APP_ID` is honoured when the app sets it, and the
|
||||
/// executable's file stem is the fallback — stable for a given binary, which is
|
||||
/// enough to keep the folders apart and across runs.
|
||||
///
|
||||
/// When `host_info().app_id` is added, this function should read it and the two
|
||||
/// fallbacks should go, because a renamed binary would otherwise move the app's
|
||||
/// data and cache out from under the user.
|
||||
fn app_id() -> String {
|
||||
let from_environment = std::env::var("RN_GPUI_APP_ID").ok();
|
||||
let from_executable = std::env::current_exe().ok().and_then(|exe| {
|
||||
exe.file_stem()
|
||||
.map(|stem| stem.to_string_lossy().into_owned())
|
||||
});
|
||||
pick_app_id(from_environment.as_deref(), from_executable.as_deref())
|
||||
}
|
||||
|
||||
/// The selection half of `app_id`, split out so the precedence and the fallback
|
||||
/// can be tested without touching the environment, which other tests share.
|
||||
fn pick_app_id(from_environment: Option<&str>, from_executable: Option<&str>) -> String {
|
||||
[from_environment, from_executable]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(sanitise)
|
||||
.find(|id| !id.is_empty())
|
||||
// A process whose executable has no usable name still needs folders, and
|
||||
// the name only has to be stable, not accurate.
|
||||
.unwrap_or_else(|| "rn-gpui-app".to_string())
|
||||
}
|
||||
|
||||
/// Strips the characters that cannot appear in a directory name on any of the
|
||||
/// three platforms, so an id from the environment cannot escape its parent
|
||||
/// directory or fail `create_dir_all` on Windows.
|
||||
///
|
||||
/// Leading and trailing dots and dashes go too: an id that was only separators
|
||||
/// sanitises to nothing rather than to a directory named `-`, and one that began
|
||||
/// with `..` cannot end up looking like a parent reference.
|
||||
fn sanitise(id: &str) -> String {
|
||||
id.trim()
|
||||
.chars()
|
||||
.map(|character| match character {
|
||||
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' | '\0' => '-',
|
||||
other => other,
|
||||
})
|
||||
.collect::<String>()
|
||||
.trim_matches(|character| character == '.' || character == '-')
|
||||
.to_string()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn every_known_path_is_a_non_empty_string() {
|
||||
let paths = known_paths().expect("known paths");
|
||||
for (name, value) in [
|
||||
("home", &paths.home),
|
||||
("documents", &paths.documents),
|
||||
("downloads", &paths.downloads),
|
||||
("desktop", &paths.desktop),
|
||||
("temp", &paths.temp),
|
||||
("appData", &paths.app_data),
|
||||
("appCache", &paths.app_cache),
|
||||
("appLogs", &paths.app_logs),
|
||||
] {
|
||||
assert!(!value.is_empty(), "{name} was empty");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_app_folders_exist_after_the_first_read() {
|
||||
let paths = known_paths().expect("known paths");
|
||||
for (name, value) in [
|
||||
("appData", &paths.app_data),
|
||||
("appCache", &paths.app_cache),
|
||||
("appLogs", &paths.app_logs),
|
||||
] {
|
||||
assert!(
|
||||
Path::new(value).is_dir(),
|
||||
"{name} was not created at {value}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_app_folders_are_distinct() {
|
||||
let paths = known_paths().expect("known paths");
|
||||
let app_data = Path::new(&paths.app_data);
|
||||
let app_cache = Path::new(&paths.app_cache);
|
||||
let app_logs = Path::new(&paths.app_logs);
|
||||
|
||||
assert_ne!(app_data, app_cache);
|
||||
assert_ne!(app_data, app_logs);
|
||||
assert_ne!(app_cache, app_logs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_second_read_returns_the_same_folders() {
|
||||
// Creating a directory that already exists has to keep succeeding, since
|
||||
// the JS layer may call `getPaths()` more than once per run.
|
||||
let first = known_paths().expect("known paths");
|
||||
let second = known_paths().expect("known paths");
|
||||
assert_eq!(first.app_data, second.app_data);
|
||||
assert_eq!(first.app_cache, second.app_cache);
|
||||
assert_eq!(first.app_logs, second.app_logs);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_id_from_the_environment_names_the_app_folders() {
|
||||
// Only the pure part is asserted: mutating the environment would race
|
||||
// with the other tests in this module, which read it concurrently.
|
||||
assert_eq!(sanitise("my-app"), "my-app");
|
||||
assert_eq!(sanitise(" ../escape "), "escape");
|
||||
assert_eq!(sanitise("a/b\\c"), "a-b-c");
|
||||
assert_eq!(sanitise(" "), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_data_dir_override_roots_the_three_app_folders() {
|
||||
use std::ffi::OsStr;
|
||||
let (data, cache, logs) = app_folders(Some(OsStr::new("/srv/instance")), "notes");
|
||||
assert_eq!(data, Some(PathBuf::from("/srv/instance/notes")));
|
||||
assert_eq!(cache, Some(PathBuf::from("/srv/instance/cache/notes")));
|
||||
assert_eq!(logs, Some(PathBuf::from("/srv/instance/logs/notes")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_or_missing_data_dir_override_keeps_the_platform_folders() {
|
||||
use std::ffi::OsStr;
|
||||
let default = app_folders(None, "notes");
|
||||
assert_eq!(app_folders(Some(OsStr::new("")), "notes"), default);
|
||||
assert_eq!(default.0, dirs::data_dir().map(|dir| dir.join("notes")));
|
||||
assert_eq!(default.1, dirs::cache_dir().map(|dir| dir.join("notes")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_environment_id_wins_over_the_executable_name() {
|
||||
assert_eq!(pick_app_id(Some("from-env"), Some("from-exe")), "from-env");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unusable_environment_id_falls_through_to_the_executable_name() {
|
||||
assert_eq!(pick_app_id(Some(" "), Some("from-exe")), "from-exe");
|
||||
// A separator-only id would escape its parent, which `sanitise` turns
|
||||
// into a name rather than a path.
|
||||
assert_eq!(pick_app_id(Some("/"), Some("from-exe")), "from-exe");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_id_at_all_still_yields_a_stable_name() {
|
||||
assert_eq!(pick_app_id(None, None), "rn-gpui-app");
|
||||
assert_eq!(pick_app_id(Some(""), Some("")), "rn-gpui-app");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hostile_executable_name_cannot_escape_the_base_directory() {
|
||||
assert_eq!(pick_app_id(None, Some("../../etc/passwd")), "etc-passwd");
|
||||
}
|
||||
}
|
||||
@@ -1,237 +0,0 @@
|
||||
//! `upload`: a file, or a range of one, streamed as an HTTP request body.
|
||||
//!
|
||||
//! The file is read in small blocks as the connection takes them, so the body is
|
||||
//! never held in memory. The request runs on its own thread; the cancel flag is
|
||||
//! checked on every block, which is what makes a cancel prompt while the
|
||||
//! connection is busy.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{self, Read, Seek, SeekFrom};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use rn_gpui_module::ModuleError;
|
||||
use ureq::config::Config;
|
||||
use ureq::tls::{RootCerts, TlsConfig};
|
||||
use ureq::{Agent, SendBody};
|
||||
|
||||
use crate::{
|
||||
errors, DownloadHeader, NativeFsEvents, RNFsModule, UploadOptions, UploadProgress,
|
||||
UploadResult,
|
||||
};
|
||||
|
||||
const BLOCK: usize = 64 * 1024;
|
||||
const PROGRESS_INTERVAL: Duration = Duration::from_millis(100);
|
||||
const RESPONSE_BODY_LIMIT: u64 = 2048;
|
||||
|
||||
pub type Uploads = Mutex<HashMap<u32, Arc<AtomicBool>>>;
|
||||
|
||||
fn agent() -> &'static Agent {
|
||||
static AGENT: OnceLock<Agent> = OnceLock::new();
|
||||
AGENT.get_or_init(|| {
|
||||
let config = Config::builder()
|
||||
.http_status_as_error(false)
|
||||
.timeout_connect(Some(Duration::from_secs(30)))
|
||||
.tls_config(
|
||||
TlsConfig::builder()
|
||||
.root_certs(RootCerts::PlatformVerifier)
|
||||
.build(),
|
||||
)
|
||||
.build();
|
||||
Agent::new_with_config(config)
|
||||
})
|
||||
}
|
||||
|
||||
fn aborted() -> ModuleError {
|
||||
ModuleError::with_code("ABORT_ERR", "The upload was aborted")
|
||||
}
|
||||
|
||||
fn failed(message: impl Into<String>) -> ModuleError {
|
||||
ModuleError::with_code("ERR_UPLOAD", message.into())
|
||||
}
|
||||
|
||||
fn id_of(value: f64) -> u32 {
|
||||
value as u32
|
||||
}
|
||||
|
||||
pub fn cancel(module: &RNFsModule, id: f64) -> bool {
|
||||
match module.uploads.lock().expect("uploads").get(&id_of(id)) {
|
||||
Some(stop) => {
|
||||
stop.store(true, Ordering::SeqCst);
|
||||
true
|
||||
}
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
|
||||
struct Body {
|
||||
file: File,
|
||||
remaining: u64,
|
||||
sent: u64,
|
||||
total: u64,
|
||||
id: u32,
|
||||
events: NativeFsEvents,
|
||||
stop: Arc<AtomicBool>,
|
||||
last_progress: Instant,
|
||||
}
|
||||
|
||||
impl Body {
|
||||
fn emit(&self) {
|
||||
self.events.on_upload_progress(UploadProgress {
|
||||
id: f64::from(self.id),
|
||||
sent: self.sent as f64,
|
||||
total: self.total as f64,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Read for Body {
|
||||
fn read(&mut self, buffer: &mut [u8]) -> io::Result<usize> {
|
||||
if self.stop.load(Ordering::SeqCst) {
|
||||
return Err(io::Error::new(io::ErrorKind::Other, "aborted"));
|
||||
}
|
||||
if self.remaining == 0 {
|
||||
return Ok(0);
|
||||
}
|
||||
let wanted = buffer.len().min(BLOCK).min(self.remaining as usize);
|
||||
let read = self.file.read(&mut buffer[..wanted])?;
|
||||
if read == 0 {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::UnexpectedEof,
|
||||
"the file is shorter than the requested range",
|
||||
));
|
||||
}
|
||||
self.remaining -= read as u64;
|
||||
self.sent += read as u64;
|
||||
if self.last_progress.elapsed() >= PROGRESS_INTERVAL || self.remaining == 0 {
|
||||
self.last_progress = Instant::now();
|
||||
self.emit();
|
||||
}
|
||||
Ok(read)
|
||||
}
|
||||
}
|
||||
|
||||
fn run(
|
||||
events: NativeFsEvents,
|
||||
id: u32,
|
||||
url: String,
|
||||
path: PathBuf,
|
||||
options: UploadOptions,
|
||||
stop: Arc<AtomicBool>,
|
||||
) -> Result<UploadResult, ModuleError> {
|
||||
let shown = path.display().to_string();
|
||||
let mut file = File::open(&path).map_err(|e| errors::from_io(e, &shown))?;
|
||||
let size = file
|
||||
.metadata()
|
||||
.map_err(|e| errors::from_io(e, &shown))?
|
||||
.len();
|
||||
let offset = if options.offset > 0.0 {
|
||||
options.offset as u64
|
||||
} else {
|
||||
0
|
||||
};
|
||||
if offset > size {
|
||||
return Err(failed("the offset is past the end of the file"));
|
||||
}
|
||||
let length = if options.length < 0.0 {
|
||||
size - offset
|
||||
} else {
|
||||
options.length as u64
|
||||
};
|
||||
if offset + length > size {
|
||||
return Err(failed("the range is past the end of the file"));
|
||||
}
|
||||
file.seek(SeekFrom::Start(offset))
|
||||
.map_err(|e| errors::from_io(e, &shown))?;
|
||||
|
||||
let method = if options.method.is_empty() {
|
||||
"PUT".to_string()
|
||||
} else {
|
||||
options.method.clone()
|
||||
};
|
||||
let mut builder = ureq::http::Request::builder().method(method.as_str()).uri(&url);
|
||||
let mut has_length = false;
|
||||
for header in &options.headers {
|
||||
if header.name.eq_ignore_ascii_case("content-length") {
|
||||
has_length = true;
|
||||
}
|
||||
builder = builder.header(header.name.as_str(), header.value.as_str());
|
||||
}
|
||||
if !has_length {
|
||||
builder = builder.header("content-length", length.to_string());
|
||||
}
|
||||
|
||||
let mut body = Body {
|
||||
file,
|
||||
remaining: length,
|
||||
sent: 0,
|
||||
total: length,
|
||||
id,
|
||||
events,
|
||||
stop: stop.clone(),
|
||||
last_progress: Instant::now() - PROGRESS_INTERVAL,
|
||||
};
|
||||
body.emit();
|
||||
let request = builder
|
||||
.body(SendBody::from_reader(&mut body))
|
||||
.map_err(|e| failed(e.to_string()))?;
|
||||
let outcome = agent().run(request);
|
||||
if stop.load(Ordering::SeqCst) {
|
||||
return Err(aborted());
|
||||
}
|
||||
let mut response = outcome.map_err(|e| failed(e.to_string()))?;
|
||||
let status = response.status().as_u16();
|
||||
let mut headers: Vec<DownloadHeader> = Vec::new();
|
||||
for (name, value) in response.headers() {
|
||||
let value = String::from_utf8_lossy(value.as_bytes()).into_owned();
|
||||
match headers.iter_mut().find(|h| h.name == name.as_str()) {
|
||||
Some(existing) => {
|
||||
existing.value.push_str(", ");
|
||||
existing.value.push_str(&value);
|
||||
}
|
||||
None => headers.push(DownloadHeader {
|
||||
name: name.as_str().to_string(),
|
||||
value,
|
||||
}),
|
||||
}
|
||||
}
|
||||
let mut text = Vec::new();
|
||||
let _ = response
|
||||
.body_mut()
|
||||
.as_reader()
|
||||
.take(RESPONSE_BODY_LIMIT)
|
||||
.read_to_end(&mut text);
|
||||
Ok(UploadResult {
|
||||
status: f64::from(status),
|
||||
headers,
|
||||
body: String::from_utf8_lossy(&text).into_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn upload(
|
||||
module: &RNFsModule,
|
||||
url: String,
|
||||
path: String,
|
||||
options: UploadOptions,
|
||||
) -> Result<UploadResult, ModuleError> {
|
||||
let id = id_of(options.id);
|
||||
let stop = Arc::new(AtomicBool::new(false));
|
||||
{
|
||||
let mut running = module.uploads.lock().expect("uploads");
|
||||
if running.contains_key(&id) {
|
||||
return Err(failed(format!("upload {id} is already running")));
|
||||
}
|
||||
running.insert(id, stop.clone());
|
||||
}
|
||||
let (sender, receiver) = futures::channel::oneshot::channel();
|
||||
let events = module.events.clone();
|
||||
std::thread::spawn(move || {
|
||||
let _ = sender.send(run(events, id, url, PathBuf::from(path), options, stop));
|
||||
});
|
||||
let result = receiver.await;
|
||||
module.uploads.lock().expect("uploads").remove(&id);
|
||||
result.unwrap_or_else(|_| Err(failed("the upload stopped unexpectedly")))
|
||||
}
|
||||
@@ -1,392 +0,0 @@
|
||||
//! The watcher behind `watch` and `unwatch`.
|
||||
//!
|
||||
//! `notify` gives each watcher a thread of its own and calls the handler on it.
|
||||
//! That handler cannot borrow the module — it may outlive any single call and
|
||||
//! runs on a thread the module does not own — so it is handed a clone of the
|
||||
//! event emitter and an `Arc` of the debounce table instead. The module only
|
||||
//! stores the `RecommendedWatcher`; dropping that value stops the OS watch, and
|
||||
//! with it the thread, which is how a JS reload closes every watcher at once.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use notify::event::EventKind;
|
||||
use notify::Watcher as _;
|
||||
use notify::{RecommendedWatcher, RecursiveMode};
|
||||
use rn_gpui_module::ModuleError;
|
||||
|
||||
use crate::{errors, RNFsModule, WatchEvent};
|
||||
|
||||
/// How long a path stays quiet after being reported.
|
||||
///
|
||||
/// An editor saving a file usually writes it several times in a burst (write,
|
||||
/// rename over the original, a truncated open), and a recursive watch on a build
|
||||
/// directory can see dozens of events for one logical change. Fifty milliseconds
|
||||
/// collapses a burst into the single event a caller can act on without making a
|
||||
/// genuinely repeated edit invisible.
|
||||
const DEBOUNCE: Duration = Duration::from_millis(50);
|
||||
|
||||
/// An installed watch, kept alive by the module's table.
|
||||
///
|
||||
/// The field is never read: it exists so that removing the entry drops it, which
|
||||
/// is what stops notify's thread and the platform watch.
|
||||
pub struct Watcher {
|
||||
_watcher: RecommendedWatcher,
|
||||
}
|
||||
|
||||
/// What was last reported for a path, and when.
|
||||
#[derive(Clone, Copy)]
|
||||
struct LastReport {
|
||||
at: Instant,
|
||||
kind: EventKindTag,
|
||||
}
|
||||
|
||||
/// Which of the three stable names an event carries.
|
||||
///
|
||||
/// A copy of the mapping `kind_name` produces, so the debounce can compare two
|
||||
/// events without allocating the string it will not send.
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||
enum EventKindTag {
|
||||
Create,
|
||||
Modify,
|
||||
Remove,
|
||||
Other,
|
||||
}
|
||||
|
||||
/// Per-path timestamps of the last event that was reported.
|
||||
///
|
||||
/// Shared with the handler through an `Arc` because the handler owns its state
|
||||
/// for as long as the watcher lives, which is longer than any borrow of the
|
||||
/// module. Paths that fall out of the window are pruned on every report, so a
|
||||
/// long-running recursive watch cannot grow this without bound.
|
||||
///
|
||||
/// The rule, in one sentence: **a burst is collapsed per path and per kind, and a
|
||||
/// removal is never collapsed away.** Keeping only the first event of a burst
|
||||
/// lost the end of it — a create followed by a remove reported only `create`, so
|
||||
/// a caller was told about a file that no longer existed. Two adjustments fix
|
||||
/// that without giving up the collapsing that makes an editor's write burst
|
||||
/// readable:
|
||||
///
|
||||
/// - a path's window is per kind, so a `remove` after a `create` is a different
|
||||
/// event rather than a suppressed one;
|
||||
/// - a removal always reports, whatever the window, because it is the last state
|
||||
/// a path can reach and swallowing it leaves the caller's view permanently
|
||||
/// wrong. The cost is a possible duplicate removal for one logical deletion,
|
||||
/// which a watcher callback can tolerate; a missing one it cannot.
|
||||
#[derive(Default)]
|
||||
struct Debounce {
|
||||
last: Mutex<HashMap<(PathBuf, EventKindTag), Instant>>,
|
||||
}
|
||||
|
||||
impl Debounce {
|
||||
/// Whether this event should be delivered now.
|
||||
///
|
||||
/// Takes `now` rather than reading the clock so the window can be tested
|
||||
/// without sleeping.
|
||||
fn should_report_at(&self, path: &Path, kind: EventKindTag, now: Instant) -> bool {
|
||||
let mut last = self.last.lock().unwrap_or_else(|error| error.into_inner());
|
||||
let key = (path.to_path_buf(), kind);
|
||||
let report = match last.get(&key).copied() {
|
||||
Some(previous) => {
|
||||
kind == EventKindTag::Remove || now.saturating_duration_since(previous) >= DEBOUNCE
|
||||
}
|
||||
None => true,
|
||||
};
|
||||
if report {
|
||||
last.retain(|_, previous| now.saturating_duration_since(*previous) < DEBOUNCE);
|
||||
last.insert(key, now);
|
||||
}
|
||||
report
|
||||
}
|
||||
|
||||
fn should_report(&self, path: &Path, kind: EventKindTag) -> bool {
|
||||
self.should_report_at(path, kind, Instant::now())
|
||||
}
|
||||
}
|
||||
|
||||
/// The stable string the JS layer matches on.
|
||||
///
|
||||
/// Everything that is not a create, modify or remove — access events, and the
|
||||
/// catch-all notify reports when a backend cannot be more precise — becomes
|
||||
/// `other` rather than leaking a platform-specific spelling.
|
||||
fn kind_name(kind: &EventKind) -> &'static str {
|
||||
match kind {
|
||||
EventKind::Create(_) => "create",
|
||||
EventKind::Modify(_) => "modify",
|
||||
EventKind::Remove(_) => "remove",
|
||||
_ => "other",
|
||||
}
|
||||
}
|
||||
|
||||
/// The tag `kind_name` corresponds to, for the debounce to compare by.
|
||||
fn kind_tag(kind: &EventKind) -> EventKindTag {
|
||||
match kind {
|
||||
EventKind::Create(_) => EventKindTag::Create,
|
||||
EventKind::Modify(_) => EventKindTag::Modify,
|
||||
EventKind::Remove(_) => EventKindTag::Remove,
|
||||
_ => EventKindTag::Other,
|
||||
}
|
||||
}
|
||||
|
||||
/// An event path as the caller should see it.
|
||||
///
|
||||
/// Node reports the path relative to the watch root, and a name alone for a
|
||||
/// single-file watch. The platform reports an absolute path, and on macOS a
|
||||
/// *canonical* one: FSEvents resolves `/var` to `/private/var`, so a plain string
|
||||
/// prefix check against the path the caller gave strips nothing and the callback
|
||||
/// receives an absolute path. Both roots are therefore tried, and the given one
|
||||
/// last so a caller watching through a symlink still gets a relative name.
|
||||
fn relative_event_path(path: &Path, roots: &[PathBuf], watching_file: bool) -> String {
|
||||
if watching_file {
|
||||
return path
|
||||
.file_name()
|
||||
.map(|name| name.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| path.to_string_lossy().into_owned());
|
||||
}
|
||||
for root in roots {
|
||||
if let Ok(rest) = path.strip_prefix(root) {
|
||||
// Normalised to `/`: Node reports relative paths with forward
|
||||
// slashes on every platform, and a Windows caller would otherwise get
|
||||
// `nested\file.txt` where Node gives `nested/file.txt`.
|
||||
return rest.to_string_lossy().replace('\\', "/");
|
||||
}
|
||||
}
|
||||
path.to_string_lossy().into_owned()
|
||||
}
|
||||
|
||||
/// A notify failure as the error JS expects.
|
||||
///
|
||||
/// Most failures wrap an `io::Error` that already maps through `errors::from_io`;
|
||||
/// the ones that do not are described well enough by notify to be reported with
|
||||
/// the closest Node code.
|
||||
fn watcher_error(error: notify::Error, path: &str) -> ModuleError {
|
||||
use notify::ErrorKind;
|
||||
let io = match error.kind {
|
||||
ErrorKind::Io(error) => error,
|
||||
ErrorKind::PathNotFound => std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
|
||||
ErrorKind::MaxFilesWatch => {
|
||||
std::io::Error::new(std::io::ErrorKind::Other, "OS file watch limit reached")
|
||||
}
|
||||
other => std::io::Error::new(std::io::ErrorKind::Other, format!("{other:?}")),
|
||||
};
|
||||
errors::from_io(io, path)
|
||||
}
|
||||
|
||||
/// Starts watching `path`, returning the id events carry.
|
||||
pub fn watch(module: &RNFsModule, path: String, recursive: bool) -> Result<f64, ModuleError> {
|
||||
// Node fails a watch on a missing path immediately, and so must this: the
|
||||
// platform watchers otherwise accept the path and then go silent, which is
|
||||
// indistinguishable at the call site from a directory nothing ever touches.
|
||||
let target = PathBuf::from(&path);
|
||||
if !target.exists() {
|
||||
return Err(errors::from_io(
|
||||
std::io::Error::new(std::io::ErrorKind::NotFound, "not found"),
|
||||
&path,
|
||||
));
|
||||
}
|
||||
|
||||
let id = module.take_watcher_id();
|
||||
let debounce = Arc::new(Debounce::default());
|
||||
let events = module.events.clone();
|
||||
// The canonical root is what macOS actually reports inside, and the given
|
||||
// root is what the caller passed; keeping both means the relative name
|
||||
// survives either spelling.
|
||||
let watching_file = target.is_file();
|
||||
let roots: Vec<PathBuf> = {
|
||||
let mut roots = Vec::new();
|
||||
if let Ok(canonical) = target.canonicalize() {
|
||||
roots.push(canonical);
|
||||
}
|
||||
roots.push(target.clone());
|
||||
roots
|
||||
};
|
||||
let handler = move |result: notify::Result<notify::Event>| {
|
||||
// A backend error has nowhere to go: it is not tied to a call, and
|
||||
// reporting it through the event stream would make it look like a file
|
||||
// change. notify's thread keeps running, so later events still arrive.
|
||||
let Ok(event) = result else { return };
|
||||
let kind = kind_name(&event.kind);
|
||||
let tag = kind_tag(&event.kind);
|
||||
// One platform event can name several paths — a directory move, for
|
||||
// instance — and a caller wants one delivery per path.
|
||||
for path in &event.paths {
|
||||
if !debounce.should_report(path, tag) {
|
||||
continue;
|
||||
}
|
||||
events.on_watch_event(WatchEvent {
|
||||
id: f64::from(id),
|
||||
kind: kind.to_string(),
|
||||
path: relative_event_path(path, &roots, watching_file),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let mut watcher =
|
||||
notify::recommended_watcher(handler).map_err(|error| watcher_error(error, &path))?;
|
||||
let mode = if recursive {
|
||||
RecursiveMode::Recursive
|
||||
} else {
|
||||
RecursiveMode::NonRecursive
|
||||
};
|
||||
watcher
|
||||
.watch(&target, mode)
|
||||
.map_err(|error| watcher_error(error, &path))?;
|
||||
|
||||
module
|
||||
.watchers
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner())
|
||||
.insert(id, Watcher { _watcher: watcher });
|
||||
Ok(f64::from(id))
|
||||
}
|
||||
|
||||
/// Stops a watcher. An id that is already gone is not a failure: JS may close a
|
||||
/// watcher twice, or close one whose module has already dropped it.
|
||||
pub fn unwatch(module: &RNFsModule, id: f64) {
|
||||
let Ok(id) = crate::module::descriptor(id) else {
|
||||
return;
|
||||
};
|
||||
// Removing the entry drops the watcher, which stops notify's thread.
|
||||
module
|
||||
.watchers
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner())
|
||||
.remove(&id);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use notify::event::{CreateKind, ModifyKind, RemoveKind};
|
||||
|
||||
#[test]
|
||||
fn event_kinds_map_to_stable_names() {
|
||||
assert_eq!(kind_name(&EventKind::Create(CreateKind::File)), "create");
|
||||
assert_eq!(kind_name(&EventKind::Create(CreateKind::Any)), "create");
|
||||
assert_eq!(kind_name(&EventKind::Modify(ModifyKind::Any)), "modify");
|
||||
assert_eq!(kind_name(&EventKind::Remove(RemoveKind::Any)), "remove");
|
||||
assert_eq!(kind_name(&EventKind::Any), "other");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_event_for_a_path_is_reported() {
|
||||
let debounce = Debounce::default();
|
||||
let now = Instant::now();
|
||||
assert!(debounce.should_report_at(Path::new("/a"), EventKindTag::Modify, now));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_burst_is_collapsed_into_one_event() {
|
||||
let debounce = Debounce::default();
|
||||
let start = Instant::now();
|
||||
assert!(debounce.should_report_at(Path::new("/a"), EventKindTag::Modify, start));
|
||||
assert!(!debounce.should_report_at(
|
||||
Path::new("/a"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(10)
|
||||
));
|
||||
assert!(!debounce.should_report_at(
|
||||
Path::new("/a"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(49)
|
||||
));
|
||||
assert!(debounce.should_report_at(
|
||||
Path::new("/a"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(50)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_remove_inside_a_burst_is_not_swallowed() {
|
||||
let debounce = Debounce::default();
|
||||
let start = Instant::now();
|
||||
let path = Path::new("/a");
|
||||
// A create and the removal that follows it are two states of the path,
|
||||
// not one event seen twice: reporting only the create tells a caller
|
||||
// about a file that is already gone.
|
||||
assert!(debounce.should_report_at(path, EventKindTag::Create, start));
|
||||
assert!(debounce.should_report_at(
|
||||
path,
|
||||
EventKindTag::Remove,
|
||||
start + Duration::from_millis(5)
|
||||
));
|
||||
// A second removal for the same deletion may arrive; a missing one may
|
||||
// not, so this is deliberately the lenient side of the trade.
|
||||
assert!(debounce.should_report_at(
|
||||
path,
|
||||
EventKindTag::Remove,
|
||||
start + Duration::from_millis(10)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_modify_burst_still_collapses() {
|
||||
let debounce = Debounce::default();
|
||||
let start = Instant::now();
|
||||
let path = Path::new("/a");
|
||||
assert!(debounce.should_report_at(path, EventKindTag::Modify, start));
|
||||
// Two writes a millisecond apart are one logical change.
|
||||
assert!(!debounce.should_report_at(
|
||||
path,
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(1)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_canonical_root_still_yields_a_relative_name() {
|
||||
// macOS reports canonical paths, so a watch on `/var/...` sees
|
||||
// `/private/var/...`. Both roots are tried, and the caller gets a name
|
||||
// relative to the one they asked about either way.
|
||||
let canonical = PathBuf::from("/private/var/folders/x/tmp");
|
||||
let given = PathBuf::from("/var/folders/x/tmp");
|
||||
let roots = vec![canonical.clone(), given];
|
||||
let event = canonical.join("nested").join("file.txt");
|
||||
assert_eq!(
|
||||
relative_event_path(&event, &roots, false),
|
||||
"nested/file.txt"
|
||||
);
|
||||
// A path outside every root is passed through rather than mangled.
|
||||
assert_eq!(
|
||||
relative_event_path(Path::new("/elsewhere/file.txt"), &roots, false),
|
||||
"/elsewhere/file.txt"
|
||||
);
|
||||
// Watching a single file reports its name, which is what Node does.
|
||||
assert_eq!(relative_event_path(&event, &roots, true), "file.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn debouncing_is_per_path() {
|
||||
let debounce = Debounce::default();
|
||||
let start = Instant::now();
|
||||
assert!(debounce.should_report_at(Path::new("/a"), EventKindTag::Modify, start));
|
||||
assert!(debounce.should_report_at(
|
||||
Path::new("/b"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(1)
|
||||
));
|
||||
assert!(!debounce.should_report_at(
|
||||
Path::new("/a"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(2)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn paths_outside_the_window_are_forgotten() {
|
||||
let debounce = Debounce::default();
|
||||
let start = Instant::now();
|
||||
assert!(debounce.should_report_at(Path::new("/a"), EventKindTag::Modify, start));
|
||||
assert!(debounce.should_report_at(
|
||||
Path::new("/b"),
|
||||
EventKindTag::Modify,
|
||||
start + Duration::from_millis(60)
|
||||
));
|
||||
let last = debounce.last.lock().unwrap();
|
||||
assert_eq!(last.len(), 1, "the stale path should have been pruned");
|
||||
assert!(last.contains_key(&(PathBuf::from("/b"), EventKindTag::Modify)));
|
||||
}
|
||||
}
|
||||
@@ -1,142 +0,0 @@
|
||||
---
|
||||
id: desktop-file-system
|
||||
title: File system
|
||||
---
|
||||
|
||||
`@react-native-gpui/fs` is a file system API for desktop apps. It follows `node:fs`, so code and habits from Node carry over, and adds file dialogs and the well-known folders of the operating system.
|
||||
|
||||
```shell
|
||||
npm install @react-native-gpui/fs
|
||||
```
|
||||
|
||||
```js
|
||||
import fs, { dialog, paths } from '@react-native-gpui/fs';
|
||||
```
|
||||
|
||||
The module runs on its own thread, so a slow file never blocks JavaScript or the interface.
|
||||
|
||||
## Reading and writing
|
||||
|
||||
The default export is the `node:fs` object, with synchronous, callback and promise forms:
|
||||
|
||||
```js
|
||||
const text = fs.readFileSync(`${paths.documents}/notes.txt`, 'utf8');
|
||||
|
||||
await fs.promises.writeFile(
|
||||
`${paths.appData}/state.json`,
|
||||
JSON.stringify(state),
|
||||
);
|
||||
|
||||
const entries = await fs.promises.readdir(paths.downloads, {
|
||||
withFileTypes: true,
|
||||
});
|
||||
```
|
||||
|
||||
The common operations are available: `readFile`, `writeFile`, `appendFile`, `stat`, `lstat`, `readdir`, `mkdir`, `rm`, `rename`, `copyFile`, `cp`, `access`, `realpath`, `readlink`, `symlink`, `truncate`, `utimes`, `chmod`, `mkdtemp`, `open` with `FileHandle`, and `existsSync`. Encodings, `Stats`, `Dirent`, flags and `constants` behave as they do in Node. Errors carry Node's error codes, such as `ENOENT`.
|
||||
|
||||
Text is read and written as UTF-8 directly in native code. Other encodings and binary data go through `ArrayBuffer`.
|
||||
|
||||
## Well-known folders
|
||||
|
||||
`paths` holds the folders the operating system defines. They are created the first time you read them:
|
||||
|
||||
| Name | Folder |
|
||||
| ----------- | ------------------------------------------ |
|
||||
| `home` | The user's home folder. |
|
||||
| `documents` | Documents. |
|
||||
| `downloads` | Downloads. |
|
||||
| `desktop` | The desktop. |
|
||||
| `temp` | The temporary folder. |
|
||||
| `appData` | Where the app keeps its data. |
|
||||
| `appCache` | Where the app keeps files it can recreate. |
|
||||
| `appLogs` | Where the app writes logs. |
|
||||
|
||||
When the environment variable `RN_GPUI_APP_DATA_DIR` is set to a non-empty folder, the three app folders move under it so several copies of an app can run with separate data without overriding `HOME`: `appData` is `$RN_GPUI_APP_DATA_DIR/<id>`, `appCache` is `$RN_GPUI_APP_DATA_DIR/cache/<id>` and `appLogs` is `$RN_GPUI_APP_DATA_DIR/logs/<id>`. They are still created on first read. The other paths are unchanged.
|
||||
|
||||
## Dialogs
|
||||
|
||||
`dialog` opens the platform's file pickers. Cancelling is not an error: `open` resolves with an empty array, and `save` with `null`.
|
||||
|
||||
```js
|
||||
const [file] = await dialog.open({
|
||||
title: 'Choose a document',
|
||||
filters: [{ name: 'Text', extensions: ['txt', 'md'] }],
|
||||
});
|
||||
|
||||
const target = await dialog.save({ defaultName: 'export.json' });
|
||||
```
|
||||
|
||||
| `dialog.open` option | Description |
|
||||
| -------------------- | ---------------------------------- |
|
||||
| `title` | The title of the dialog. |
|
||||
| `defaultPath` | The folder to start in. |
|
||||
| `multiple` | Allow several files. |
|
||||
| `directory` | Choose a folder instead of a file. |
|
||||
| `filters` | `{name, extensions}` entries. |
|
||||
|
||||
`dialog.save` takes `title`, `defaultPath`, `defaultName` and `filters`. On Linux the dialogs use the XDG portal, so GTK is not required.
|
||||
|
||||
## Reveal and open
|
||||
|
||||
```js
|
||||
await fs.promises.reveal(path); // show the file in Finder, Explorer or the file manager
|
||||
await fs.promises.openExternal(path); // open a file or URL with its default application
|
||||
```
|
||||
|
||||
## Downloading
|
||||
|
||||
`download(url, path, options)` streams an HTTP response into a file without holding it in memory. The body is written to `<path>.part` and renamed onto `path` when it is complete, so `path` never holds half a file; an existing file is replaced only on success. The partial file is removed when the request fails or is aborted.
|
||||
|
||||
```js
|
||||
const controller = new AbortController();
|
||||
|
||||
const { status, headers, bytes } = await fs.download(
|
||||
url,
|
||||
`${paths.downloads}/report.pdf`,
|
||||
{
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
onProgress: (received, total) =>
|
||||
setProgress(total > 0 ? received / total : 0),
|
||||
signal: controller.signal,
|
||||
},
|
||||
);
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ------------ | -------------------------------------------------------------------- |
|
||||
| `method` | `GET` unless set. No request body is sent. |
|
||||
| `headers` | Request headers. |
|
||||
| `onProgress` | `(received, total)`; `total` is `-1` when the server gave no length. |
|
||||
| `signal` | Aborting rejects with an `AbortError` within a fraction of a second. |
|
||||
|
||||
The promise resolves with the final `status`, the response `headers` (lower-cased) and the `bytes` written. A status outside 200 to 299 rejects with `code` `ERR_HTTP_STATUS` and the `status`, `headers` and the first 2 KiB of the `body`, and writes nothing. Redirects are followed. A connection that closes before the announced length rejects, and the parent folder of `path` must exist. Certificates are checked against the operating system's store.
|
||||
|
||||
## Uploading
|
||||
|
||||
`upload(url, path, options)` sends a file, or a range of one, as an HTTP request body without holding it in memory. The native side reads the file in 64 KiB blocks while the connection takes them.
|
||||
|
||||
```js
|
||||
const { status, headers } = await fs.upload(url, `${paths.temp}/part-1`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/octet-stream' },
|
||||
offset: 0,
|
||||
length: 10 * 1024 * 1024,
|
||||
onProgress: (sent, total) => setProgress(sent / total),
|
||||
signal: controller.signal,
|
||||
});
|
||||
```
|
||||
|
||||
| Option | Description |
|
||||
| ------------ | -------------------------------------------------------------------- |
|
||||
| `method` | `PUT` unless set. |
|
||||
| `headers` | Request headers. `Content-Length` is set from the range. |
|
||||
| `offset` | Where in the file the body starts. Zero unless set. |
|
||||
| `length` | How many bytes to send. Everything from `offset` unless set. |
|
||||
| `onProgress` | `(sent, total)`. |
|
||||
| `signal` | Aborting rejects with an `AbortError` within a fraction of a second. |
|
||||
|
||||
The promise resolves with the final `status`, the response `headers` (lower-cased) and the first 2 KiB of the response `body`, whatever the status is, so a caller can read an `ETag` or an error document. A range past the end of the file rejects. Certificates are checked against the operating system's store.
|
||||
|
||||
## Watching
|
||||
|
||||
`watch(path, listener)` watches a file or folder, as in Node. The listener receives `'rename'` or `'change'` and the file name, and the watcher it returns has a `close()` method.
|
||||
@@ -1,40 +0,0 @@
|
||||
{
|
||||
"name": "@react-native-gpui/fs",
|
||||
"version": "0.0.1-alpha.1",
|
||||
"description": "File system access for React Native GPUI apps",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/streetwriters/react-native-gpui-modules.git",
|
||||
"directory": "modules/fs"
|
||||
},
|
||||
"homepage": "https://github.com/streetwriters/react-native-gpui-modules/tree/main/modules/fs#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/streetwriters/react-native-gpui-modules/issues"
|
||||
},
|
||||
"main": "src/index.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"desktop",
|
||||
"react-native.config.js",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md",
|
||||
"!**/*.test.js",
|
||||
"!**/*.test.mjs",
|
||||
"!**/*.test.ts",
|
||||
"!**/*.test.cjs"
|
||||
],
|
||||
"codegenConfig": {
|
||||
"name": "RNFsModuleSpec",
|
||||
"type": "modules",
|
||||
"jsSrcsDir": "src"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"react-native": "0.87.x"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "latest"
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
module.exports = {
|
||||
dependency: {
|
||||
platforms: {
|
||||
desktop: {
|
||||
crate: 'rn-gpui-fs',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
-235
@@ -1,235 +0,0 @@
|
||||
import type { CodegenTypes, TurboModule } from 'react-native';
|
||||
import { TurboModuleRegistry } from 'react-native';
|
||||
|
||||
/**
|
||||
* How a path entry is classified: 0 file, 1 directory, 2 symlink, 3 other.
|
||||
*
|
||||
* A plain number rather than a string or enum so it crosses the bridge without a
|
||||
* lookup table and survives being read back out of a plain object in JS.
|
||||
*/
|
||||
export type FileKind = number;
|
||||
|
||||
export type StatResult = {
|
||||
size: number;
|
||||
mode: number;
|
||||
kind: FileKind;
|
||||
atimeMs: number;
|
||||
mtimeMs: number;
|
||||
ctimeMs: number;
|
||||
birthtimeMs: number;
|
||||
dev: number;
|
||||
ino: number;
|
||||
nlink: number;
|
||||
uid: number;
|
||||
gid: number;
|
||||
};
|
||||
|
||||
export type DirEntry = {
|
||||
name: string;
|
||||
kind: FileKind;
|
||||
parentPath: string;
|
||||
};
|
||||
|
||||
export type DialogFilter = {
|
||||
name: string;
|
||||
extensions: Array<string>;
|
||||
};
|
||||
|
||||
export type OpenDialogOptions = {
|
||||
title?: string;
|
||||
defaultPath?: string;
|
||||
multiple: boolean;
|
||||
directory: boolean;
|
||||
filters: Array<DialogFilter>;
|
||||
};
|
||||
|
||||
export type SaveDialogOptions = {
|
||||
title?: string;
|
||||
defaultPath?: string;
|
||||
defaultName?: string;
|
||||
filters: Array<DialogFilter>;
|
||||
};
|
||||
|
||||
export type KnownPaths = {
|
||||
home: string;
|
||||
documents: string;
|
||||
downloads: string;
|
||||
desktop: string;
|
||||
temp: string;
|
||||
appData: string;
|
||||
appCache: string;
|
||||
appLogs: string;
|
||||
};
|
||||
|
||||
export type WatchEvent = {
|
||||
id: number;
|
||||
kind: string;
|
||||
path: string;
|
||||
};
|
||||
|
||||
export type DownloadHeader = {
|
||||
name: string;
|
||||
value: string;
|
||||
};
|
||||
|
||||
export type DownloadOptions = {
|
||||
id: number;
|
||||
method: string;
|
||||
headers: Array<DownloadHeader>;
|
||||
};
|
||||
|
||||
/**
|
||||
* What a download resolves with. A non-2xx answer is not an error here: it
|
||||
* comes back with its `status` and a small `body`, and no file is written.
|
||||
*/
|
||||
export type DownloadResult = {
|
||||
status: number;
|
||||
headers: Array<DownloadHeader>;
|
||||
bytes: number;
|
||||
body?: string;
|
||||
};
|
||||
|
||||
export type DownloadProgress = {
|
||||
id: number;
|
||||
received: number;
|
||||
// -1 when the server did not say how long the body is.
|
||||
total: number;
|
||||
};
|
||||
|
||||
export type UploadOptions = {
|
||||
id: number;
|
||||
method: string;
|
||||
headers: Array<DownloadHeader>;
|
||||
// Where in the file the body starts.
|
||||
offset: number;
|
||||
// How many bytes to send; -1 sends everything from `offset`.
|
||||
length: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* What an upload resolves with. Any status is a result; `body` is the first
|
||||
* part of the response.
|
||||
*/
|
||||
export type UploadResult = {
|
||||
status: number;
|
||||
headers: Array<DownloadHeader>;
|
||||
body: string;
|
||||
};
|
||||
|
||||
export type UploadProgress = {
|
||||
id: number;
|
||||
sent: number;
|
||||
total: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* The file system module: one instance, on its own thread, so a slow file never
|
||||
* blocks JS or the UI.
|
||||
*
|
||||
* A method that returns a value runs on the JS thread; `void` and `Promise`
|
||||
* methods run on the module's thread. The writers below therefore return
|
||||
* `boolean` — a `void` method would be moved off the JS thread, which is wrong
|
||||
* for a `*Sync` call. They always return true and throw on failure.
|
||||
*/
|
||||
export interface Spec extends TurboModule {
|
||||
// Text is separate from bytes so a `utf8` read or write needs no buffer.
|
||||
readFile(path: string): Promise<ArrayBuffer>;
|
||||
readTextFile(path: string): Promise<string>;
|
||||
writeFile(
|
||||
path: string,
|
||||
data: ArrayBuffer,
|
||||
flags: number,
|
||||
mode: number,
|
||||
): Promise<void>;
|
||||
writeTextFile(
|
||||
path: string,
|
||||
data: string,
|
||||
flags: number,
|
||||
mode: number,
|
||||
): Promise<void>;
|
||||
|
||||
readdir(path: string, recursive: boolean): Promise<Array<DirEntry>>;
|
||||
// `followSymlinks` false is `lstat`.
|
||||
stat(path: string, followSymlinks: boolean): Promise<StatResult>;
|
||||
mkdir(path: string, recursive: boolean, mode: number): Promise<void>;
|
||||
rm(path: string, recursive: boolean, force: boolean): Promise<void>;
|
||||
rename(from: string, to: string): Promise<void>;
|
||||
copy(from: string, to: string, recursive: boolean): Promise<void>;
|
||||
access(path: string, mode: number): Promise<void>;
|
||||
realpath(path: string): Promise<string>;
|
||||
readlink(path: string): Promise<string>;
|
||||
// `type` is Node's, and only Windows acts on it: `'dir'` and `'junction'`
|
||||
// create a directory link, `null` decides from the resolved target, and a
|
||||
// plain `'file'` is a file link. Unix ignores it, because the syscall does.
|
||||
// Without it a Windows directory link was created as a file link and could
|
||||
// not be traversed.
|
||||
symlink(target: string, path: string, type: string | null): Promise<void>;
|
||||
truncate(path: string, length: number): Promise<void>;
|
||||
utimes(path: string, atimeMs: number, mtimeMs: number): Promise<void>;
|
||||
chmod(path: string, mode: number): Promise<void>;
|
||||
mkdtemp(prefix: string): Promise<string>;
|
||||
|
||||
// File handles. `position` -1 means "at the current offset".
|
||||
open(path: string, flags: number, mode: number): Promise<number>;
|
||||
read(fd: number, length: number, position: number): Promise<ArrayBuffer>;
|
||||
write(fd: number, data: ArrayBuffer, position: number): Promise<number>;
|
||||
fstat(fd: number): Promise<StatResult>;
|
||||
fsync(fd: number): Promise<void>;
|
||||
ftruncate(fd: number, length: number): Promise<void>;
|
||||
close(fd: number): Promise<void>;
|
||||
|
||||
showOpenDialog(options: OpenDialogOptions): Promise<Array<string>>;
|
||||
showSaveDialog(options: SaveDialogOptions): Promise<string | null>;
|
||||
reveal(path: string): Promise<void>;
|
||||
openExternal(path: string): Promise<void>;
|
||||
|
||||
// Returns the watcher id; events arrive through `onWatchEvent`.
|
||||
watch(path: string, recursive: boolean): Promise<number>;
|
||||
unwatch(id: number): void;
|
||||
|
||||
readFileSync(path: string): ArrayBuffer;
|
||||
readTextFileSync(path: string): string;
|
||||
writeFileSync(
|
||||
path: string,
|
||||
data: ArrayBuffer,
|
||||
flags: number,
|
||||
mode: number,
|
||||
): boolean;
|
||||
writeTextFileSync(
|
||||
path: string,
|
||||
data: string,
|
||||
flags: number,
|
||||
mode: number,
|
||||
): boolean;
|
||||
statSync(path: string, followSymlinks: boolean): StatResult | null;
|
||||
existsSync(path: string): boolean;
|
||||
readdirSync(path: string, recursive: boolean): Array<DirEntry>;
|
||||
mkdirSync(path: string, recursive: boolean, mode: number): boolean;
|
||||
rmSync(path: string, recursive: boolean, force: boolean): boolean;
|
||||
renameSync(from: string, to: string): boolean;
|
||||
copyFileSync(from: string, to: string): boolean;
|
||||
|
||||
download(
|
||||
url: string,
|
||||
path: string,
|
||||
options: DownloadOptions,
|
||||
): Promise<DownloadResult>;
|
||||
// Returns whether a download with that id was running.
|
||||
cancelDownload(id: number): boolean;
|
||||
|
||||
upload(
|
||||
url: string,
|
||||
path: string,
|
||||
options: UploadOptions,
|
||||
): Promise<UploadResult>;
|
||||
// Returns whether an upload with that id was running.
|
||||
cancelUpload(id: number): boolean;
|
||||
|
||||
getPaths(): KnownPaths;
|
||||
|
||||
readonly onWatchEvent: CodegenTypes.EventEmitter<WatchEvent>;
|
||||
readonly onDownloadProgress: CodegenTypes.EventEmitter<DownloadProgress>;
|
||||
readonly onUploadProgress: CodegenTypes.EventEmitter<UploadProgress>;
|
||||
}
|
||||
|
||||
export default TurboModuleRegistry.getEnforcing<Spec>('RNFsModule');
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* `fs.constants`, with Node's numbers.
|
||||
*
|
||||
* The `O_*` values are Node's table rather than the platform's `open(2)` flags,
|
||||
* because that is what the Rust side decodes: it translates them into
|
||||
* `OpenOptions` instead of passing them through. The numbers here must stay in
|
||||
* step with `modules/fs/desktop/src/module.rs`, which owns the other half of the
|
||||
* contract.
|
||||
*/
|
||||
|
||||
export const constants = {
|
||||
// `access` modes.
|
||||
F_OK: 0,
|
||||
X_OK: 1,
|
||||
W_OK: 2,
|
||||
R_OK: 4,
|
||||
|
||||
// `open` flags.
|
||||
O_RDONLY: 0,
|
||||
O_WRONLY: 1,
|
||||
O_RDWR: 2,
|
||||
O_CREAT: 64,
|
||||
O_EXCL: 128,
|
||||
O_TRUNC: 512,
|
||||
O_APPEND: 1024,
|
||||
|
||||
// `copyFile` modes.
|
||||
COPYFILE_EXCL: 1,
|
||||
|
||||
// File-type bits, for the predicates on `Stats` that cannot be answered from
|
||||
// the module's coarse `kind` alone (a socket and a block device are both
|
||||
// "other" there).
|
||||
S_IFMT: 0o170000,
|
||||
S_IFREG: 0o100000,
|
||||
S_IFDIR: 0o040000,
|
||||
S_IFLNK: 0o120000,
|
||||
S_IFBLK: 0o060000,
|
||||
S_IFCHR: 0o020000,
|
||||
S_IFIFO: 0o010000,
|
||||
S_IFSOCK: 0o140000,
|
||||
} as const;
|
||||
|
||||
export type Constants = typeof constants;
|
||||
-110
@@ -1,110 +0,0 @@
|
||||
/**
|
||||
* `download`: an HTTP response streamed into a file.
|
||||
*
|
||||
* The body goes to `<path>.part` as it arrives and is renamed onto `path` once it
|
||||
* is complete, so `path` never holds half a file. The partial file is removed when
|
||||
* the request fails or is aborted.
|
||||
*/
|
||||
|
||||
import NativeFs from './NativeFs';
|
||||
import { nodeError, toPath, withContext, type NodeError } from './shared';
|
||||
|
||||
export interface DownloadOptions {
|
||||
/** `GET` unless set. No request body is sent. */
|
||||
method?: string;
|
||||
headers?: Record<string, string>;
|
||||
/** Called while the body arrives; `total` is `-1` when the server did not say. */
|
||||
onProgress?: (received: number, total: number) => void;
|
||||
/** Aborting rejects with an `AbortError` and removes the partial file. */
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export interface DownloadResult {
|
||||
status: number;
|
||||
/** Response headers, lower-cased; repeated headers are joined with `, `. */
|
||||
headers: Record<string, string>;
|
||||
/** The size of the file that was written. */
|
||||
bytes: number;
|
||||
}
|
||||
|
||||
/** What a non-2xx response rejects with. */
|
||||
export interface DownloadHttpError extends NodeError {
|
||||
status: number;
|
||||
headers: Record<string, string>;
|
||||
/** The first part of the response body. */
|
||||
body: string;
|
||||
}
|
||||
|
||||
let nextId = 1;
|
||||
|
||||
function abortError(): NodeError {
|
||||
const error = nodeError('ABORT_ERR', 'The download was aborted');
|
||||
error.name = 'AbortError';
|
||||
return error;
|
||||
}
|
||||
|
||||
function toRecord(list: Array<{ name: string; value: string }>) {
|
||||
const record: Record<string, string> = {};
|
||||
for (const { name, value } of list) {
|
||||
record[name] = value;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
export async function download(
|
||||
url: string,
|
||||
path: string,
|
||||
options: DownloadOptions = {},
|
||||
): Promise<DownloadResult> {
|
||||
const { signal, onProgress } = options;
|
||||
if (signal?.aborted) {
|
||||
throw abortError();
|
||||
}
|
||||
const target = toPath(path);
|
||||
const id = nextId++;
|
||||
const subscription = onProgress
|
||||
? NativeFs.onDownloadProgress(event => {
|
||||
if (event.id === id) {
|
||||
onProgress(event.received, event.total);
|
||||
}
|
||||
})
|
||||
: null;
|
||||
const onAbort = () => {
|
||||
NativeFs.cancelDownload(id);
|
||||
};
|
||||
signal?.addEventListener('abort', onAbort);
|
||||
|
||||
try {
|
||||
const result = await NativeFs.download(url, target, {
|
||||
id,
|
||||
method: options.method ?? 'GET',
|
||||
headers: Object.entries(options.headers ?? {}).map(([name, value]) => ({
|
||||
name,
|
||||
value,
|
||||
})),
|
||||
});
|
||||
const headers = toRecord(result.headers);
|
||||
if (result.status < 200 || result.status >= 300) {
|
||||
const error = nodeError(
|
||||
'ERR_HTTP_STATUS',
|
||||
`HTTP ${result.status} downloading ${url}`,
|
||||
) as DownloadHttpError;
|
||||
error.status = result.status;
|
||||
error.headers = headers;
|
||||
error.body = result.body ?? '';
|
||||
throw error;
|
||||
}
|
||||
return { status: result.status, headers, bytes: result.bytes };
|
||||
} catch (error) {
|
||||
if (signal?.aborted) {
|
||||
throw abortError();
|
||||
}
|
||||
if ((error as NodeError)?.code === 'ABORT_ERR') {
|
||||
throw abortError();
|
||||
}
|
||||
throw withContext(error, 'download', target);
|
||||
} finally {
|
||||
signal?.removeEventListener('abort', onAbort);
|
||||
subscription?.remove();
|
||||
}
|
||||
}
|
||||
-788
@@ -1,788 +0,0 @@
|
||||
/**
|
||||
* `@react-native-gpui/fs`: a Node-shaped file API for the desktop app.
|
||||
*
|
||||
* The native module underneath is one TurboModule on its own thread with
|
||||
* `ArrayBuffer` arguments and plain-object results. Everything Node adds on top
|
||||
* of that — encodings, `Stats` and `Dirent` instances, callback forms, the
|
||||
* `fs/promises` object — lives in this package's JS layer, so the bridge stays a
|
||||
* byte mover and the API stays recognisably Node's.
|
||||
*
|
||||
* The default export is the `node:fs` object; `dialog`, `paths` and `watch` are
|
||||
* named exports because they have no `fs` counterpart to hang off.
|
||||
*/
|
||||
|
||||
import { constants } from './constants';
|
||||
import NativeFs from './NativeFs';
|
||||
import {
|
||||
Dirent,
|
||||
FileHandle,
|
||||
Stats,
|
||||
promises,
|
||||
readFileImpl,
|
||||
type MkdirOptions,
|
||||
type OpenOptions,
|
||||
type RmOptions,
|
||||
type WriteOptions,
|
||||
} from './promises';
|
||||
import {
|
||||
decodeString,
|
||||
flagFrom,
|
||||
missingPath,
|
||||
modeFrom,
|
||||
normalizeEncoding,
|
||||
relativeEntryName,
|
||||
toArrayBuffer,
|
||||
toPath,
|
||||
withContext,
|
||||
type BinaryData,
|
||||
type Encoding,
|
||||
type NodeError,
|
||||
} from './shared';
|
||||
import { download } from './download';
|
||||
import { upload } from './upload';
|
||||
import { watch } from './watch';
|
||||
|
||||
export type {
|
||||
CpOptions,
|
||||
MkdirOptions,
|
||||
OpenOptions,
|
||||
ReaddirOptions,
|
||||
RmOptions,
|
||||
TimeLike,
|
||||
WriteOptions,
|
||||
} from './promises';
|
||||
export type { BinaryData, Encoding, NodeError } from './shared';
|
||||
export type { UploadOptions, UploadResult } from './upload';
|
||||
export type {
|
||||
DownloadHttpError,
|
||||
DownloadOptions,
|
||||
DownloadResult,
|
||||
} from './download';
|
||||
export type {
|
||||
FSWatcher,
|
||||
WatchEventType,
|
||||
WatchListener,
|
||||
WatchOptions,
|
||||
} from './watch';
|
||||
export {
|
||||
Dirent,
|
||||
Stats,
|
||||
FileHandle,
|
||||
promises,
|
||||
constants,
|
||||
watch,
|
||||
download,
|
||||
upload,
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Dialogs */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
export interface DialogFilter {
|
||||
name: string;
|
||||
extensions: Array<string>;
|
||||
}
|
||||
|
||||
export interface OpenDialogOptions {
|
||||
title?: string;
|
||||
defaultPath?: string;
|
||||
multiple?: boolean;
|
||||
directory?: boolean;
|
||||
filters?: Array<DialogFilter>;
|
||||
}
|
||||
|
||||
export interface SaveDialogOptions {
|
||||
title?: string;
|
||||
defaultPath?: string;
|
||||
defaultName?: string;
|
||||
filters?: Array<DialogFilter>;
|
||||
}
|
||||
|
||||
/**
|
||||
* The native pickers.
|
||||
*
|
||||
* Cancelling is not an error: `open` resolves with `[]` and `save` with `null`,
|
||||
* which is what the design specifies and what libraries pairing the two expect.
|
||||
*/
|
||||
export const dialog = {
|
||||
async open(options: OpenDialogOptions = {}): Promise<string[]> {
|
||||
return NativeFs.showOpenDialog({
|
||||
title: options.title,
|
||||
defaultPath:
|
||||
options.defaultPath === undefined
|
||||
? undefined
|
||||
: toPath(options.defaultPath),
|
||||
multiple: options.multiple ?? false,
|
||||
directory: options.directory ?? false,
|
||||
filters: options.filters ?? [],
|
||||
});
|
||||
},
|
||||
|
||||
async save(options: SaveDialogOptions = {}): Promise<string | null> {
|
||||
return NativeFs.showSaveDialog({
|
||||
title: options.title,
|
||||
defaultPath:
|
||||
options.defaultPath === undefined
|
||||
? undefined
|
||||
: toPath(options.defaultPath),
|
||||
defaultName: options.defaultName,
|
||||
filters: options.filters ?? [],
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Known paths */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/** The well-known directories, as the native side reports them. */
|
||||
export interface KnownPaths {
|
||||
home: string;
|
||||
documents: string;
|
||||
downloads: string;
|
||||
desktop: string;
|
||||
temp: string;
|
||||
appData: string;
|
||||
appCache: string;
|
||||
appLogs: string;
|
||||
}
|
||||
|
||||
const KNOWN_PATH_NAMES = [
|
||||
'home',
|
||||
'documents',
|
||||
'downloads',
|
||||
'desktop',
|
||||
'temp',
|
||||
'appData',
|
||||
'appCache',
|
||||
'appLogs',
|
||||
] as const;
|
||||
|
||||
let cachedPaths: KnownPaths | null = null;
|
||||
|
||||
/**
|
||||
* Reads the known paths once and remembers them.
|
||||
*
|
||||
* Lazy rather than at import time so that importing this package cannot fail: the
|
||||
* native module is only asked when an app actually names a directory, and the
|
||||
* native side creates the per-app folders on that first read.
|
||||
*/
|
||||
function knownPaths(): KnownPaths {
|
||||
if (cachedPaths === null) {
|
||||
cachedPaths = NativeFs.getPaths();
|
||||
}
|
||||
return cachedPaths;
|
||||
}
|
||||
|
||||
/**
|
||||
* `paths.home`, `paths.documents` and the rest.
|
||||
*
|
||||
* Each property is a getter over one cached native read, so destructuring works
|
||||
* and the object can be exported before the module has been asked anything.
|
||||
*/
|
||||
export const paths: KnownPaths = Object.defineProperties(
|
||||
{},
|
||||
Object.fromEntries(
|
||||
KNOWN_PATH_NAMES.map(name => [
|
||||
name,
|
||||
{ enumerable: true, get: () => knownPaths()[name] },
|
||||
]),
|
||||
),
|
||||
) as KnownPaths;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Sync forms */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
function readFileSync(path: string): Uint8Array;
|
||||
function readFileSync(
|
||||
path: string,
|
||||
options: Encoding | { encoding: Encoding },
|
||||
): string;
|
||||
function readFileSync(
|
||||
path: string,
|
||||
options?: Encoding | { encoding?: Encoding | null } | null,
|
||||
): Uint8Array | string {
|
||||
const target = toPath(path);
|
||||
const encoding =
|
||||
typeof options === 'string' ? options : (options?.encoding ?? null);
|
||||
try {
|
||||
if (encoding === null) {
|
||||
return new Uint8Array(NativeFs.readFileSync(target));
|
||||
}
|
||||
const normalized = normalizeEncoding(encoding) ?? 'utf8';
|
||||
if (normalized === 'utf8') {
|
||||
// The dedicated text method decodes in Rust instead of handing JS bytes
|
||||
// to decode a second time.
|
||||
return NativeFs.readTextFileSync(target);
|
||||
}
|
||||
return decodeString(
|
||||
new Uint8Array(NativeFs.readFileSync(target)),
|
||||
normalized,
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'open', target);
|
||||
}
|
||||
}
|
||||
|
||||
function writeFileSync(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options?: WriteOptions | Encoding,
|
||||
): void {
|
||||
const target = toPath(path);
|
||||
const opts: WriteOptions =
|
||||
typeof options === 'string' ? { encoding: options } : (options ?? {});
|
||||
const flag = flagFrom(
|
||||
opts.flag,
|
||||
constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC,
|
||||
);
|
||||
const mode = modeFrom(opts.mode);
|
||||
try {
|
||||
if (typeof data === 'string') {
|
||||
const encoding = normalizeEncoding(opts.encoding ?? 'utf8') ?? 'utf8';
|
||||
if (encoding === 'utf8') {
|
||||
NativeFs.writeTextFileSync(target, data, flag, mode);
|
||||
return;
|
||||
}
|
||||
NativeFs.writeFileSync(target, toArrayBuffer(data, encoding), flag, mode);
|
||||
return;
|
||||
}
|
||||
NativeFs.writeFileSync(target, toArrayBuffer(data), flag, mode);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'open', target);
|
||||
}
|
||||
}
|
||||
|
||||
function appendFileSync(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options?: WriteOptions | Encoding,
|
||||
): void {
|
||||
const opts: WriteOptions =
|
||||
typeof options === 'string' ? { encoding: options } : (options ?? {});
|
||||
writeFileSync(path, data, {
|
||||
...opts,
|
||||
flag:
|
||||
opts.flag ?? constants.O_WRONLY | constants.O_CREAT | constants.O_APPEND,
|
||||
});
|
||||
}
|
||||
|
||||
function existsSync(path: string): boolean {
|
||||
try {
|
||||
return NativeFs.existsSync(toPath(path));
|
||||
} catch {
|
||||
// `existsSync` answers a question rather than reporting a failure, so every
|
||||
// error — a permission problem as much as a missing path — is a `false`.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function statSync(path: string): Stats {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
const result = NativeFs.statSync(target, true);
|
||||
if (result === null) {
|
||||
throw missingPath(target, 'stat');
|
||||
}
|
||||
return new Stats(result);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'stat', target);
|
||||
}
|
||||
}
|
||||
|
||||
function lstatSync(path: string): Stats {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
const result = NativeFs.statSync(target, false);
|
||||
if (result === null) {
|
||||
throw missingPath(target, 'lstat');
|
||||
}
|
||||
return new Stats(result);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'lstat', target);
|
||||
}
|
||||
}
|
||||
|
||||
function readdirSync(
|
||||
path: string,
|
||||
options?: { withFileTypes?: false; recursive?: boolean } | null,
|
||||
): string[];
|
||||
function readdirSync(
|
||||
path: string,
|
||||
options: { withFileTypes: true; recursive?: boolean },
|
||||
): Dirent[];
|
||||
function readdirSync(
|
||||
path: string,
|
||||
options?: { withFileTypes?: boolean; recursive?: boolean } | null,
|
||||
): string[] | Dirent[] {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
const recursive = options?.recursive ?? false;
|
||||
const entries = NativeFs.readdirSync(target, recursive);
|
||||
return options?.withFileTypes
|
||||
? entries.map(entry => new Dirent(entry))
|
||||
: entries.map(entry =>
|
||||
recursive
|
||||
? relativeEntryName(target, entry.parentPath, entry.name)
|
||||
: entry.name,
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'scandir', target);
|
||||
}
|
||||
}
|
||||
|
||||
function mkdirSync(path: string, options?: MkdirOptions | number): void {
|
||||
const target = toPath(path);
|
||||
const opts: MkdirOptions =
|
||||
typeof options === 'number' ? { mode: options } : (options ?? {});
|
||||
try {
|
||||
NativeFs.mkdirSync(
|
||||
target,
|
||||
opts.recursive ?? false,
|
||||
modeFrom(opts.mode, 0o777),
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'mkdir', target);
|
||||
}
|
||||
}
|
||||
|
||||
function rmSync(path: string, options?: RmOptions): void {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
NativeFs.rmSync(
|
||||
target,
|
||||
options?.recursive ?? false,
|
||||
options?.force ?? false,
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'rm', target);
|
||||
}
|
||||
}
|
||||
|
||||
function renameSync(from: string, to: string): void {
|
||||
const source = toPath(from);
|
||||
const destination = toPath(to);
|
||||
try {
|
||||
NativeFs.renameSync(source, destination);
|
||||
} catch (error) {
|
||||
const wrapped = withContext(error, 'rename', source);
|
||||
// Node's rename error names both ends; the second is `dest`, not `path`.
|
||||
if (wrapped.dest === undefined) {
|
||||
wrapped.dest = destination;
|
||||
}
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
function copyFileSync(from: string, to: string): void {
|
||||
const source = toPath(from);
|
||||
const destination = toPath(to);
|
||||
try {
|
||||
NativeFs.copyFileSync(source, destination);
|
||||
} catch (error) {
|
||||
const wrapped = withContext(error, 'copyfile', source);
|
||||
if (wrapped.dest === undefined) {
|
||||
wrapped.dest = destination;
|
||||
}
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Callback forms */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* The callback forms are wrappers over the promise implementations, as the
|
||||
* design says, so there is one implementation of each operation.
|
||||
*
|
||||
* The trailing argument is the callback in every form; the optional argument
|
||||
* before it is an options object (or an encoding, or a mode) when it is not a
|
||||
* function. That is the whole of Node's overloading convention here.
|
||||
*/
|
||||
|
||||
type NodeCallback<T> = (error: NodeError | null, value?: T) => void;
|
||||
|
||||
function callbackOf<T>(args: unknown[]): NodeCallback<T> {
|
||||
const callback = args[args.length - 1];
|
||||
if (typeof callback !== 'function') {
|
||||
throw new TypeError('A callback function is required');
|
||||
}
|
||||
return callback as NodeCallback<T>;
|
||||
}
|
||||
|
||||
/** The options argument, if the caller supplied one rather than the callback. */
|
||||
function before<T>(args: unknown[], index: number, fallback: T): T {
|
||||
const value = args[index];
|
||||
return value === undefined || typeof value === 'function'
|
||||
? fallback
|
||||
: (value as T);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a settled promise the Node way.
|
||||
*
|
||||
* A rejection that has a callback is delivered to it; nothing is thrown, because
|
||||
* a throw from here would escape the caller's stack rather than the callback.
|
||||
*/
|
||||
function settle<T>(promise: Promise<T>, callback: NodeCallback<T>): void {
|
||||
promise.then(
|
||||
value => callback(null, value),
|
||||
(error: NodeError) => callback(error),
|
||||
);
|
||||
}
|
||||
|
||||
function readFile(
|
||||
path: string,
|
||||
callback: NodeCallback<Uint8Array | string>,
|
||||
): void;
|
||||
function readFile(
|
||||
path: string,
|
||||
options: Encoding | { encoding?: Encoding | null } | null,
|
||||
callback: NodeCallback<Uint8Array | string>,
|
||||
): void;
|
||||
function readFile(path: string, ...args: unknown[]): void {
|
||||
// The promise overloads keep the result precise; the callback form goes
|
||||
// through the coarse alias because at that point the encoding is a runtime
|
||||
// value and either shape is possible.
|
||||
const options = before(args, 0, undefined) as
|
||||
Encoding | { encoding?: Encoding | null } | null | undefined;
|
||||
settle(readFileImpl(path, options), callbackOf(args));
|
||||
}
|
||||
|
||||
function writeFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function writeFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options: WriteOptions | Encoding | null,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function writeFile(path: string, data: BinaryData, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.writeFile(
|
||||
path,
|
||||
data,
|
||||
before(args, 0, undefined) as WriteOptions | Encoding | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function appendFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function appendFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options: WriteOptions | Encoding | null,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function appendFile(path: string, data: BinaryData, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.appendFile(
|
||||
path,
|
||||
data,
|
||||
before(args, 0, undefined) as WriteOptions | Encoding | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function readdir(
|
||||
path: string,
|
||||
callback: NodeCallback<string[] | Dirent[]>,
|
||||
): void;
|
||||
function readdir(
|
||||
path: string,
|
||||
options: { withFileTypes?: boolean; recursive?: boolean } | null,
|
||||
callback: NodeCallback<string[] | Dirent[]>,
|
||||
): void;
|
||||
function readdir(path: string, ...args: unknown[]): void {
|
||||
const options = before(args, 0, null) as {
|
||||
withFileTypes?: boolean;
|
||||
recursive?: boolean;
|
||||
} | null;
|
||||
const pending: Promise<string[] | Dirent[]> = options?.withFileTypes
|
||||
? promises.readdir(path, { ...options, withFileTypes: true })
|
||||
: promises.readdir(path, {
|
||||
recursive: options?.recursive,
|
||||
withFileTypes: false,
|
||||
});
|
||||
settle(pending, callbackOf(args));
|
||||
}
|
||||
|
||||
function stat(path: string, callback: NodeCallback<Stats>): void;
|
||||
function stat(path: string, ...args: unknown[]): void {
|
||||
settle(promises.stat(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function lstat(path: string, callback: NodeCallback<Stats>): void;
|
||||
function lstat(path: string, ...args: unknown[]): void {
|
||||
settle(promises.lstat(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function mkdir(path: string, callback: NodeCallback<string | undefined>): void;
|
||||
function mkdir(
|
||||
path: string,
|
||||
options: MkdirOptions | number | null,
|
||||
callback: NodeCallback<string | undefined>,
|
||||
): void;
|
||||
function mkdir(path: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.mkdir(
|
||||
path,
|
||||
before(args, 0, undefined) as MkdirOptions | number | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function rm(path: string, callback: NodeCallback<void>): void;
|
||||
function rm(
|
||||
path: string,
|
||||
options: RmOptions | null,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function rm(path: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.rm(path, before(args, 0, undefined) as RmOptions | undefined),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function rename(from: string, to: string, callback: NodeCallback<void>): void;
|
||||
function rename(from: string, to: string, ...args: unknown[]): void {
|
||||
settle(promises.rename(from, to), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function copyFile(from: string, to: string, callback: NodeCallback<void>): void;
|
||||
function copyFile(
|
||||
from: string,
|
||||
to: string,
|
||||
mode: number,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function copyFile(from: string, to: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.copyFile(
|
||||
from,
|
||||
to,
|
||||
before(args, 0, undefined) as number | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function cp(from: string, to: string, callback: NodeCallback<void>): void;
|
||||
function cp(
|
||||
from: string,
|
||||
to: string,
|
||||
options: { recursive?: boolean } | null,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function cp(from: string, to: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.cp(
|
||||
from,
|
||||
to,
|
||||
before(args, 0, undefined) as { recursive?: boolean } | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function access(path: string, callback: NodeCallback<void>): void;
|
||||
function access(path: string, mode: number, callback: NodeCallback<void>): void;
|
||||
function access(path: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.access(path, before(args, 0, undefined) as number | undefined),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function realpath(path: string, callback: NodeCallback<string>): void;
|
||||
function realpath(path: string, ...args: unknown[]): void {
|
||||
settle(promises.realpath(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function readlink(path: string, callback: NodeCallback<string>): void;
|
||||
function readlink(path: string, ...args: unknown[]): void {
|
||||
settle(promises.readlink(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function symlink(
|
||||
target: string,
|
||||
path: string,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function symlink(
|
||||
target: string,
|
||||
path: string,
|
||||
type: string,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function symlink(target: string, path: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.symlink(
|
||||
target,
|
||||
path,
|
||||
before(args, 0, undefined) as 'file' | 'dir' | 'junction' | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function truncate(path: string, callback: NodeCallback<void>): void;
|
||||
function truncate(
|
||||
path: string,
|
||||
length: number,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function truncate(path: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.truncate(path, before(args, 0, undefined) as number | undefined),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function utimes(
|
||||
path: string,
|
||||
atime: Date | number | string,
|
||||
mtime: Date | number | string,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function utimes(
|
||||
path: string,
|
||||
atime: Date | number | string,
|
||||
mtime: Date | number | string,
|
||||
...args: unknown[]
|
||||
): void {
|
||||
settle(promises.utimes(path, atime, mtime), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function chmod(
|
||||
path: string,
|
||||
mode: number | string,
|
||||
callback: NodeCallback<void>,
|
||||
): void;
|
||||
function chmod(path: string, mode: number | string, ...args: unknown[]): void {
|
||||
settle(promises.chmod(path, mode), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function mkdtemp(prefix: string, callback: NodeCallback<string>): void;
|
||||
function mkdtemp(
|
||||
prefix: string,
|
||||
options: Encoding | { encoding?: Encoding },
|
||||
callback: NodeCallback<string>,
|
||||
): void;
|
||||
function mkdtemp(prefix: string, ...args: unknown[]): void {
|
||||
settle(
|
||||
promises.mkdtemp(
|
||||
prefix,
|
||||
before(args, 0, undefined) as
|
||||
Encoding | { encoding?: Encoding } | undefined,
|
||||
),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function open(path: string, callback: NodeCallback<FileHandle>): void;
|
||||
function open(
|
||||
path: string,
|
||||
flags: string | number | OpenOptions,
|
||||
callback: NodeCallback<FileHandle>,
|
||||
): void;
|
||||
function open(
|
||||
path: string,
|
||||
flags: string | number | OpenOptions,
|
||||
mode: number | string,
|
||||
callback: NodeCallback<FileHandle>,
|
||||
): void;
|
||||
function open(path: string, ...args: unknown[]): void {
|
||||
const flags = before(args, 0, undefined) as
|
||||
string | number | OpenOptions | undefined;
|
||||
const mode = before(args, 1, undefined) as number | string | undefined;
|
||||
settle(
|
||||
mode === undefined
|
||||
? promises.open(path, flags)
|
||||
: promises.open(path, flags, mode),
|
||||
callbackOf(args),
|
||||
);
|
||||
}
|
||||
|
||||
function reveal(path: string, callback: NodeCallback<void>): void;
|
||||
function reveal(path: string, ...args: unknown[]): void {
|
||||
settle(promises.reveal(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
function openExternal(path: string, callback: NodeCallback<void>): void;
|
||||
function openExternal(path: string, ...args: unknown[]): void {
|
||||
settle(promises.openExternal(path), callbackOf([...args]));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* The `fs` object */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Node's `fs`, as far as this platform implements it.
|
||||
*
|
||||
* The callback forms wrap the promise implementations; the sync forms call the
|
||||
* bridge's JS-thread methods directly; `promises` is the same object shape Node
|
||||
* exposes as `fs.promises`.
|
||||
*/
|
||||
const fs = {
|
||||
constants,
|
||||
promises,
|
||||
|
||||
readFile,
|
||||
writeFile,
|
||||
appendFile,
|
||||
readdir,
|
||||
stat,
|
||||
lstat,
|
||||
mkdir,
|
||||
rm,
|
||||
rename,
|
||||
copyFile,
|
||||
cp,
|
||||
access,
|
||||
realpath,
|
||||
readlink,
|
||||
symlink,
|
||||
truncate,
|
||||
utimes,
|
||||
chmod,
|
||||
mkdtemp,
|
||||
open,
|
||||
reveal,
|
||||
openExternal,
|
||||
download,
|
||||
upload,
|
||||
|
||||
readFileSync,
|
||||
writeFileSync,
|
||||
appendFileSync,
|
||||
existsSync,
|
||||
statSync,
|
||||
lstatSync,
|
||||
readdirSync,
|
||||
mkdirSync,
|
||||
rmSync,
|
||||
renameSync,
|
||||
copyFileSync,
|
||||
|
||||
Stats,
|
||||
Dirent,
|
||||
FileHandle,
|
||||
};
|
||||
|
||||
export default fs;
|
||||
-771
@@ -1,771 +0,0 @@
|
||||
/**
|
||||
* `fs.promises`: the Node-shaped asynchronous face of the module.
|
||||
*
|
||||
* Every call here is a thin translation. The native side does the work on its own
|
||||
* thread and reports Node codes, so this layer's job is argument shapes, the
|
||||
* encodings Node implements above the bytes, and the fields (`syscall`, `path`,
|
||||
* `dest`) that only a wrapper can know.
|
||||
*/
|
||||
|
||||
import NativeFs from './NativeFs';
|
||||
import { constants } from './constants';
|
||||
import {
|
||||
checkBufferRange,
|
||||
decodeString,
|
||||
encodeString,
|
||||
flagFrom,
|
||||
modeFrom,
|
||||
nodeError,
|
||||
normalizeEncoding,
|
||||
relativeEntryName,
|
||||
toArrayBuffer,
|
||||
toBytes,
|
||||
toPath,
|
||||
withContext,
|
||||
type BinaryData,
|
||||
type Encoding,
|
||||
type NormalizedEncoding,
|
||||
} from './shared';
|
||||
import { Dirent, Stats, type DirEntryResult, type StatResult } from './stats';
|
||||
|
||||
/** An encoding, an options object, or nothing, as Node accepts for reads. */
|
||||
export type ReadOptions =
|
||||
Encoding | { encoding?: Encoding | null } | null | undefined;
|
||||
|
||||
export interface WriteOptions {
|
||||
encoding?: Encoding | null;
|
||||
mode?: number | string;
|
||||
flag?: string | number;
|
||||
}
|
||||
|
||||
export interface MkdirOptions {
|
||||
recursive?: boolean;
|
||||
mode?: number | string;
|
||||
}
|
||||
|
||||
export interface RmOptions {
|
||||
recursive?: boolean;
|
||||
force?: boolean;
|
||||
}
|
||||
|
||||
export interface ReaddirOptions {
|
||||
withFileTypes?: boolean;
|
||||
recursive?: boolean;
|
||||
}
|
||||
|
||||
export interface CpOptions {
|
||||
recursive?: boolean;
|
||||
}
|
||||
|
||||
export interface OpenOptions {
|
||||
flags?: string | number;
|
||||
mode?: number | string;
|
||||
}
|
||||
|
||||
/** The chunk size for a read that has no caller-supplied buffer. */
|
||||
const READ_CHUNK = 64 * 1024;
|
||||
|
||||
/** A time as Node accepts one: `Date`, milliseconds, or a date string. */
|
||||
export type TimeLike = Date | number | string;
|
||||
|
||||
function toMillis(value: TimeLike, name: string): number {
|
||||
if (value instanceof Date) {
|
||||
return value.getTime();
|
||||
}
|
||||
if (typeof value === 'number') {
|
||||
return value;
|
||||
}
|
||||
const parsed = Date.parse(value);
|
||||
if (Number.isNaN(parsed)) {
|
||||
throw nodeError(
|
||||
'ERR_INVALID_ARG_VALUE',
|
||||
`The argument '${name}' is invalid time. Received '${value}'`,
|
||||
);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function encodingOf(options: ReadOptions): NormalizedEncoding | null {
|
||||
if (typeof options === 'string') {
|
||||
return normalizeEncoding(options);
|
||||
}
|
||||
return normalizeEncoding(options?.encoding ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* `readFile`, in whichever of the two shapes the caller asked for.
|
||||
*
|
||||
* UTF-8 goes through the dedicated text method: the native side decodes it while
|
||||
* the bytes are still in Rust, so a text read never allocates an `ArrayBuffer`
|
||||
* that JS then copies. Every other encoding is decoded here, because Node's
|
||||
* `utf16le`, `latin1`, `base64` and `hex` are JS-level conventions rather than
|
||||
* something `std::fs` has an opinion about.
|
||||
*/
|
||||
function readFile(path: string): Promise<Uint8Array>;
|
||||
function readFile(
|
||||
path: string,
|
||||
options: { encoding?: null } | null | undefined,
|
||||
): Promise<Uint8Array>;
|
||||
function readFile(
|
||||
path: string,
|
||||
options: Encoding | { encoding: Encoding },
|
||||
): Promise<string>;
|
||||
async function readFile(
|
||||
path: string,
|
||||
options?: ReadOptions,
|
||||
): Promise<Uint8Array | string> {
|
||||
const target = toPath(path);
|
||||
const encoding = encodingOf(options);
|
||||
try {
|
||||
if (encoding === 'utf8') {
|
||||
return await NativeFs.readTextFile(target);
|
||||
}
|
||||
const buffer = await NativeFs.readFile(target);
|
||||
const bytes = new Uint8Array(buffer);
|
||||
return encoding === null ? bytes : decodeString(bytes, encoding);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'open', target);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The same read, typed coarsely.
|
||||
*
|
||||
* `readFile`'s public overloads are what a caller sees; the callback layer calls
|
||||
* through this alias because at that point the encoding is a runtime value and
|
||||
* the result is genuinely either shape.
|
||||
*/
|
||||
export const readFileImpl: (
|
||||
path: string,
|
||||
options?: ReadOptions,
|
||||
) => Promise<Uint8Array | string> = readFile;
|
||||
|
||||
/**
|
||||
* `writeFile`.
|
||||
*
|
||||
* A string is written through `writeTextFile` when it is UTF-8; anything else —
|
||||
* a different encoding, or binary data — is normalised to an `ArrayBuffer` and
|
||||
* sent through the byte method.
|
||||
*/
|
||||
async function writeFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options?: WriteOptions | Encoding,
|
||||
): Promise<void> {
|
||||
const target = toPath(path);
|
||||
const opts: WriteOptions =
|
||||
typeof options === 'string' ? { encoding: options } : (options ?? {});
|
||||
const flag = flagFrom(
|
||||
opts.flag,
|
||||
constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC,
|
||||
);
|
||||
const mode = modeFrom(opts.mode);
|
||||
try {
|
||||
if (typeof data === 'string') {
|
||||
// `normalizeEncoding` answers `null` only when it is given nothing, and a
|
||||
// default is supplied here, so the fallback to `utf8` is unreachable — it
|
||||
// exists to keep the type honest instead of asserting it away.
|
||||
const encoding = normalizeEncoding(opts.encoding ?? 'utf8') ?? 'utf8';
|
||||
if (encoding === 'utf8') {
|
||||
await NativeFs.writeTextFile(target, data, flag, mode);
|
||||
} else {
|
||||
await NativeFs.writeFile(
|
||||
target,
|
||||
toArrayBuffer(encodeString(data, encoding)),
|
||||
flag,
|
||||
mode,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
await NativeFs.writeFile(target, toArrayBuffer(data), flag, mode);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'open', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function appendFile(
|
||||
path: string,
|
||||
data: BinaryData,
|
||||
options?: WriteOptions | Encoding,
|
||||
): Promise<void> {
|
||||
const opts: WriteOptions =
|
||||
typeof options === 'string' ? { encoding: options } : (options ?? {});
|
||||
return writeFile(path, data, {
|
||||
...opts,
|
||||
flag:
|
||||
opts.flag ?? constants.O_WRONLY | constants.O_CREAT | constants.O_APPEND,
|
||||
});
|
||||
}
|
||||
|
||||
function readdir(
|
||||
path: string,
|
||||
options?: { withFileTypes?: false; recursive?: boolean } | null,
|
||||
): Promise<string[]>;
|
||||
function readdir(
|
||||
path: string,
|
||||
options: { withFileTypes: true; recursive?: boolean },
|
||||
): Promise<Dirent[]>;
|
||||
async function readdir(
|
||||
path: string,
|
||||
options?: ReaddirOptions | null,
|
||||
): Promise<string[] | Dirent[]> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
const recursive = options?.recursive ?? false;
|
||||
const entries = await NativeFs.readdir(target, recursive);
|
||||
return options?.withFileTypes
|
||||
? entries.map(entry => new Dirent(entry as DirEntryResult))
|
||||
: entries.map(entry =>
|
||||
recursive
|
||||
? relativeEntryName(target, entry.parentPath, entry.name)
|
||||
: entry.name,
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'scandir', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function stat(path: string): Promise<Stats> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
return new Stats((await NativeFs.stat(target, true)) as StatResult);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'stat', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function lstat(path: string): Promise<Stats> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
return new Stats((await NativeFs.stat(target, false)) as StatResult);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'lstat', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function mkdir(
|
||||
path: string,
|
||||
options?: MkdirOptions | number,
|
||||
): Promise<string | undefined> {
|
||||
const target = toPath(path);
|
||||
const opts: MkdirOptions =
|
||||
typeof options === 'number' ? { mode: options } : (options ?? {});
|
||||
try {
|
||||
await NativeFs.mkdir(
|
||||
target,
|
||||
opts.recursive ?? false,
|
||||
modeFrom(opts.mode, 0o777),
|
||||
);
|
||||
return undefined;
|
||||
} catch (error) {
|
||||
throw withContext(error, 'mkdir', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function rm(path: string, options?: RmOptions): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.rm(
|
||||
target,
|
||||
options?.recursive ?? false,
|
||||
options?.force ?? false,
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'rm', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function rename(from: string, to: string): Promise<void> {
|
||||
const source = toPath(from);
|
||||
const destination = toPath(to);
|
||||
try {
|
||||
await NativeFs.rename(source, destination);
|
||||
} catch (error) {
|
||||
// Node's rename error names both ends; the second is `dest`, not `path`.
|
||||
const wrapped = withContext(error, 'rename', source);
|
||||
if (wrapped.dest === undefined) {
|
||||
wrapped.dest = destination;
|
||||
}
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
async function copyFile(
|
||||
from: string,
|
||||
to: string,
|
||||
mode?: number,
|
||||
): Promise<void> {
|
||||
const source = toPath(from);
|
||||
const destination = toPath(to);
|
||||
if (mode !== undefined && (mode & constants.COPYFILE_EXCL) !== 0) {
|
||||
// The native copy has no exclusive flag, so the check is made here. It is
|
||||
// advisory — a file created between the two calls still wins — but it gives
|
||||
// the caller the error Node promises for the ordinary case.
|
||||
try {
|
||||
await NativeFs.stat(destination, false);
|
||||
throw nodeError(
|
||||
'EEXIST',
|
||||
`EEXIST: file already exists, copyfile '${source}' -> '${destination}'`,
|
||||
);
|
||||
} catch (error) {
|
||||
if ((error as { code?: string }).code !== 'ENOENT') {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {
|
||||
await NativeFs.copy(source, destination, false);
|
||||
} catch (error) {
|
||||
const wrapped = withContext(error, 'copyfile', source);
|
||||
if (wrapped.dest === undefined) {
|
||||
wrapped.dest = destination;
|
||||
}
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
async function cp(
|
||||
from: string,
|
||||
to: string,
|
||||
options?: CpOptions,
|
||||
): Promise<void> {
|
||||
const source = toPath(from);
|
||||
const destination = toPath(to);
|
||||
try {
|
||||
await NativeFs.copy(source, destination, options?.recursive ?? false);
|
||||
} catch (error) {
|
||||
const wrapped = withContext(error, 'cp', source);
|
||||
if (wrapped.dest === undefined) {
|
||||
wrapped.dest = destination;
|
||||
}
|
||||
throw wrapped;
|
||||
}
|
||||
}
|
||||
|
||||
async function access(path: string, mode?: number): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.access(target, mode ?? constants.F_OK);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'access', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function realpath(path: string): Promise<string> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
return await NativeFs.realpath(target);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'realpath', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function readlink(path: string): Promise<string> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
return await NativeFs.readlink(target);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'readlink', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function symlink(
|
||||
target: string,
|
||||
path: string,
|
||||
type?: 'file' | 'dir' | 'junction',
|
||||
): Promise<void> {
|
||||
// `type` is passed through rather than dropped. It is the only way to ask for a
|
||||
// directory link on Windows, where creating one as a file link leaves a path
|
||||
// that cannot be traversed; Unix ignores it, as the syscall does.
|
||||
const link = toPath(path);
|
||||
try {
|
||||
await NativeFs.symlink(toPath(target), link, type ?? null);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'symlink', link);
|
||||
}
|
||||
}
|
||||
|
||||
async function truncate(path: string, length?: number): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.truncate(target, length ?? 0);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'truncate', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function utimes(
|
||||
path: string,
|
||||
atime: TimeLike,
|
||||
mtime: TimeLike,
|
||||
): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.utimes(
|
||||
target,
|
||||
toMillis(atime, 'atime'),
|
||||
toMillis(mtime, 'mtime'),
|
||||
);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'utime', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function chmod(path: string, mode: number | string): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.chmod(target, modeFrom(mode));
|
||||
} catch (error) {
|
||||
throw withContext(error, 'chmod', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function mkdtemp(
|
||||
prefix: string,
|
||||
options?: Encoding | { encoding?: Encoding },
|
||||
): Promise<string> {
|
||||
// The native side creates the directory under the system temp directory and
|
||||
// returns the path; the encoding only affects how a non-ASCII prefix arrives,
|
||||
// and every string crosses the bridge as UTF-8.
|
||||
options;
|
||||
try {
|
||||
return await NativeFs.mkdtemp(toPath(prefix));
|
||||
} catch (error) {
|
||||
throw withContext(error, 'mkdtemp', prefix);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A descriptor plus the operations Node puts on `FileHandle`.
|
||||
*
|
||||
* Positioned reads and writes do not touch the file's offset, which is what
|
||||
* makes concurrent chunked I/O safe: two readers on one handle cannot move each
|
||||
* other's cursor.
|
||||
*/
|
||||
export class FileHandle {
|
||||
/** The native descriptor. */
|
||||
readonly fd: number;
|
||||
private closed = false;
|
||||
|
||||
constructor(fd: number) {
|
||||
this.fd = fd;
|
||||
}
|
||||
|
||||
private assertOpen(): void {
|
||||
if (this.closed) {
|
||||
throw nodeError('EBADF', `EBADF: bad file descriptor, ${this.fd}`);
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads from the current offset when `position` is `-1`. */
|
||||
async read(): Promise<{ bytesRead: number; buffer: Uint8Array }>;
|
||||
async read(options: {
|
||||
buffer?: Uint8Array;
|
||||
offset?: number;
|
||||
length?: number;
|
||||
position?: number | null;
|
||||
}): Promise<{ bytesRead: number; buffer: Uint8Array }>;
|
||||
async read(
|
||||
buffer: Uint8Array,
|
||||
offset?: number,
|
||||
length?: number,
|
||||
position?: number | null,
|
||||
): Promise<{ bytesRead: number; buffer: Uint8Array }>;
|
||||
/**
|
||||
* Node's positional form with the buffer implied: `read(size, offset, length,
|
||||
* position)`. `offset` and `length` are a window into the freshly allocated
|
||||
* buffer, and a missing `position` reads from the cursor.
|
||||
*/
|
||||
async read(
|
||||
size: number,
|
||||
offset?: number,
|
||||
length?: number,
|
||||
position?: number | null,
|
||||
): Promise<{ bytesRead: number; buffer: Uint8Array }>;
|
||||
async read(
|
||||
first?:
|
||||
| number
|
||||
| Uint8Array
|
||||
| {
|
||||
buffer?: Uint8Array;
|
||||
offset?: number;
|
||||
length?: number;
|
||||
position?: number | null;
|
||||
},
|
||||
second?: number,
|
||||
third?: number,
|
||||
fourth?: number | null,
|
||||
): Promise<{ bytesRead: number; buffer: Uint8Array }> {
|
||||
this.assertOpen();
|
||||
let destination: Uint8Array;
|
||||
let offset = 0;
|
||||
let length: number;
|
||||
let position: number;
|
||||
// The two no-buffer forms mean "read to the end", which is a different
|
||||
// operation from a single bounded read and is handled by `readToEnd`.
|
||||
let toEnd = false;
|
||||
if (first === undefined) {
|
||||
toEnd = true;
|
||||
destination = new Uint8Array(0);
|
||||
length = 0;
|
||||
position = -1;
|
||||
} else if (typeof first === 'number') {
|
||||
// A size instead of a buffer: allocate it and apply the same window
|
||||
// arguments Node's buffer form takes.
|
||||
destination = new Uint8Array(first);
|
||||
offset = second ?? 0;
|
||||
length = third ?? Math.max(destination.byteLength - offset, 0);
|
||||
position = fourth ?? -1;
|
||||
} else if (first instanceof Uint8Array) {
|
||||
destination = first;
|
||||
offset = second ?? 0;
|
||||
length = third ?? destination.byteLength - offset;
|
||||
position = fourth ?? -1;
|
||||
} else {
|
||||
destination = first.buffer ?? new Uint8Array(0);
|
||||
offset = first.offset ?? 0;
|
||||
length = first.length ?? Math.max(destination.byteLength - offset, 0);
|
||||
position = first.position ?? -1;
|
||||
toEnd = first.buffer === undefined;
|
||||
}
|
||||
|
||||
try {
|
||||
if (toEnd) {
|
||||
const bytes = await this.readToEnd(position);
|
||||
return { bytesRead: bytes.byteLength, buffer: bytes };
|
||||
}
|
||||
// Validated before the native call, not after. The native side reads
|
||||
// `length` bytes and advances the cursor, so a window that overruns the
|
||||
// buffer used to consume bytes and then drop them when the result was
|
||||
// trimmed to fit. Node rejects the call instead, and so must this.
|
||||
const window = checkBufferRange(destination, offset, length);
|
||||
const buffer = await NativeFs.read(this.fd, window.length, position);
|
||||
const bytes = new Uint8Array(buffer);
|
||||
const bytesRead = Math.min(bytes.byteLength, window.length);
|
||||
destination.set(bytes.subarray(0, bytesRead), window.offset);
|
||||
return { bytesRead, buffer: destination };
|
||||
} catch (error) {
|
||||
throw withContext(error, 'read', undefined);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads until a short read, which is end of file.
|
||||
*
|
||||
* The native `read` returns at most what it was asked for, so "the rest of the
|
||||
* file" has to be assembled: a single call with a huge length would ask Rust to
|
||||
* allocate that much up front. A cursor read (`position` -1) advances the
|
||||
* handle's offset, so the loop continues where the previous chunk ended.
|
||||
*/
|
||||
private async readToEnd(position: number): Promise<Uint8Array> {
|
||||
const chunks: Uint8Array[] = [];
|
||||
let total = 0;
|
||||
let at = position;
|
||||
for (;;) {
|
||||
const buffer = await NativeFs.read(this.fd, READ_CHUNK, at);
|
||||
const chunk = new Uint8Array(buffer);
|
||||
if (chunk.byteLength === 0) {
|
||||
break;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
total += chunk.byteLength;
|
||||
if (chunk.byteLength < READ_CHUNK) {
|
||||
break;
|
||||
}
|
||||
if (at >= 0) {
|
||||
at += chunk.byteLength;
|
||||
}
|
||||
}
|
||||
const all = new Uint8Array(total);
|
||||
let cursor = 0;
|
||||
for (const chunk of chunks) {
|
||||
all.set(chunk, cursor);
|
||||
cursor += chunk.byteLength;
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
async write(
|
||||
data: BinaryData,
|
||||
position?: number | null,
|
||||
encoding?: Encoding,
|
||||
): Promise<{ bytesWritten: number; buffer: Uint8Array }>;
|
||||
async write(
|
||||
buffer: Uint8Array,
|
||||
offset: number,
|
||||
length: number,
|
||||
position?: number | null,
|
||||
): Promise<{ bytesWritten: number; buffer: Uint8Array }>;
|
||||
async write(
|
||||
data: BinaryData,
|
||||
second?: number | null,
|
||||
third?: number | Encoding,
|
||||
fourth?: number | null,
|
||||
): Promise<{ bytesWritten: number; buffer: Uint8Array }> {
|
||||
this.assertOpen();
|
||||
let bytes: Uint8Array;
|
||||
let position: number | null = null;
|
||||
if (typeof data === 'string') {
|
||||
// `write(string, position, encoding)`: here the number is a file position.
|
||||
const encodingName =
|
||||
typeof third === 'string' ? normalizeEncoding(third) : 'utf8';
|
||||
bytes = encodeString(data, encodingName ?? 'utf8');
|
||||
position = second ?? null;
|
||||
} else if (
|
||||
data instanceof Uint8Array &&
|
||||
third !== undefined &&
|
||||
typeof third !== 'number'
|
||||
) {
|
||||
// `write(buffer, { offset, length, position })`, which Node also accepts.
|
||||
const options = third as unknown as {
|
||||
offset?: number;
|
||||
length?: number;
|
||||
position?: number | null;
|
||||
};
|
||||
const window = checkBufferRange(data, options.offset, options.length);
|
||||
bytes = data.subarray(window.offset, window.offset + window.length);
|
||||
position = options.position ?? null;
|
||||
} else if (
|
||||
data instanceof Uint8Array &&
|
||||
second !== undefined &&
|
||||
second !== null
|
||||
) {
|
||||
// `write(buffer, offset[, length[, position]])` — the numeric argument is
|
||||
// into the *buffer*, not into the file. Reading it as a file position
|
||||
// wrote the whole buffer at that offset instead of a slice at the cursor.
|
||||
const window = checkBufferRange(
|
||||
data,
|
||||
second,
|
||||
typeof third === 'number' ? third : undefined,
|
||||
);
|
||||
bytes = data.subarray(window.offset, window.offset + window.length);
|
||||
position = fourth ?? null;
|
||||
} else {
|
||||
bytes = toBytes(data);
|
||||
position = typeof second === 'number' ? second : null;
|
||||
}
|
||||
try {
|
||||
const bytesWritten = await NativeFs.write(
|
||||
this.fd,
|
||||
toArrayBuffer(bytes),
|
||||
position ?? -1,
|
||||
);
|
||||
return { bytesWritten, buffer: bytes };
|
||||
} catch (error) {
|
||||
throw withContext(error, 'write', undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async stat(): Promise<Stats> {
|
||||
this.assertOpen();
|
||||
try {
|
||||
return new Stats((await NativeFs.fstat(this.fd)) as StatResult);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'fstat', undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async sync(): Promise<void> {
|
||||
this.assertOpen();
|
||||
try {
|
||||
await NativeFs.fsync(this.fd);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'fsync', undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async truncate(length?: number): Promise<void> {
|
||||
this.assertOpen();
|
||||
try {
|
||||
await NativeFs.ftruncate(this.fd, length ?? 0);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'ftruncate', undefined);
|
||||
}
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.closed) {
|
||||
return;
|
||||
}
|
||||
this.closed = true;
|
||||
try {
|
||||
await NativeFs.close(this.fd);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'close', undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function open(
|
||||
path: string,
|
||||
flags?: string | number | OpenOptions,
|
||||
mode?: number | string,
|
||||
): Promise<FileHandle> {
|
||||
const target = toPath(path);
|
||||
const opts: OpenOptions =
|
||||
typeof flags === 'object' && flags !== null ? flags : { flags, mode };
|
||||
const flag = flagFrom(opts.flags, constants.O_RDONLY);
|
||||
try {
|
||||
const fd = await NativeFs.open(target, flag, modeFrom(opts.mode));
|
||||
return new FileHandle(fd);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'open', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function reveal(path: string): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.reveal(target);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'reveal', target);
|
||||
}
|
||||
}
|
||||
|
||||
async function openExternal(path: string): Promise<void> {
|
||||
const target = toPath(path);
|
||||
try {
|
||||
await NativeFs.openExternal(target);
|
||||
} catch (error) {
|
||||
throw withContext(error, 'openExternal', target);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The `fs/promises` object.
|
||||
*
|
||||
* The functions are declared separately and collected here so the callback layer
|
||||
* in `index.ts` can wrap the same implementations rather than reimplementing
|
||||
* them.
|
||||
*/
|
||||
export const promises = {
|
||||
readFile,
|
||||
writeFile,
|
||||
appendFile,
|
||||
readdir,
|
||||
stat,
|
||||
lstat,
|
||||
mkdir,
|
||||
rm,
|
||||
rename,
|
||||
copyFile,
|
||||
cp,
|
||||
access,
|
||||
realpath,
|
||||
readlink,
|
||||
symlink,
|
||||
truncate,
|
||||
utimes,
|
||||
chmod,
|
||||
mkdtemp,
|
||||
open,
|
||||
reveal,
|
||||
openExternal,
|
||||
};
|
||||
|
||||
export type Promises = typeof promises;
|
||||
|
||||
/** Re-exported so `fs.stat` and `fs.promises.stat` share one class identity. */
|
||||
export { Dirent, Stats };
|
||||
export type { StatResult };
|
||||
-539
@@ -1,539 +0,0 @@
|
||||
/**
|
||||
* The pieces every face of the API needs: Node error objects, `file://` path
|
||||
* conversion, the encodings Node supports, and the flag/mode translation.
|
||||
*
|
||||
* Nothing here touches the native module, so it stays usable from the sync,
|
||||
* callback and promise layers without a cycle.
|
||||
*/
|
||||
|
||||
/** Bytes in any of the shapes a caller may hand a writer. */
|
||||
export type BinaryData = string | ArrayBuffer | ArrayBufferView;
|
||||
|
||||
/** The encodings this module implements, after aliasing. */
|
||||
export type NormalizedEncoding =
|
||||
'utf8' | 'utf16le' | 'latin1' | 'base64' | 'hex';
|
||||
|
||||
/** The encodings accepted in an options object, with Node's aliases. */
|
||||
export type Encoding =
|
||||
| 'utf8'
|
||||
| 'utf-8'
|
||||
| 'utf16le'
|
||||
| 'utf-16le'
|
||||
| 'ucs2'
|
||||
| 'ucs-2'
|
||||
| 'latin1'
|
||||
| 'binary'
|
||||
| 'ascii'
|
||||
| 'base64'
|
||||
| 'hex';
|
||||
|
||||
/** What a rejected call looks like: Node's `ErrnoException`. */
|
||||
export interface NodeError extends Error {
|
||||
code?: string;
|
||||
errno?: number;
|
||||
syscall?: string;
|
||||
path?: string;
|
||||
/** The second path of a two-path call, as Node names it on `rename` and `cp`. */
|
||||
dest?: string;
|
||||
}
|
||||
|
||||
/** Builds an error with a code but no OS call behind it. */
|
||||
export function nodeError(code: string, message: string): NodeError {
|
||||
const error = new Error(message) as NodeError;
|
||||
error.code = code;
|
||||
return error;
|
||||
}
|
||||
|
||||
/** The error Node raises when a path is not there, in its message format. */
|
||||
export function missingPath(path: string, syscall: string): NodeError {
|
||||
return nodeError(
|
||||
'ENOENT',
|
||||
`ENOENT: no such file or directory, ${syscall} '${path}'`,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the fields only the JS side knows to an error a native call rejected
|
||||
* with.
|
||||
*
|
||||
* The Rust layer reports `code` and a Node-format message, but it deliberately
|
||||
* does not guess `syscall` — one native `stat` serves `stat` and `lstat`, and it
|
||||
* cannot know the spelling of the path the caller passed.
|
||||
*/
|
||||
export function withContext(
|
||||
error: unknown,
|
||||
syscall: string,
|
||||
path?: string,
|
||||
): NodeError {
|
||||
const wrapped: NodeError =
|
||||
error instanceof Error
|
||||
? (error as NodeError)
|
||||
: nodeError('EIO', String(error));
|
||||
if (wrapped.syscall === undefined) {
|
||||
wrapped.syscall = syscall;
|
||||
}
|
||||
if (path !== undefined && wrapped.path === undefined) {
|
||||
wrapped.path = path;
|
||||
}
|
||||
return wrapped;
|
||||
}
|
||||
|
||||
/**
|
||||
* `file://` URLs to paths, leaving plain paths alone.
|
||||
*
|
||||
* Node accepts `URL` objects and `file:` strings interchangeably; this API takes
|
||||
* strings, so the conversion is textual. Percent escapes are decoded because a
|
||||
* path with a space arrives as `file:///a%20b`.
|
||||
*/
|
||||
export function toPath(path: string): string {
|
||||
if (!path.startsWith('file://')) {
|
||||
return path;
|
||||
}
|
||||
let rest = path.slice('file://'.length);
|
||||
// An empty or `localhost` authority is the local machine.
|
||||
if (rest.startsWith('localhost/')) {
|
||||
rest = rest.slice('localhost'.length);
|
||||
}
|
||||
const decoded = decodeURIComponent(rest);
|
||||
// `file:///C:/x` is a drive path on Windows, not a path under `/`.
|
||||
if (/^\/[A-Za-z]:[\\/]/.test(decoded)) {
|
||||
return decoded.slice(1);
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Node's bounds check for a buffer window, as `RangeError` with
|
||||
* `code = 'ERR_OUT_OF_RANGE'`.
|
||||
*
|
||||
* Both `read` and `write` take a window into a caller's buffer, and the native
|
||||
* side reads or writes `length` bytes before the result is trimmed to fit — so an
|
||||
* oversized `length` would consume bytes from the file and silently drop them
|
||||
* rather than failing. Node rejects the call instead, and its wording is
|
||||
* reproduced because callers match on it:
|
||||
*
|
||||
* The value of "length" is out of range. It must be <= 2. Received 8
|
||||
*/
|
||||
export function checkBufferRange(
|
||||
buffer: Uint8Array,
|
||||
offset: number | undefined,
|
||||
length: number | undefined,
|
||||
): { offset: number; length: number } {
|
||||
const start = offset ?? 0;
|
||||
if (!Number.isInteger(start) || start < 0 || start > buffer.byteLength) {
|
||||
throw nodeError(
|
||||
'ERR_OUT_OF_RANGE',
|
||||
`The value of "offset" is out of range. It must be >= 0 && <= ${buffer.byteLength}. Received ${start}`,
|
||||
);
|
||||
}
|
||||
const count = length ?? buffer.byteLength - start;
|
||||
const available = buffer.byteLength - start;
|
||||
if (!Number.isInteger(count) || count < 0 || count > available) {
|
||||
throw nodeError(
|
||||
'ERR_OUT_OF_RANGE',
|
||||
`The value of "length" is out of range. It must be <= ${available}. Received ${count}`,
|
||||
);
|
||||
}
|
||||
return { offset: start, length: count };
|
||||
}
|
||||
|
||||
/**
|
||||
* A recursive `readdir` entry as the path Node reports.
|
||||
*
|
||||
* Node's `readdir(dir, { recursive: true })` returns paths relative to `dir`
|
||||
* — `nested/deep/leaf.txt`, not `leaf.txt` — so a caller can join them onto the
|
||||
* root and get a real path. The native side reports each entry's absolute
|
||||
* containing directory, so the relative name is what remains after removing the
|
||||
* root. A non-recursive listing names the direct children only, which is already
|
||||
* what `entry.name` holds, so this is used for the recursive case alone.
|
||||
*
|
||||
* The separator is taken from the paths themselves rather than from a platform
|
||||
* check: the native side returns native separators, and a module that reaches
|
||||
* for `process.platform` needs Node's types for a question the data already
|
||||
* answers.
|
||||
*/
|
||||
export function relativeEntryName(
|
||||
root: string,
|
||||
parentPath: string,
|
||||
name: string,
|
||||
): string {
|
||||
const separator = root.includes('\\') ? '\\' : '/';
|
||||
const split = (value: string) => value.split(/[\\/]+/u).filter(Boolean);
|
||||
const rootParts = split(root);
|
||||
const parentParts = split(parentPath);
|
||||
// Compare from the front. A parent that does not start with the root means the
|
||||
// entry is somewhere the caller did not ask about, which should not happen, so
|
||||
// the bare name is a safer answer than a fabricated path.
|
||||
const inside = rootParts.every((part, index) => parentParts[index] === part);
|
||||
if (!inside) {
|
||||
return name;
|
||||
}
|
||||
return [...parentParts.slice(rootParts.length), name].join(separator);
|
||||
}
|
||||
|
||||
/** The encoding name, folded to the set implemented here. */
|
||||
export function normalizeEncoding(
|
||||
encoding: string | null | undefined,
|
||||
): NormalizedEncoding | null {
|
||||
if (encoding === null || encoding === undefined) {
|
||||
return null;
|
||||
}
|
||||
switch (encoding.toLowerCase()) {
|
||||
case 'utf8':
|
||||
case 'utf-8':
|
||||
return 'utf8';
|
||||
case 'utf16le':
|
||||
case 'utf-16le':
|
||||
case 'ucs2':
|
||||
case 'ucs-2':
|
||||
return 'utf16le';
|
||||
case 'latin1':
|
||||
case 'binary':
|
||||
case 'ascii':
|
||||
return 'latin1';
|
||||
case 'base64':
|
||||
return 'base64';
|
||||
case 'hex':
|
||||
return 'hex';
|
||||
default:
|
||||
throw nodeError(
|
||||
'ERR_INVALID_ARG_VALUE',
|
||||
`The argument 'encoding' is invalid encoding. Received '${encoding}'`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const BASE64_ALPHABET =
|
||||
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
|
||||
const BASE64_VALUES: Record<string, number> = (() => {
|
||||
const values: Record<string, number> = {};
|
||||
for (let index = 0; index < BASE64_ALPHABET.length; index += 1) {
|
||||
values[BASE64_ALPHABET[index]] = index;
|
||||
}
|
||||
// The URL-safe alphabet is accepted too: `Buffer` decodes either.
|
||||
values['-'] = 62;
|
||||
values._ = 63;
|
||||
return values;
|
||||
})();
|
||||
|
||||
function encodeBase64(bytes: Uint8Array): string {
|
||||
let out = '';
|
||||
for (let index = 0; index < bytes.byteLength; index += 3) {
|
||||
const first = bytes[index];
|
||||
const second = index + 1 < bytes.byteLength ? bytes[index + 1] : 0;
|
||||
const third = index + 2 < bytes.byteLength ? bytes[index + 2] : 0;
|
||||
const triple = (first << 16) | (second << 8) | third;
|
||||
out += BASE64_ALPHABET[(triple >> 18) & 0x3f];
|
||||
out += BASE64_ALPHABET[(triple >> 12) & 0x3f];
|
||||
out +=
|
||||
index + 1 < bytes.byteLength
|
||||
? BASE64_ALPHABET[(triple >> 6) & 0x3f]
|
||||
: '=';
|
||||
out += index + 2 < bytes.byteLength ? BASE64_ALPHABET[triple & 0x3f] : '=';
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function decodeBase64(text: string): Uint8Array {
|
||||
const bytes: number[] = [];
|
||||
let pending = 0;
|
||||
let bits = 0;
|
||||
for (const character of text) {
|
||||
if (
|
||||
character === '=' ||
|
||||
character === '\n' ||
|
||||
character === '\r' ||
|
||||
character === ' '
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const value = BASE64_VALUES[character];
|
||||
if (value === undefined) {
|
||||
// Node stops at the first character that is not base64 rather than
|
||||
// failing, so `Buffer.from('abc$', 'base64')` decodes the prefix.
|
||||
break;
|
||||
}
|
||||
pending = (pending << 6) | value;
|
||||
bits += 6;
|
||||
if (bits >= 8) {
|
||||
bits -= 8;
|
||||
bytes.push((pending >> bits) & 0xff);
|
||||
}
|
||||
}
|
||||
return Uint8Array.from(bytes);
|
||||
}
|
||||
|
||||
function encodeHex(bytes: Uint8Array): string {
|
||||
let out = '';
|
||||
for (let index = 0; index < bytes.byteLength; index += 1) {
|
||||
out += bytes[index].toString(16).padStart(2, '0');
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function decodeHex(text: string): Uint8Array {
|
||||
const bytes: number[] = [];
|
||||
for (let index = 0; index + 1 < text.length; index += 2) {
|
||||
const value = Number.parseInt(text.slice(index, index + 2), 16);
|
||||
if (Number.isNaN(value)) {
|
||||
break;
|
||||
}
|
||||
bytes.push(value);
|
||||
}
|
||||
return Uint8Array.from(bytes);
|
||||
}
|
||||
|
||||
function encodeUtf8(text: string): Uint8Array {
|
||||
const bytes: number[] = [];
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
let code = text.charCodeAt(index);
|
||||
// A surrogate pair is one code point; without this an emoji becomes two
|
||||
// sequences of replacement characters.
|
||||
if (code >= 0xd800 && code <= 0xdbff && index + 1 < text.length) {
|
||||
const next = text.charCodeAt(index + 1);
|
||||
if (next >= 0xdc00 && next <= 0xdfff) {
|
||||
code = 0x10000 + ((code - 0xd800) << 10) + (next - 0xdc00);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
if (code < 0x80) {
|
||||
bytes.push(code);
|
||||
} else if (code < 0x800) {
|
||||
bytes.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
|
||||
} else if (code < 0x10000) {
|
||||
bytes.push(
|
||||
0xe0 | (code >> 12),
|
||||
0x80 | ((code >> 6) & 0x3f),
|
||||
0x80 | (code & 0x3f),
|
||||
);
|
||||
} else {
|
||||
bytes.push(
|
||||
0xf0 | (code >> 18),
|
||||
0x80 | ((code >> 12) & 0x3f),
|
||||
0x80 | ((code >> 6) & 0x3f),
|
||||
0x80 | (code & 0x3f),
|
||||
);
|
||||
}
|
||||
}
|
||||
return Uint8Array.from(bytes);
|
||||
}
|
||||
|
||||
function decodeUtf8(bytes: Uint8Array): string {
|
||||
let out = '';
|
||||
let index = 0;
|
||||
while (index < bytes.byteLength) {
|
||||
const first = bytes[index];
|
||||
const width =
|
||||
first < 0x80
|
||||
? 1
|
||||
: first >= 0xf0
|
||||
? 4
|
||||
: first >= 0xe0
|
||||
? 3
|
||||
: first >= 0xc0
|
||||
? 2
|
||||
: 0;
|
||||
if (width === 0 || index + width > bytes.byteLength) {
|
||||
// Node decodes malformed input with U+FFFD instead of throwing, so a
|
||||
// file that is not valid UTF-8 still reads as text.
|
||||
out += '\ufffd';
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
let code = width === 1 ? first : first & (0xff >> (width + 1));
|
||||
let valid = true;
|
||||
for (let offset = 1; offset < width; offset += 1) {
|
||||
const continuation = bytes[index + offset];
|
||||
if ((continuation & 0xc0) !== 0x80) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
code = (code << 6) | (continuation & 0x3f);
|
||||
}
|
||||
if (!valid) {
|
||||
out += '\ufffd';
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
out += String.fromCodePoint(code);
|
||||
index += width;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function encodeUtf16le(text: string): Uint8Array {
|
||||
const bytes = new Uint8Array(text.length * 2);
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
const code = text.charCodeAt(index);
|
||||
bytes[index * 2] = code & 0xff;
|
||||
bytes[index * 2 + 1] = code >> 8;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
function decodeUtf16le(bytes: Uint8Array): string {
|
||||
const codes: number[] = [];
|
||||
for (let index = 0; index + 1 < bytes.byteLength; index += 2) {
|
||||
codes.push(bytes[index] | (bytes[index + 1] << 8));
|
||||
}
|
||||
return chunkedFromCharCode(codes);
|
||||
}
|
||||
|
||||
function chunkedFromCharCode(codes: number[]): string {
|
||||
let out = '';
|
||||
// `apply` has an argument-count limit, so long strings are built in pieces.
|
||||
for (let index = 0; index < codes.length; index += 8192) {
|
||||
out += String.fromCharCode.apply(null, codes.slice(index, index + 8192));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Text to bytes for the encodings implemented in JS. */
|
||||
export function encodeString(
|
||||
text: string,
|
||||
encoding: NormalizedEncoding,
|
||||
): Uint8Array {
|
||||
switch (encoding) {
|
||||
case 'utf8':
|
||||
return encodeUtf8(text);
|
||||
case 'utf16le':
|
||||
return encodeUtf16le(text);
|
||||
case 'latin1': {
|
||||
const bytes = new Uint8Array(text.length);
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
bytes[index] = text.charCodeAt(index) & 0xff;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
case 'base64':
|
||||
return decodeBase64(text);
|
||||
case 'hex':
|
||||
return decodeHex(text);
|
||||
}
|
||||
}
|
||||
|
||||
/** Bytes to text for the encodings implemented in JS. */
|
||||
export function decodeString(
|
||||
bytes: Uint8Array,
|
||||
encoding: NormalizedEncoding,
|
||||
): string {
|
||||
switch (encoding) {
|
||||
case 'utf8':
|
||||
return decodeUtf8(bytes);
|
||||
case 'utf16le':
|
||||
return decodeUtf16le(bytes);
|
||||
case 'latin1': {
|
||||
const codes: number[] = [];
|
||||
for (let index = 0; index < bytes.byteLength; index += 1) {
|
||||
codes.push(bytes[index]);
|
||||
}
|
||||
return chunkedFromCharCode(codes);
|
||||
}
|
||||
case 'base64':
|
||||
return encodeBase64(bytes);
|
||||
case 'hex':
|
||||
return encodeHex(bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalises any accepted data shape to an `ArrayBuffer` the bridge can send.
|
||||
*
|
||||
* A `Uint8Array` may be a window onto a larger allocation; slicing to the view's
|
||||
* own range is what stops a writer from sending bytes that belong to something
|
||||
* else in the same buffer.
|
||||
*/
|
||||
export function toArrayBuffer(
|
||||
data: BinaryData,
|
||||
encoding: NormalizedEncoding = 'utf8',
|
||||
): ArrayBuffer {
|
||||
if (typeof data === 'string') {
|
||||
const bytes = encodeString(data, encoding);
|
||||
return bytes.buffer.slice(
|
||||
bytes.byteOffset,
|
||||
bytes.byteOffset + bytes.byteLength,
|
||||
) as ArrayBuffer;
|
||||
}
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return data;
|
||||
}
|
||||
const view = data as ArrayBufferView;
|
||||
const buffer = view.buffer as ArrayBuffer;
|
||||
if (view.byteOffset === 0 && view.byteLength === buffer.byteLength) {
|
||||
return buffer;
|
||||
}
|
||||
return buffer.slice(view.byteOffset, view.byteOffset + view.byteLength);
|
||||
}
|
||||
|
||||
/** A byte view of `toArrayBuffer`'s result, without copying. */
|
||||
export function toBytes(
|
||||
data: BinaryData,
|
||||
encoding: NormalizedEncoding = 'utf8',
|
||||
): Uint8Array {
|
||||
return new Uint8Array(toArrayBuffer(data, encoding));
|
||||
}
|
||||
|
||||
/** Node's numeric `open` flags, by their string spelling. */
|
||||
const FLAG_STRINGS: Record<string, number> = {
|
||||
r: 0,
|
||||
'r+': 2,
|
||||
rs: 0,
|
||||
sr: 0,
|
||||
w: 1 | 64 | 512,
|
||||
wx: 1 | 64 | 512 | 128,
|
||||
xw: 1 | 64 | 512 | 128,
|
||||
'w+': 2 | 64 | 512,
|
||||
'wx+': 2 | 64 | 512 | 128,
|
||||
'xw+': 2 | 64 | 512 | 128,
|
||||
a: 1 | 64 | 1024,
|
||||
ax: 1 | 64 | 1024 | 128,
|
||||
xa: 1 | 64 | 1024 | 128,
|
||||
'a+': 2 | 64 | 1024,
|
||||
'ax+': 2 | 64 | 1024 | 128,
|
||||
'xa+': 2 | 64 | 1024 | 128,
|
||||
as: 1 | 64 | 1024,
|
||||
sa: 1 | 64 | 1024,
|
||||
};
|
||||
|
||||
/** A `flag` option as the number the native side decodes. */
|
||||
export function flagFrom(
|
||||
flag: string | number | undefined,
|
||||
fallback: number,
|
||||
): number {
|
||||
if (flag === undefined) {
|
||||
return fallback;
|
||||
}
|
||||
if (typeof flag === 'number') {
|
||||
return flag;
|
||||
}
|
||||
const value = FLAG_STRINGS[flag];
|
||||
if (value === undefined) {
|
||||
throw nodeError(
|
||||
'ERR_INVALID_ARG_VALUE',
|
||||
`The argument 'flag' is invalid. Received '${flag}'`,
|
||||
);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** A `mode` option as a number. A string is octal, as in Node. */
|
||||
export function modeFrom(
|
||||
mode: number | string | undefined,
|
||||
fallback = 0o666,
|
||||
): number {
|
||||
if (mode === undefined) {
|
||||
return fallback;
|
||||
}
|
||||
if (typeof mode === 'number') {
|
||||
return mode;
|
||||
}
|
||||
const parsed = Number.parseInt(mode, 8);
|
||||
if (Number.isNaN(parsed)) {
|
||||
throw nodeError(
|
||||
'ERR_INVALID_ARG_VALUE',
|
||||
`The argument 'mode' is invalid. Received '${mode}'`,
|
||||
);
|
||||
}
|
||||
return parsed;
|
||||
}
|
||||
-196
@@ -1,196 +0,0 @@
|
||||
/**
|
||||
* `Stats` and `Dirent`, built over the plain objects the native module returns.
|
||||
*
|
||||
* The module crosses the bridge with numbers rather than methods — a generated
|
||||
* TurboModule result cannot carry predicates — so the classes live here and
|
||||
* reproduce the shape Node's callers test for. The `kind` field is the coarse
|
||||
* classification the native side can make without a platform syscall; `mode` is
|
||||
* there for the finer predicates, and is zero on Windows where there are no
|
||||
* Unix mode bits at all.
|
||||
*/
|
||||
|
||||
import { constants } from './constants';
|
||||
|
||||
/** The native `FileKind` numbers; must match `module::kind` in Rust. */
|
||||
export const FILE_KIND = {
|
||||
file: 0,
|
||||
directory: 1,
|
||||
symlink: 2,
|
||||
other: 3,
|
||||
} as const;
|
||||
|
||||
/** The shape `stat`, `lstat` and `fstat` return across the bridge. */
|
||||
export interface StatResult {
|
||||
size: number;
|
||||
mode: number;
|
||||
kind: number;
|
||||
atimeMs: number;
|
||||
mtimeMs: number;
|
||||
ctimeMs: number;
|
||||
birthtimeMs: number;
|
||||
dev: number;
|
||||
ino: number;
|
||||
nlink: number;
|
||||
uid: number;
|
||||
gid: number;
|
||||
}
|
||||
|
||||
/** The shape `readdir` returns across the bridge. */
|
||||
export interface DirEntryResult {
|
||||
name: string;
|
||||
kind: number;
|
||||
parentPath: string;
|
||||
}
|
||||
|
||||
/** The type bits of a mode, or 0 when the platform does not report them. */
|
||||
function fileTypeBits(mode: number): number {
|
||||
return mode & constants.S_IFMT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Node's `fs.Stats`.
|
||||
*
|
||||
* The fields are plain properties rather than getters: they are copied once from
|
||||
* the native result, and a caller destructuring the object sees the same values
|
||||
* Node's does.
|
||||
*/
|
||||
export class Stats {
|
||||
readonly dev: number;
|
||||
readonly ino: number;
|
||||
readonly mode: number;
|
||||
readonly nlink: number;
|
||||
readonly uid: number;
|
||||
readonly gid: number;
|
||||
readonly rdev: number;
|
||||
readonly size: number;
|
||||
readonly blksize: number;
|
||||
readonly blocks: number;
|
||||
|
||||
readonly atimeMs: number;
|
||||
readonly mtimeMs: number;
|
||||
readonly ctimeMs: number;
|
||||
readonly birthtimeMs: number;
|
||||
|
||||
readonly atime: Date;
|
||||
readonly mtime: Date;
|
||||
readonly ctime: Date;
|
||||
readonly birthtime: Date;
|
||||
|
||||
/** The native classification, kept because it answers predicates the mode
|
||||
* cannot on platforms that report no type bits. */
|
||||
private readonly kind: number;
|
||||
|
||||
constructor(result: StatResult) {
|
||||
this.dev = result.dev;
|
||||
this.ino = result.ino;
|
||||
this.mode = result.mode;
|
||||
this.nlink = result.nlink;
|
||||
this.uid = result.uid;
|
||||
this.gid = result.gid;
|
||||
// The native result has no `rdev`, block size or block count; Node reports
|
||||
// zero for them where the platform cannot say, so zero is the honest value.
|
||||
this.rdev = 0;
|
||||
this.size = result.size;
|
||||
this.blksize = 0;
|
||||
this.blocks = 0;
|
||||
|
||||
this.atimeMs = result.atimeMs;
|
||||
this.mtimeMs = result.mtimeMs;
|
||||
this.ctimeMs = result.ctimeMs;
|
||||
this.birthtimeMs = result.birthtimeMs;
|
||||
|
||||
this.atime = new Date(result.atimeMs);
|
||||
this.mtime = new Date(result.mtimeMs);
|
||||
this.ctime = new Date(result.ctimeMs);
|
||||
this.birthtime = new Date(result.birthtimeMs);
|
||||
|
||||
this.kind = result.kind;
|
||||
}
|
||||
|
||||
isFile(): boolean {
|
||||
const bits = fileTypeBits(this.mode);
|
||||
return bits === 0
|
||||
? this.kind === FILE_KIND.file
|
||||
: bits === constants.S_IFREG;
|
||||
}
|
||||
|
||||
isDirectory(): boolean {
|
||||
const bits = fileTypeBits(this.mode);
|
||||
return bits === 0
|
||||
? this.kind === FILE_KIND.directory
|
||||
: bits === constants.S_IFDIR;
|
||||
}
|
||||
|
||||
isSymbolicLink(): boolean {
|
||||
const bits = fileTypeBits(this.mode);
|
||||
return bits === 0
|
||||
? this.kind === FILE_KIND.symlink
|
||||
: bits === constants.S_IFLNK;
|
||||
}
|
||||
|
||||
isBlockDevice(): boolean {
|
||||
return fileTypeBits(this.mode) === constants.S_IFBLK;
|
||||
}
|
||||
|
||||
isCharacterDevice(): boolean {
|
||||
return fileTypeBits(this.mode) === constants.S_IFCHR;
|
||||
}
|
||||
|
||||
isFIFO(): boolean {
|
||||
return fileTypeBits(this.mode) === constants.S_IFIFO;
|
||||
}
|
||||
|
||||
isSocket(): boolean {
|
||||
return fileTypeBits(this.mode) === constants.S_IFSOCK;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Node's `fs.Dirent`.
|
||||
*
|
||||
* `parentPath` is `''` when the caller already held the parent, matching what
|
||||
* `readdir` with `{withFileTypes: true}` produces there.
|
||||
*/
|
||||
export class Dirent {
|
||||
readonly name: string;
|
||||
readonly parentPath: string;
|
||||
readonly path: string;
|
||||
|
||||
private readonly kind: number;
|
||||
|
||||
constructor(result: DirEntryResult) {
|
||||
this.name = result.name;
|
||||
this.parentPath = result.parentPath;
|
||||
// `path` is the older name for the same field, and libraries still read it.
|
||||
this.path = result.parentPath;
|
||||
this.kind = result.kind;
|
||||
}
|
||||
|
||||
isFile(): boolean {
|
||||
return this.kind === FILE_KIND.file;
|
||||
}
|
||||
|
||||
isDirectory(): boolean {
|
||||
return this.kind === FILE_KIND.directory;
|
||||
}
|
||||
|
||||
isSymbolicLink(): boolean {
|
||||
return this.kind === FILE_KIND.symlink;
|
||||
}
|
||||
|
||||
isBlockDevice(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
isCharacterDevice(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
isFIFO(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
isSocket(): boolean {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* `upload`: a file, or a range of one, streamed as an HTTP request body.
|
||||
*
|
||||
* The native side reads the file in small blocks while the connection takes
|
||||
* them, so nothing larger than a block is ever held in memory.
|
||||
*/
|
||||
|
||||
import NativeFs from './NativeFs';
|
||||
import { nodeError, toPath, withContext, type NodeError } from './shared';
|
||||
|
||||
export interface UploadOptions {
|
||||
/** `PUT` unless set. */
|
||||
method?: string;
|
||||
headers?: Record<string, string>;
|
||||
/** Where in the file the body starts. Zero unless set. */
|
||||
offset?: number;
|
||||
/** How many bytes to send. Everything from `offset` unless set. */
|
||||
length?: number;
|
||||
/** Called while the body is sent. */
|
||||
onProgress?: (sent: number, total: number) => void;
|
||||
/** Aborting rejects with an `AbortError`. */
|
||||
signal?: AbortSignal;
|
||||
}
|
||||
|
||||
export interface UploadResult {
|
||||
status: number;
|
||||
/** Response headers, lower-cased; repeated headers are joined with `, `. */
|
||||
headers: Record<string, string>;
|
||||
/** The first part of the response body. */
|
||||
body: string;
|
||||
}
|
||||
|
||||
let nextId = 1;
|
||||
|
||||
function abortError(): NodeError {
|
||||
const error = nodeError('ABORT_ERR', 'The upload was aborted');
|
||||
error.name = 'AbortError';
|
||||
return error;
|
||||
}
|
||||
|
||||
function toRecord(list: Array<{ name: string; value: string }>) {
|
||||
const record: Record<string, string> = {};
|
||||
for (const { name, value } of list) {
|
||||
record[name] = value;
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
export async function upload(
|
||||
url: string,
|
||||
path: string,
|
||||
options: UploadOptions = {},
|
||||
): Promise<UploadResult> {
|
||||
const { signal, onProgress } = options;
|
||||
if (signal?.aborted) {
|
||||
throw abortError();
|
||||
}
|
||||
const source = toPath(path);
|
||||
const id = nextId++;
|
||||
const subscription = onProgress
|
||||
? NativeFs.onUploadProgress(event => {
|
||||
if (event.id === id) {
|
||||
onProgress(event.sent, event.total);
|
||||
}
|
||||
})
|
||||
: null;
|
||||
const onAbort = () => {
|
||||
NativeFs.cancelUpload(id);
|
||||
};
|
||||
signal?.addEventListener('abort', onAbort);
|
||||
|
||||
try {
|
||||
const result = await NativeFs.upload(url, source, {
|
||||
id,
|
||||
method: options.method ?? 'PUT',
|
||||
headers: Object.entries(options.headers ?? {}).map(([name, value]) => ({
|
||||
name,
|
||||
value,
|
||||
})),
|
||||
offset: options.offset ?? 0,
|
||||
length: options.length ?? -1,
|
||||
});
|
||||
return {
|
||||
status: result.status,
|
||||
headers: toRecord(result.headers),
|
||||
body: result.body,
|
||||
};
|
||||
} catch (error) {
|
||||
if (signal?.aborted || (error as NodeError)?.code === 'ABORT_ERR') {
|
||||
throw abortError();
|
||||
}
|
||||
throw withContext(error, 'upload', source);
|
||||
} finally {
|
||||
signal?.removeEventListener('abort', onAbort);
|
||||
subscription?.remove();
|
||||
}
|
||||
}
|
||||
-141
@@ -1,141 +0,0 @@
|
||||
/**
|
||||
* `watch`: the JS half of the typed watcher event.
|
||||
*
|
||||
* The native module returns a watcher id; every event it produces carries that
|
||||
* id, so one shared emitter serves every open watcher and each listener keeps
|
||||
* only its own. The subscription is taken through the generated typed emitter on
|
||||
* the module (`NativeFs.onWatchEvent`), which is the same mechanism the fixture's
|
||||
* `onProgress` uses in `RustTurboModuleFixture.tsx`, rather than through
|
||||
* `NativeEventEmitter`: the spec declares `onWatchEvent` as a typed
|
||||
* `CodegenTypes.EventEmitter`, and the bridge installs a listener for exactly
|
||||
* that; `NativeEventEmitter` would subscribe on the global device-emitter channel
|
||||
* that only `sendEvent` writes to, so it would never fire.
|
||||
*/
|
||||
|
||||
import NativeFs, { type WatchEvent } from './NativeFs';
|
||||
import { toPath, withContext, type NodeError } from './shared';
|
||||
|
||||
/** The event names Node uses for the two things a watcher reports. */
|
||||
export type WatchEventType = 'rename' | 'change';
|
||||
|
||||
export interface WatchOptions {
|
||||
recursive?: boolean;
|
||||
encoding?: string;
|
||||
}
|
||||
|
||||
export interface FSWatcher {
|
||||
close(): Promise<void>;
|
||||
}
|
||||
|
||||
export type WatchListener = (
|
||||
eventType: WatchEventType,
|
||||
filename: string | null,
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* Which of Node's two event types a native kind is.
|
||||
*
|
||||
* Node folds everything it sees into `rename` (the entry appeared, vanished or
|
||||
* moved) and `change` (its contents or metadata changed). The caller's callback
|
||||
* signature is Node's, so the mapping happens here while the native kind stays
|
||||
* available to anyone who subscribes to the module directly.
|
||||
*/
|
||||
function eventTypeOf(kind: string): WatchEventType {
|
||||
return kind === 'modify' ? 'change' : 'rename';
|
||||
}
|
||||
|
||||
/** The file a path names, relative to the watched root where it is below it. */
|
||||
/**
|
||||
* The filename a listener is given.
|
||||
*
|
||||
* The native side already reported it relative to the watch root, and its own
|
||||
* name for a single-file watch: it canonicalises the root, which is where macOS
|
||||
* decides between `/var` and `/private/var`, and it strips whichever spelling the
|
||||
* event actually carries. Stripping a prefix here as well would be a second,
|
||||
* weaker guess at the same question — a relative root never matches, and Windows
|
||||
* separates with `\` — so the event path is passed through unchanged.
|
||||
*/
|
||||
function fileNameOf(eventPath: string): string {
|
||||
return eventPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a watcher.
|
||||
*
|
||||
* The id arrives asynchronously, but the caller expects a handle it can close
|
||||
* immediately, so the handle is created first and closing it before the id is
|
||||
* known sets a flag the resolution honours. That is the one ordering hazard in
|
||||
* this API, and it is invisible from the outside.
|
||||
*
|
||||
* Node reports a failure to start through an `error` event on the watcher. This
|
||||
* handle has no event surface, so the failure is held and rejected from
|
||||
* `close()` instead: it can then never become an unhandled rejection, and a
|
||||
* caller that closes what it opened is guaranteed to see it.
|
||||
*/
|
||||
export function watch(path: string, listener: WatchListener): FSWatcher;
|
||||
export function watch(
|
||||
path: string,
|
||||
options: WatchOptions | null,
|
||||
listener: WatchListener,
|
||||
): FSWatcher;
|
||||
export function watch(
|
||||
path: string,
|
||||
optionsOrListener: WatchOptions | null | WatchListener,
|
||||
maybeListener?: WatchListener,
|
||||
): FSWatcher {
|
||||
const options =
|
||||
typeof optionsOrListener === 'function' ? {} : (optionsOrListener ?? {});
|
||||
const listener =
|
||||
typeof optionsOrListener === 'function' ? optionsOrListener : maybeListener;
|
||||
if (typeof listener !== 'function') {
|
||||
throw new TypeError('watch requires a listener function');
|
||||
}
|
||||
|
||||
const target = toPath(path);
|
||||
let id: number | null = null;
|
||||
let closed = false;
|
||||
let failure: NodeError | null = null;
|
||||
let subscription: { remove(): void } | null = null;
|
||||
|
||||
const ready = NativeFs.watch(target, options.recursive ?? false)
|
||||
.then(watcherId => {
|
||||
id = watcherId;
|
||||
if (closed) {
|
||||
// `close()` ran while the id was in flight; stop the native watcher now
|
||||
// that there is something to stop.
|
||||
NativeFs.unwatch(watcherId);
|
||||
return;
|
||||
}
|
||||
subscription = NativeFs.onWatchEvent((event: WatchEvent) => {
|
||||
if (event.id !== id) {
|
||||
return;
|
||||
}
|
||||
// The encoding option only affects how a non-ASCII name is decoded, and
|
||||
// every name already arrives as a JS string.
|
||||
listener(eventTypeOf(event.kind), fileNameOf(event.path));
|
||||
});
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
failure = withContext(error, 'watch', target);
|
||||
});
|
||||
|
||||
return {
|
||||
async close(): Promise<void> {
|
||||
closed = true;
|
||||
subscription?.remove();
|
||||
subscription = null;
|
||||
if (id !== null) {
|
||||
NativeFs.unwatch(id);
|
||||
id = null;
|
||||
}
|
||||
// Awaiting the start makes `close()` immediately after `watch()` still stop
|
||||
// the native watcher rather than racing it.
|
||||
await ready;
|
||||
if (failure !== null) {
|
||||
const error = failure;
|
||||
failure = null;
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/gesture-handler` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## 3.3.0-alpha.1 - 2026-11-12
|
||||
|
||||
- A gesture whose callbacks run on the JS thread (`runOnJS(true)`, or a handler from the old API) receives its events. They were sent to its view, where only worklet callbacks listen, so a sheet dragged by such a gesture did not move.
|
||||
|
||||
## 3.3.0-alpha.0 - 2026-11-01
|
||||
|
||||
- First release, for `react-native-gesture-handler` 3.3.x with Reanimated 4.7.x and Worklets 0.13.x.
|
||||
- Gesture recognition for mouse, trackpad and touch input on desktop.
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,25 +0,0 @@
|
||||
# @react-native-gpui/gesture-handler
|
||||
|
||||
Adapter for [`react-native-gesture-handler`](https://github.com/software-mansion/react-native-gesture-handler) (MIT): recognizes gestures from the mouse, trackpad and touch screen.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
Install it next to `react-native-gesture-handler`:
|
||||
|
||||
```sh
|
||||
npm install react-native-gesture-handler@3.3.x @react-native-gpui/gesture-handler
|
||||
```
|
||||
|
||||
Gesture Handler needs Reanimated 4.7.x and Worklets 0.13.x, with their desktop packages [`@react-native-gpui/reanimated`](https://www.npmjs.com/package/@react-native-gpui/reanimated) and [`@react-native-gpui/worklets`](https://www.npmjs.com/package/@react-native-gpui/worklets).
|
||||
|
||||
Keep using `react-native-gesture-handler` as you do on iOS and Android; its JavaScript API is unchanged. Autolinking builds the desktop side when the app is built.
|
||||
|
||||
## Documentation
|
||||
|
||||
[Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,19 +0,0 @@
|
||||
if(NOT RN_GPUI_PROJECT_ROOT)
|
||||
message(FATAL_ERROR
|
||||
"@react-native-gpui/gesture-handler was added by the platform; RN_GPUI_PROJECT_ROOT must be set by its build.rs")
|
||||
endif()
|
||||
rn_gpui_find_node_module(react-native-gesture-handler _gesture_root)
|
||||
if(NOT EXISTS "${_gesture_root}/src/specs/NativeRNGestureHandlerModule.ts")
|
||||
message(FATAL_ERROR "react-native-gesture-handler 3.3.x was not found at ${_gesture_root}")
|
||||
endif()
|
||||
|
||||
file(READ "${_gesture_root}/package.json" _gesture_package_json)
|
||||
string(JSON _gesture_version GET "${_gesture_package_json}" version)
|
||||
if(NOT _gesture_version MATCHES "^3\\.3\\.")
|
||||
message(FATAL_ERROR "@react-native-gpui/gesture-handler supports react-native-gesture-handler 3.3.x, found ${_gesture_version}")
|
||||
endif()
|
||||
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_SOURCES
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GPUIGestureHandlerModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_INCLUDE_DIRS
|
||||
"${RN_GPUI_SYS_CPP_DIR}")
|
||||
-548
@@ -1,548 +0,0 @@
|
||||
#include "GpuiHost.h"
|
||||
#include "GpuiModules.h"
|
||||
|
||||
#include <GPUIGestureHandlerSpec.h>
|
||||
#include <ReactCommon/TurboModule.h>
|
||||
#include <folly/dynamic.h>
|
||||
#include <folly/json.h>
|
||||
#include <glog/logging.h>
|
||||
#include <jsi/JSIDynamic.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using facebook::jsi::Runtime;
|
||||
using facebook::react::TurboModule;
|
||||
using rngpui::extension::RawPointerEvent;
|
||||
using rngpui::extension::RecognizedGesture;
|
||||
|
||||
constexpr int32_t kUndetermined = 0;
|
||||
constexpr int32_t kFailed = 1;
|
||||
constexpr int32_t kBegan = 2;
|
||||
constexpr int32_t kCancelled = 3;
|
||||
constexpr int32_t kActive = 4;
|
||||
constexpr int32_t kEnd = 5;
|
||||
|
||||
struct Handler {
|
||||
std::string name;
|
||||
folly::dynamic config{folly::dynamic::object};
|
||||
int32_t viewTag{-1};
|
||||
int32_t actionType{0};
|
||||
int32_t state{kUndetermined};
|
||||
float startX{0};
|
||||
float startY{0};
|
||||
float lastX{0};
|
||||
float lastY{0};
|
||||
double startTime{0};
|
||||
double lastTime{0};
|
||||
bool recognized{false};
|
||||
std::vector<int32_t> simultaneousHandlers;
|
||||
std::vector<int32_t> waitFor;
|
||||
std::vector<int32_t> blocksHandlers;
|
||||
};
|
||||
|
||||
std::mutex handlersMutex;
|
||||
std::unordered_map<int32_t, Handler> handlers;
|
||||
|
||||
// What one pointer event produced. A gesture whose callbacks run on the JS
|
||||
// thread is told through device events, as GestureDetector listens for them;
|
||||
// any other through events on its view.
|
||||
struct Recognized {
|
||||
std::vector<RecognizedGesture> viewEvents;
|
||||
std::vector<std::pair<std::string, folly::dynamic>> deviceEvents;
|
||||
};
|
||||
|
||||
bool runsOnJS(const Handler& handler) {
|
||||
constexpr int32_t kJSFunctionOldAPI = 3;
|
||||
constexpr int32_t kJSFunctionNewAPI = 4;
|
||||
return handler.actionType == kJSFunctionOldAPI ||
|
||||
handler.actionType == kJSFunctionNewAPI;
|
||||
}
|
||||
|
||||
folly::dynamic configObject(folly::dynamic config) {
|
||||
return config.isObject() ? std::move(config) : folly::dynamic::object();
|
||||
}
|
||||
|
||||
float number(const folly::dynamic& object, const char* key, float fallback) {
|
||||
if (!object.isObject() || !object.get_ptr(key))
|
||||
return fallback;
|
||||
const auto& value = object[key];
|
||||
if (value.isNumber())
|
||||
return static_cast<float>(value.asDouble());
|
||||
return fallback;
|
||||
}
|
||||
|
||||
bool enabled(const Handler& handler) {
|
||||
const auto* value = handler.config.get_ptr("enabled");
|
||||
return value == nullptr || !value->isBool() || value->asBool();
|
||||
}
|
||||
|
||||
bool isPan(const Handler& handler) {
|
||||
return handler.name == "Pan" || handler.name == "PanGestureHandler";
|
||||
}
|
||||
|
||||
bool isTap(const Handler& handler) {
|
||||
return handler.name == "Tap" || handler.name == "TapGestureHandler";
|
||||
}
|
||||
|
||||
bool isLongPress(const Handler& handler) {
|
||||
return handler.name == "LongPress" ||
|
||||
handler.name == "LongPressGestureHandler";
|
||||
}
|
||||
|
||||
bool gestureTracingEnabled() {
|
||||
return std::getenv("RN_GPUI_DEBUG_GESTURES") != nullptr;
|
||||
}
|
||||
|
||||
folly::dynamic payloadFor(
|
||||
int32_t handlerTag,
|
||||
const Handler& handler,
|
||||
int32_t state,
|
||||
int32_t oldState,
|
||||
const RawPointerEvent& pointer,
|
||||
bool stateChange) {
|
||||
const float dx = pointer.x - handler.startX;
|
||||
const float dy = pointer.y - handler.startY;
|
||||
const double elapsed = std::max(1.0, pointer.timestamp - handler.lastTime);
|
||||
folly::dynamic payload = folly::dynamic::object("handlerTag", handlerTag)(
|
||||
"state", state)("x", pointer.x - handler.startX)(
|
||||
"y", pointer.y - handler.startY)("absoluteX", pointer.x)(
|
||||
"absoluteY", pointer.y)("translationX", dx)("translationY", dy)(
|
||||
"velocityX", (pointer.x - handler.lastX) * 1000.0 / elapsed)(
|
||||
"velocityY", (pointer.y - handler.lastY) * 1000.0 / elapsed)(
|
||||
"numberOfPointers", 1)("pointerType", 2)(
|
||||
"duration", pointer.timestamp - handler.startTime);
|
||||
if (stateChange) {
|
||||
payload["oldState"] = oldState;
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
void appendEvent(
|
||||
Recognized& events,
|
||||
const Handler& handler,
|
||||
const char* type,
|
||||
const folly::dynamic& payload) {
|
||||
if (runsOnJS(handler)) {
|
||||
const bool stateChange =
|
||||
std::string_view(type) == "topGestureHandlerStateChange";
|
||||
events.deviceEvents.emplace_back(
|
||||
stateChange ? "onGestureHandlerStateChange" : "onGestureHandlerEvent",
|
||||
payload);
|
||||
return;
|
||||
}
|
||||
events.viewEvents.push_back({handler.viewTag, type, folly::toJson(payload)});
|
||||
}
|
||||
|
||||
void appendState(
|
||||
Recognized& events,
|
||||
int32_t handlerTag,
|
||||
Handler& handler,
|
||||
int32_t nextState,
|
||||
const RawPointerEvent& pointer) {
|
||||
if (handler.state == nextState)
|
||||
return;
|
||||
const auto oldState = handler.state;
|
||||
handler.state = nextState;
|
||||
if (nextState == kActive) {
|
||||
handler.recognized = true;
|
||||
}
|
||||
appendEvent(
|
||||
events,
|
||||
handler,
|
||||
"topGestureHandlerStateChange",
|
||||
payloadFor(handlerTag, handler, nextState, oldState, pointer, true));
|
||||
}
|
||||
|
||||
std::vector<int32_t> relationTags(
|
||||
const folly::dynamic& relations,
|
||||
const char* key) {
|
||||
std::vector<int32_t> tags;
|
||||
const auto* values = relations.get_ptr(key);
|
||||
if (values == nullptr || !values->isArray()) {
|
||||
return tags;
|
||||
}
|
||||
tags.reserve(values->size());
|
||||
for (const auto& value : *values) {
|
||||
if (value.isInt()) {
|
||||
tags.push_back(static_cast<int32_t>(value.asInt()));
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
bool hasTag(const std::vector<int32_t>& tags, int32_t tag) {
|
||||
return std::find(tags.begin(), tags.end(), tag) != tags.end();
|
||||
}
|
||||
|
||||
bool hasRecognizedWaitFor(const Handler& handler) {
|
||||
for (const auto tag : handler.waitFor) {
|
||||
const auto dependency = handlers.find(tag);
|
||||
if (dependency != handlers.end() && dependency->second.recognized) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool hasUnresolvedWaitFor(const Handler& handler) {
|
||||
for (const auto tag : handler.waitFor) {
|
||||
const auto dependency = handlers.find(tag);
|
||||
if (dependency != handlers.end() && dependency->second.state == kBegan &&
|
||||
!dependency->second.recognized) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool blockedByActiveHandler(int32_t handlerTag) {
|
||||
for (const auto& [otherTag, other] : handlers) {
|
||||
if (otherTag != handlerTag && other.state == kActive &&
|
||||
hasTag(other.blocksHandlers, handlerTag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Recognized processPointer(const RawPointerEvent& pointer) {
|
||||
Recognized events;
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
if (gestureTracingEnabled() && pointer.phase != 4) {
|
||||
LOG(INFO) << "[gesture-handler] pointer phase=" << pointer.phase << " at ("
|
||||
<< pointer.x << ',' << pointer.y
|
||||
<< ") chain=" << pointer.targetChain.size() << " tags";
|
||||
}
|
||||
if (pointer.phase == 0) {
|
||||
for (auto& [_, handler] : handlers) {
|
||||
if (!enabled(handler) || handler.viewTag < 0 ||
|
||||
std::find(
|
||||
pointer.targetChain.begin(),
|
||||
pointer.targetChain.end(),
|
||||
handler.viewTag) == pointer.targetChain.end()) {
|
||||
continue;
|
||||
}
|
||||
handler.state = kUndetermined;
|
||||
handler.startX = pointer.x;
|
||||
handler.startY = pointer.y;
|
||||
handler.lastX = pointer.x;
|
||||
handler.lastY = pointer.y;
|
||||
handler.startTime = pointer.timestamp;
|
||||
handler.lastTime = pointer.timestamp;
|
||||
handler.recognized = false;
|
||||
appendState(events, _, handler, kBegan, pointer);
|
||||
if (gestureTracingEnabled()) {
|
||||
LOG(INFO) << "[gesture-handler] began handler=" << _
|
||||
<< " name=" << handler.name << " view=" << handler.viewTag;
|
||||
}
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
if (pointer.phase == 2) {
|
||||
// Resolve recognizers that cannot succeed on release before testing tap
|
||||
// handlers that may be waiting for them to fail.
|
||||
for (auto& [handlerTag, handler] : handlers) {
|
||||
if (handler.state != kBegan || isTap(handler)) {
|
||||
continue;
|
||||
}
|
||||
const float dx = pointer.x - handler.startX;
|
||||
const float dy = pointer.y - handler.startY;
|
||||
if (isLongPress(handler)) {
|
||||
const auto elapsed = pointer.timestamp - handler.startTime;
|
||||
const auto minDuration =
|
||||
number(handler.config, "minDurationMs", 500.0f);
|
||||
if (elapsed >= minDuration &&
|
||||
std::hypot(dx, dy) <= number(handler.config, "maxDist", 10.0f) &&
|
||||
!hasRecognizedWaitFor(handler) &&
|
||||
!blockedByActiveHandler(handlerTag)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
} else {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
}
|
||||
} else {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& [handlerTag, handler] : handlers) {
|
||||
if (handler.state != kBegan && handler.state != kActive)
|
||||
continue;
|
||||
if (pointer.phase == 4) {
|
||||
const float dx = pointer.x - handler.startX;
|
||||
const float dy = pointer.y - handler.startY;
|
||||
const auto elapsed = pointer.timestamp - handler.startTime;
|
||||
const auto minDuration = number(handler.config, "minDurationMs", 500.0f);
|
||||
if (gestureTracingEnabled() && handler.state == kBegan &&
|
||||
isLongPress(handler) && elapsed >= minDuration) {
|
||||
LOG(INFO) << "[gesture-handler] long-press threshold handler="
|
||||
<< handlerTag << " elapsed=" << elapsed
|
||||
<< " required=" << minDuration
|
||||
<< " distance=" << std::hypot(dx, dy);
|
||||
}
|
||||
if (handler.state == kBegan && isLongPress(handler) &&
|
||||
elapsed >= minDuration &&
|
||||
std::hypot(dx, dy) <= number(handler.config, "maxDist", 10.0f)) {
|
||||
if (hasRecognizedWaitFor(handler) ||
|
||||
blockedByActiveHandler(handlerTag)) {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
} else if (!hasUnresolvedWaitFor(handler)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
}
|
||||
}
|
||||
} else if (pointer.phase == 1) {
|
||||
const float dx = pointer.x - handler.startX;
|
||||
const float dy = pointer.y - handler.startY;
|
||||
if (handler.state == kBegan && isPan(handler) &&
|
||||
std::hypot(dx, dy) >= number(handler.config, "minDist", 3.0f)) {
|
||||
if (hasRecognizedWaitFor(handler) ||
|
||||
blockedByActiveHandler(handlerTag)) {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
} else if (!hasUnresolvedWaitFor(handler)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
}
|
||||
} else if (
|
||||
handler.state == kBegan && isLongPress(handler) &&
|
||||
pointer.timestamp - handler.startTime >=
|
||||
number(handler.config, "minDurationMs", 500.0f) &&
|
||||
std::hypot(dx, dy) <= number(handler.config, "maxDist", 10.0f)) {
|
||||
if (hasRecognizedWaitFor(handler) ||
|
||||
blockedByActiveHandler(handlerTag)) {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
} else if (!hasUnresolvedWaitFor(handler)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
}
|
||||
}
|
||||
if (handler.state == kActive &&
|
||||
(isPan(handler) || isLongPress(handler))) {
|
||||
appendEvent(
|
||||
events,
|
||||
handler,
|
||||
"topGestureHandlerEvent",
|
||||
payloadFor(
|
||||
handlerTag,
|
||||
handler,
|
||||
handler.state,
|
||||
kUndetermined,
|
||||
pointer,
|
||||
false));
|
||||
}
|
||||
} else if (pointer.phase == 2) {
|
||||
const float dx = pointer.x - handler.startX;
|
||||
const float dy = pointer.y - handler.startY;
|
||||
if (isTap(handler) && handler.state == kBegan &&
|
||||
pointer.timestamp - handler.startTime <=
|
||||
number(handler.config, "maxDurationMs", 500.0f) &&
|
||||
std::hypot(dx, dy) <= number(handler.config, "maxDist", 10.0f)) {
|
||||
if (hasRecognizedWaitFor(handler) ||
|
||||
blockedByActiveHandler(handlerTag)) {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
} else if (!hasUnresolvedWaitFor(handler)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
} else {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
}
|
||||
} else if (
|
||||
isLongPress(handler) && handler.state == kBegan &&
|
||||
pointer.timestamp - handler.startTime >=
|
||||
number(handler.config, "minDurationMs", 500.0f) &&
|
||||
std::hypot(dx, dy) <= number(handler.config, "maxDist", 10.0f)) {
|
||||
appendState(events, handlerTag, handler, kActive, pointer);
|
||||
} else if (handler.state == kBegan) {
|
||||
appendState(events, handlerTag, handler, kFailed, pointer);
|
||||
}
|
||||
if (handler.state == kActive) {
|
||||
if (isTap(handler) || isLongPress(handler)) {
|
||||
appendEvent(
|
||||
events,
|
||||
handler,
|
||||
"topGestureHandlerEvent",
|
||||
payloadFor(
|
||||
handlerTag,
|
||||
handler,
|
||||
handler.state,
|
||||
kUndetermined,
|
||||
pointer,
|
||||
false));
|
||||
}
|
||||
appendState(events, handlerTag, handler, kEnd, pointer);
|
||||
}
|
||||
handler.lastX = pointer.x;
|
||||
handler.lastY = pointer.y;
|
||||
handler.lastTime = pointer.timestamp;
|
||||
} else if (pointer.phase == 3) {
|
||||
appendState(
|
||||
events,
|
||||
handlerTag,
|
||||
handler,
|
||||
handler.state == kActive ? kCancelled : kFailed,
|
||||
pointer);
|
||||
}
|
||||
}
|
||||
if (gestureTracingEnabled() &&
|
||||
(pointer.phase != 4 || !events.viewEvents.empty() ||
|
||||
!events.deviceEvents.empty())) {
|
||||
LOG(INFO) << "[gesture-handler] recognized events="
|
||||
<< events.viewEvents.size() + events.deviceEvents.size();
|
||||
}
|
||||
return events;
|
||||
}
|
||||
|
||||
class GPUIGestureHandlerModule final
|
||||
: public facebook::react::NativeRNGestureHandlerModuleSpec<
|
||||
GPUIGestureHandlerModule> {
|
||||
public:
|
||||
explicit GPUIGestureHandlerModule(
|
||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker)
|
||||
: NativeRNGestureHandlerModuleSpec(std::move(jsInvoker)) {}
|
||||
|
||||
// The JS side queues operations and flushes them in order, so every method
|
||||
// has to observe the same order the JS thread produced.
|
||||
static constexpr rngpui::ModuleThread kThread = rngpui::ModuleThread::JS;
|
||||
|
||||
void createGestureHandler(
|
||||
Runtime&,
|
||||
std::string handlerName,
|
||||
double handlerTag,
|
||||
folly::dynamic config) {
|
||||
Handler handler;
|
||||
handler.name = std::move(handlerName);
|
||||
handler.config = configObject(std::move(config));
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
handlers.insert_or_assign(
|
||||
static_cast<int32_t>(handlerTag), std::move(handler));
|
||||
}
|
||||
|
||||
void attachGestureHandler(
|
||||
Runtime&,
|
||||
double handlerTag,
|
||||
double viewTag,
|
||||
double actionType) {
|
||||
const auto tag = static_cast<int32_t>(handlerTag);
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
auto found = handlers.find(tag);
|
||||
if (found != handlers.end()) {
|
||||
found->second.viewTag = static_cast<int32_t>(viewTag);
|
||||
found->second.actionType = static_cast<int32_t>(actionType);
|
||||
if (gestureTracingEnabled()) {
|
||||
LOG(INFO) << "[gesture-handler] attached handler=" << tag
|
||||
<< " view=" << found->second.viewTag
|
||||
<< " action=" << found->second.actionType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setGestureHandlerConfig(
|
||||
Runtime&,
|
||||
double handlerTag,
|
||||
folly::dynamic newConfig) {
|
||||
const auto tag = static_cast<int32_t>(handlerTag);
|
||||
auto config = configObject(std::move(newConfig));
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
auto found = handlers.find(tag);
|
||||
if (found != handlers.end())
|
||||
found->second.config = std::move(config);
|
||||
}
|
||||
|
||||
void updateGestureHandlerConfig(
|
||||
Runtime&,
|
||||
double handlerTag,
|
||||
folly::dynamic newConfig) {
|
||||
const auto tag = static_cast<int32_t>(handlerTag);
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
auto found = handlers.find(tag);
|
||||
if (found != handlers.end() && newConfig.isObject()) {
|
||||
for (const auto& [key, value] : newConfig.items())
|
||||
found->second.config[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
configureRelations(Runtime&, double handlerTag, folly::dynamic relations) {
|
||||
const auto tag = static_cast<int32_t>(handlerTag);
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
const auto found = handlers.find(tag);
|
||||
if (found != handlers.end()) {
|
||||
found->second.simultaneousHandlers =
|
||||
relationTags(relations, "simultaneousHandlers");
|
||||
found->second.waitFor = relationTags(relations, "waitFor");
|
||||
found->second.blocksHandlers = relationTags(relations, "blocksHandlers");
|
||||
if (gestureTracingEnabled()) {
|
||||
auto formatTags = [](const std::vector<int32_t>& tags) {
|
||||
std::string result;
|
||||
for (const auto value : tags) {
|
||||
if (!result.empty())
|
||||
result += ",";
|
||||
result += std::to_string(value);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
LOG(INFO) << "[gesture-handler] relations handler=" << tag
|
||||
<< " name=" << found->second.name << " simultaneous="
|
||||
<< formatTags(found->second.simultaneousHandlers)
|
||||
<< " waitFor=" << formatTags(found->second.waitFor)
|
||||
<< " blocks=" << formatTags(found->second.blocksHandlers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dropGestureHandler(Runtime&, double handlerTag) {
|
||||
std::lock_guard<std::mutex> lock(handlersMutex);
|
||||
handlers.erase(static_cast<int32_t>(handlerTag));
|
||||
}
|
||||
|
||||
void flushOperations(Runtime&) {}
|
||||
|
||||
bool installUIRuntimeBindings(Runtime&) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void emit(const std::string& name, folly::dynamic payload) {
|
||||
emitDeviceEvent(
|
||||
name,
|
||||
[payload = std::move(payload)](
|
||||
Runtime& runtime, std::vector<facebook::jsi::Value>& args) {
|
||||
args.emplace_back(facebook::jsi::valueFromDynamic(runtime, payload));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
std::weak_ptr<GPUIGestureHandlerModule> currentModule;
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<facebook::react::TurboModule>
|
||||
create_gpui_gesture_handler_module(
|
||||
const std::string& name,
|
||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
|
||||
if (name != GPUIGestureHandlerModule::kModuleName) {
|
||||
return nullptr;
|
||||
}
|
||||
rngpui::registerPointerObserver(
|
||||
[](const rngpui::extension::RawPointerEvent& pointer) {
|
||||
auto events = processPointer(pointer);
|
||||
if (auto module = currentModule.lock()) {
|
||||
for (auto& [name, payload] : events.deviceEvents) {
|
||||
module->emit(name, std::move(payload));
|
||||
}
|
||||
}
|
||||
return std::move(events.viewEvents);
|
||||
});
|
||||
if (gestureTracingEnabled()) {
|
||||
LOG(INFO) << "[gesture-handler] native pointer processor installed";
|
||||
}
|
||||
auto module =
|
||||
std::make_shared<GPUIGestureHandlerModule>(std::move(jsInvoker));
|
||||
currentModule = module;
|
||||
return module;
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
{
|
||||
"name": "@react-native-gpui/gesture-handler",
|
||||
"version": "3.3.0-alpha.1",
|
||||
"description": "Adapter for react-native-gesture-handler (MIT): recognizes gestures from the mouse, trackpad and touch screen",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/streetwriters/react-native-gpui-modules.git",
|
||||
"directory": "modules/gesture-handler"
|
||||
},
|
||||
"homepage": "https://github.com/streetwriters/react-native-gpui-modules/tree/main/modules/gesture-handler#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/streetwriters/react-native-gpui-modules/issues"
|
||||
},
|
||||
"files": [
|
||||
"desktop",
|
||||
"react-native.config.js",
|
||||
"src",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md",
|
||||
"!**/*.test.js",
|
||||
"!**/*.test.mjs",
|
||||
"!**/*.test.ts",
|
||||
"!**/*.test.cjs"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"react-native": "0.87.x",
|
||||
"react-native-gesture-handler": "3.3.x",
|
||||
"react-native-reanimated": "4.7.x",
|
||||
"react-native-worklets": "0.13.x"
|
||||
},
|
||||
"codegenConfig": {
|
||||
"name": "GPUIGestureHandlerSpec",
|
||||
"type": "modules",
|
||||
"jsSrcsDir": "src"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "latest"
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
module.exports = {
|
||||
dependency: {
|
||||
platforms: {
|
||||
desktop: {
|
||||
cmake: 'desktop',
|
||||
cppProvider: 'create_gpui_gesture_handler_module',
|
||||
componentCodegenPackage: 'react-native-gesture-handler',
|
||||
componentDescriptors: [
|
||||
'RNGestureHandlerButtonComponentDescriptor',
|
||||
'RNGestureHandlerRootViewComponentDescriptor',
|
||||
],
|
||||
componentHeaders: [
|
||||
'react/renderer/components/rngesturehandler_codegen/ComponentDescriptors.h',
|
||||
],
|
||||
// Metro: replace these files of installed packages for gpui.
|
||||
sourceOverrides: {
|
||||
'react-native-gesture-handler/src/handlers/customDirectEventTypes.ts':
|
||||
'./src/customDirectEventTypes.desktop.ts',
|
||||
'react-native-gesture-handler/src/v3/components/GestureButtons.tsx':
|
||||
'./src/GestureButtons.desktop.tsx',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
-71
@@ -1,71 +0,0 @@
|
||||
import React from 'react';
|
||||
import './customDirectEventTypes.desktop';
|
||||
import {
|
||||
Pressable,
|
||||
type ColorValue,
|
||||
type PressableProps,
|
||||
type StyleProp,
|
||||
type ViewStyle,
|
||||
} from 'react-native';
|
||||
|
||||
type DesktopButtonProps = Omit<PressableProps, 'style'> & {
|
||||
style?: StyleProp<ViewStyle>;
|
||||
activeOpacity?: number;
|
||||
underlayColor?: ColorValue;
|
||||
onActiveStateChange?: (active: boolean) => void;
|
||||
pressedStyle?: ViewStyle;
|
||||
borderless?: boolean;
|
||||
rippleColor?: ColorValue | number | null;
|
||||
rippleRadius?: number | null;
|
||||
foreground?: boolean;
|
||||
exclusive?: boolean;
|
||||
cancelable?: boolean;
|
||||
};
|
||||
|
||||
function DesktopButton({
|
||||
activeOpacity = 0.75,
|
||||
onActiveStateChange,
|
||||
onPressIn,
|
||||
onPressOut,
|
||||
pressedStyle,
|
||||
style,
|
||||
...props
|
||||
}: DesktopButtonProps) {
|
||||
return (
|
||||
<Pressable
|
||||
{...props}
|
||||
style={({ pressed }) => [
|
||||
style,
|
||||
pressed ? [{ opacity: activeOpacity }, pressedStyle] : undefined,
|
||||
]}
|
||||
onPressIn={event => {
|
||||
onActiveStateChange?.(true);
|
||||
onPressIn?.(event);
|
||||
}}
|
||||
onPressOut={event => {
|
||||
onActiveStateChange?.(false);
|
||||
onPressOut?.(event);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const RectButton = ({ underlayColor, ...props }: DesktopButtonProps) => (
|
||||
<DesktopButton
|
||||
{...props}
|
||||
pressedStyle={
|
||||
underlayColor == null ? undefined : { backgroundColor: underlayColor }
|
||||
}
|
||||
/>
|
||||
);
|
||||
|
||||
export const BorderlessButton = (props: DesktopButtonProps) => (
|
||||
<DesktopButton {...props} />
|
||||
);
|
||||
|
||||
export const RawButton = DesktopButton;
|
||||
export const BaseButton = DesktopButton;
|
||||
export const LegacyRectButton = RectButton;
|
||||
export const LegacyBorderlessButton = BorderlessButton;
|
||||
export const LegacyRawButton = RawButton;
|
||||
export const LegacyBaseButton = BaseButton;
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
import type { CodegenTypes, TurboModule } from 'react-native';
|
||||
import { TurboModuleRegistry } from 'react-native';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
createGestureHandler: (
|
||||
handlerName: string,
|
||||
handlerTag: CodegenTypes.Double,
|
||||
config: Object,
|
||||
) => void;
|
||||
attachGestureHandler: (
|
||||
handlerTag: CodegenTypes.Double,
|
||||
viewTag: CodegenTypes.Double,
|
||||
actionType: CodegenTypes.Double,
|
||||
) => void;
|
||||
setGestureHandlerConfig: (
|
||||
handlerTag: CodegenTypes.Double,
|
||||
newConfig: Object,
|
||||
) => void;
|
||||
updateGestureHandlerConfig: (
|
||||
handlerTag: CodegenTypes.Double,
|
||||
newConfig: Object,
|
||||
) => void;
|
||||
configureRelations: (
|
||||
handlerTag: CodegenTypes.Double,
|
||||
relations: Object,
|
||||
) => void;
|
||||
dropGestureHandler: (handlerTag: CodegenTypes.Double) => void;
|
||||
flushOperations: () => void;
|
||||
installUIRuntimeBindings: () => boolean;
|
||||
}
|
||||
|
||||
export default TurboModuleRegistry.getEnforcing<Spec>('RNGestureHandlerModule');
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
// RNGH's legacy handler path registers only the continuous event in its JS
|
||||
// module. GPUI dispatches the codegen state-change event directly, so expose
|
||||
// both direct-event registrations from this platform package.
|
||||
import { customDirectEventTypes } from 'react-native/Libraries/Renderer/shims/ReactNativeViewConfigRegistry';
|
||||
|
||||
customDirectEventTypes.topGestureHandlerEvent = {
|
||||
registrationName: 'onGestureHandlerEvent',
|
||||
};
|
||||
customDirectEventTypes.topGestureHandlerStateChange = {
|
||||
registrationName: 'onGestureHandlerStateChange',
|
||||
};
|
||||
|
||||
export { customDirectEventTypes };
|
||||
@@ -1,17 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/reanimated` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Builds with MSVC 14.51 (Visual Studio 18): the members of the resolvable transform interpolator are defined in the class body, since that compiler cannot match their out-of-line definitions.
|
||||
|
||||
## 4.7.0-alpha.1 - 2026-11-12
|
||||
|
||||
- CSS transitions and animations now run: the registries' updates are applied after every frame, as the display link does on iOS, where before nothing applied them and a transition or keyframe animation never changed what was painted. Needs a prebuilt runtime built with the change.
|
||||
|
||||
## 4.7.0-alpha.0 - 2026-11-01
|
||||
|
||||
- First release, for `react-native-reanimated` 4.7.x with Worklets 0.13.x.
|
||||
- Animations, layout animations and CSS animations on desktop.
|
||||
- Works with the npm release of `react-native-reanimated`: the C++ fixes it needs for MSVC and Linux are applied to a copy in the build directory. UI props update synchronously on Linux, as on iOS.
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,27 +0,0 @@
|
||||
# @react-native-gpui/reanimated
|
||||
|
||||
Adapter for [`react-native-reanimated`](https://github.com/software-mansion/react-native-reanimated) (MIT): runs animations, layout animations and CSS animations on the desktop renderer.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
Install it next to `react-native-reanimated`:
|
||||
|
||||
```sh
|
||||
npm install react-native-reanimated@4.7.x @react-native-gpui/reanimated
|
||||
```
|
||||
|
||||
Reanimated needs Worklets, so install `react-native-worklets@0.13.x` and [`@react-native-gpui/worklets`](https://www.npmjs.com/package/@react-native-gpui/worklets) too.
|
||||
|
||||
Install the regular npm release of `react-native-reanimated`. Its C++ needs small fixes to build with MSVC and on Linux; this package applies them to a copy in the desktop build directory and never changes `node_modules`.
|
||||
|
||||
Keep using `react-native-reanimated` as you do on iOS and Android; its JavaScript API is unchanged. Autolinking builds the desktop side when the app is built.
|
||||
|
||||
## Documentation
|
||||
|
||||
[Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,116 +0,0 @@
|
||||
if(NOT RN_GPUI_PROJECT_ROOT)
|
||||
message(FATAL_ERROR
|
||||
"@react-native-gpui/reanimated was added by the platform; RN_GPUI_PROJECT_ROOT must be set by its build.rs")
|
||||
endif()
|
||||
set(_gpui_app_root "${RN_GPUI_PROJECT_ROOT}")
|
||||
find_program(_gpui_node node REQUIRED)
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" -e "process.stdout.write(require.resolve('react-native-reanimated/package.json', {paths: [process.argv[1]]}))" "${_gpui_app_root}"
|
||||
RESULT_VARIABLE _reanimated_resolve_result
|
||||
OUTPUT_VARIABLE _reanimated_package_json
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT _reanimated_resolve_result EQUAL 0 OR NOT EXISTS "${_reanimated_package_json}")
|
||||
message(FATAL_ERROR "react-native-reanimated could not be resolved from ${_gpui_app_root}")
|
||||
endif()
|
||||
get_filename_component(_reanimated_root "${_reanimated_package_json}" DIRECTORY)
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" -e "process.stdout.write(require.resolve('react-native/package.json', {paths: [process.argv[1]]}))" "${_gpui_app_root}"
|
||||
RESULT_VARIABLE _react_native_resolve_result
|
||||
OUTPUT_VARIABLE _react_native_package_json
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT _react_native_resolve_result EQUAL 0 OR NOT EXISTS "${_react_native_package_json}")
|
||||
message(FATAL_ERROR "react-native could not be resolved from ${_gpui_app_root}")
|
||||
endif()
|
||||
get_filename_component(_react_native_root "${_react_native_package_json}" DIRECTORY)
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" -e "process.stdout.write(require.resolve('react-native-worklets/package.json', {paths: [process.argv[1]]}))" "${_gpui_app_root}"
|
||||
RESULT_VARIABLE _worklets_resolve_result
|
||||
OUTPUT_VARIABLE _worklets_package_json
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT _worklets_resolve_result EQUAL 0 OR NOT EXISTS "${_worklets_package_json}")
|
||||
message(FATAL_ERROR "react-native-worklets could not be resolved from ${_gpui_app_root}")
|
||||
endif()
|
||||
get_filename_component(_worklets_root "${_worklets_package_json}" DIRECTORY)
|
||||
set(_reanimated_common "${_reanimated_root}/Common/cpp")
|
||||
set(_worklets_common "${_worklets_root}/Common/cpp")
|
||||
if(NOT EXISTS "${_reanimated_common}/reanimated/NativeModules/ReanimatedModuleProxy.h")
|
||||
message(FATAL_ERROR "react-native-reanimated 4.7.x was not found in ${_gpui_app_root} or any ancestor node_modules directory")
|
||||
endif()
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" -e "process.stdout.write(require.resolve('@react-native-gpui/worklets/package.json', {paths: [process.argv[1]]}))" "${_gpui_app_root}"
|
||||
RESULT_VARIABLE _gpui_worklets_resolve_result
|
||||
OUTPUT_VARIABLE _gpui_worklets_package_json
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT _gpui_worklets_resolve_result EQUAL 0 OR NOT EXISTS "${_gpui_worklets_package_json}")
|
||||
message(FATAL_ERROR "@react-native-gpui/worklets could not be resolved from ${_gpui_app_root}")
|
||||
endif()
|
||||
get_filename_component(_gpui_worklets_root "${_gpui_worklets_package_json}" DIRECTORY)
|
||||
if(NOT EXISTS "${_worklets_common}/worklets/Compat/StableApi.h")
|
||||
message(FATAL_ERROR "react-native-worklets 0.13.x is required by GPUI Reanimated")
|
||||
endif()
|
||||
|
||||
# Upstream's C++ does not build with MSVC or on Linux as published; compile
|
||||
# patched copies in the build tree and leave node_modules untouched. Worklets
|
||||
# is patched here too because Reanimated includes its headers.
|
||||
set(_reanimated_patch "${CMAKE_CURRENT_LIST_DIR}/patches/react-native-reanimated-4.7.1.patch")
|
||||
set(_worklets_patch "${_gpui_worklets_root}/desktop/patches/react-native-worklets-0.13.0.patch")
|
||||
foreach(_patched reanimated worklets)
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" "${_gpui_worklets_root}/desktop/patch-sources.mjs"
|
||||
"${_${_patched}_root}" "${_${_patched}_patch}"
|
||||
"${CMAKE_BINARY_DIR}/rn-gpui-patched/react-native-${_patched}"
|
||||
RESULT_VARIABLE _patch_result
|
||||
OUTPUT_VARIABLE _${_patched}_common
|
||||
ERROR_VARIABLE _patch_error
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
if(NOT _patch_result EQUAL 0)
|
||||
message(FATAL_ERROR "${_patch_error}")
|
||||
endif()
|
||||
endforeach()
|
||||
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
|
||||
"${_reanimated_patch}" "${_worklets_patch}"
|
||||
"${_reanimated_package_json}" "${_worklets_package_json}")
|
||||
|
||||
# Match the static feature-flag definition normally supplied by Reanimated's
|
||||
# CocoaPods/Gradle builds. Without this, FeatureFlags.h silently treats every
|
||||
# flag as false, including defaults which change how animated props are
|
||||
# committed through Fabric.
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" -e
|
||||
"const fs=require('fs'); const flags=JSON.parse(fs.readFileSync(process.argv[1],'utf8')); const app=JSON.parse(fs.readFileSync(process.argv[2],'utf8')); Object.assign(flags, app.reanimated?.staticFeatureFlags ?? {}); if(process.platform==='darwin' && !Object.hasOwn(app.reanimated?.staticFeatureFlags ?? {}, 'IOS_SYNCHRONOUSLY_UPDATE_UI_PROPS')) flags.IOS_SYNCHRONOUSLY_UPDATE_UI_PROPS=true; if(process.platform==='linux' && !Object.hasOwn(app.reanimated?.staticFeatureFlags ?? {}, 'LINUX_SYNCHRONOUSLY_UPDATE_UI_PROPS')) flags.LINUX_SYNCHRONOUSLY_UPDATE_UI_PROPS=true; process.stdout.write(Object.entries(flags).map(([key,value])=>'['+key+':'+value+']').join(''));"
|
||||
"${_reanimated_root}/src/featureFlags/staticFlags.json"
|
||||
"${_gpui_app_root}/package.json"
|
||||
RESULT_VARIABLE _reanimated_flags_result
|
||||
OUTPUT_VARIABLE _reanimated_feature_flags
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||
)
|
||||
if(NOT _reanimated_flags_result EQUAL 0 OR _reanimated_feature_flags STREQUAL "")
|
||||
message(FATAL_ERROR "Failed to load Reanimated static feature flags for GPUI")
|
||||
endif()
|
||||
|
||||
execute_process(
|
||||
COMMAND "${_gpui_node}" "${_react_native_root}/scripts/generate-codegen-artifacts.js"
|
||||
--path "${_gpui_app_root}"
|
||||
--targetPlatform ios
|
||||
--outputPath "${_gpui_app_root}/desktop/build/codegen"
|
||||
--source app
|
||||
RESULT_VARIABLE _reanimated_codegen_result
|
||||
)
|
||||
if(NOT _reanimated_codegen_result EQUAL 0)
|
||||
message(FATAL_ERROR "React Native codegen failed while configuring GPUI Reanimated")
|
||||
endif()
|
||||
set(_rn_codegen "${_gpui_app_root}/desktop/build/codegen/build/generated/ios/ReactCodegen")
|
||||
|
||||
file(GLOB_RECURSE _reanimated_sources CONFIGURE_DEPENDS "${_reanimated_common}/reanimated/*.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_SOURCES
|
||||
${_reanimated_sources}
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GPUIReanimatedModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_INCLUDE_DIRS
|
||||
"${_reanimated_common}"
|
||||
"${_reanimated_root}"
|
||||
"${_worklets_common}"
|
||||
"${_rn_codegen}"
|
||||
"${_gpui_worklets_root}/desktop/cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_DEFINITIONS
|
||||
"REANIMATED_VERSION=4.7.1"
|
||||
"REANIMATED_FEATURE_FLAGS=\"${_reanimated_feature_flags}\"")
|
||||
-169
@@ -1,169 +0,0 @@
|
||||
#include "GPUIWorkletsModule.h"
|
||||
#include "GpuiHost.h"
|
||||
|
||||
#include <GPUIReanimatedSpec.h>
|
||||
#include <reanimated/NativeModules/ReanimatedModuleProxy.h>
|
||||
#include <reanimated/RuntimeDecorators/RNRuntimeDecorator.h>
|
||||
#include <reanimated/Tools/PlatformDepMethodsHolder.h>
|
||||
#include <worklets/Compat/StableApi.h>
|
||||
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
bool animationTracingEnabled() {
|
||||
static const bool enabled = std::getenv("RN_GPUI_DEBUG_ANIMATION") != nullptr;
|
||||
return enabled;
|
||||
}
|
||||
|
||||
using facebook::jsi::Runtime;
|
||||
using reanimated::PlatformDepMethodsHolder;
|
||||
using reanimated::ReanimatedModuleProxy;
|
||||
|
||||
PlatformDepMethodsHolder platformMethods(
|
||||
const std::shared_ptr<std::weak_ptr<ReanimatedModuleProxy>>& proxyHolder) {
|
||||
PlatformDepMethodsHolder methods;
|
||||
methods.requestRender = [](std::function<void(const double)> callback) {
|
||||
static std::atomic<uint64_t> requests{0};
|
||||
const auto count = ++requests;
|
||||
if (animationTracingEnabled() && (count <= 5 || count % 60 == 0)) {
|
||||
LOG(INFO) << "[gpui-reanimated] requestRender count=" << count;
|
||||
}
|
||||
auto module = gpui_worklets::currentModule();
|
||||
if (module)
|
||||
module->requestAnimationFrame(std::move(callback));
|
||||
};
|
||||
methods.synchronouslyUpdateUIPropsFunction = [](const int tag,
|
||||
const folly::dynamic& props) {
|
||||
rngpui::gpui_synchronously_update_props(tag, props);
|
||||
};
|
||||
methods.getAnimationTimestamp = [] {
|
||||
return std::chrono::duration<double, std::milli>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
};
|
||||
methods.registerSensor =
|
||||
[](int, int, int, std::function<void(double[], int)>) { return 0; };
|
||||
methods.unregisterSensor = [](int) {};
|
||||
methods.setGestureStateFunction = [](int, int) {};
|
||||
methods.maybeFlushUIUpdatesQueueFunction = [proxyHolder] {
|
||||
if (auto proxy = proxyHolder->lock()) {
|
||||
proxy->performOperations();
|
||||
}
|
||||
};
|
||||
methods.attachPseudoSelector = [](facebook::react::Tag,
|
||||
reanimated::PseudoSelector,
|
||||
std::function<void(bool)> callback) {
|
||||
if (callback)
|
||||
callback(false);
|
||||
};
|
||||
methods.detachPseudoSelector = [](facebook::react::Tag,
|
||||
reanimated::PseudoSelector) {};
|
||||
#if defined(__APPLE__)
|
||||
methods.forceScreenSnapshotFunction = [](facebook::react::Tag) {
|
||||
return false;
|
||||
};
|
||||
methods.readMountedViewPropsFunction =
|
||||
[](facebook::react::Tag) -> facebook::react::Props::Shared {
|
||||
return nullptr;
|
||||
};
|
||||
#endif
|
||||
return methods;
|
||||
}
|
||||
|
||||
class Module final
|
||||
: public facebook::react::NativeReanimatedModuleSpec<Module> {
|
||||
public:
|
||||
explicit Module(std::shared_ptr<facebook::react::CallInvoker> jsInvoker)
|
||||
: NativeReanimatedModuleSpec(std::move(jsInvoker)) {}
|
||||
|
||||
bool installTurboModule(Runtime& runtime) {
|
||||
if (proxy_)
|
||||
return true;
|
||||
auto worklets = gpui_worklets::currentModule();
|
||||
auto uiManager = rngpui::get_gpui_ui_manager();
|
||||
if (!worklets || !worklets->uiRuntime() || !worklets->uiScheduler() ||
|
||||
!uiManager) {
|
||||
throw facebook::jsi::JSError(
|
||||
runtime, "GPUI Reanimated requires initialized Worklets and Fabric");
|
||||
}
|
||||
|
||||
// The platform flush callback is constructed before the proxy exists.
|
||||
// Retain only a weak reference so the decorated UI runtime cannot keep
|
||||
// the module alive after React Native tears it down.
|
||||
auto proxyHolder = std::make_shared<std::weak_ptr<ReanimatedModuleProxy>>();
|
||||
auto methods = platformMethods(proxyHolder);
|
||||
proxy_ = std::make_shared<ReanimatedModuleProxy>(
|
||||
worklets->uiRuntime(),
|
||||
worklets->uiScheduler(),
|
||||
runtime,
|
||||
jsInvoker_,
|
||||
methods,
|
||||
false);
|
||||
*proxyHolder = proxy_;
|
||||
// The platform runs the registries' pending updates after each frame's
|
||||
// animation callbacks, which is what applies a CSS transition or animation.
|
||||
worklets->setAfterFrame(
|
||||
[weak = std::weak_ptr<ReanimatedModuleProxy>(proxy_)] {
|
||||
if (auto proxy = weak.lock()) {
|
||||
proxy->performOperations();
|
||||
}
|
||||
});
|
||||
proxy_->init(methods);
|
||||
proxy_->initializeFabric(uiManager);
|
||||
LOG(INFO) << "[gpui-reanimated] module proxy initialized";
|
||||
auto& uiRuntime =
|
||||
worklets::getJSIRuntimeFromWorkletRuntime(worklets->uiRuntime());
|
||||
reanimated::RNRuntimeDecorator::decorate(runtime, uiRuntime, proxy_);
|
||||
std::weak_ptr<ReanimatedModuleProxy> weakProxy = proxy_;
|
||||
eventListener_ = std::make_shared<const facebook::react::EventListener>(
|
||||
[weakProxy](const facebook::react::RawEvent& event) {
|
||||
auto proxy = weakProxy.lock();
|
||||
if (!proxy)
|
||||
return false;
|
||||
const auto timestamp =
|
||||
std::chrono::duration<double, std::milli>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
static std::atomic<uint64_t> events{0};
|
||||
const auto count = ++events;
|
||||
if (animationTracingEnabled() &&
|
||||
event.type.find("GestureHandler") != std::string::npos) {
|
||||
const auto* target = event.eventTarget.get();
|
||||
const auto tag = target ? target->getTag() : -1;
|
||||
auto eventName = event.type;
|
||||
if (eventName.rfind("top", 0) == 0) {
|
||||
eventName = "on" + eventName.substr(3);
|
||||
}
|
||||
LOG(INFO) << "[gpui-reanimated] gesture lookup type=" << eventName
|
||||
<< " tag=" << tag << " waiting="
|
||||
<< proxy->isAnyHandlerWaitingForEvent(eventName, tag);
|
||||
}
|
||||
if (animationTracingEnabled() &&
|
||||
(count <= 10 ||
|
||||
event.type.find("GestureHandler") != std::string::npos)) {
|
||||
LOG(INFO) << "[gpui-reanimated] raw event count=" << count
|
||||
<< " type=" << event.type;
|
||||
}
|
||||
return proxy->handleRawEvent(event, timestamp);
|
||||
});
|
||||
rngpui::register_gpui_event_listener(eventListener_);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::shared_ptr<ReanimatedModuleProxy> proxy_;
|
||||
std::shared_ptr<const facebook::react::EventListener> eventListener_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<facebook::react::TurboModule> create_gpui_reanimated_module(
|
||||
const std::string& name,
|
||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
|
||||
return rngpui::provideTurboModule<Module>(name, std::move(jsInvoker));
|
||||
}
|
||||
Vendored
-530
@@ -1,530 +0,0 @@
|
||||
--- a/reanimated/CSS/common/filters/FilterOp.cpp
|
||||
+++ b/reanimated/CSS/common/filters/FilterOp.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/common/filters/FilterOp.h>
|
||||
|
||||
#include <array>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
--- a/reanimated/CSS/common/transforms/TransformMatrix2D.cpp
|
||||
+++ b/reanimated/CSS/common/transforms/TransformMatrix2D.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/common/transforms/TransformMatrix2D.h>
|
||||
|
||||
#include <cmath>
|
||||
+#include <stdexcept>
|
||||
|
||||
namespace reanimated::css {
|
||||
|
||||
--- a/reanimated/CSS/common/transforms/TransformMatrix3D.cpp
|
||||
+++ b/reanimated/CSS/common/transforms/TransformMatrix3D.cpp
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <reanimated/CSS/common/transforms/TransformMatrix3D.h>
|
||||
#include <reanimated/CSS/interpolation/transforms/TransformOperation.h>
|
||||
|
||||
+#include <cmath>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/common/transforms/TransformOp.cpp
|
||||
+++ b/reanimated/CSS/common/transforms/TransformOp.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/common/transforms/TransformOp.h>
|
||||
|
||||
#include <array>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
--- a/reanimated/CSS/common/transforms/vectors.cpp
|
||||
+++ b/reanimated/CSS/common/transforms/vectors.cpp
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <reanimated/CSS/common/transforms/vectors.h>
|
||||
+#include <cmath>
|
||||
|
||||
namespace reanimated::css {
|
||||
|
||||
--- a/reanimated/CSS/common/values/CSSAngle.cpp
|
||||
+++ b/reanimated/CSS/common/values/CSSAngle.cpp
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <iomanip>
|
||||
#include <regex>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
--- a/reanimated/CSS/common/values/CSSColor.cpp
|
||||
+++ b/reanimated/CSS/common/values/CSSColor.cpp
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <utility>
|
||||
|
||||
+#include <cmath>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/common/values/CSSLength.cpp
|
||||
+++ b/reanimated/CSS/common/values/CSSLength.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/common/values/CSSLength.h>
|
||||
#include <reanimated/CSS/misc/ViewStylesRepository.h>
|
||||
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/common/values/CSSValueVariant.cpp
|
||||
+++ b/reanimated/CSS/common/values/CSSValueVariant.cpp
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <reanimated/CSS/svg/values/SVGStrokeDashArray.h>
|
||||
#include <reanimated/Compat/WorkletsApi.h>
|
||||
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
--- a/reanimated/CSS/configs/CSSAnimationConfig.cpp
|
||||
+++ b/reanimated/CSS/configs/CSSAnimationConfig.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/configs/CSSAnimationConfig.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
--- a/reanimated/CSS/easing/EasingFunctions.cpp
|
||||
+++ b/reanimated/CSS/easing/EasingFunctions.cpp
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <reanimated/CSS/easing/EasingFunctions.h>
|
||||
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
--- a/reanimated/CSS/interpolation/InterpolatorFactory.cpp
|
||||
+++ b/reanimated/CSS/interpolation/InterpolatorFactory.cpp
|
||||
@@ -27,7 +27,7 @@ class RecordInterpolatorFactory : public PropertyInterpolatorFactory {
|
||||
// Helper private type just for a default value
|
||||
struct EmptyObjectValue : public CSSValue {
|
||||
folly::dynamic toDynamic() const override {
|
||||
- return folly::dynamic::object;
|
||||
+ return folly::dynamic::object();
|
||||
}
|
||||
|
||||
std::string toString() const override {
|
||||
@@ -53,7 +53,7 @@ class ArrayLikeInterpolatorFactory : public PropertyInterpolatorFactory {
|
||||
// Helper private type just for a default value
|
||||
struct EmptyArrayValue : public CSSValue {
|
||||
folly::dynamic toDynamic() const override {
|
||||
- return folly::dynamic::array;
|
||||
+ return folly::dynamic::array();
|
||||
}
|
||||
|
||||
std::string toString() const override {
|
||||
--- a/reanimated/CSS/interpolation/InterpolatorFactory.h
|
||||
+++ b/reanimated/CSS/interpolation/InterpolatorFactory.h
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <reanimated/CSS/interpolation/values/SimpleValueInterpolator.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
--- a/reanimated/CSS/interpolation/PropertyInterpolator.cpp
|
||||
+++ b/reanimated/CSS/interpolation/PropertyInterpolator.cpp
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <reanimated/Compat/WorkletsApi.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
--- a/reanimated/CSS/interpolation/filters/FilterOperation.cpp
|
||||
+++ b/reanimated/CSS/interpolation/filters/FilterOperation.cpp
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <reanimated/CSS/interpolation/filters/operations/saturate.h>
|
||||
#include <reanimated/CSS/interpolation/filters/operations/sepia.h>
|
||||
|
||||
+#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/interpolation/transforms/TransformOperation.cpp
|
||||
+++ b/reanimated/CSS/interpolation/transforms/TransformOperation.cpp
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <reanimated/CSS/interpolation/transforms/operations/skew.h>
|
||||
#include <reanimated/CSS/interpolation/transforms/operations/translate.h>
|
||||
|
||||
+#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.cpp
|
||||
+++ b/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.cpp
|
||||
@@ -7,22 +7,11 @@
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
+#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace reanimated::css {
|
||||
|
||||
-// Base implementation for simple operations
|
||||
-template <typename TOperation>
|
||||
-std::unique_ptr<StyleOperation> TransformOperationInterpolator<TOperation>::interpolate(
|
||||
- double progress,
|
||||
- const std::shared_ptr<StyleOperation> &from,
|
||||
- const std::shared_ptr<StyleOperation> &to,
|
||||
- const StyleOperationsInterpolationContext & /* context */) const {
|
||||
- const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
|
||||
- const auto &toOp = *std::static_pointer_cast<TOperation>(to);
|
||||
- return std::make_unique<TOperation>(fromOp.value.interpolate(progress, toOp.value));
|
||||
-}
|
||||
-
|
||||
// Specialization for PerspectiveOperation
|
||||
TransformOperationInterpolator<PerspectiveOperation>::TransformOperationInterpolator(
|
||||
const std::shared_ptr<PerspectiveOperation> &defaultOperation)
|
||||
@@ -148,52 +137,6 @@ template TransformMatrix3D TransformOperationInterpolator<MatrixOperation>::inte
|
||||
const TransformMatrix::Shared &,
|
||||
const TransformMatrix::Shared &) const;
|
||||
|
||||
-// Specialization for resolvable operations
|
||||
-template <ResolvableOp TOperation>
|
||||
-TransformOperationInterpolator<TOperation>::TransformOperationInterpolator(
|
||||
- const std::shared_ptr<TOperation> &defaultOperation,
|
||||
- ResolvableValueInterpolatorConfig config)
|
||||
- : StyleOperationInterpolator(defaultOperation), config_(std::move(config)) {}
|
||||
-
|
||||
-template <ResolvableOp TOperation>
|
||||
-std::unique_ptr<StyleOperation> TransformOperationInterpolator<TOperation>::interpolate(
|
||||
- double progress,
|
||||
- const std::shared_ptr<StyleOperation> &from,
|
||||
- const std::shared_ptr<StyleOperation> &to,
|
||||
- const StyleOperationsInterpolationContext &context) const {
|
||||
- const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
|
||||
- const auto &toOp = *std::static_pointer_cast<TOperation>(to);
|
||||
-
|
||||
- return std::make_unique<TOperation>(fromOp.value.interpolate(progress, toOp.value, getRelativeValueContext(context)));
|
||||
-}
|
||||
-
|
||||
-template <ResolvableOp TOperation>
|
||||
-std::shared_ptr<StyleOperation> TransformOperationInterpolator<TOperation>::resolveOperation(
|
||||
- const std::shared_ptr<StyleOperation> &operation,
|
||||
- const StyleOperationsInterpolationContext &context) const {
|
||||
- const auto &resolvableOp = std::static_pointer_cast<TOperation>(operation);
|
||||
- const auto &resolved = resolvableOp->value.resolve(getRelativeValueContext(context));
|
||||
-
|
||||
- if (!resolved.has_value()) {
|
||||
- throw std::invalid_argument(
|
||||
- "[Reanimated] Cannot resolve resolvable operation: " + operation->getOperationName() +
|
||||
- " for node with tag: " + std::to_string(context.node->getTag()));
|
||||
- }
|
||||
-
|
||||
- return std::make_shared<TOperation>(resolved.value());
|
||||
-}
|
||||
-
|
||||
-template <ResolvableOp TOperation>
|
||||
-RelativeValueInterpolationContext TransformOperationInterpolator<TOperation>::getRelativeValueContext(
|
||||
- const StyleOperationsInterpolationContext &context) const {
|
||||
- return RelativeValueInterpolationContext{
|
||||
- .node = context.node,
|
||||
- .fallbackInterpolateThreshold = context.fallbackInterpolateThreshold,
|
||||
- .viewStylesRepository = context.viewStylesRepository,
|
||||
- .relativeProperty = config_.relativeProperty,
|
||||
- .relativeTo = config_.relativeTo};
|
||||
-}
|
||||
-
|
||||
// Rotate operations
|
||||
template class TransformOperationInterpolator<RotateOperation>;
|
||||
template class TransformOperationInterpolator<RotateXOperation>;
|
||||
--- a/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.h
|
||||
+++ b/reanimated/CSS/interpolation/transforms/TransformOperationInterpolator.h
|
||||
@@ -6,6 +6,9 @@
|
||||
#include <reanimated/CSS/interpolation/transforms/operations/perspective.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
+#include <string>
|
||||
+#include <utility>
|
||||
|
||||
namespace reanimated::css {
|
||||
|
||||
@@ -15,11 +18,18 @@ class TransformOperationInterpolator : public StyleOperationInterpolator {
|
||||
public:
|
||||
using StyleOperationInterpolator::StyleOperationInterpolator;
|
||||
|
||||
+ // Defined in the class body: MSVC rejects an out-of-line definition of the
|
||||
+ // primary template member next to the constrained partial specialization
|
||||
+ // below (C3855).
|
||||
std::unique_ptr<StyleOperation> interpolate(
|
||||
double progress,
|
||||
const std::shared_ptr<StyleOperation> &from,
|
||||
const std::shared_ptr<StyleOperation> &to,
|
||||
- const StyleOperationsInterpolationContext &context) const override;
|
||||
+ const StyleOperationsInterpolationContext & /* context */) const override {
|
||||
+ const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
|
||||
+ const auto &toOp = *std::static_pointer_cast<TOperation>(to);
|
||||
+ return std::make_unique<TOperation>(fromOp.value.interpolate(progress, toOp.value));
|
||||
+ }
|
||||
};
|
||||
|
||||
// Specialization for PerspectiveOperation
|
||||
@@ -59,27 +69,54 @@ class TransformOperationInterpolator<MatrixOperation> : public StyleOperationInt
|
||||
};
|
||||
|
||||
// Specialization for resolvable operations
|
||||
+// Members are defined in the class body: MSVC 14.51 cannot match out-of-line
|
||||
+// definitions of a constrained partial specialization's members (C2244, C3855).
|
||||
template <ResolvableOp TOperation>
|
||||
class TransformOperationInterpolator<TOperation> : public StyleOperationInterpolator {
|
||||
public:
|
||||
TransformOperationInterpolator(
|
||||
const std::shared_ptr<TOperation> &defaultOperation,
|
||||
- ResolvableValueInterpolatorConfig config);
|
||||
+ ResolvableValueInterpolatorConfig config)
|
||||
+ : StyleOperationInterpolator(defaultOperation), config_(std::move(config)) {}
|
||||
|
||||
std::unique_ptr<StyleOperation> interpolate(
|
||||
double progress,
|
||||
const std::shared_ptr<StyleOperation> &from,
|
||||
const std::shared_ptr<StyleOperation> &to,
|
||||
- const StyleOperationsInterpolationContext &context) const override;
|
||||
+ const StyleOperationsInterpolationContext &context) const override {
|
||||
+ const auto &fromOp = *std::static_pointer_cast<TOperation>(from);
|
||||
+ const auto &toOp = *std::static_pointer_cast<TOperation>(to);
|
||||
+
|
||||
+ return std::make_unique<TOperation>(
|
||||
+ fromOp.value.interpolate(progress, toOp.value, getRelativeValueContext(context)));
|
||||
+ }
|
||||
|
||||
std::shared_ptr<StyleOperation> resolveOperation(
|
||||
const std::shared_ptr<StyleOperation> &operation,
|
||||
- const StyleOperationsInterpolationContext &context) const override;
|
||||
+ const StyleOperationsInterpolationContext &context) const override {
|
||||
+ const auto &resolvableOp = std::static_pointer_cast<TOperation>(operation);
|
||||
+ const auto &resolved = resolvableOp->value.resolve(getRelativeValueContext(context));
|
||||
+
|
||||
+ if (!resolved.has_value()) {
|
||||
+ throw std::invalid_argument(
|
||||
+ "[Reanimated] Cannot resolve resolvable operation: " + operation->getOperationName() +
|
||||
+ " for node with tag: " + std::to_string(context.node->getTag()));
|
||||
+ }
|
||||
+
|
||||
+ return std::make_shared<TOperation>(resolved.value());
|
||||
+ }
|
||||
|
||||
protected:
|
||||
const ResolvableValueInterpolatorConfig config_;
|
||||
|
||||
- RelativeValueInterpolationContext getRelativeValueContext(const StyleOperationsInterpolationContext &context) const;
|
||||
+ RelativeValueInterpolationContext getRelativeValueContext(const StyleOperationsInterpolationContext &context) const {
|
||||
+ return RelativeValueInterpolationContext{
|
||||
+ .node = context.node,
|
||||
+ .fallbackInterpolateThreshold = context.fallbackInterpolateThreshold,
|
||||
+ .viewStylesRepository = context.viewStylesRepository,
|
||||
+ .relativeProperty = config_.relativeProperty,
|
||||
+ .relativeTo = config_.relativeTo};
|
||||
+ }
|
||||
};
|
||||
|
||||
} // namespace reanimated::css
|
||||
--- a/reanimated/CSS/interpolation/transforms/operations/matrix.cpp
|
||||
+++ b/reanimated/CSS/interpolation/transforms/operations/matrix.cpp
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/interpolation/transforms/operations/translate.cpp
|
||||
+++ b/reanimated/CSS/interpolation/transforms/operations/translate.cpp
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <reanimated/CSS/interpolation/transforms/operations/translate.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/interpolation/values/SimpleValueInterpolator.cpp
|
||||
+++ b/reanimated/CSS/interpolation/values/SimpleValueInterpolator.cpp
|
||||
@@ -18,6 +18,7 @@
|
||||
#include <reanimated/CSS/svg/values/SVGStrokeDashArray.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
|
||||
namespace reanimated::css {
|
||||
|
||||
--- a/reanimated/CSS/misc/ViewStylesRepository.cpp
|
||||
+++ b/reanimated/CSS/misc/ViewStylesRepository.cpp
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <reanimated/Tools/FeatureFlags.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
--- a/reanimated/CSS/registries/CSSAnimationsRegistry.cpp
|
||||
+++ b/reanimated/CSS/registries/CSSAnimationsRegistry.cpp
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <react/debug/react_native_assert.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
--- a/reanimated/CSS/svg/values/SVGPath.cpp
|
||||
+++ b/reanimated/CSS/svg/values/SVGPath.cpp
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <regex>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/CSS/svg/values/SVGStops.cpp
|
||||
+++ b/reanimated/CSS/svg/values/SVGStops.cpp
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <reanimated/CSS/svg/values/SVGStops.h>
|
||||
|
||||
#include <algorithm>
|
||||
+#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
--- a/reanimated/CSS/utils/interpolators.cpp
|
||||
+++ b/reanimated/CSS/utils/interpolators.cpp
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <reanimated/CSS/utils/interpolators.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
namespace reanimated::css {
|
||||
--- a/reanimated/Fabric/updates/UpdatesRegistryManager.cpp
|
||||
+++ b/reanimated/Fabric/updates/UpdatesRegistryManager.cpp
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <react/debug/react_native_assert.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
--- a/reanimated/LayoutAnimations/LayoutAnimationsManager.cpp
|
||||
+++ b/reanimated/LayoutAnimations/LayoutAnimationsManager.cpp
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <react/debug/react_native_assert.h>
|
||||
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
--- a/reanimated/NativeModules/ReanimatedModuleProxy.cpp
|
||||
+++ b/reanimated/NativeModules/ReanimatedModuleProxy.cpp
|
||||
@@ -30,6 +30,7 @@
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
@@ -72,6 +73,10 @@ constexpr bool shouldUseSynchronousUpdatesInPerformOperations() {
|
||||
constexpr bool shouldUseSynchronousUpdatesInPerformOperations() {
|
||||
return StaticFeatureFlags::getFlag("IOS_SYNCHRONOUSLY_UPDATE_UI_PROPS");
|
||||
}
|
||||
+#elif defined(__linux__)
|
||||
+constexpr bool shouldUseSynchronousUpdatesInPerformOperations() {
|
||||
+ return StaticFeatureFlags::getFlag("LINUX_SYNCHRONOUSLY_UPDATE_UI_PROPS");
|
||||
+}
|
||||
#else
|
||||
constexpr bool shouldUseSynchronousUpdatesInPerformOperations() {
|
||||
return false;
|
||||
@@ -1089,11 +1094,11 @@ void ReanimatedModuleProxy::writeSynchronousPropsToViews(const UpdatesBatch &syn
|
||||
}
|
||||
#endif // ANDROID
|
||||
|
||||
-#if __APPLE__
|
||||
+#ifndef ANDROID
|
||||
for (const auto &[shadowNodeFamily, props] : synchronousUpdatesBatch) {
|
||||
synchronouslyUpdateUIPropsFunction_(shadowNodeFamily->getTag(), props);
|
||||
}
|
||||
-#endif // __APPLE__
|
||||
+#endif // ANDROID
|
||||
}
|
||||
|
||||
void ReanimatedModuleProxy::requestFlushRegistry() {
|
||||
--- a/reanimated/Tools/FeatureFlags.h
|
||||
+++ b/reanimated/Tools/FeatureFlags.h
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include <mutex>
|
||||
+#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
--- a/reanimated/Tools/PlatformDepMethodsHolder.h
|
||||
+++ b/reanimated/Tools/PlatformDepMethodsHolder.h
|
||||
@@ -9,7 +9,9 @@
|
||||
#include <react/renderer/core/Props.h>
|
||||
#include <react/renderer/core/ReactPrimitives.h>
|
||||
|
||||
+#include <functional>
|
||||
#include <memory>
|
||||
+#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
@@ -32,7 +34,7 @@ using MeasureFunction = std::function<jsi::Value(jsi::Runtime &rt, const jsi::Va
|
||||
using RequestRenderFunction = std::function<void(std::function<void(const double)>)>;
|
||||
#ifdef ANDROID
|
||||
using SynchronouslyUpdateUIPropsFunction = std::function<void(const std::vector<int> &, const std::vector<double> &)>;
|
||||
-#elif __APPLE__
|
||||
+#else
|
||||
using SynchronouslyUpdateUIPropsFunction = std::function<void(const int, const folly::dynamic &)>;
|
||||
#endif // ANDROID
|
||||
using PreserveMountedTagsFunction = std::function<std::optional<std::unique_ptr<int[]>>(std::vector<int> &)>;
|
||||
--- a/reanimated/Tools/SingleInstanceChecker.h
|
||||
+++ b/reanimated/Tools/SingleInstanceChecker.h
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
#include <react/debug/react_native_assert.h>
|
||||
|
||||
+#ifndef _MSC_VER
|
||||
#include <cxxabi.h>
|
||||
+#endif
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
@@ -51,10 +53,14 @@ class SingleInstanceChecker {
|
||||
|
||||
template <class T>
|
||||
SingleInstanceChecker<T>::SingleInstanceChecker() {
|
||||
+#ifdef _MSC_VER
|
||||
+ std::string className = typeid(T).name();
|
||||
+#else
|
||||
int status = 0;
|
||||
char *demangled = __cxxabiv1::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &status);
|
||||
std::string className = (status == 0 && demangled != nullptr) ? demangled : typeid(T).name();
|
||||
std::free(demangled);
|
||||
+#endif
|
||||
|
||||
// React Native can spawn up to two instances of a Native Module at the same
|
||||
// time. This happens during a reload when a new instance of React Native is
|
||||
@@ -1,42 +0,0 @@
|
||||
{
|
||||
"name": "@react-native-gpui/reanimated",
|
||||
"version": "4.7.0-alpha.1",
|
||||
"description": "Adapter for react-native-reanimated (MIT): runs animations, layout animations and CSS animations on the desktop renderer",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/streetwriters/react-native-gpui-modules.git",
|
||||
"directory": "modules/reanimated"
|
||||
},
|
||||
"homepage": "https://github.com/streetwriters/react-native-gpui-modules/tree/main/modules/reanimated#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/streetwriters/react-native-gpui-modules/issues"
|
||||
},
|
||||
"files": [
|
||||
"desktop",
|
||||
"react-native.config.js",
|
||||
"src",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md",
|
||||
"!**/*.test.js",
|
||||
"!**/*.test.mjs",
|
||||
"!**/*.test.ts",
|
||||
"!**/*.test.cjs"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"react-native": "0.87.x",
|
||||
"react-native-reanimated": "4.7.x",
|
||||
"react-native-worklets": "0.13.x",
|
||||
"@react-native-gpui/worklets": "0.13.0-alpha.1"
|
||||
},
|
||||
"codegenConfig": {
|
||||
"name": "GPUIReanimatedSpec",
|
||||
"type": "modules",
|
||||
"jsSrcsDir": "src"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "latest"
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
module.exports = {
|
||||
dependency: {
|
||||
platforms: {
|
||||
desktop: {
|
||||
cmake: 'desktop',
|
||||
cppProvider: 'create_gpui_reanimated_module',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,8 +0,0 @@
|
||||
import type { TurboModule } from 'react-native';
|
||||
import { TurboModuleRegistry } from 'react-native';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
installTurboModule: () => boolean;
|
||||
}
|
||||
|
||||
export default TurboModuleRegistry.get<Spec>('ReanimatedModule');
|
||||
@@ -1,8 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/safe-area-context` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## 5.10.0-alpha.0 - 2026-11-01
|
||||
|
||||
- First release, for `react-native-safe-area-context` 5.10.x.
|
||||
- Safe area insets for desktop windows, including custom title bars.
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,23 +0,0 @@
|
||||
# @react-native-gpui/safe-area-context
|
||||
|
||||
Adapter for [`react-native-safe-area-context`](https://github.com/AppAndFlow/react-native-safe-area-context) (MIT): reports safe area insets for desktop windows, including windows with a custom title bar.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
Install it next to `react-native-safe-area-context`:
|
||||
|
||||
```sh
|
||||
npm install react-native-safe-area-context@5.10.x @react-native-gpui/safe-area-context
|
||||
```
|
||||
|
||||
Keep using `react-native-safe-area-context` as you do on iOS and Android; its JavaScript API is unchanged. Autolinking builds the desktop side when the app is built.
|
||||
|
||||
## Documentation
|
||||
|
||||
[Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,33 +0,0 @@
|
||||
if(NOT RN_GPUI_PROJECT_ROOT)
|
||||
message(FATAL_ERROR
|
||||
"@react-native-gpui/safe-area-context was added by the platform; RN_GPUI_PROJECT_ROOT must be set by its build.rs")
|
||||
endif()
|
||||
rn_gpui_find_node_module(react-native-safe-area-context _safe_area_root)
|
||||
set(_safe_area_common "${_safe_area_root}/common/cpp")
|
||||
set(_safe_area_shadow_node "${_safe_area_common}/react/renderer/components/safeareacontext/RNCSafeAreaViewShadowNode.cpp")
|
||||
set(_safe_area_package_json "${_safe_area_root}/package.json")
|
||||
|
||||
if(NOT EXISTS "${_safe_area_shadow_node}" OR NOT EXISTS "${_safe_area_package_json}")
|
||||
message(FATAL_ERROR "react-native-safe-area-context 5.10.x was not found at ${_safe_area_root}")
|
||||
endif()
|
||||
file(READ "${_safe_area_package_json}" _safe_area_package_json_contents)
|
||||
string(JSON _safe_area_version GET "${_safe_area_package_json_contents}" version)
|
||||
if(NOT _safe_area_version MATCHES "^5\\.10\\.")
|
||||
message(FATAL_ERROR "@react-native-gpui/safe-area-context supports react-native-safe-area-context 5.10.x, found ${_safe_area_version}")
|
||||
endif()
|
||||
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_SOURCES
|
||||
"${_safe_area_shadow_node}"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GPUISafeAreaContextModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_INCLUDE_DIRS
|
||||
"${_safe_area_common}"
|
||||
"${RN_GPUI_REACT_CXX_PLATFORM_DIR}"
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp")
|
||||
# Safe-area insets reach the mounting manager through this glue, which the
|
||||
# platform compiles only because the package is installed.
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_SOURCES
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GpuiSafeAreaModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INSTALLERS safe_area)
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INCLUDE_DIRS
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp"
|
||||
"${RN_GPUI_PROJECT_ROOT}/desktop/autolink/codegen/safeareacontext")
|
||||
Vendored
-48
@@ -1,48 +0,0 @@
|
||||
#include <GPUISafeAreaContextSpec.h>
|
||||
#include <react/coremodules/DeviceInfoModule.h>
|
||||
#include "GPUISafeAreaInsets.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
using facebook::react::TurboModule;
|
||||
|
||||
class SafeAreaContextModule final
|
||||
: public facebook::react::NativeSafeAreaContextSpec<SafeAreaContextModule> {
|
||||
public:
|
||||
explicit SafeAreaContextModule(
|
||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker)
|
||||
: NativeSafeAreaContextSpec(std::move(jsInvoker)) {}
|
||||
|
||||
facebook::jsi::Object getConstants(facebook::jsi::Runtime& runtime) {
|
||||
const auto windowSize = facebook::react::DeviceInfoModule::windowSize();
|
||||
facebook::jsi::Object insets(runtime);
|
||||
const auto safeInsets = gpui_safe_area::gpui_safe_area_insets(0);
|
||||
insets.setProperty(runtime, "top", safeInsets.top);
|
||||
insets.setProperty(runtime, "right", safeInsets.right);
|
||||
insets.setProperty(runtime, "bottom", safeInsets.bottom);
|
||||
insets.setProperty(runtime, "left", safeInsets.left);
|
||||
|
||||
facebook::jsi::Object frame(runtime);
|
||||
frame.setProperty(runtime, "x", 0.0);
|
||||
frame.setProperty(runtime, "y", 0.0);
|
||||
frame.setProperty(runtime, "width", windowSize.width);
|
||||
frame.setProperty(runtime, "height", windowSize.height);
|
||||
|
||||
facebook::jsi::Object metrics(runtime);
|
||||
metrics.setProperty(runtime, "insets", std::move(insets));
|
||||
metrics.setProperty(runtime, "frame", std::move(frame));
|
||||
|
||||
facebook::jsi::Object constants(runtime);
|
||||
constants.setProperty(runtime, "initialWindowMetrics", std::move(metrics));
|
||||
return constants;
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<TurboModule> create_gpui_safe_area_context_module(
|
||||
const std::string& name,
|
||||
std::shared_ptr<facebook::react::CallInvoker> jsInvoker) {
|
||||
return rngpui::provideTurboModule<SafeAreaContextModule>(
|
||||
name, std::move(jsInvoker));
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
|
||||
namespace gpui_safe_area {
|
||||
|
||||
struct Insets {
|
||||
double top;
|
||||
double right;
|
||||
double bottom;
|
||||
double left;
|
||||
};
|
||||
|
||||
inline std::atomic<double> windowTopInset{0.0};
|
||||
|
||||
inline Insets gpui_safe_area_insets(int32_t /*surfaceId*/) {
|
||||
return {
|
||||
.top = windowTopInset.load(), .right = 0.0, .bottom = 0.0, .left = 0.0};
|
||||
}
|
||||
|
||||
} // namespace gpui_safe_area
|
||||
-143
@@ -1,143 +0,0 @@
|
||||
#include "GPUISafeAreaInsets.h"
|
||||
#include "GpuiModules.h"
|
||||
|
||||
#include <react/renderer/components/safeareacontext/RNCSafeAreaViewState.h>
|
||||
#include <react/renderer/core/ConcreteState.h>
|
||||
|
||||
#include <folly/json.h>
|
||||
#include <glog/logging.h>
|
||||
#include <array>
|
||||
#include <mutex>
|
||||
#include <string_view>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace rngpui {
|
||||
using namespace facebook::react;
|
||||
|
||||
namespace {
|
||||
struct TrackedView {
|
||||
std::array<float, 4> frame;
|
||||
SharedEventEmitter emitter;
|
||||
std::shared_ptr<const ConcreteState<RNCSafeAreaViewState>> state;
|
||||
};
|
||||
|
||||
std::mutex trackedMutex;
|
||||
std::unordered_map<Tag, TrackedView> tracked;
|
||||
|
||||
std::shared_ptr<const ConcreteState<RNCSafeAreaViewState>> safeAreaState(
|
||||
const ShadowView& view) {
|
||||
if (std::string_view(view.componentName) != "RNCSafeAreaView")
|
||||
return nullptr;
|
||||
return std::dynamic_pointer_cast<const ConcreteState<RNCSafeAreaViewState>>(
|
||||
view.state);
|
||||
}
|
||||
|
||||
void dispatchInsets(
|
||||
const SharedEventEmitter& emitter,
|
||||
const std::array<float, 4>& frame,
|
||||
float top,
|
||||
float right,
|
||||
float bottom,
|
||||
float left) {
|
||||
if (!emitter)
|
||||
return;
|
||||
emitter->dispatchEvent(
|
||||
"insetsChange",
|
||||
folly::dynamic::object(
|
||||
"insets",
|
||||
folly::dynamic::object("top", top)("right", right)("bottom", bottom)(
|
||||
"left", left))(
|
||||
"frame",
|
||||
folly::dynamic::object("x", frame[0])("y", frame[1])(
|
||||
"width", frame[2])("height", frame[3])));
|
||||
}
|
||||
|
||||
void applyInsets(float top, float right, float bottom, float left) {
|
||||
const bool changed = gpui_safe_area::windowTopInset.exchange(top) != top;
|
||||
std::vector<std::shared_ptr<const ConcreteState<RNCSafeAreaViewState>>>
|
||||
states;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(trackedMutex);
|
||||
for (const auto& [tag, entry] : tracked) {
|
||||
(void)tag;
|
||||
if (changed && entry.emitter)
|
||||
dispatchInsets(entry.emitter, entry.frame, top, right, bottom, left);
|
||||
if (!entry.state)
|
||||
continue;
|
||||
// The tracked state is the one the view had when its frame last changed.
|
||||
// A state update does not move the frame, so it would stay at the old
|
||||
// insets, and every batch would send the same update again: one commit
|
||||
// per batch, for ever.
|
||||
const auto latest =
|
||||
std::dynamic_pointer_cast<const ConcreteState<RNCSafeAreaViewState>>(
|
||||
entry.state->getMostRecentState());
|
||||
const auto& current = latest ? latest : entry.state;
|
||||
if (current->getData().insets.top != top)
|
||||
states.push_back(current);
|
||||
}
|
||||
}
|
||||
for (const auto& state : states) {
|
||||
auto next = state->getData();
|
||||
next.insets.top = top;
|
||||
state->updateState(std::move(next));
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void rn_gpui_install_safe_area_module() {
|
||||
registerMountObserver(extension::MountObserver{
|
||||
.onFrameChanged =
|
||||
[](SurfaceId surfaceId, const ShadowView& view) {
|
||||
const auto& frame = view.layoutMetrics.frame;
|
||||
const std::array<float, 4> nextFrame = {
|
||||
frame.origin.x,
|
||||
frame.origin.y,
|
||||
frame.size.width,
|
||||
frame.size.height};
|
||||
const bool isProvider =
|
||||
std::string_view(view.componentName) == "RNCSafeAreaProvider";
|
||||
if (!isProvider && !safeAreaState(view))
|
||||
return;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(trackedMutex);
|
||||
auto& entry = tracked[view.tag];
|
||||
if (isProvider && entry.emitter && entry.frame == nextFrame)
|
||||
return;
|
||||
entry.frame = nextFrame;
|
||||
entry.emitter = view.eventEmitter;
|
||||
entry.state = safeAreaState(view);
|
||||
}
|
||||
if (!isProvider)
|
||||
return;
|
||||
const auto insets = gpui_safe_area::gpui_safe_area_insets(
|
||||
static_cast<int32_t>(surfaceId));
|
||||
LOG(INFO) << "[gpui-safe-area] onInsetsChange tag=" << view.tag
|
||||
<< " surface=" << surfaceId << " frame=("
|
||||
<< frame.origin.x << ',' << frame.origin.y << ' '
|
||||
<< frame.size.width << 'x' << frame.size.height << ')';
|
||||
dispatchInsets(
|
||||
view.eventEmitter,
|
||||
nextFrame,
|
||||
insets.top,
|
||||
insets.right,
|
||||
insets.bottom,
|
||||
insets.left);
|
||||
},
|
||||
.onDeleted =
|
||||
[](Tag tag) {
|
||||
std::lock_guard<std::mutex> lock(trackedMutex);
|
||||
tracked.erase(tag);
|
||||
},
|
||||
.afterBatch =
|
||||
[] {
|
||||
applyInsets(
|
||||
gpui_safe_area::windowTopInset.load(), 0.0f, 0.0f, 0.0f);
|
||||
}});
|
||||
|
||||
registerWindowInsetsObserver(
|
||||
[](float top, float right, float bottom, float left) {
|
||||
applyInsets(top, right, bottom, left);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace rngpui
|
||||
@@ -1,40 +0,0 @@
|
||||
{
|
||||
"name": "@react-native-gpui/safe-area-context",
|
||||
"version": "5.10.0-alpha.0",
|
||||
"description": "Adapter for react-native-safe-area-context (MIT): reports safe area insets for desktop windows, including windows with a custom title bar",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/streetwriters/react-native-gpui-modules.git",
|
||||
"directory": "modules/safe-area-context"
|
||||
},
|
||||
"homepage": "https://github.com/streetwriters/react-native-gpui-modules/tree/main/modules/safe-area-context#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/streetwriters/react-native-gpui-modules/issues"
|
||||
},
|
||||
"files": [
|
||||
"desktop",
|
||||
"react-native.config.js",
|
||||
"src",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md",
|
||||
"!**/*.test.js",
|
||||
"!**/*.test.mjs",
|
||||
"!**/*.test.ts",
|
||||
"!**/*.test.cjs"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"react-native": "0.87.x",
|
||||
"react-native-safe-area-context": "5.10.x"
|
||||
},
|
||||
"codegenConfig": {
|
||||
"name": "GPUISafeAreaContextSpec",
|
||||
"type": "modules",
|
||||
"jsSrcsDir": "src"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "latest"
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
module.exports = {
|
||||
dependency: {
|
||||
platforms: {
|
||||
desktop: {
|
||||
cmake: 'desktop',
|
||||
cppProvider: 'create_gpui_safe_area_context_module',
|
||||
componentCodegenPackage: 'react-native-safe-area-context',
|
||||
componentDescriptors: [
|
||||
'RNCSafeAreaProviderComponentDescriptor',
|
||||
'RNCSafeAreaViewComponentDescriptor',
|
||||
],
|
||||
componentHeaders: [
|
||||
'react/renderer/components/safeareacontext/ComponentDescriptors.h',
|
||||
'react/renderer/components/safeareacontext/RNCSafeAreaViewComponentDescriptor.h',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
import type { CodegenTypes, TurboModule } from 'react-native';
|
||||
import { TurboModuleRegistry } from 'react-native';
|
||||
|
||||
export interface Spec extends TurboModule {
|
||||
getConstants: () => {
|
||||
initialWindowMetrics?: {
|
||||
insets: {
|
||||
top: CodegenTypes.Double;
|
||||
right: CodegenTypes.Double;
|
||||
bottom: CodegenTypes.Double;
|
||||
left: CodegenTypes.Double;
|
||||
};
|
||||
frame: {
|
||||
x: CodegenTypes.Double;
|
||||
y: CodegenTypes.Double;
|
||||
width: CodegenTypes.Double;
|
||||
height: CodegenTypes.Double;
|
||||
};
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export default TurboModuleRegistry.get<Spec>('RNCSafeAreaContext');
|
||||
@@ -1,20 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/screens` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## Unreleased
|
||||
|
||||
- Builds against gpui-fast `1ad3cc3`.
|
||||
|
||||
## 4.28.0-alpha.2 - 2026-11-12
|
||||
|
||||
- Builds against gpui-fast `5392403`.
|
||||
|
||||
## 4.28.0-alpha.1 - 2026-11-05
|
||||
|
||||
- Builds against gpui-fast `9e455ae`, which needs Rust 1.98 or newer.
|
||||
|
||||
## 4.28.0-alpha.0 - 2026-11-01
|
||||
|
||||
- First release, for `react-native-screens` 4.28.x.
|
||||
- Native stacks and screens on desktop, for React Navigation.
|
||||
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,25 +0,0 @@
|
||||
# @react-native-gpui/screens
|
||||
|
||||
Adapter for [`react-native-screens`](https://github.com/software-mansion/react-native-screens) (MIT): draws native stacks and screens, as used by React Navigation.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
Install it next to `react-native-screens`:
|
||||
|
||||
```sh
|
||||
npm install react-native-screens@4.28.x @react-native-gpui/screens
|
||||
```
|
||||
|
||||
Keep using `react-native-screens` as you do on iOS and Android; its JavaScript API is unchanged. Autolinking builds the desktop side when the app is built.
|
||||
|
||||
## Documentation
|
||||
|
||||
[Navigating between screens](docs/navigation.md)
|
||||
|
||||
[Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,30 +0,0 @@
|
||||
if(NOT RN_GPUI_PROJECT_ROOT)
|
||||
message(FATAL_ERROR
|
||||
"@react-native-gpui/screens was added by the platform; RN_GPUI_PROJECT_ROOT must be set by its build.rs")
|
||||
endif()
|
||||
rn_gpui_find_node_module(react-native-screens _screens_root)
|
||||
set(_screens_common "${_screens_root}/common/cpp")
|
||||
set(_screens_component_dir "${_screens_common}/react/renderer/components/rnscreens")
|
||||
set(_screens_package_json "${_screens_root}/package.json")
|
||||
|
||||
if(NOT EXISTS "${_screens_component_dir}/RNSScreenShadowNode.cpp" OR NOT EXISTS "${_screens_package_json}")
|
||||
message(FATAL_ERROR "react-native-screens 4.28.x was not found at ${_screens_root}")
|
||||
endif()
|
||||
|
||||
file(READ "${_screens_package_json}" _screens_package_json_contents)
|
||||
string(JSON _screens_version GET "${_screens_package_json_contents}" version)
|
||||
if(NOT _screens_version MATCHES "^4\\.28\\.")
|
||||
message(FATAL_ERROR "@react-native-gpui/screens supports react-native-screens 4.28.x, found ${_screens_version}")
|
||||
endif()
|
||||
|
||||
file(GLOB _screens_common_sources CONFIGURE_DEPENDS "${_screens_component_dir}/*.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_SOURCES ${_screens_common_sources})
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_APP_CPP_INCLUDE_DIRS "${_screens_common}")
|
||||
# The mount pipeline knows nothing about screens: this glue fills the header and
|
||||
# screen fields of a mount op, and the platform compiles it only because the
|
||||
# package is installed.
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_SOURCES
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GpuiScreensModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INSTALLERS screens)
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INCLUDE_DIRS
|
||||
"${_screens_common}")
|
||||
@@ -1,54 +0,0 @@
|
||||
[package]
|
||||
name = "rn-gpui-screens"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
react-native-gpui = "0.1"
|
||||
rn-gpui-sys = "0.1"
|
||||
gpui = "0.2"
|
||||
serde_json = "1"
|
||||
log = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
# `TestNode` and `TreeView::for_test` are gated behind `test-driver`, and a
|
||||
# dependency cannot enable another package's feature. It is a dev-dependency
|
||||
# rather than a normal one so a build of the module never pays for it, and it only
|
||||
# becomes usable because this crate is its own workspace root below.
|
||||
react-native-gpui = { version = "0.1", features = ["test-driver"] }
|
||||
|
||||
# Its own workspace root, deliberately. Cargo refuses to test a package that has
|
||||
# dev-dependencies unless it is a workspace member, and core's workspace cannot
|
||||
# hold it: a module resolves core by version and must not be a member of the
|
||||
# checkout's workspace. A root with itself as the only member satisfies the rule
|
||||
# without making the module part of anything else.
|
||||
[workspace]
|
||||
|
||||
# The checkout's shared dev profile (see core's `ReactGpui/Cargo.toml`): line tables
|
||||
# only, no debug info for dependencies, which are optimized. Every workspace root
|
||||
# builds into the one root `target/`, so a matching profile is also what lets them
|
||||
# reuse each other's dependency builds instead of storing one copy each.
|
||||
[profile.dev]
|
||||
debug = "line-tables-only"
|
||||
split-debuginfo = "packed"
|
||||
|
||||
[profile.dev.package."*"]
|
||||
debug = false
|
||||
opt-level = 3
|
||||
|
||||
[profile.dev.package.rn-gpui-sys]
|
||||
opt-level = 0
|
||||
|
||||
# This block has to live here because Cargo honours `[patch.crates-io]` only at a
|
||||
# workspace root, and this crate is one. `npm run test:rust` regenerates it from
|
||||
# core's manifest before running these tests and fails if it changed, so the gpui
|
||||
# revision is never written down by hand — commit whatever the run rewrites.
|
||||
[patch.crates-io]
|
||||
# Managed by @react-native-gpui/cli autolink; do not edit between these markers.
|
||||
react-native-gpui = { path = "../../../node_modules/@react-native-gpui/core/ReactGpui/rn-gpui" }
|
||||
rn-gpui-sys = { path = "../../../node_modules/@react-native-gpui/core/ReactGpui/rn-gpui-sys" }
|
||||
rn-gpui-module = { path = "../../../node_modules/@react-native-gpui/core/ReactGpui/rn-gpui-module" }
|
||||
rn-gpui-module-build = { path = "../../../node_modules/@react-native-gpui/core/ReactGpui/rn-gpui-module-build" }
|
||||
gpui = { git = "https://github.com/streetwriters/gpui-fast", rev = "1ad3cc3" }
|
||||
gpui_platform = { git = "https://github.com/streetwriters/gpui-fast", rev = "1ad3cc3" }
|
||||
# End managed by autolink.
|
||||
@@ -1,38 +0,0 @@
|
||||
//! Puts the C++ runtime's shared libraries on this crate's test binary rpath.
|
||||
//!
|
||||
//! `rn-gpui-screens`'s tests build gpui trees, which link `libjsi.dylib` and the
|
||||
//! rest of the Node-API runtime. Core's `build.rs` emits
|
||||
//! `cargo:rustc-link-arg=-Wl,-rpath,<dir>` for its own binary — the app — and that
|
||||
//! flag is not inherited by a dependency's test binary, so the test process
|
||||
//! aborted in dyld with `Library not loaded: @rpath/libjsi.dylib` before running a
|
||||
//! single test.
|
||||
//!
|
||||
//! Core exports the directories it was given as `DEP_RN_GPUI_CORE_RPATHS`, which
|
||||
//! is the same variable core's own `build.rs` reads. Re-emitting it here scopes
|
||||
//! the flag to this crate's test binary, since `cargo:rustc-link-arg` without a
|
||||
//! target suffix applies to the crate's own targets and not to the app that
|
||||
//! depends on it.
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-env-changed=DEP_RN_GPUI_CORE_RPATHS");
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
let Ok(dirs) = std::env::var("DEP_RN_GPUI_CORE_RPATHS") else {
|
||||
return;
|
||||
};
|
||||
let dirs = dirs
|
||||
.split('|')
|
||||
.filter(|dir| !dir.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
if dirs.is_empty() {
|
||||
return;
|
||||
}
|
||||
// MSVC and MinGW have no rpath, and the flag is rejected outright; Windows
|
||||
// finds the DLLs through PATH, which the test runner already arranges.
|
||||
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
|
||||
return;
|
||||
}
|
||||
for dir in dirs {
|
||||
println!("cargo:rustc-link-arg=-Wl,-rpath,{dir}");
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
|
||||
#include "GpuiModules.h"
|
||||
|
||||
#include <folly/json.h>
|
||||
#include <react/renderer/components/rnscreens/Props.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace rngpui {
|
||||
namespace {
|
||||
|
||||
using namespace facebook::react;
|
||||
|
||||
std::string screenPropsJson(
|
||||
const std::string& /*componentName*/,
|
||||
const ShadowView& view) {
|
||||
if (!view.props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
auto props = std::dynamic_pointer_cast<const RNSScreenProps>(view.props);
|
||||
if (!props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
folly::dynamic object = folly::dynamic::object;
|
||||
object["activityState"] = props->activityState;
|
||||
object["stackAnimation"] = toString(props->stackAnimation);
|
||||
object["stackPresentation"] = toString(props->stackPresentation);
|
||||
return folly::toJson(object);
|
||||
}
|
||||
|
||||
std::string headerConfigPropsJson(
|
||||
const std::string& /*componentName*/,
|
||||
const ShadowView& view) {
|
||||
if (!view.props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
auto props = std::dynamic_pointer_cast<const RNSScreenStackHeaderConfigProps>(
|
||||
view.props);
|
||||
if (!props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
folly::dynamic object = folly::dynamic::object;
|
||||
object["title"] = props->title;
|
||||
object["backgroundColor"] = argb(props->backgroundColor);
|
||||
object["titleColor"] = argb(props->titleColor);
|
||||
object["tintColor"] = argb(props->color);
|
||||
object["hidden"] = props->hidden;
|
||||
object["hideBackButton"] = props->hideBackButton;
|
||||
return folly::toJson(object);
|
||||
}
|
||||
|
||||
std::string headerSubviewPropsJson(
|
||||
const std::string& /*componentName*/,
|
||||
const ShadowView& view) {
|
||||
if (!view.props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
auto props =
|
||||
std::dynamic_pointer_cast<const RNSScreenStackHeaderSubviewProps>(
|
||||
view.props);
|
||||
if (!props) {
|
||||
return std::string("{}");
|
||||
}
|
||||
folly::dynamic object = folly::dynamic::object;
|
||||
object["type"] = toString(props->type);
|
||||
return folly::toJson(object);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void rn_gpui_install_screens_module() {
|
||||
registerComponentProps("RNSScreen", screenPropsJson);
|
||||
registerComponentProps("RNSScreenStackHeaderConfig", headerConfigPropsJson);
|
||||
registerComponentProps("RNSScreenStackHeaderSubview", headerSubviewPropsJson);
|
||||
}
|
||||
|
||||
} // namespace rngpui
|
||||
@@ -1,29 +0,0 @@
|
||||
//! The screens module.
|
||||
//!
|
||||
//! Everything react-native-screens needs lives here: its props extraction in
|
||||
//! `cpp/`, and its layout, painting and lifecycle in the modules below. Core is
|
||||
//! told only what any library can say — a component name, a props reader, and a
|
||||
//! set of generic hooks.
|
||||
pub mod lifecycle;
|
||||
pub mod props;
|
||||
pub mod render;
|
||||
pub mod stack;
|
||||
pub mod state;
|
||||
pub mod transition;
|
||||
pub mod unsupported;
|
||||
|
||||
/// Registers this module's renderers and observer.
|
||||
///
|
||||
/// Autolink calls this from `ensure_linked()` while `main` starts, before any
|
||||
/// `gpui::App` exists, so nothing here may touch the app.
|
||||
pub fn rn_gpui_register() {
|
||||
react_native_gpui::extensions::register_store_observer(std::sync::Arc::new(
|
||||
stack::StackObserver,
|
||||
));
|
||||
for (component, build) in render::RENDERED_COMPONENTS {
|
||||
react_native_gpui::extensions::register_component_renderer(
|
||||
*component,
|
||||
std::sync::Arc::from(build()),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
//! The lifecycle events a stack sends to its screens as they appear and leave.
|
||||
//!
|
||||
//! React Native exposes these as `topWillAppear`, `topAppear`,
|
||||
//! `topWillDisappear` and `topDisappear`, in that order around a transition.
|
||||
use gpui::App;
|
||||
use react_native_gpui::extensions::StoreHandle;
|
||||
|
||||
/// The events an activity-state change produces, in the order they fire.
|
||||
pub fn lifecycle_events(previous: f32, next: f32) -> &'static [&'static str] {
|
||||
match (previous < 1.0, previous >= 2.0, next < 1.0, next >= 2.0) {
|
||||
(true, _, false, true) => &["topWillAppear", "topAppear"],
|
||||
(true, _, false, false) => &["topWillAppear"],
|
||||
(_, true, false, false) => &["topWillDisappear"],
|
||||
(false, false, true, _) => &["topDisappear"],
|
||||
(false, true, true, _) => &["topWillDisappear", "topDisappear"],
|
||||
(false, false, false, true) => &["topAppear"],
|
||||
_ => &[],
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends the "will" half of a transition: the outgoing screen then the incoming.
|
||||
pub fn dispatch_will(
|
||||
handle: &StoreHandle,
|
||||
outgoing: Option<i32>,
|
||||
incoming: Option<i32>,
|
||||
cx: &mut App,
|
||||
) {
|
||||
for (tag, event) in [
|
||||
outgoing.map(|tag| (tag, "topWillDisappear")),
|
||||
incoming.map(|tag| (tag, "topWillAppear")),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
log::info!("[screens] tag={tag} lifecycle={event}");
|
||||
handle.dispatch_event(tag, event, &serde_json::json!({}), cx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends the "did" half: the outgoing screen then the incoming, and wakes
|
||||
/// Fabric so the callbacks arrive before a later pop unmounts them.
|
||||
pub fn dispatch_did(
|
||||
handle: &StoreHandle,
|
||||
outgoing: Option<i32>,
|
||||
incoming: Option<i32>,
|
||||
cx: &mut App,
|
||||
) {
|
||||
for (tag, event) in [
|
||||
outgoing.map(|tag| (tag, "topDisappear")),
|
||||
incoming.map(|tag| (tag, "topAppear")),
|
||||
]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
log::info!("[screens] tag={tag} lifecycle={event}");
|
||||
handle.dispatch_event(tag, event, &serde_json::json!({}), cx);
|
||||
}
|
||||
// A timer can complete after the display-frame chain has gone idle, so the
|
||||
// JavaScript that handles these events needs a render to arrive in.
|
||||
handle.wake(cx);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn activity_state_edges_emit_lifecycle_in_order() {
|
||||
assert_eq!(lifecycle_events(0.0, 1.0), &["topWillAppear"]);
|
||||
assert_eq!(lifecycle_events(1.0, 2.0), &["topAppear"]);
|
||||
assert_eq!(lifecycle_events(2.0, 1.0), &["topWillDisappear"]);
|
||||
assert_eq!(lifecycle_events(1.0, 0.0), &["topDisappear"]);
|
||||
assert_eq!(lifecycle_events(0.0, 2.0), &["topWillAppear", "topAppear"]);
|
||||
assert_eq!(
|
||||
lifecycle_events(2.0, 0.0),
|
||||
&["topWillDisappear", "topDisappear"]
|
||||
);
|
||||
assert!(lifecycle_events(1.0, 1.0).is_empty());
|
||||
}
|
||||
}
|
||||
@@ -1,177 +0,0 @@
|
||||
//! Typed readers over the JSON this module's C++ half produced.
|
||||
//!
|
||||
//! A missing key reads as today's zero value, so a component that has not been
|
||||
//! given the prop yet behaves the way it did before it arrived.
|
||||
use react_native_gpui::extensions::ComponentNode;
|
||||
|
||||
/// A number, or 0 when absent.
|
||||
fn number(node: &ComponentNode, key: &str) -> f64 {
|
||||
node.props
|
||||
.get(key)
|
||||
.and_then(|value| value.as_f64())
|
||||
.unwrap_or(0.0)
|
||||
}
|
||||
|
||||
/// A number as `f32`, or 0 when absent.
|
||||
fn number_f32(node: &ComponentNode, key: &str) -> f32 {
|
||||
number(node, key) as f32
|
||||
}
|
||||
|
||||
/// A string, borrowed from the props.
|
||||
fn string<'a>(node: &'a ComponentNode, key: &str) -> &'a str {
|
||||
node.props
|
||||
.get(key)
|
||||
.and_then(|value| value.as_str())
|
||||
.unwrap_or("")
|
||||
}
|
||||
|
||||
/// A boolean, false when absent.
|
||||
fn boolean(node: &ComponentNode, key: &str) -> bool {
|
||||
node.props
|
||||
.get(key)
|
||||
.and_then(|value| value.as_bool())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// A colour as 0xAARRGGBB, 0 when unset.
|
||||
fn color(node: &ComponentNode, key: &str) -> u32 {
|
||||
number(node, key) as u32
|
||||
}
|
||||
|
||||
/// An `RNSScreen`'s activity state. 0 is detached, 1 is transitioning, 2 is
|
||||
/// fully on screen.
|
||||
pub fn activity_state(node: &ComponentNode) -> f32 {
|
||||
number_f32(node, "activityState")
|
||||
}
|
||||
|
||||
/// An `RNSScreen`'s `stackAnimation`.
|
||||
pub fn stack_animation(node: &ComponentNode) -> &str {
|
||||
string(node, "stackAnimation")
|
||||
}
|
||||
|
||||
/// An `RNSScreen`'s `stackPresentation`.
|
||||
pub fn stack_presentation(node: &ComponentNode) -> &str {
|
||||
string(node, "stackPresentation")
|
||||
}
|
||||
|
||||
/// Whether a presentation paints its own card over the one underneath.
|
||||
pub fn is_modal_presentation(node: &ComponentNode) -> bool {
|
||||
matches!(
|
||||
stack_presentation(node),
|
||||
"modal" | "transparentModal" | "containedModal" | "formSheet"
|
||||
)
|
||||
}
|
||||
|
||||
/// Whether that card lets the screen underneath show through.
|
||||
pub fn is_transparent_presentation(node: &ComponentNode) -> bool {
|
||||
stack_presentation(node) == "transparentModal"
|
||||
}
|
||||
|
||||
/// A header config's title.
|
||||
pub fn header_title(node: &ComponentNode) -> &str {
|
||||
string(node, "title")
|
||||
}
|
||||
|
||||
/// A header config's background colour.
|
||||
pub fn header_background_color(node: &ComponentNode) -> u32 {
|
||||
color(node, "backgroundColor")
|
||||
}
|
||||
|
||||
/// A header config's title colour.
|
||||
pub fn header_title_color(node: &ComponentNode) -> u32 {
|
||||
color(node, "titleColor")
|
||||
}
|
||||
|
||||
/// A header config's tint colour, used for the back button.
|
||||
pub fn header_tint_color(node: &ComponentNode) -> u32 {
|
||||
color(node, "tintColor")
|
||||
}
|
||||
|
||||
/// Whether a header config hides the whole bar.
|
||||
pub fn header_hidden(node: &ComponentNode) -> bool {
|
||||
boolean(node, "hidden")
|
||||
}
|
||||
|
||||
/// Whether a header config hides only the back button.
|
||||
pub fn header_hide_back(node: &ComponentNode) -> bool {
|
||||
boolean(node, "hideBackButton")
|
||||
}
|
||||
|
||||
/// A header subview's placement: `left`, `right`, `center`, `title` or a Yoga x.
|
||||
pub fn subview_type(node: &ComponentNode) -> &str {
|
||||
string(node, "type")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn node(json: serde_json::Value) -> ComponentNode {
|
||||
ComponentNode {
|
||||
component: "RNSScreen".to_string(),
|
||||
tag: 1,
|
||||
frame: rn_gpui_sys::ffi::RectF::default(),
|
||||
scale: (1.0, 1.0),
|
||||
props: Arc::new(json),
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_key_reads_as_the_zero_value() {
|
||||
let empty = node(serde_json::json!({}));
|
||||
assert_eq!(activity_state(&empty), 0.0);
|
||||
assert_eq!(stack_animation(&empty), "");
|
||||
assert_eq!(stack_presentation(&empty), "");
|
||||
assert_eq!(header_title(&empty), "");
|
||||
assert_eq!(header_background_color(&empty), 0);
|
||||
assert!(!header_hidden(&empty));
|
||||
assert!(!header_hide_back(&empty));
|
||||
assert!(!is_modal_presentation(&empty));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_the_fields_the_props_half_writes() {
|
||||
let screen = node(serde_json::json!({
|
||||
"activityState": 2,
|
||||
"stackAnimation": "slide_from_right",
|
||||
"stackPresentation": "transparentModal",
|
||||
}));
|
||||
assert_eq!(activity_state(&screen), 2.0);
|
||||
assert_eq!(stack_animation(&screen), "slide_from_right");
|
||||
assert!(is_modal_presentation(&screen));
|
||||
assert!(is_transparent_presentation(&screen));
|
||||
|
||||
let header = node(serde_json::json!({
|
||||
"title": "Home",
|
||||
"backgroundColor": 0xff112233u32,
|
||||
"titleColor": 0xffaabbccu32,
|
||||
"tintColor": 0xff445566u32,
|
||||
"hidden": true,
|
||||
"hideBackButton": true,
|
||||
}));
|
||||
assert_eq!(header_title(&header), "Home");
|
||||
assert_eq!(header_background_color(&header), 0xff112233);
|
||||
assert_eq!(header_title_color(&header), 0xffaabbcc);
|
||||
assert_eq!(header_tint_color(&header), 0xff445566);
|
||||
assert!(header_hidden(&header));
|
||||
assert!(header_hide_back(&header));
|
||||
|
||||
let subview = node(serde_json::json!({"type": "right"}));
|
||||
assert_eq!(subview_type(&subview), "right");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_the_modal_presentations_count_as_modal() {
|
||||
for presentation in ["modal", "transparentModal", "containedModal", "formSheet"] {
|
||||
let node = node(serde_json::json!({"stackPresentation": presentation}));
|
||||
assert!(is_modal_presentation(&node), "{presentation}");
|
||||
}
|
||||
for presentation in ["push", "card", ""] {
|
||||
let node = node(serde_json::json!({"stackPresentation": presentation}));
|
||||
assert!(!is_modal_presentation(&node), "{presentation}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,829 +0,0 @@
|
||||
//! Painting react-native-screens through the generic renderer hooks.
|
||||
//!
|
||||
//! Core builds a box, a background and an accessibility node for every
|
||||
//! component. What is screens-specific — which screen is on top, where a header
|
||||
//! subview sits, whether a card is showing — is decided here, from the props this
|
||||
//! module's C++ half produced and a read-only view of the tree.
|
||||
use crate::props;
|
||||
use crate::stack::{
|
||||
CONTENT_WRAPPER, FULL_WINDOW_OVERLAY, HEADER_CONFIG, HEADER_SUBVIEW, MODAL_SCREEN, SCREEN,
|
||||
STACK,
|
||||
};
|
||||
use gpui::{div, prelude::*, AnyElement, App, MouseButton, SharedString, Window};
|
||||
use react_native_gpui::extensions::{
|
||||
store_handle, ComponentNode, ComponentRenderer, Decoration, Portal, StoreHandle, TreeView,
|
||||
};
|
||||
use rn_gpui_sys::ffi::RectF;
|
||||
|
||||
/// The header bar's height, which the content wrapper is inset by.
|
||||
pub const HEADER_HEIGHT: f32 = 56.0;
|
||||
|
||||
/// A screen inside a stack.
|
||||
pub struct ScreenRenderer;
|
||||
|
||||
impl ComponentRenderer for ScreenRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn children(
|
||||
&self,
|
||||
node: &ComponentNode,
|
||||
children: Vec<AnyElement>,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Vec<AnyElement> {
|
||||
if !props::is_modal_presentation(node) {
|
||||
return children;
|
||||
}
|
||||
let material = props::is_transparent_presentation(node);
|
||||
// The frame is in unscaled coordinates and the element this returns is
|
||||
// painted scaled, so the sheet has to be scaled to land where the card
|
||||
// it covers does. The old code did the same with `scale_x`/`scale_y`.
|
||||
let (scale_x, scale_y) = node.scale;
|
||||
let width = (node.frame.w - SHEET_MARGIN).clamp(0.0, SHEET_MAX_WIDTH) * scale_x;
|
||||
let height = (node.frame.h - SHEET_MARGIN).clamp(0.0, SHEET_MAX_HEIGHT) * scale_y;
|
||||
let left = ((node.frame.w * scale_x - width) * 0.5).max(0.0);
|
||||
let top = ((node.frame.h * scale_y - height) * 0.5).max(0.0);
|
||||
let mut layers = Vec::with_capacity(2);
|
||||
if !material {
|
||||
layers.push(
|
||||
div()
|
||||
.absolute()
|
||||
.inset_0()
|
||||
.size_full()
|
||||
.bg(argb(0x80000000))
|
||||
.into_any_element(),
|
||||
);
|
||||
}
|
||||
layers.push(
|
||||
div()
|
||||
.absolute()
|
||||
.left(gpui::px(left))
|
||||
.top(gpui::px(top))
|
||||
.w(gpui::px(width))
|
||||
.h(gpui::px(height))
|
||||
.overflow_hidden()
|
||||
.bg(argb(if material { 0x00000000 } else { 0xff171923 }))
|
||||
.children(children)
|
||||
.into_any_element(),
|
||||
);
|
||||
layers
|
||||
}
|
||||
|
||||
fn hides_subtree(&self, node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
if props::activity_state(node) > 0.0 {
|
||||
return false;
|
||||
}
|
||||
if tree
|
||||
.transition(node.tag)
|
||||
.is_some_and(|state| state.progress < 1.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
!covered_by_modal_sibling(node, tree)
|
||||
}
|
||||
|
||||
fn decorate(&self, node: &ComponentNode, tree: &TreeView) -> Decoration {
|
||||
if props::activity_state(node) > 0.0 {
|
||||
return Decoration::default();
|
||||
}
|
||||
let transitioning = tree
|
||||
.transition(node.tag)
|
||||
.is_some_and(|state| state.progress < 1.0);
|
||||
let visible_under_modal = covered_by_modal_sibling(node, tree);
|
||||
if !transitioning && !visible_under_modal {
|
||||
return Decoration {
|
||||
display: Some(false),
|
||||
..Decoration::default()
|
||||
};
|
||||
}
|
||||
Decoration::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a later sibling in the same stack is an active modal presentation.
|
||||
fn covered_by_modal_sibling(node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
let Some(parent) = node.parent else {
|
||||
return false;
|
||||
};
|
||||
let siblings = tree.children(parent);
|
||||
let Some(position) = siblings.iter().position(|tag| *tag == node.tag) else {
|
||||
return false;
|
||||
};
|
||||
siblings
|
||||
.into_iter()
|
||||
.skip(position + 1)
|
||||
.filter_map(|tag| tree.node(tag))
|
||||
.any(|sibling| {
|
||||
sibling.component == SCREEN
|
||||
&& props::activity_state(&sibling) > 0.0
|
||||
&& props::is_modal_presentation(&sibling)
|
||||
})
|
||||
}
|
||||
|
||||
/// The stack itself: it clips, and it owns the Escape gesture.
|
||||
pub struct StackRenderer;
|
||||
|
||||
impl ComponentRenderer for StackRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn decorate(&self, _node: &ComponentNode, _tree: &TreeView) -> Decoration {
|
||||
Decoration {
|
||||
clip: true,
|
||||
..Decoration::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn handles_keys(&self, _node: &ComponentNode) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn on_key_down(
|
||||
&self,
|
||||
node: &ComponentNode,
|
||||
key: &str,
|
||||
tree: &TreeView,
|
||||
handle: &react_native_gpui::extensions::StoreHandle,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> bool {
|
||||
if key != "escape" {
|
||||
return false;
|
||||
}
|
||||
if has_screen_ancestor(node, tree) {
|
||||
return false;
|
||||
}
|
||||
let screens: Vec<i32> = tree
|
||||
.children(node.tag)
|
||||
.into_iter()
|
||||
.filter(|tag| {
|
||||
tree.is_live(*tag) && tree.node(*tag).is_some_and(|n| n.component == SCREEN)
|
||||
})
|
||||
.collect();
|
||||
let Some(top) = screens.last().copied() else {
|
||||
return false;
|
||||
};
|
||||
if crate::state::with(|state| state.native_dismiss_pending.is_some()) {
|
||||
return true;
|
||||
}
|
||||
dismiss_top(top, node.tag, tree, handle, window, cx);
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether any ancestor of `node` is a screen, i.e. this stack is nested.
|
||||
fn has_screen_ancestor(node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
tree.ancestors(node.tag)
|
||||
.into_iter()
|
||||
.filter_map(|tag| tree.node(tag))
|
||||
.any(|ancestor| ancestor.component == SCREEN)
|
||||
}
|
||||
|
||||
/// The header bar: a background, a title and a back button.
|
||||
pub struct HeaderConfigRenderer;
|
||||
|
||||
impl ComponentRenderer for HeaderConfigRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn layout_frame(&self, node: &ComponentNode, tree: &TreeView) -> Option<RectF> {
|
||||
let mut frame = node.frame;
|
||||
frame.y += tree.window_top_inset();
|
||||
frame.h = if props::header_hidden(node) || !is_top_header(node, tree) {
|
||||
0.0
|
||||
} else {
|
||||
HEADER_HEIGHT
|
||||
};
|
||||
Some(frame)
|
||||
}
|
||||
|
||||
fn hides_subtree(&self, node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
props::header_hidden(node) || !is_top_header(node, tree)
|
||||
}
|
||||
|
||||
fn decorate(&self, node: &ComponentNode, tree: &TreeView) -> Decoration {
|
||||
let top = is_top_header(node, tree);
|
||||
let shown = should_show_back(node, tree);
|
||||
crate::state::set_header_facts(
|
||||
node.tag,
|
||||
top,
|
||||
shown,
|
||||
has_title_subview(node, tree),
|
||||
node.parent,
|
||||
);
|
||||
if props::header_hidden(node) || !top {
|
||||
return Decoration {
|
||||
display: Some(false),
|
||||
..Decoration::default()
|
||||
};
|
||||
}
|
||||
let background = first_nonzero(props::header_background_color(node), 0xff171923);
|
||||
Decoration {
|
||||
background: Some(background),
|
||||
..Decoration::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn depends_on_window_insets(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn content(
|
||||
&self,
|
||||
node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
if props::header_hidden(node) || !crate::state::header_is_top(node.tag) {
|
||||
return None;
|
||||
}
|
||||
// React Native declares `onHeaderBackButtonClicked` on the screen, so
|
||||
// the config's parent is the tag that has a listener for it.
|
||||
let Some(screen_tag) = crate::state::header_screen(node.tag) else {
|
||||
return None;
|
||||
};
|
||||
let title_color = first_nonzero(props::header_title_color(node), 0xffffffff);
|
||||
let mut bar = div()
|
||||
.absolute()
|
||||
.top_0()
|
||||
.left_0()
|
||||
.size_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.text_color(argb(title_color))
|
||||
.text_size(gpui::px(18.0));
|
||||
if !crate::state::header_has_title_subview(node.tag) {
|
||||
bar = bar.child(SharedString::from(props::header_title(node).to_string()));
|
||||
}
|
||||
if crate::state::header_shows_back(node.tag) {
|
||||
bar = bar.child(
|
||||
div()
|
||||
.absolute()
|
||||
.left_0()
|
||||
.top_0()
|
||||
.h(gpui::px(HEADER_HEIGHT))
|
||||
.w(gpui::px(HEADER_HEIGHT))
|
||||
.flex()
|
||||
.items_center()
|
||||
.justify_center()
|
||||
.text_color(argb(first_nonzero(
|
||||
props::header_tint_color(node),
|
||||
0xffffffff,
|
||||
)))
|
||||
.text_size(gpui::px(30.0))
|
||||
.cursor_pointer()
|
||||
.child("‹")
|
||||
.on_mouse_down(MouseButton::Left, move |_event, window, cx| {
|
||||
if let Some(handle) = store_handle(cx) {
|
||||
handle.dispatch_event(
|
||||
screen_tag,
|
||||
"headerBackButtonClicked",
|
||||
&serde_json::json!({}),
|
||||
cx,
|
||||
);
|
||||
handle.request_frame(window);
|
||||
}
|
||||
cx.stop_propagation();
|
||||
}),
|
||||
);
|
||||
}
|
||||
Some(bar.into_any_element())
|
||||
}
|
||||
}
|
||||
|
||||
/// Escape dismisses the top screen the way a native gesture would.
|
||||
///
|
||||
/// The card runs its exit animation, the screen gets the `will` pair, and only
|
||||
/// when the animation ends does it get the final progress, the `did` pair and
|
||||
/// `topDismissed`. `native_dismiss_pending` tells the stack observer that this
|
||||
/// dismissal already ran its animation, so it must not run a second one.
|
||||
fn dismiss_top(
|
||||
top: i32,
|
||||
stack: i32,
|
||||
tree: &TreeView,
|
||||
handle: &StoreHandle,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
crate::state::with(|state| state.native_dismiss_pending = Some(top));
|
||||
let incoming = previous_screen(top, stack, tree);
|
||||
crate::lifecycle::dispatch_will(handle, Some(top), incoming, cx);
|
||||
// The animation belongs to the screen being dismissed, not to the stack: a
|
||||
// stack carries no `stackAnimation` prop of its own. A dismissal plays the
|
||||
// entry mirrored, which is what `reverse` is for.
|
||||
let animation = tree
|
||||
.node(top)
|
||||
.map(|node| props::stack_animation(&node).to_string())
|
||||
.unwrap_or_default();
|
||||
let mode = crate::transition::transition_mode(&animation)
|
||||
.filter(|_| tree.animations_enabled())
|
||||
.map(crate::transition::TransitionMode::reverse);
|
||||
let duration = mode.map(|mode| mode.duration());
|
||||
if let Some(mode) = mode {
|
||||
let exit = crate::transition::transition(mode, crate::transition::Role::Exit, 1.0);
|
||||
crate::state::with(|state| {
|
||||
state.roles.insert(top, (crate::state::Role::Exit, 1.0));
|
||||
});
|
||||
handle.start_transition(top, exit, cx);
|
||||
// The card underneath comes back to rest from wherever the push left
|
||||
// it. Without this it stays offset for the whole dismissal.
|
||||
if let Some(incoming) = incoming {
|
||||
handle.clear_transition(incoming, cx);
|
||||
}
|
||||
handle.request_frame(window);
|
||||
}
|
||||
let finish = move |handle: &StoreHandle, cx: &mut App| {
|
||||
handle.dispatch_event(
|
||||
top,
|
||||
"topTransitionProgress",
|
||||
&serde_json::json!({"progress": 1, "closing": 1, "goingForward": 0}),
|
||||
cx,
|
||||
);
|
||||
crate::lifecycle::dispatch_did(handle, Some(top), incoming, cx);
|
||||
handle.dispatch_event(
|
||||
top,
|
||||
"topDismissed",
|
||||
&serde_json::json!({ "dismissCount": 1 }),
|
||||
cx,
|
||||
);
|
||||
};
|
||||
match duration {
|
||||
Some(duration) => {
|
||||
let handle = handle.clone();
|
||||
handle.after(duration, cx, move |handle, cx| finish(handle, cx));
|
||||
}
|
||||
None => finish(handle, cx),
|
||||
}
|
||||
}
|
||||
|
||||
/// A Fabric colour as GPUI sees it, alpha included.
|
||||
fn argb(color: u32) -> gpui::Hsla {
|
||||
let alpha = color >> 24;
|
||||
gpui::rgba((color << 8) | alpha).into()
|
||||
}
|
||||
|
||||
fn first_nonzero(value: u32, fallback: u32) -> u32 {
|
||||
if value == 0 {
|
||||
fallback
|
||||
} else {
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the screen has a previous live screen in its stack to go back to.
|
||||
fn should_show_back(node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
if props::header_hide_back(node) {
|
||||
return false;
|
||||
}
|
||||
let Some(screen) = node.parent else {
|
||||
return false;
|
||||
};
|
||||
let Some(stack) = stack_of(screen, tree) else {
|
||||
return false;
|
||||
};
|
||||
let screens: Vec<i32> = tree
|
||||
.children(stack)
|
||||
.into_iter()
|
||||
.filter(|tag| tree.is_live(*tag) && tree.node(*tag).is_some_and(|n| n.component == SCREEN))
|
||||
.collect();
|
||||
screens
|
||||
.iter()
|
||||
.position(|tag| *tag == screen)
|
||||
.is_some_and(|position| position > 0)
|
||||
}
|
||||
|
||||
/// Whether this header belongs to the screen currently on top of its stack.
|
||||
fn is_top_header(node: &ComponentNode, tree: &TreeView) -> bool {
|
||||
let Some(parent) = node.parent else {
|
||||
return true;
|
||||
};
|
||||
let Some(stack) = stack_of(parent, tree) else {
|
||||
return true;
|
||||
};
|
||||
let screens: Vec<i32> = tree
|
||||
.children(stack)
|
||||
.into_iter()
|
||||
.filter(|tag| tree.node(*tag).is_some_and(|n| n.component == SCREEN))
|
||||
.collect();
|
||||
let is_last = screens.last().copied() == Some(parent);
|
||||
// A screen that is leaving keeps its header while its transition runs, so
|
||||
// the header does not blink out before the card has finished moving. An
|
||||
// entry transition is never cleared once it ends, so only a running one
|
||||
// counts: otherwise the first screen ever pushed keeps its header for good.
|
||||
is_last
|
||||
|| tree
|
||||
.transition(parent)
|
||||
.is_some_and(|state| state.progress < 1.0)
|
||||
}
|
||||
|
||||
/// Whether the header config carries a subview that paints the title.
|
||||
///
|
||||
/// React Navigation puts a custom title in a `title` subview, and a centred one
|
||||
/// in a `center` subview; either way that subview paints the title, so core must
|
||||
/// not paint a second copy.
|
||||
fn has_title_subview(config: &ComponentNode, tree: &TreeView) -> bool {
|
||||
tree.children(config.tag)
|
||||
.into_iter()
|
||||
.filter_map(|tag| tree.node(tag))
|
||||
.any(|child| {
|
||||
child.component == HEADER_SUBVIEW
|
||||
&& matches!(props::subview_type(&child), "title" | "center")
|
||||
})
|
||||
}
|
||||
|
||||
/// The live screen directly below `top` in its stack, if any.
|
||||
fn previous_screen(top: i32, stack: i32, tree: &TreeView) -> Option<i32> {
|
||||
let screens: Vec<i32> = tree
|
||||
.children(stack)
|
||||
.into_iter()
|
||||
.filter(|tag| tree.is_live(*tag) && tree.node(*tag).is_some_and(|n| n.component == SCREEN))
|
||||
.collect();
|
||||
let index = screens.iter().position(|tag| *tag == top)?;
|
||||
index.checked_sub(1).and_then(|i| screens.get(i)).copied()
|
||||
}
|
||||
|
||||
/// The stack a screen sits in, walking up past intermediate containers.
|
||||
fn stack_of(screen: i32, tree: &TreeView) -> Option<i32> {
|
||||
tree.ancestors(screen)
|
||||
.into_iter()
|
||||
.find(|tag| tree.node(*tag).is_some_and(|n| n.component == STACK))
|
||||
}
|
||||
|
||||
/// A header subview: positioned by its declared type.
|
||||
pub struct HeaderSubviewRenderer;
|
||||
|
||||
impl ComponentRenderer for HeaderSubviewRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn layout_frame(&self, node: &ComponentNode, tree: &TreeView) -> Option<RectF> {
|
||||
let mut frame = node.frame;
|
||||
let Some(config) = node.parent else {
|
||||
return Some(frame);
|
||||
};
|
||||
let container_width = tree
|
||||
.node(config)
|
||||
.map(|config| config.frame.w)
|
||||
.unwrap_or(frame.w);
|
||||
let tallest = tree
|
||||
.children(node.tag)
|
||||
.into_iter()
|
||||
.filter_map(|tag| tree.node(tag))
|
||||
.fold(0.0f32, |height, child| height.max(child.frame.h));
|
||||
frame.x = match props::subview_type(node) {
|
||||
"right" => container_width - frame.w - 12.0,
|
||||
"center" | "title" => (container_width - frame.w) / 2.0,
|
||||
"left" => 12.0,
|
||||
_ => frame.x,
|
||||
};
|
||||
frame.y = ((HEADER_HEIGHT - tallest).max(0.0)) / 2.0;
|
||||
Some(frame)
|
||||
}
|
||||
}
|
||||
|
||||
/// A screen's content area, inset by the header when one is showing.
|
||||
pub struct ContentWrapperRenderer;
|
||||
|
||||
impl ComponentRenderer for ContentWrapperRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn layout_frame(&self, node: &ComponentNode, tree: &TreeView) -> Option<RectF> {
|
||||
let Some(screen) = node.parent else {
|
||||
return None;
|
||||
};
|
||||
let mut frame = node.frame;
|
||||
// Only a header that is actually showing takes space. When it is hidden
|
||||
// the C++ half already gave the wrapper the whole screen, so the Yoga
|
||||
// frame is the answer and any adjustment here would move it twice.
|
||||
if !header_hidden(screen, tree) {
|
||||
frame.y += HEADER_HEIGHT;
|
||||
frame.h = (frame.h - HEADER_HEIGHT).max(0.0);
|
||||
}
|
||||
Some(frame)
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a screen's header config is hidden, whether or not it exists.
|
||||
fn header_hidden(screen: i32, tree: &TreeView) -> bool {
|
||||
tree.children(screen)
|
||||
.into_iter()
|
||||
.filter_map(|tag| tree.node(tag))
|
||||
.find(|node| node.component == HEADER_CONFIG)
|
||||
.is_some_and(|config| props::header_hidden(&config))
|
||||
}
|
||||
|
||||
/// The gap a modal sheet leaves around itself, and the size it is capped at.
|
||||
const SHEET_MARGIN: f32 = 48.0;
|
||||
const SHEET_MAX_WIDTH: f32 = 760.0;
|
||||
const SHEET_MAX_HEIGHT: f32 = 640.0;
|
||||
|
||||
/// A modal presentation. It paints like a screen; the difference is that the
|
||||
/// card underneath stays visible.
|
||||
pub struct ModalScreenRenderer;
|
||||
|
||||
impl ComponentRenderer for ModalScreenRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A full-window overlay paints over the whole window, not inside the tree.
|
||||
pub struct FullWindowOverlayRenderer;
|
||||
|
||||
impl ComponentRenderer for FullWindowOverlayRenderer {
|
||||
fn render(
|
||||
&self,
|
||||
_node: &ComponentNode,
|
||||
_window: &mut Window,
|
||||
_cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
None
|
||||
}
|
||||
|
||||
fn portal(&self, _props: &serde_json::Value) -> Option<Portal> {
|
||||
Some(Portal {
|
||||
visible: true,
|
||||
close_request_event: None,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Every component name this module paints, and how to build its renderer.
|
||||
pub const RENDERED_COMPONENTS: &[(&str, fn() -> Box<dyn ComponentRenderer>)] = &[
|
||||
(STACK, || Box::new(StackRenderer)),
|
||||
(SCREEN, || Box::new(ScreenRenderer)),
|
||||
(MODAL_SCREEN, || Box::new(ModalScreenRenderer)),
|
||||
(HEADER_CONFIG, || Box::new(HeaderConfigRenderer)),
|
||||
(HEADER_SUBVIEW, || Box::new(HeaderSubviewRenderer)),
|
||||
(CONTENT_WRAPPER, || Box::new(ContentWrapperRenderer)),
|
||||
(FULL_WINDOW_OVERLAY, || Box::new(FullWindowOverlayRenderer)),
|
||||
];
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::state;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Builds a node with the props this module's C++ half would have produced.
|
||||
fn node(component: &str, tag: i32, props: serde_json::Value) -> ComponentNode {
|
||||
ComponentNode {
|
||||
component: component.to_string(),
|
||||
tag,
|
||||
frame: RectF::default(),
|
||||
scale: (1.0, 1.0),
|
||||
props: Arc::new(props),
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn frame(x: f32, y: f32, w: f32, h: f32) -> RectF {
|
||||
RectF { x, y, w, h }
|
||||
}
|
||||
|
||||
/// A tree built from plain values, matching what core would hand a hook.
|
||||
fn tree(
|
||||
nodes: Vec<react_native_gpui::extensions::TestNode>,
|
||||
edges: &[(i32, i32)],
|
||||
top_inset: f32,
|
||||
) -> TreeView<'static> {
|
||||
TreeView::for_test(nodes, edges, top_inset)
|
||||
}
|
||||
|
||||
fn entry(
|
||||
tag: i32,
|
||||
component: &'static str,
|
||||
frame: RectF,
|
||||
props: serde_json::Value,
|
||||
) -> react_native_gpui::extensions::TestNode {
|
||||
react_native_gpui::extensions::TestNode {
|
||||
tag,
|
||||
component: component.to_string(),
|
||||
frame,
|
||||
scale: (1.0, 1.0),
|
||||
props,
|
||||
}
|
||||
}
|
||||
|
||||
/// A stack with one screen, its header config and its content wrapper.
|
||||
///
|
||||
/// `header` is the header config's props and `content_height` the wrapper's
|
||||
/// Yoga height, which the C++ half computes for the whole screen.
|
||||
fn stack_with_header(
|
||||
header: serde_json::Value,
|
||||
content_height: f32,
|
||||
) -> (TreeView<'static>, i32, i32) {
|
||||
let nodes = vec![
|
||||
entry(
|
||||
1,
|
||||
STACK,
|
||||
frame(0.0, 0.0, 400.0, 600.0),
|
||||
serde_json::json!({}),
|
||||
),
|
||||
entry(
|
||||
2,
|
||||
SCREEN,
|
||||
frame(0.0, 0.0, 400.0, 600.0),
|
||||
serde_json::json!({"activityState": 2, "stackPresentation": "card"}),
|
||||
),
|
||||
entry(3, HEADER_CONFIG, frame(0.0, 0.0, 400.0, 56.0), header),
|
||||
entry(
|
||||
4,
|
||||
CONTENT_WRAPPER,
|
||||
frame(0.0, 0.0, 400.0, content_height),
|
||||
serde_json::json!({}),
|
||||
),
|
||||
];
|
||||
let edges = [(1, 2), (2, 3), (2, 4)];
|
||||
(tree(nodes, &edges, 0.0), 3, 4)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_visible_header_pushes_the_content_down_by_its_height() {
|
||||
// Yoga gives the wrapper the whole screen; a visible header takes the
|
||||
// top 56 px of it, so the content starts below the bar.
|
||||
let (tree, _, wrapper) = stack_with_header(serde_json::json!({"title": "Home"}), 600.0);
|
||||
let node = tree.node(wrapper).expect("wrapper is mounted");
|
||||
let laid_out = ContentWrapperRenderer
|
||||
.layout_frame(&node, &tree)
|
||||
.expect("the wrapper is laid out");
|
||||
assert_eq!(laid_out.y, HEADER_HEIGHT);
|
||||
assert_eq!(laid_out.h, 600.0 - HEADER_HEIGHT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hidden_header_leaves_the_content_where_yoga_put_it() {
|
||||
// The C++ half gives a hidden-header screen's wrapper the full height,
|
||||
// so any adjustment here would move the content a second time: the
|
||||
// reported bug was content sitting 56 px above the top of the window.
|
||||
let (tree, _, wrapper) =
|
||||
stack_with_header(serde_json::json!({"title": "Home", "hidden": true}), 600.0);
|
||||
let node = tree.node(wrapper).expect("wrapper is mounted");
|
||||
let laid_out = ContentWrapperRenderer
|
||||
.layout_frame(&node, &tree)
|
||||
.expect("the wrapper is laid out");
|
||||
assert_eq!(laid_out.y, 0.0);
|
||||
assert_eq!(laid_out.h, 600.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_header_sits_below_the_window_inset_and_is_one_bar_tall() {
|
||||
let (tree, header, _) = stack_with_header(serde_json::json!({"title": "Home"}), 600.0);
|
||||
// A window whose title bar takes 28 px: the bar must start below it.
|
||||
let tree = tree_with_inset(&tree, 28.0);
|
||||
let node = tree.node(header).expect("header is mounted");
|
||||
let laid_out = HeaderConfigRenderer
|
||||
.layout_frame(&node, &tree)
|
||||
.expect("the header is laid out");
|
||||
assert_eq!(laid_out.y, 28.0);
|
||||
assert_eq!(laid_out.h, HEADER_HEIGHT);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hidden_header_takes_no_height_and_paints_nothing() {
|
||||
let (tree, header, _) =
|
||||
stack_with_header(serde_json::json!({"title": "Home", "hidden": true}), 600.0);
|
||||
let node = tree.node(header).expect("header is mounted");
|
||||
let laid_out = HeaderConfigRenderer
|
||||
.layout_frame(&node, &tree)
|
||||
.expect("the header is laid out");
|
||||
assert_eq!(laid_out.h, 0.0);
|
||||
assert!(HeaderConfigRenderer.hides_subtree(&node, &tree));
|
||||
assert_eq!(
|
||||
HeaderConfigRenderer.decorate(&node, &tree).display,
|
||||
Some(false)
|
||||
);
|
||||
}
|
||||
|
||||
/// A header carrying a `title` or `center` subview must not paint a second
|
||||
/// title of its own: the subview is the title.
|
||||
#[test]
|
||||
fn a_title_subview_suppresses_the_headers_own_title() {
|
||||
for subview_type in ["title", "center"] {
|
||||
let nodes = vec![
|
||||
entry(
|
||||
1,
|
||||
STACK,
|
||||
frame(0.0, 0.0, 400.0, 600.0),
|
||||
serde_json::json!({}),
|
||||
),
|
||||
entry(
|
||||
2,
|
||||
SCREEN,
|
||||
frame(0.0, 0.0, 400.0, 600.0),
|
||||
serde_json::json!({"activityState": 2, "stackPresentation": "card"}),
|
||||
),
|
||||
entry(
|
||||
3,
|
||||
HEADER_CONFIG,
|
||||
frame(0.0, 0.0, 400.0, 56.0),
|
||||
serde_json::json!({"title": "Details 1"}),
|
||||
),
|
||||
entry(
|
||||
4,
|
||||
HEADER_SUBVIEW,
|
||||
frame(0.0, 0.0, 80.0, 20.0),
|
||||
serde_json::json!({ "type": subview_type }),
|
||||
),
|
||||
];
|
||||
let edges = [(1, 2), (2, 3), (3, 4)];
|
||||
let tree = tree(nodes, &edges, 0.0);
|
||||
let config = tree.node(3).expect("header config is mounted");
|
||||
assert!(
|
||||
has_title_subview(&config, &tree),
|
||||
"a {subview_type} subview paints the title"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_header_without_a_title_subview_paints_its_title() {
|
||||
let (tree, header, _) = stack_with_header(serde_json::json!({"title": "Home"}), 600.0);
|
||||
let node = tree.node(header).expect("header is mounted");
|
||||
assert!(!has_title_subview(&node, &tree));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_header_paints_the_colours_react_native_asked_for() {
|
||||
let (tree, header, _) = stack_with_header(
|
||||
serde_json::json!({
|
||||
"title": "Home",
|
||||
"backgroundColor": 0xff203040u32,
|
||||
}),
|
||||
600.0,
|
||||
);
|
||||
let node = tree.node(header).expect("header is mounted");
|
||||
let decoration = HeaderConfigRenderer.decorate(&node, &tree);
|
||||
assert_eq!(decoration.background, Some(0xff203040));
|
||||
assert_ne!(decoration.display, Some(false));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_header_without_a_background_falls_back_to_the_default() {
|
||||
let (tree, header, _) = stack_with_header(serde_json::json!({"title": "Home"}), 600.0);
|
||||
let node = tree.node(header).expect("header is mounted");
|
||||
assert_eq!(
|
||||
HeaderConfigRenderer.decorate(&node, &tree).background,
|
||||
Some(0xff171923)
|
||||
);
|
||||
}
|
||||
|
||||
/// The same tree as `stack_with_header` but with a window inset.
|
||||
fn tree_with_inset(source: &TreeView<'_>, inset: f32) -> TreeView<'static> {
|
||||
let mut nodes: Vec<react_native_gpui::extensions::TestNode> = Vec::new();
|
||||
let mut edges = Vec::new();
|
||||
for tag in [1, 2, 3, 4] {
|
||||
let node = source.node(tag).expect("node is mounted");
|
||||
nodes.push(react_native_gpui::extensions::TestNode {
|
||||
tag,
|
||||
component: node.component,
|
||||
frame: node.frame,
|
||||
scale: node.scale,
|
||||
props: (*node.props).clone(),
|
||||
});
|
||||
if let Some(parent) = node.parent {
|
||||
edges.push((parent, tag));
|
||||
}
|
||||
}
|
||||
tree(nodes, &edges, inset)
|
||||
}
|
||||
}
|
||||
@@ -1,396 +0,0 @@
|
||||
//! The stack observer: everything react-native-screens does between a mount
|
||||
//! batch and a paint.
|
||||
//!
|
||||
//! Core tells an observer about a batch before it applies it, about each insert
|
||||
//! and removal as it applies them, and about the batch once it is done. All the
|
||||
//! navigation decisions — which stack is nested, which card is leaving, when a
|
||||
//! lifecycle event fires, when a card is kept alive for its exit — are made here
|
||||
//! and remembered in `state`.
|
||||
use crate::props;
|
||||
use crate::state::{self, PopPlan, Role};
|
||||
use crate::transition::{self, Role as TransitionRole};
|
||||
use react_native_gpui::extensions::{
|
||||
BatchOpKind, BatchView, ComponentNode, StoreHandle, StoreObserver, TreeView,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// The component names this module owns.
|
||||
pub const STACK: &str = "RNSScreenStack";
|
||||
pub const SCREEN: &str = "RNSScreen";
|
||||
pub const HEADER_CONFIG: &str = "RNSScreenStackHeaderConfig";
|
||||
pub const HEADER_SUBVIEW: &str = "RNSScreenStackHeaderSubview";
|
||||
pub const CONTENT_WRAPPER: &str = "RNSScreenContentWrapper";
|
||||
pub const MODAL_SCREEN: &str = "RNSModalScreen";
|
||||
pub const FULL_WINDOW_OVERLAY: &str = "RNSFullWindowOverlay";
|
||||
|
||||
/// Watches the batch and drives the stack.
|
||||
pub struct StackObserver;
|
||||
|
||||
impl StoreObserver for StackObserver {
|
||||
fn before_batch(
|
||||
&self,
|
||||
batch: &BatchView,
|
||||
tree: &TreeView,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) {
|
||||
let nested = nested_stacks(batch, tree);
|
||||
if std::env::var_os("RN_GPUI_DEBUG_SCREENS").is_some() && !nested.is_empty() {
|
||||
log::info!("[screens] nested stack tags={nested:?}");
|
||||
}
|
||||
let plans = pop_plans(batch, tree, &nested, handle, cx);
|
||||
for (tag, plan) in &plans {
|
||||
if plan.retain {
|
||||
handle.retain_subtree(*tag);
|
||||
}
|
||||
}
|
||||
state::with(|state| {
|
||||
state.nested_stacks = nested;
|
||||
state.pop_plans = plans;
|
||||
});
|
||||
}
|
||||
|
||||
fn on_mount(&self, node: &ComponentNode, _handle: &StoreHandle, _cx: &mut gpui::App) {
|
||||
if node.component == FULL_WINDOW_OVERLAY {
|
||||
log::info!(
|
||||
"[screens] full window overlay created tag={} frame={}x{}",
|
||||
node.tag,
|
||||
node.frame.w,
|
||||
node.frame.h
|
||||
);
|
||||
} else if crate::unsupported::unsupported_component(&node.component) {
|
||||
log::error!(
|
||||
"[screens] component {} is not implemented on gpui",
|
||||
node.component
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn on_insert(
|
||||
&self,
|
||||
child: &ComponentNode,
|
||||
parent: i32,
|
||||
previous_children: &[i32],
|
||||
tree: &TreeView,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) {
|
||||
if child.component != SCREEN || !is_outer_stack(parent) {
|
||||
return;
|
||||
}
|
||||
let animation = props::stack_animation(child).to_string();
|
||||
let presentation = props::stack_presentation(child).to_string();
|
||||
log::info!(
|
||||
"[screens] screen inserted tag={} animation={} presentation={} reduce_motion={}",
|
||||
child.tag,
|
||||
animation,
|
||||
presentation,
|
||||
!tree.animations_enabled()
|
||||
);
|
||||
let previous_top = previous_children
|
||||
.iter()
|
||||
.rev()
|
||||
.copied()
|
||||
.find(|tag| tree.node(*tag).is_some_and(|node| node.component == SCREEN));
|
||||
let mode = transition::transition_mode(&animation).filter(|_| tree.animations_enabled());
|
||||
if let Some(mode) = mode {
|
||||
log::info!(
|
||||
"[screens] push transition incoming={} outgoing={:?} animation={} mode={mode:?}",
|
||||
child.tag,
|
||||
previous_top,
|
||||
animation
|
||||
);
|
||||
if presentation == "push" {
|
||||
if let Some(previous) = previous_top {
|
||||
let distance = transition::PARALLAX_DISTANCE;
|
||||
handle.start_transition(
|
||||
previous,
|
||||
transition::transition(mode, TransitionRole::Exit, distance),
|
||||
cx,
|
||||
);
|
||||
state::with(|state| {
|
||||
state.roles.insert(previous, (Role::Exit, distance));
|
||||
});
|
||||
}
|
||||
}
|
||||
handle.start_transition(
|
||||
child.tag,
|
||||
transition::transition(mode, TransitionRole::Enter, 1.0),
|
||||
cx,
|
||||
);
|
||||
state::with(|state| {
|
||||
state.roles.insert(child.tag, (Role::Enter, 1.0));
|
||||
});
|
||||
}
|
||||
crate::lifecycle::dispatch_will(handle, previous_top, Some(child.tag), cx);
|
||||
match mode {
|
||||
Some(mode) => {
|
||||
let outgoing = previous_top;
|
||||
let incoming = Some(child.tag);
|
||||
let handle = handle.clone();
|
||||
handle.after(mode.duration(), cx, move |handle, cx| {
|
||||
if let Some(outgoing) = outgoing {
|
||||
handle.clear_transition(outgoing, cx);
|
||||
}
|
||||
crate::lifecycle::dispatch_did(handle, outgoing, incoming, cx);
|
||||
});
|
||||
}
|
||||
None => crate::lifecycle::dispatch_did(handle, previous_top, Some(child.tag), cx),
|
||||
}
|
||||
}
|
||||
|
||||
fn on_remove_child(
|
||||
&self,
|
||||
child: i32,
|
||||
parent: i32,
|
||||
retained: bool,
|
||||
tree: &TreeView,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) {
|
||||
let Some(node) = tree.node(child) else {
|
||||
return;
|
||||
};
|
||||
if node.component != SCREEN {
|
||||
return;
|
||||
}
|
||||
let plan = state::with(|state| state.pop_plans.remove(&child));
|
||||
let Some(plan) = plan else {
|
||||
return;
|
||||
};
|
||||
let _ = parent;
|
||||
if let Some(revealed) = plan.revealed {
|
||||
handle.clear_transition(revealed, cx);
|
||||
}
|
||||
let native_dismiss = state::with(|state| state.native_dismiss_pending == Some(child));
|
||||
let mode = transition::transition_mode(&plan.animation)
|
||||
.filter(|_| tree.animations_enabled())
|
||||
.map(transition::TransitionMode::reverse);
|
||||
let retain = plan.retain && mode.is_some();
|
||||
log::info!(
|
||||
"[screens] pop transition outgoing={} incoming={:?} animation={} retained={retain}",
|
||||
child,
|
||||
plan.revealed,
|
||||
plan.animation
|
||||
);
|
||||
if retain {
|
||||
if let Some(mode) = mode {
|
||||
handle.start_transition(
|
||||
child,
|
||||
transition::transition(mode, TransitionRole::Exit, 1.0),
|
||||
cx,
|
||||
);
|
||||
state::with(|state| {
|
||||
state.roles.insert(child, (Role::Exit, 1.0));
|
||||
});
|
||||
}
|
||||
}
|
||||
if !native_dismiss {
|
||||
crate::lifecycle::dispatch_will(handle, Some(child), plan.revealed, cx);
|
||||
}
|
||||
if retained {
|
||||
let duration = mode.map(|mode| mode.duration()).unwrap_or_default();
|
||||
let revealed = plan.revealed;
|
||||
let handle = handle.clone();
|
||||
handle.after(duration, cx, move |handle, cx| {
|
||||
handle.release_retained(child, cx);
|
||||
log::info!("[screens] pop finished outgoing={child}");
|
||||
crate::lifecycle::dispatch_did(handle, Some(child), revealed, cx);
|
||||
});
|
||||
} else if !native_dismiss {
|
||||
crate::lifecycle::dispatch_did(handle, Some(child), plan.revealed, cx);
|
||||
}
|
||||
state::with(|state| {
|
||||
if state.native_dismiss_pending == Some(child) {
|
||||
state.native_dismiss_pending = None;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn on_remove(&self, tag: i32, _handle: &StoreHandle, _cx: &mut gpui::App) {
|
||||
state::with(|state| {
|
||||
state.pop_plans.remove(&tag);
|
||||
state.roles.remove(&tag);
|
||||
state.headers.remove(&tag);
|
||||
});
|
||||
}
|
||||
|
||||
fn on_update(
|
||||
&self,
|
||||
node: &ComponentNode,
|
||||
previous: &ComponentNode,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) {
|
||||
if node.component != SCREEN {
|
||||
return;
|
||||
}
|
||||
let before = props::activity_state(previous);
|
||||
let after = props::activity_state(node);
|
||||
if before == after {
|
||||
return;
|
||||
}
|
||||
for event in crate::lifecycle::lifecycle_events(before, after) {
|
||||
log::info!(
|
||||
"[screens] tag={} activity={}->{} event={event}",
|
||||
node.tag,
|
||||
before,
|
||||
after
|
||||
);
|
||||
handle.dispatch_event(node.tag, event, &serde_json::json!({}), cx);
|
||||
}
|
||||
}
|
||||
|
||||
fn after_batch(&self, _tree: &TreeView, _handle: &StoreHandle, _cx: &mut gpui::App) {
|
||||
state::clear_batch_state();
|
||||
}
|
||||
|
||||
fn on_transition_frame(
|
||||
&self,
|
||||
tag: i32,
|
||||
progress: f32,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) {
|
||||
let forward = state::with(|state| {
|
||||
state
|
||||
.roles
|
||||
.get(&tag)
|
||||
.is_none_or(|(role, distance)| *role == Role::Enter || *distance < 1.0)
|
||||
});
|
||||
handle.dispatch_event(
|
||||
tag,
|
||||
"topTransitionProgress",
|
||||
&serde_json::json!({
|
||||
"progress": progress,
|
||||
"closing": if forward { 0 } else { 1 },
|
||||
"goingForward": if forward { 1 } else { 0 }
|
||||
}),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `stack` is the outer stack, the only one that animates.
|
||||
fn is_outer_stack(stack: i32) -> bool {
|
||||
!state::with(|state| state.nested_stacks.contains(&stack))
|
||||
}
|
||||
|
||||
/// Every stack that sits inside a screen, found from the batch topology before
|
||||
/// any of it is applied.
|
||||
fn nested_stacks(batch: &BatchView, tree: &TreeView) -> HashSet<i32> {
|
||||
let stacks: Vec<i32> = batch
|
||||
.ops
|
||||
.iter()
|
||||
.filter(|op| op.kind == BatchOpKind::Create && op.component == STACK)
|
||||
.map(|op| op.tag)
|
||||
.chain(
|
||||
batch
|
||||
.ops
|
||||
.iter()
|
||||
.filter(|op| op.kind != BatchOpKind::Create && op.component == STACK)
|
||||
.map(|op| op.tag),
|
||||
)
|
||||
.collect();
|
||||
let mut nested = HashSet::new();
|
||||
for stack in stacks {
|
||||
let mut cursor = Some(stack);
|
||||
let mut visited = HashSet::new();
|
||||
while let Some(tag) = cursor {
|
||||
if !visited.insert(tag) {
|
||||
break;
|
||||
}
|
||||
let Some(parent) = batch.parent_of(tag).or_else(|| tree.parent(tag)) else {
|
||||
break;
|
||||
};
|
||||
if batch.component_of(parent, tree).as_deref() == Some(SCREEN) {
|
||||
nested.insert(stack);
|
||||
break;
|
||||
}
|
||||
cursor = Some(parent);
|
||||
}
|
||||
}
|
||||
nested
|
||||
}
|
||||
|
||||
/// The cards leaving in this batch that should animate their exit.
|
||||
fn pop_plans(
|
||||
batch: &BatchView,
|
||||
tree: &TreeView,
|
||||
nested: &HashSet<i32>,
|
||||
handle: &StoreHandle,
|
||||
cx: &mut gpui::App,
|
||||
) -> std::collections::HashMap<i32, PopPlan> {
|
||||
let removed: HashSet<i32> = batch
|
||||
.ops
|
||||
.iter()
|
||||
.filter(|op| op.kind == BatchOpKind::Remove && op.component == SCREEN)
|
||||
.map(|op| op.tag)
|
||||
.collect();
|
||||
let mut plans = std::collections::HashMap::new();
|
||||
for op in batch
|
||||
.ops
|
||||
.iter()
|
||||
.filter(|op| op.kind == BatchOpKind::Remove && op.component == SCREEN)
|
||||
{
|
||||
if nested.contains(&op.parent_tag) {
|
||||
continue;
|
||||
}
|
||||
let Some(stack) = tree.node(op.parent_tag) else {
|
||||
continue;
|
||||
};
|
||||
if stack.component != STACK {
|
||||
continue;
|
||||
}
|
||||
let live: Vec<i32> = tree
|
||||
.children(op.parent_tag)
|
||||
.into_iter()
|
||||
.filter(|tag| {
|
||||
tree.is_live(*tag) && tree.node(*tag).is_some_and(|node| node.component == SCREEN)
|
||||
})
|
||||
.collect();
|
||||
if live.last().copied() != Some(op.tag) {
|
||||
continue;
|
||||
}
|
||||
let revealed = live
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|tag| !removed.contains(tag))
|
||||
.copied();
|
||||
let Some(screen) = tree.node(op.tag) else {
|
||||
continue;
|
||||
};
|
||||
let animation = props::stack_animation(&screen).to_string();
|
||||
let native_dismiss = state::with(|state| state.native_dismiss_pending == Some(op.tag));
|
||||
let mode = transition::transition_mode(&animation).filter(|_| tree.animations_enabled());
|
||||
let retain = !native_dismiss && revealed.is_some() && mode.is_some();
|
||||
if retain {
|
||||
log::info!(
|
||||
"[screens] preserve outgoing={} descendants={}",
|
||||
op.tag,
|
||||
descendant_count(op.tag, tree)
|
||||
);
|
||||
}
|
||||
plans.insert(
|
||||
op.tag,
|
||||
PopPlan {
|
||||
revealed,
|
||||
animation,
|
||||
duration: mode.map(|mode| mode.duration()).unwrap_or_default(),
|
||||
retain,
|
||||
},
|
||||
);
|
||||
}
|
||||
plans
|
||||
}
|
||||
|
||||
/// How many nodes sit below `tag`, at any depth.
|
||||
fn descendant_count(tag: i32, tree: &TreeView) -> usize {
|
||||
let mut total = 0;
|
||||
let mut stack = tree.children(tag);
|
||||
while let Some(next) = stack.pop() {
|
||||
total += 1;
|
||||
stack.extend(tree.children(next));
|
||||
}
|
||||
total
|
||||
}
|
||||
@@ -1,162 +0,0 @@
|
||||
//! The module's own state, kept outside core.
|
||||
//!
|
||||
//! Core hands an observer facts about a batch and forgets them; everything a
|
||||
//! stack has to remember between batches — which stacks are nested, which cards
|
||||
//! are leaving, what a dismissal already animated — is here.
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::time::Duration;
|
||||
|
||||
/// What `decorate` learned about a header, for `content` to use.
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct HeaderFacts {
|
||||
pub is_top: bool,
|
||||
pub shows_back: bool,
|
||||
pub has_title_subview: bool,
|
||||
/// The screen this header belongs to, which is where a back-button event
|
||||
/// has to go: React Native declares `onHeaderBackButtonClicked` on
|
||||
/// `RNSScreen`, not on the header config.
|
||||
pub screen: Option<i32>,
|
||||
}
|
||||
|
||||
/// Records what `decorate` learned, so `content` can read it.
|
||||
pub fn set_header_facts(
|
||||
tag: i32,
|
||||
is_top: bool,
|
||||
shows_back: bool,
|
||||
has_title_subview: bool,
|
||||
screen: Option<i32>,
|
||||
) {
|
||||
with(|state| {
|
||||
state.headers.insert(
|
||||
tag,
|
||||
HeaderFacts {
|
||||
is_top,
|
||||
shows_back,
|
||||
has_title_subview,
|
||||
screen,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Whether this header is the top screen's, as of the last `decorate` call.
|
||||
pub fn header_is_top(tag: i32) -> bool {
|
||||
with(|state| state.headers.get(&tag).is_some_and(|facts| facts.is_top))
|
||||
}
|
||||
|
||||
/// Whether this header carries a `title` subview, as of the last `decorate` call.
|
||||
///
|
||||
/// When it does, that subview paints the title and core must not paint its own.
|
||||
pub fn header_has_title_subview(tag: i32) -> bool {
|
||||
with(|state| {
|
||||
state
|
||||
.headers
|
||||
.get(&tag)
|
||||
.is_some_and(|facts| facts.has_title_subview)
|
||||
})
|
||||
}
|
||||
|
||||
/// The screen a header belongs to, as of the last `decorate` call.
|
||||
pub fn header_screen(tag: i32) -> Option<i32> {
|
||||
with(|state| state.headers.get(&tag).and_then(|facts| facts.screen))
|
||||
}
|
||||
|
||||
/// Whether this header shows a back button, as of the last `decorate` call.
|
||||
pub fn header_shows_back(tag: i32) -> bool {
|
||||
with(|state| {
|
||||
state
|
||||
.headers
|
||||
.get(&tag)
|
||||
.is_some_and(|facts| facts.shows_back)
|
||||
})
|
||||
}
|
||||
|
||||
/// A card that is leaving, and what should happen when it is gone.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PopPlan {
|
||||
/// The screen that becomes top when this one is gone.
|
||||
pub revealed: Option<i32>,
|
||||
/// The animation name React Native asked for, unparsed.
|
||||
pub animation: String,
|
||||
/// How long the exit runs.
|
||||
pub duration: Duration,
|
||||
/// Whether the card must stay attached so its exit can paint.
|
||||
pub retain: bool,
|
||||
}
|
||||
|
||||
/// Everything the module remembers between batches.
|
||||
#[derive(Default)]
|
||||
pub struct State {
|
||||
/// A native dismissal already ran the exit animation, so the removal must
|
||||
/// not run a second one and must not send the "will" events again.
|
||||
pub native_dismiss_pending: Option<i32>,
|
||||
/// Stacks that sit inside a screen, so only the outer stack animates.
|
||||
pub nested_stacks: HashSet<i32>,
|
||||
/// Cards leaving in this batch, by tag.
|
||||
pub pop_plans: HashMap<i32, PopPlan>,
|
||||
/// The cards this module started a transition on: whether it is an entry
|
||||
/// (the screen is arriving) or an exit, and how far it travels.
|
||||
pub roles: HashMap<i32, (Role, f32)>,
|
||||
/// The latest facts about each header, written by `decorate`.
|
||||
pub headers: HashMap<i32, HeaderFacts>,
|
||||
}
|
||||
|
||||
/// Whether a transition the module started is an arrival or a departure.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Role {
|
||||
Enter,
|
||||
Exit,
|
||||
}
|
||||
|
||||
fn state() -> &'static Mutex<State> {
|
||||
static STATE: OnceLock<Mutex<State>> = OnceLock::new();
|
||||
STATE.get_or_init(|| Mutex::new(State::default()))
|
||||
}
|
||||
|
||||
/// Runs `f` with the module state.
|
||||
pub fn with<R>(f: impl FnOnce(&mut State) -> R) -> R {
|
||||
let mut state = state()
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
f(&mut state)
|
||||
}
|
||||
|
||||
/// Forgets the per-batch decisions. Call from `after_batch`.
|
||||
pub fn clear_batch_state() {
|
||||
with(|state| {
|
||||
state.pop_plans.clear();
|
||||
});
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn batch_state_is_cleared_but_lifetimes_are_not() {
|
||||
with(|state| {
|
||||
state.pop_plans.insert(
|
||||
7,
|
||||
PopPlan {
|
||||
revealed: Some(3),
|
||||
animation: "slide_from_right".to_string(),
|
||||
duration: Duration::from_millis(240),
|
||||
retain: true,
|
||||
},
|
||||
);
|
||||
state.nested_stacks.insert(9);
|
||||
state.native_dismiss_pending = Some(7);
|
||||
});
|
||||
clear_batch_state();
|
||||
with(|state| {
|
||||
assert!(state.pop_plans.is_empty());
|
||||
assert!(state.nested_stacks.contains(&9));
|
||||
assert_eq!(state.native_dismiss_pending, Some(7));
|
||||
});
|
||||
with(|state| {
|
||||
state.nested_stacks.clear();
|
||||
state.native_dismiss_pending = None;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,193 +0,0 @@
|
||||
//! The animation modes a stack accepts, and the poses each one corresponds to.
|
||||
//!
|
||||
//! Core owns the timing and the paint; this file is the module's own statement
|
||||
//! of what "slide from the right" and "fade" mean as movement between two
|
||||
//! poses, so core never learns a navigation name.
|
||||
use react_native_gpui::extensions::{Pose, Transition};
|
||||
use std::time::Duration;
|
||||
|
||||
/// A stack animation, as `stackAnimation` names it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum TransitionMode {
|
||||
Fade,
|
||||
SlideFromRight,
|
||||
SlideFromLeft,
|
||||
}
|
||||
|
||||
impl TransitionMode {
|
||||
/// The mode the reverse of this one uses, so a pop plays the entry mirrored.
|
||||
#[must_use]
|
||||
pub fn reverse(self) -> Self {
|
||||
match self {
|
||||
Self::SlideFromRight => Self::SlideFromLeft,
|
||||
Self::SlideFromLeft => Self::SlideFromRight,
|
||||
Self::Fade => Self::Fade,
|
||||
}
|
||||
}
|
||||
|
||||
/// How long this mode runs.
|
||||
pub fn duration(self) -> Duration {
|
||||
match self {
|
||||
Self::Fade => Duration::from_millis(150),
|
||||
_ => Duration::from_millis(240),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mode a `stackAnimation` string asks for. Unknown values fade, which is
|
||||
/// what the platform's own default is.
|
||||
pub fn transition_mode(animation: &str) -> Option<TransitionMode> {
|
||||
match animation {
|
||||
"none" => None,
|
||||
"slide_from_right" | "simple_push" => Some(TransitionMode::SlideFromRight),
|
||||
"slide_from_left" => Some(TransitionMode::SlideFromLeft),
|
||||
"default" | "" | "fade" => Some(TransitionMode::Fade),
|
||||
_ => Some(TransitionMode::Fade),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether entering or leaving.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Role {
|
||||
Enter,
|
||||
Exit,
|
||||
}
|
||||
|
||||
/// The poses a mode and role correspond to.
|
||||
///
|
||||
/// Entering slides from off-screen to rest; leaving slides from rest to
|
||||
/// off-screen; a fade keeps the node still while its opacity moves. The offset
|
||||
/// is a fraction of the node's own width, so `distance` is how far it travels.
|
||||
pub fn poses(mode: TransitionMode, role: Role, distance: f32) -> (Pose, Pose) {
|
||||
let rest = Pose {
|
||||
offset_x: 0.0,
|
||||
opacity: 1.0,
|
||||
};
|
||||
let transparent = Pose {
|
||||
offset_x: 0.0,
|
||||
opacity: 0.0,
|
||||
};
|
||||
let off_screen = |sign: f32| Pose {
|
||||
offset_x: sign * distance,
|
||||
opacity: 1.0,
|
||||
};
|
||||
match (mode, role) {
|
||||
(TransitionMode::Fade, Role::Enter) => (transparent, rest),
|
||||
(TransitionMode::Fade, Role::Exit) => (rest, transparent),
|
||||
(TransitionMode::SlideFromRight, Role::Enter) => (off_screen(1.0), rest),
|
||||
(TransitionMode::SlideFromLeft, Role::Enter) => (off_screen(-1.0), rest),
|
||||
(TransitionMode::SlideFromRight, Role::Exit) => (rest, off_screen(-1.0)),
|
||||
(TransitionMode::SlideFromLeft, Role::Exit) => (rest, off_screen(1.0)),
|
||||
}
|
||||
}
|
||||
|
||||
/// The transition a mode and role describe.
|
||||
pub fn transition(mode: TransitionMode, role: Role, distance: f32) -> Transition {
|
||||
let (from, to) = poses(mode, role, distance);
|
||||
Transition {
|
||||
from,
|
||||
to,
|
||||
duration: mode.duration(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The card underneath a push parallaxes a short way rather than leaving.
|
||||
pub const PARALLAX_DISTANCE: f32 = 0.12;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn maps_supported_animations_and_falls_back_to_fade() {
|
||||
assert_eq!(transition_mode("none"), None);
|
||||
assert_eq!(transition_mode("fade"), Some(TransitionMode::Fade));
|
||||
assert_eq!(transition_mode("default"), Some(TransitionMode::Fade));
|
||||
assert_eq!(transition_mode(""), Some(TransitionMode::Fade));
|
||||
assert_eq!(
|
||||
transition_mode("slide_from_right"),
|
||||
Some(TransitionMode::SlideFromRight)
|
||||
);
|
||||
assert_eq!(
|
||||
transition_mode("simple_push"),
|
||||
Some(TransitionMode::SlideFromRight)
|
||||
);
|
||||
assert_eq!(
|
||||
transition_mode("slide_from_left"),
|
||||
Some(TransitionMode::SlideFromLeft)
|
||||
);
|
||||
assert_eq!(transition_mode("unsupported"), Some(TransitionMode::Fade));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reversing_a_slide_swaps_its_direction() {
|
||||
assert_eq!(
|
||||
TransitionMode::SlideFromRight.reverse(),
|
||||
TransitionMode::SlideFromLeft
|
||||
);
|
||||
assert_eq!(
|
||||
TransitionMode::SlideFromLeft.reverse(),
|
||||
TransitionMode::SlideFromRight
|
||||
);
|
||||
assert_eq!(TransitionMode::Fade.reverse(), TransitionMode::Fade);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_poses_reproduce_the_original_slide_and_fade_curves() {
|
||||
for (mode, role, distance, expected) in [
|
||||
(
|
||||
TransitionMode::SlideFromRight,
|
||||
Role::Enter,
|
||||
1.0,
|
||||
[1.0, 0.5, 0.0],
|
||||
),
|
||||
(
|
||||
TransitionMode::SlideFromLeft,
|
||||
Role::Enter,
|
||||
1.0,
|
||||
[-1.0, -0.5, 0.0],
|
||||
),
|
||||
(
|
||||
TransitionMode::SlideFromRight,
|
||||
Role::Exit,
|
||||
1.0,
|
||||
[0.0, -0.5, -1.0],
|
||||
),
|
||||
(
|
||||
TransitionMode::SlideFromLeft,
|
||||
Role::Exit,
|
||||
1.0,
|
||||
[0.0, 0.5, 1.0],
|
||||
),
|
||||
(
|
||||
TransitionMode::SlideFromRight,
|
||||
Role::Exit,
|
||||
PARALLAX_DISTANCE,
|
||||
[0.0, -0.06, -0.12],
|
||||
),
|
||||
] {
|
||||
let transition = transition(mode, role, distance);
|
||||
for (progress, expected_offset) in
|
||||
[(0.0, expected[0]), (0.5, expected[1]), (1.0, expected[2])]
|
||||
{
|
||||
let pose = transition.pose_at(progress);
|
||||
assert!(
|
||||
(pose.offset_x - expected_offset).abs() < 0.001,
|
||||
"{mode:?} {role:?} at {progress}: {} != {expected_offset}",
|
||||
pose.offset_x
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_fade_moves_opacity_and_nothing_else() {
|
||||
let transition = transition(TransitionMode::Fade, Role::Enter, 1.0);
|
||||
for (progress, expected) in [(0.0, 0.0), (0.5, 0.5), (1.0, 1.0)] {
|
||||
let pose = transition.pose_at(progress);
|
||||
assert!((pose.opacity - expected).abs() < 0.001);
|
||||
assert!(pose.offset_x.abs() < 0.001);
|
||||
}
|
||||
assert_eq!(transition.duration, Duration::from_millis(150));
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
//! Components react-native-screens ships that this platform does not implement.
|
||||
//!
|
||||
//! They are named here so the module can say so once, instead of core carrying a
|
||||
//! list of a library's components.
|
||||
/// Whether this component is one the platform cannot render.
|
||||
pub fn unsupported_component(name: &str) -> bool {
|
||||
matches!(
|
||||
name,
|
||||
"RNSSearchBar"
|
||||
| "RNSBottomTabs"
|
||||
| "RNSBottomTabsScreen"
|
||||
| "RNSScreenFooter"
|
||||
| "RNSSplitView"
|
||||
| "RNSSplitViewScreen"
|
||||
) || name.starts_with("RNSSplitView")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn identifies_the_components_the_platform_cannot_render() {
|
||||
for name in [
|
||||
"RNSSearchBar",
|
||||
"RNSBottomTabs",
|
||||
"RNSBottomTabsScreen",
|
||||
"RNSScreenFooter",
|
||||
"RNSSplitView",
|
||||
"RNSSplitViewScreen",
|
||||
] {
|
||||
assert!(unsupported_component(name), "{name} should be unsupported");
|
||||
}
|
||||
assert!(unsupported_component("RNSSplitViewSomethingElse"));
|
||||
for name in ["RNSScreen", "RNSScreenStack", "View", "RNSScreenContainer"] {
|
||||
assert!(!unsupported_component(name), "{name} should be supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
---
|
||||
id: navigation
|
||||
title: Navigating Between Screens
|
||||
---
|
||||
|
||||
[React Navigation](https://reactnavigation.org) works on desktop with its native stack. Follow React Native's [navigation guide](https://reactnative.dev/docs/0.87/navigation) and the [React Navigation getting started guide](https://reactnavigation.org/docs/getting-started); this page lists only what is different on desktop.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the desktop packages next to React Navigation's peer dependencies:
|
||||
|
||||
```shell
|
||||
npm install @react-navigation/native @react-navigation/native-stack
|
||||
npm install react-native-screens react-native-safe-area-context
|
||||
npm install @react-native-gpui/screens @react-native-gpui/safe-area-context
|
||||
```
|
||||
|
||||
See [Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries) for the versions that the desktop packages support. Autolinking builds the native code, so there is no extra linking step. Run the app with `npx react-native run-desktop`.
|
||||
|
||||
## The native stack
|
||||
|
||||
The `native-stack` navigator is drawn by react-native-gpui through `react-native-screens`, so the same code runs on macOS, Windows and Linux.
|
||||
|
||||
## Mouse back and forward buttons
|
||||
|
||||
The back button fires `BackHandler`'s `hardwareBackPress`, which React Navigation already handles. The forward button moves forward in a stack with `useDesktopForwardNavigation` from `@react-native-gpui/core/navigation`:
|
||||
|
||||
```tsx
|
||||
import { useDesktopForwardNavigation } from '@react-native-gpui/core/navigation';
|
||||
|
||||
function App() {
|
||||
const navigationRef = useNavigationContainerRef();
|
||||
useDesktopForwardNavigation(navigationRef);
|
||||
return (
|
||||
<NavigationContainer ref={navigationRef}>{/* ... */}</NavigationContainer>
|
||||
);
|
||||
}
|
||||
```
|
||||
@@ -1,35 +0,0 @@
|
||||
{
|
||||
"name": "@react-native-gpui/screens",
|
||||
"version": "4.28.0-alpha.2",
|
||||
"description": "Adapter for react-native-screens (MIT): draws native stacks and screens, as used by React Navigation",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/streetwriters/react-native-gpui-modules.git",
|
||||
"directory": "modules/screens"
|
||||
},
|
||||
"homepage": "https://github.com/streetwriters/react-native-gpui-modules/tree/main/modules/screens#readme",
|
||||
"bugs": {
|
||||
"url": "https://github.com/streetwriters/react-native-gpui-modules/issues"
|
||||
},
|
||||
"files": [
|
||||
"desktop",
|
||||
"react-native.config.js",
|
||||
"src",
|
||||
"README.md",
|
||||
"LICENSE",
|
||||
"CHANGELOG.md",
|
||||
"!**/*.test.js",
|
||||
"!**/*.test.mjs",
|
||||
"!**/*.test.ts",
|
||||
"!**/*.test.cjs"
|
||||
],
|
||||
"peerDependencies": {
|
||||
"react-native": "0.87.x",
|
||||
"react-native-screens": "4.28.x"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"tag": "latest"
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
module.exports = {
|
||||
dependency: {
|
||||
platforms: {
|
||||
desktop: {
|
||||
cmake: 'desktop',
|
||||
crate: 'rn-gpui-screens',
|
||||
componentCodegenPackage: 'react-native-screens',
|
||||
componentDescriptors: [
|
||||
'RNSScreenComponentDescriptor',
|
||||
'RNSScreenContainerComponentDescriptor',
|
||||
'RNSScreenContentWrapperComponentDescriptor',
|
||||
'RNSScreenNavigationContainerComponentDescriptor',
|
||||
'RNSScreenStackComponentDescriptor',
|
||||
'RNSScreenStackHeaderConfigComponentDescriptor',
|
||||
'RNSScreenStackHeaderSubviewComponentDescriptor',
|
||||
'RNSModalScreenComponentDescriptor',
|
||||
'RNSFullWindowOverlayComponentDescriptor',
|
||||
],
|
||||
componentHeaders: [
|
||||
'react/renderer/components/rnscreens/ComponentDescriptors.h',
|
||||
'react/renderer/components/rnscreens/RNSScreenComponentDescriptor.h',
|
||||
'react/renderer/components/rnscreens/RNSScreenStackHeaderConfigComponentDescriptor.h',
|
||||
'react/renderer/components/rnscreens/RNSScreenStackHeaderSubviewComponentDescriptor.h',
|
||||
'react/renderer/components/rnscreens/RNSModalScreenComponentDescriptor.h',
|
||||
'react/renderer/components/rnscreens/RNSFullWindowOverlayComponentDescriptor.h',
|
||||
],
|
||||
// Metro: replace these files of installed packages for gpui.
|
||||
sourceOverrides: {
|
||||
'react-native-screens/src/core.ts': './src/core.desktop.ts',
|
||||
'react-native-screens/src/components/FullWindowOverlay.tsx':
|
||||
'./src/FullWindowOverlay.desktop.tsx',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
@@ -1,19 +0,0 @@
|
||||
import React, { type PropsWithChildren } from 'react';
|
||||
import { StyleSheet, useWindowDimensions } from 'react-native';
|
||||
import FullWindowOverlayNativeComponent from 'react-native-screens/src/fabric/FullWindowOverlayNativeComponent';
|
||||
import type { NativeProps } from 'react-native-screens/src/fabric/FullWindowOverlayNativeComponent';
|
||||
|
||||
export default function FullWindowOverlay({
|
||||
children,
|
||||
...props
|
||||
}: PropsWithChildren<NativeProps>) {
|
||||
const { width, height } = useWindowDimensions();
|
||||
return (
|
||||
<FullWindowOverlayNativeComponent
|
||||
{...props}
|
||||
style={[StyleSheet.absoluteFill, { width, height }, props.style]}
|
||||
>
|
||||
{children}
|
||||
</FullWindowOverlayNativeComponent>
|
||||
);
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
import './customDirectEventTypes.desktop';
|
||||
|
||||
// GPUI always ships the native screens implementation.
|
||||
export const isNativePlatformSupported = true;
|
||||
|
||||
let ENABLE_SCREENS = isNativePlatformSupported;
|
||||
|
||||
export function enableScreens(shouldEnableScreens = true) {
|
||||
ENABLE_SCREENS = shouldEnableScreens;
|
||||
}
|
||||
|
||||
let ENABLE_FREEZE = false;
|
||||
|
||||
export function enableFreeze(shouldEnableReactFreeze = true) {
|
||||
if (!isNativePlatformSupported) {
|
||||
return;
|
||||
}
|
||||
|
||||
ENABLE_FREEZE = shouldEnableReactFreeze;
|
||||
}
|
||||
|
||||
export function screensEnabled() {
|
||||
return ENABLE_SCREENS;
|
||||
}
|
||||
|
||||
export function freezeEnabled() {
|
||||
return ENABLE_FREEZE;
|
||||
}
|
||||
-16
@@ -1,16 +0,0 @@
|
||||
// The native stack header dispatches this upstream screen event to notify
|
||||
// React Navigation that the user requested a native back action.
|
||||
import { customDirectEventTypes } from 'react-native/Libraries/Renderer/shims/ReactNativeViewConfigRegistry';
|
||||
|
||||
customDirectEventTypes.topHeaderBackButtonClicked = {
|
||||
registrationName: 'onHeaderBackButtonClicked',
|
||||
};
|
||||
|
||||
customDirectEventTypes.topWillAppear = { registrationName: 'onWillAppear' };
|
||||
customDirectEventTypes.topWillDisappear = {
|
||||
registrationName: 'onWillDisappear',
|
||||
};
|
||||
customDirectEventTypes.topAppear = { registrationName: 'onAppear' };
|
||||
customDirectEventTypes.topDisappear = { registrationName: 'onDisappear' };
|
||||
|
||||
export { customDirectEventTypes };
|
||||
@@ -1,21 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
All notable changes to `@react-native-gpui/svg` are listed here. Each version's section becomes the notes of its GitHub release.
|
||||
|
||||
## 15.15.0-alpha.1 - 2026-11-12
|
||||
|
||||
- A drawing is looked up by a structural hash of its props, children, frame size and scale, so a render that finds it builds no document string. Before, every render built and escaped the whole SVG document just to hash it, even when the raster was cached.
|
||||
- The rasters are kept in one cache shared by every node, bounded by bytes (64 MB; `RN_GPUI_SVG_CACHE_MB` sets another) and dropping the image used longest ago first. It held weak references before, so an icon that left the screen was rasterised again when it came back, and the same icon in two places was drawn twice.
|
||||
- A drawing that is not cached is rasterised on a background thread when it is large (over about 48 CSS pixels square at a scale of 2) or has a filter, or when its node already shows an image (a theme switch). The node keeps showing its previous image until the new one is ready, then is built again; a node with nothing to show yet draws nothing until then. A small icon whose node has no image is still drawn at once, so a row that scrolls in has its icon on the first frame. Several nodes asking for the same drawing share one rasterisation.
|
||||
- The four coverage samples of a drawing are rasterised side by side (a small one, under 128 by 128 pixels, one after the other, because starting a thread costs more than a sample) and combined in a fixed order, so the pixels are the same as when they were drawn one after the other (a test compares them in both ways). A 230 point illustration with drop shadows takes 30 ms to rasterise instead of 120, and now off the main thread.
|
||||
- `RN_GPUI_SVG_TRACE=1` logs each rasterisation with the time of every sample, and `RN_GPUI_SVG_SYNC=1` draws everything on the main thread, for measuring.
|
||||
- An SVG is drawn with the same antialiasing as Chrome: once per coverage sample at the window's scale, without antialiasing, with the four sample positions Chrome uses, and averaged. An icon's edge now takes the same five coverage steps (0, 1/4, 1/2, 3/4, 1) at the same pixels, so the Material star at 16 points differs from Chrome in 5 of 1024 pixels instead of 112, and over 80 icons in 124,480 pixels in 1,288 instead of 7,250. Needs `image` and `smallvec` in the crate.
|
||||
- An SVG is placed at a whole CSS pixel, as Chrome places an inline SVG: a 15 point icon centred in a 20 point button, which lays out at 2.5, is drawn at 3 instead of half a point to the left. That half point was the bulk of the edge pixels that differed from the web app (97 of 1,600 in a toolbar arrow, 2 now).
|
||||
- An element's `transform` is applied: react-native-svg sends it as a six number `matrix`, which was only read when it had sixteen, so every rotated or scaled shape was drawn untransformed.
|
||||
- Filters are drawn: `filter` on a shape or group, `<filter>` and the `feBlend`, `feColorMatrix`, `feComposite`, `feFlood`, `feGaussianBlur`, `feMerge` and `feOffset` primitives reach the renderer, with `sRGB` colour interpolation. An illustration with rotated cards and drop shadows at 230 points differs from Chrome in 249 of 211,600 pixels by more than 8 levels instead of 14,817.
|
||||
- Asks core for each node's subtree, which core now hands a component renderer only when it reads it.
|
||||
|
||||
## 15.15.0-alpha.0 - 2026-11-01
|
||||
|
||||
- First release, for `react-native-svg` 15.15.x.
|
||||
- Renders SVG elements with the desktop renderer.
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Streetwriters
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
# @react-native-gpui/svg
|
||||
|
||||
Adapter for [`react-native-svg`](https://github.com/software-mansion/react-native-svg) (MIT): draws SVG elements with the desktop renderer.
|
||||
|
||||
Part of [react-native-gpui](https://github.com/streetwriters/react-native-gpui), React Native for macOS, Windows and Linux.
|
||||
|
||||
## Install
|
||||
|
||||
Install it next to `react-native-svg`:
|
||||
|
||||
```sh
|
||||
npm install react-native-svg@15.15.x @react-native-gpui/svg
|
||||
```
|
||||
|
||||
Keep using `react-native-svg` as you do on iOS and Android; its JavaScript API is unchanged. Autolinking builds the desktop side when the app is built.
|
||||
|
||||
## What is drawn
|
||||
|
||||
Shapes, paths, text, gradients, masks, clip paths, symbols, patterns and markers, with the transform of each element (react-native-svg sends it as a six number matrix). Filters are drawn too: `filter` on a shape or group and the `feBlend`, `feColorMatrix`, `feComposite`, `feFlood`, `feGaussianBlur`, `feMerge` and `feOffset` primitives, always with sRGB colour interpolation. An icon is drawn with the same antialiasing as Chrome and placed at a whole CSS pixel, as Chrome places an inline SVG.
|
||||
|
||||
Limits: `overflow="visible"` is not honoured (the drawing is clipped to the SVG's frame), the other filter primitives such as `feDropShadow` and `feMorphology` are not passed to the renderer, and a blurred shadow comes out about one level lighter than Chrome's for each shadow that overlaps another.
|
||||
|
||||
## Cost
|
||||
|
||||
A drawing is cached by a hash of what it is made of, so drawing it again costs a hash and a lookup. The cache is shared by every node and holds up to 64 MB of images (`RN_GPUI_SVG_CACHE_MB` changes that). A drawing that is not cached yet is rasterised on a background thread when it is large or filtered, and the node shows its previous image, if it has one, until the new one is ready; a small icon with nothing yet to show is drawn at once. `RN_GPUI_SVG_TRACE=1` logs each rasterisation and `RN_GPUI_SVG_SYNC=1` draws on the main thread, to measure.
|
||||
|
||||
## Documentation
|
||||
|
||||
[Supported libraries](https://streetwriters.github.io/react-native-gpui/docs/supported-libraries)
|
||||
|
||||
## License
|
||||
|
||||
MIT. See [LICENSE](LICENSE).
|
||||
@@ -1,8 +0,0 @@
|
||||
# SVG shared Fabric props/shadow-node descriptors are supplied by RN Codegen
|
||||
# and react-native-svg's published common C++ shadow-node headers.
|
||||
set(_svg_codegen_dir "${RN_GPUI_PROJECT_ROOT}/desktop/autolink/codegen/rnsvg")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_SOURCES
|
||||
"${CMAKE_CURRENT_LIST_DIR}/cpp/GpuiSvgModule.cpp")
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INSTALLERS svg)
|
||||
set_property(GLOBAL APPEND PROPERTY RN_GPUI_PACKAGE_MODULE_INCLUDE_DIRS
|
||||
"${_svg_codegen_dir}")
|
||||
@@ -1,13 +0,0 @@
|
||||
[package]
|
||||
name = "rn-gpui-svg"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
react-native-gpui = { version = "0.1" }
|
||||
rn-gpui-sys = { version = "0.1" }
|
||||
gpui = { version = "0.2" }
|
||||
serde_json = "1"
|
||||
# The same versions gpui is built with, for the averaged image it draws.
|
||||
image = { version = "0.25", default-features = false }
|
||||
smallvec = "1"
|
||||
@@ -1,38 +0,0 @@
|
||||
//! Puts the C++ runtime's shared libraries on this crate's test binary rpath.
|
||||
//!
|
||||
//! `rn-gpui-svg`'s tests build gpui trees, which link `libjsi.dylib` and the rest
|
||||
//! of the Node-API runtime. Core's `build.rs` emits
|
||||
//! `cargo:rustc-link-arg=-Wl,-rpath,<dir>` for its own binary — the app — and that
|
||||
//! flag is not inherited by a dependency's test binary, so the test process
|
||||
//! aborted in dyld with `Library not loaded: @rpath/libjsi.dylib` before running a
|
||||
//! single test.
|
||||
//!
|
||||
//! Core exports the directories it was given as `DEP_RN_GPUI_CORE_RPATHS`, which
|
||||
//! is the same variable core's own `build.rs` reads. Re-emitting it here scopes
|
||||
//! the flag to this crate's test binary, since `cargo:rustc-link-arg` without a
|
||||
//! target suffix applies to the crate's own targets and not to the app that
|
||||
//! depends on it.
|
||||
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-env-changed=DEP_RN_GPUI_CORE_RPATHS");
|
||||
println!("cargo:rerun-if-changed=build.rs");
|
||||
|
||||
let Ok(dirs) = std::env::var("DEP_RN_GPUI_CORE_RPATHS") else {
|
||||
return;
|
||||
};
|
||||
let dirs = dirs
|
||||
.split('|')
|
||||
.filter(|dir| !dir.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
if dirs.is_empty() {
|
||||
return;
|
||||
}
|
||||
// MSVC and MinGW have no rpath, and the flag is rejected outright; Windows
|
||||
// finds the DLLs through PATH, which the test runner already arranges.
|
||||
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
|
||||
return;
|
||||
}
|
||||
for dir in dirs {
|
||||
println!("cargo:rustc-link-arg=-Wl,-rpath,{dir}");
|
||||
}
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
#include "GpuiModules.h"
|
||||
#include "MountBatchBuilder.h"
|
||||
|
||||
#include <react/renderer/components/rnsvg/Props.h>
|
||||
|
||||
#include <folly/json.h>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
namespace rngpui {
|
||||
using namespace facebook::react;
|
||||
|
||||
namespace {
|
||||
constexpr const char* kSvgComponents[] = {
|
||||
"RNSVGSvgView", "RNSVGCircle", "RNSVGEllipse",
|
||||
"RNSVGLine", "RNSVGPath", "RNSVGRect",
|
||||
"RNSVGGroup", "RNSVGDefs", "RNSVGClipPath",
|
||||
"RNSVGLinearGradient", "RNSVGRadialGradient", "RNSVGText",
|
||||
"RNSVGTSpan", "RNSVGFilter", "RNSVGFeBlend",
|
||||
"RNSVGFeColorMatrix", "RNSVGFeComposite", "RNSVGFeFlood",
|
||||
"RNSVGFeGaussianBlur", "RNSVGFeMerge", "RNSVGFeOffset",
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
requires(!std::is_enum_v<T>)
|
||||
folly::dynamic svgJsonValue(const T& value) {
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
requires std::is_enum_v<T>
|
||||
folly::dynamic svgJsonValue(const T& value) {
|
||||
return toString(value);
|
||||
}
|
||||
|
||||
folly::dynamic svgJsonValue(const Float& value) {
|
||||
return value;
|
||||
}
|
||||
folly::dynamic svgJsonValue(const SharedColor& value) {
|
||||
return value ? folly::dynamic(argb(value)) : folly::dynamic(nullptr);
|
||||
}
|
||||
folly::dynamic svgJsonValue(const std::vector<std::string>& values) {
|
||||
folly::dynamic result = folly::dynamic::array();
|
||||
for (const auto& value : values)
|
||||
result.push_back(value);
|
||||
return result;
|
||||
}
|
||||
folly::dynamic svgJsonValue(const std::vector<Float>& values) {
|
||||
folly::dynamic result = folly::dynamic::array();
|
||||
for (auto value : values)
|
||||
result.push_back(value);
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
folly::dynamic svgPropsJson(const T& props) {
|
||||
folly::dynamic result = folly::dynamic::object();
|
||||
#define RN_GPUI_COPY_SVG_PROP(name) \
|
||||
if constexpr (requires { props.name; }) \
|
||||
result[#name] = svgJsonValue(props.name)
|
||||
RN_GPUI_COPY_SVG_PROP(name);
|
||||
RN_GPUI_COPY_SVG_PROP(d);
|
||||
RN_GPUI_COPY_SVG_PROP(x);
|
||||
RN_GPUI_COPY_SVG_PROP(y);
|
||||
RN_GPUI_COPY_SVG_PROP(x1);
|
||||
RN_GPUI_COPY_SVG_PROP(y1);
|
||||
RN_GPUI_COPY_SVG_PROP(x2);
|
||||
RN_GPUI_COPY_SVG_PROP(y2);
|
||||
RN_GPUI_COPY_SVG_PROP(cx);
|
||||
RN_GPUI_COPY_SVG_PROP(cy);
|
||||
RN_GPUI_COPY_SVG_PROP(r);
|
||||
RN_GPUI_COPY_SVG_PROP(rx);
|
||||
RN_GPUI_COPY_SVG_PROP(ry);
|
||||
RN_GPUI_COPY_SVG_PROP(width);
|
||||
RN_GPUI_COPY_SVG_PROP(height);
|
||||
RN_GPUI_COPY_SVG_PROP(vbWidth);
|
||||
RN_GPUI_COPY_SVG_PROP(vbHeight);
|
||||
RN_GPUI_COPY_SVG_PROP(minX);
|
||||
RN_GPUI_COPY_SVG_PROP(minY);
|
||||
RN_GPUI_COPY_SVG_PROP(align);
|
||||
RN_GPUI_COPY_SVG_PROP(meetOrSlice);
|
||||
RN_GPUI_COPY_SVG_PROP(opacity);
|
||||
RN_GPUI_COPY_SVG_PROP(color);
|
||||
RN_GPUI_COPY_SVG_PROP(propList);
|
||||
RN_GPUI_COPY_SVG_PROP(fill);
|
||||
RN_GPUI_COPY_SVG_PROP(fillOpacity);
|
||||
RN_GPUI_COPY_SVG_PROP(fillRule);
|
||||
RN_GPUI_COPY_SVG_PROP(stroke);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeOpacity);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeWidth);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeLinecap);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeLinejoin);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeDasharray);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeDashoffset);
|
||||
RN_GPUI_COPY_SVG_PROP(strokeMiterlimit);
|
||||
RN_GPUI_COPY_SVG_PROP(matrix);
|
||||
RN_GPUI_COPY_SVG_PROP(mask);
|
||||
RN_GPUI_COPY_SVG_PROP(clipPath);
|
||||
RN_GPUI_COPY_SVG_PROP(offset);
|
||||
RN_GPUI_COPY_SVG_PROP(stopColor);
|
||||
RN_GPUI_COPY_SVG_PROP(stopOpacity);
|
||||
if constexpr (requires { props.filter; }) {
|
||||
if constexpr (std::is_same_v<
|
||||
std::remove_cvref_t<decltype(props.filter)>,
|
||||
std::string>)
|
||||
result["filter"] = props.filter;
|
||||
}
|
||||
RN_GPUI_COPY_SVG_PROP(filterUnits);
|
||||
RN_GPUI_COPY_SVG_PROP(primitiveUnits);
|
||||
RN_GPUI_COPY_SVG_PROP(result);
|
||||
RN_GPUI_COPY_SVG_PROP(in1);
|
||||
RN_GPUI_COPY_SVG_PROP(in2);
|
||||
RN_GPUI_COPY_SVG_PROP(mode);
|
||||
RN_GPUI_COPY_SVG_PROP(type);
|
||||
RN_GPUI_COPY_SVG_PROP(values);
|
||||
RN_GPUI_COPY_SVG_PROP(operator1);
|
||||
RN_GPUI_COPY_SVG_PROP(k1);
|
||||
RN_GPUI_COPY_SVG_PROP(k2);
|
||||
RN_GPUI_COPY_SVG_PROP(k3);
|
||||
RN_GPUI_COPY_SVG_PROP(k4);
|
||||
RN_GPUI_COPY_SVG_PROP(dx);
|
||||
RN_GPUI_COPY_SVG_PROP(dy);
|
||||
RN_GPUI_COPY_SVG_PROP(edgeMode);
|
||||
RN_GPUI_COPY_SVG_PROP(floodColor);
|
||||
RN_GPUI_COPY_SVG_PROP(floodOpacity);
|
||||
RN_GPUI_COPY_SVG_PROP(stdDeviationX);
|
||||
RN_GPUI_COPY_SVG_PROP(stdDeviationY);
|
||||
RN_GPUI_COPY_SVG_PROP(nodes);
|
||||
#undef RN_GPUI_COPY_SVG_PROP
|
||||
return result;
|
||||
}
|
||||
|
||||
folly::dynamic svgPropsJson(const std::string& component, const Props& props) {
|
||||
#define RN_GPUI_SVG_PROPS(tag, type) \
|
||||
if (component == tag) { \
|
||||
if (auto typed = dynamic_cast<const type*>(&props)) \
|
||||
return svgPropsJson(*typed); \
|
||||
}
|
||||
RN_GPUI_SVG_PROPS("RNSVGSvgView", RNSVGSvgViewProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGCircle", RNSVGCircleProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGEllipse", RNSVGEllipseProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGLine", RNSVGLineProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGPath", RNSVGPathProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGRect", RNSVGRectProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGGroup", RNSVGGroupProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGDefs", RNSVGDefsProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGClipPath", RNSVGClipPathProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGLinearGradient", RNSVGLinearGradientProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGRadialGradient", RNSVGRadialGradientProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGText", RNSVGTextProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGTSpan", RNSVGTSpanProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFilter", RNSVGFilterProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeBlend", RNSVGFeBlendProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeColorMatrix", RNSVGFeColorMatrixProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeComposite", RNSVGFeCompositeProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeFlood", RNSVGFeFloodProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeGaussianBlur", RNSVGFeGaussianBlurProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeMerge", RNSVGFeMergeProps)
|
||||
RN_GPUI_SVG_PROPS("RNSVGFeOffset", RNSVGFeOffsetProps)
|
||||
#undef RN_GPUI_SVG_PROPS
|
||||
return folly::dynamic::object();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void rn_gpui_install_svg_module() {
|
||||
for (const char* component : kSvgComponents) {
|
||||
registerComponentProps(
|
||||
component, [](const std::string& name, const ShadowView& view) {
|
||||
if (!view.props)
|
||||
return std::string("{}");
|
||||
return folly::toJson(svgPropsJson(name, *view.props));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace rngpui
|
||||
@@ -1,156 +0,0 @@
|
||||
//! The rasters, kept by key in a cache bounded by bytes.
|
||||
//!
|
||||
//! One cache for every node, so identical icons share one image. It holds the
|
||||
//! images strongly: an icon that scrolls off the screen is still here when it
|
||||
//! comes back. When the total passes the limit the image used longest ago goes
|
||||
//! first. The limit is 64 MB; `RN_GPUI_SVG_CACHE_MB` sets another.
|
||||
|
||||
use crate::key::Key;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
pub const DEFAULT_LIMIT_BYTES: usize = 64 * 1024 * 1024;
|
||||
|
||||
struct Entry {
|
||||
image: Arc<gpui::RenderImage>,
|
||||
bytes: usize,
|
||||
used: u64,
|
||||
}
|
||||
|
||||
pub struct Cache {
|
||||
limit: usize,
|
||||
bytes: usize,
|
||||
clock: u64,
|
||||
entries: HashMap<Key, Entry>,
|
||||
}
|
||||
|
||||
fn size_of(image: &gpui::RenderImage) -> usize {
|
||||
let size = image.size(0);
|
||||
size.width.0.max(0) as usize * size.height.0.max(0) as usize * 4
|
||||
}
|
||||
|
||||
impl Cache {
|
||||
pub fn new(limit: usize) -> Self {
|
||||
Self {
|
||||
limit,
|
||||
bytes: 0,
|
||||
clock: 0,
|
||||
entries: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&mut self, key: &Key) -> Option<Arc<gpui::RenderImage>> {
|
||||
self.clock += 1;
|
||||
let clock = self.clock;
|
||||
let entry = self.entries.get_mut(key)?;
|
||||
entry.used = clock;
|
||||
Some(entry.image.clone())
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, key: Key, image: Arc<gpui::RenderImage>) {
|
||||
self.clock += 1;
|
||||
let bytes = size_of(&image);
|
||||
if let Some(old) = self.entries.insert(
|
||||
key,
|
||||
Entry {
|
||||
image,
|
||||
bytes,
|
||||
used: self.clock,
|
||||
},
|
||||
) {
|
||||
self.bytes -= old.bytes;
|
||||
}
|
||||
self.bytes += bytes;
|
||||
// The newest image stays even when it alone is over the limit.
|
||||
while self.bytes > self.limit && self.entries.len() > 1 {
|
||||
let Some(oldest) = self
|
||||
.entries
|
||||
.iter()
|
||||
.min_by_key(|(_, entry)| entry.used)
|
||||
.map(|(key, _)| *key)
|
||||
else {
|
||||
break;
|
||||
};
|
||||
if let Some(removed) = self.entries.remove(&oldest) {
|
||||
self.bytes -= removed.bytes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes(&self) -> usize {
|
||||
self.bytes
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.entries.len()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn global() -> &'static Mutex<Cache> {
|
||||
static CACHE: OnceLock<Mutex<Cache>> = OnceLock::new();
|
||||
CACHE.get_or_init(|| {
|
||||
let limit = std::env::var("RN_GPUI_SVG_CACHE_MB")
|
||||
.ok()
|
||||
.and_then(|value| value.parse::<usize>().ok())
|
||||
.map_or(DEFAULT_LIMIT_BYTES, |megabytes| megabytes * 1024 * 1024);
|
||||
Mutex::new(Cache::new(limit))
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn image(side: u32) -> Arc<gpui::RenderImage> {
|
||||
let buffer =
|
||||
image::ImageBuffer::from_raw(side, side, vec![0u8; (side * side * 4) as usize])
|
||||
.expect("a buffer");
|
||||
Arc::new(gpui::RenderImage::new(smallvec::SmallVec::from_const([
|
||||
image::Frame::new(buffer),
|
||||
])))
|
||||
}
|
||||
|
||||
fn key(n: u64) -> Key {
|
||||
crate::key::test_key(n)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_returns_what_it_was_given_and_counts_bytes() {
|
||||
let mut cache = Cache::new(1 << 20);
|
||||
let a = image(10);
|
||||
cache.insert(key(1), a.clone());
|
||||
assert!(Arc::ptr_eq(&cache.get(&key(1)).unwrap(), &a));
|
||||
assert!(cache.get(&key(2)).is_none());
|
||||
assert_eq!(cache.bytes(), 400);
|
||||
// Replacing a key does not count its old image twice.
|
||||
cache.insert(key(1), image(20));
|
||||
assert_eq!(cache.bytes(), 1600);
|
||||
assert_eq!(cache.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn it_stays_within_its_limit_and_drops_the_image_used_longest_ago() {
|
||||
// Three 10 by 10 images are 1200 bytes; the limit holds two.
|
||||
let mut cache = Cache::new(900);
|
||||
cache.insert(key(1), image(10));
|
||||
cache.insert(key(2), image(10));
|
||||
// Using the first makes the second the oldest.
|
||||
assert!(cache.get(&key(1)).is_some());
|
||||
cache.insert(key(3), image(10));
|
||||
assert!(cache.bytes() <= 900);
|
||||
assert!(cache.get(&key(1)).is_some());
|
||||
assert!(cache.get(&key(2)).is_none());
|
||||
assert!(cache.get(&key(3)).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_image_over_the_limit_is_still_kept() {
|
||||
let mut cache = Cache::new(100);
|
||||
cache.insert(key(1), image(10));
|
||||
assert_eq!(cache.len(), 1);
|
||||
assert!(cache.get(&key(1)).is_some());
|
||||
cache.insert(key(2), image(10));
|
||||
assert_eq!(cache.len(), 1);
|
||||
assert!(cache.get(&key(2)).is_some());
|
||||
}
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
//! The cache key of a drawing: what decides its pixels, hashed in place.
|
||||
//!
|
||||
//! The document string is the expensive part of a render, so a lookup never
|
||||
//! builds it: the key is a structural hash of the node, its props and its
|
||||
//! children (the resolved colours are props), the frame's size and the scale.
|
||||
//! A position is not part of it, so identical icons at different places share
|
||||
//! one raster.
|
||||
|
||||
use react_native_gpui::extensions::ComponentNode;
|
||||
use rn_gpui_sys::ffi::RectF;
|
||||
use serde_json::Value;
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::Hasher;
|
||||
|
||||
/// Two independent 64 bit hashes of the same input.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Key(u64, u64);
|
||||
|
||||
struct Pair(DefaultHasher, DefaultHasher);
|
||||
|
||||
impl Hasher for Pair {
|
||||
fn write(&mut self, bytes: &[u8]) {
|
||||
self.0.write(bytes);
|
||||
self.1.write(bytes);
|
||||
}
|
||||
|
||||
fn finish(&self) -> u64 {
|
||||
self.0.finish()
|
||||
}
|
||||
}
|
||||
|
||||
fn hash_value(input: &Value, hasher: &mut Pair) {
|
||||
match input {
|
||||
Value::Null => hasher.write_u8(0),
|
||||
Value::Bool(flag) => {
|
||||
hasher.write_u8(1);
|
||||
hasher.write_u8(u8::from(*flag));
|
||||
}
|
||||
Value::Number(number) => {
|
||||
hasher.write_u8(2);
|
||||
hasher.write_u64(number.as_f64().unwrap_or(f64::NAN).to_bits());
|
||||
}
|
||||
Value::String(text) => {
|
||||
hasher.write_u8(3);
|
||||
hasher.write_usize(text.len());
|
||||
hasher.write(text.as_bytes());
|
||||
}
|
||||
Value::Array(items) => {
|
||||
hasher.write_u8(4);
|
||||
hasher.write_usize(items.len());
|
||||
for item in items {
|
||||
hash_value(item, hasher);
|
||||
}
|
||||
}
|
||||
Value::Object(entries) => {
|
||||
hasher.write_u8(5);
|
||||
hasher.write_usize(entries.len());
|
||||
for (name, entry) in entries {
|
||||
hasher.write_usize(name.len());
|
||||
hasher.write(name.as_bytes());
|
||||
hash_value(entry, hasher);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn node(node: &ComponentNode, hasher: &mut Pair) {
|
||||
hasher.write_usize(node.component.len());
|
||||
hasher.write(node.component.as_bytes());
|
||||
hash_value(&node.props, hasher);
|
||||
hasher.write_usize(node.children.len());
|
||||
for child in &node.children {
|
||||
self::node(child, hasher);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn node_key(root: &ComponentNode, frame: RectF, scale: f32) -> Key {
|
||||
let mut hasher = Pair(DefaultHasher::new(), DefaultHasher::new());
|
||||
hasher.1.write_u8(0x5a);
|
||||
node(root, &mut hasher);
|
||||
hasher.write_u32(frame.w.to_bits());
|
||||
hasher.write_u32(frame.h.to_bits());
|
||||
hasher.write_u32(scale.to_bits());
|
||||
Key(hasher.0.finish(), hasher.1.finish())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn shape(props: Value) -> ComponentNode {
|
||||
ComponentNode {
|
||||
component: "RNSVGPath".into(),
|
||||
tag: 1,
|
||||
frame: RectF {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
w: 10.0,
|
||||
h: 10.0,
|
||||
},
|
||||
scale: (1.0, 1.0),
|
||||
props: Arc::new(props),
|
||||
parent: None,
|
||||
children: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn svg(color: u32, d: &str) -> ComponentNode {
|
||||
let mut root = shape(json!({"color": color, "vbWidth": 24, "vbHeight": 24}));
|
||||
root.component = "RNSVGSvgView".into();
|
||||
root.children.push(shape(
|
||||
json!({"d": d, "fill": {"type": 0, "payload": color}}),
|
||||
));
|
||||
root
|
||||
}
|
||||
|
||||
const FRAME: RectF = RectF {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
w: 16.0,
|
||||
h: 16.0,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn the_same_drawing_has_the_same_key_wherever_it_is() {
|
||||
let a = node_key(&svg(0xff112233, "M0 0L1 1"), FRAME, 2.0);
|
||||
let mut moved = svg(0xff112233, "M0 0L1 1");
|
||||
moved.frame.x = 99.0;
|
||||
moved.tag = 7;
|
||||
assert_eq!(a, node_key(&moved, RectF { x: 40.0, ..FRAME }, 2.0));
|
||||
assert_eq!(a, node_key(&svg(0xff112233, "M0 0L1 1"), FRAME, 2.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anything_that_changes_the_pixels_changes_the_key() {
|
||||
let base = node_key(&svg(0xff112233, "M0 0L1 1"), FRAME, 2.0);
|
||||
// A colour, a path, a child, the size and the scale each make a new key.
|
||||
assert_ne!(base, node_key(&svg(0xff112234, "M0 0L1 1"), FRAME, 2.0));
|
||||
assert_ne!(base, node_key(&svg(0xff112233, "M0 0L1 2"), FRAME, 2.0));
|
||||
let mut more = svg(0xff112233, "M0 0L1 1");
|
||||
more.children.push(shape(json!({"d": "M1 1"})));
|
||||
assert_ne!(base, node_key(&more, FRAME, 2.0));
|
||||
assert_ne!(
|
||||
base,
|
||||
node_key(
|
||||
&svg(0xff112233, "M0 0L1 1"),
|
||||
RectF { w: 17.0, ..FRAME },
|
||||
2.0
|
||||
)
|
||||
);
|
||||
assert_ne!(base, node_key(&svg(0xff112233, "M0 0L1 1"), FRAME, 1.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_of_different_kinds_do_not_collide() {
|
||||
let flag = node_key(&shape(json!({"a": true})), FRAME, 1.0);
|
||||
let text = node_key(&shape(json!({"a": "true"})), FRAME, 1.0);
|
||||
let number = node_key(&shape(json!({"a": 1})), FRAME, 1.0);
|
||||
assert_ne!(flag, text);
|
||||
assert_ne!(flag, number);
|
||||
assert_ne!(
|
||||
node_key(&shape(json!({"ab": "c"})), FRAME, 1.0),
|
||||
node_key(&shape(json!({"a": "bc"})), FRAME, 1.0)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub fn test_key(n: u64) -> Key {
|
||||
Key(n, n)
|
||||
}
|
||||
@@ -1,609 +0,0 @@
|
||||
use gpui::{div, prelude::*, px, AnyElement, App, Window};
|
||||
use react_native_gpui::extensions::{
|
||||
register_component_renderer, ComponentNode, ComponentRenderer,
|
||||
};
|
||||
use rn_gpui_sys::ffi::RectF;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod cache;
|
||||
mod key;
|
||||
mod raster;
|
||||
|
||||
const COMPONENTS: &[&str] = &[
|
||||
"RNSVGSvgView",
|
||||
"RNSVGCircle",
|
||||
"RNSVGEllipse",
|
||||
"RNSVGLine",
|
||||
"RNSVGPath",
|
||||
"RNSVGRect",
|
||||
"RNSVGGroup",
|
||||
"RNSVGDefs",
|
||||
"RNSVGClipPath",
|
||||
"RNSVGLinearGradient",
|
||||
"RNSVGRadialGradient",
|
||||
"RNSVGText",
|
||||
"RNSVGTSpan",
|
||||
"RNSVGMask",
|
||||
"RNSVGSymbol",
|
||||
"RNSVGPattern",
|
||||
"RNSVGMarker",
|
||||
"RNSVGTextPath",
|
||||
"RNSVGUse",
|
||||
"RNSVGFilter",
|
||||
"RNSVGFeBlend",
|
||||
"RNSVGFeColorMatrix",
|
||||
"RNSVGFeComposite",
|
||||
"RNSVGFeFlood",
|
||||
"RNSVGFeGaussianBlur",
|
||||
"RNSVGFeMerge",
|
||||
"RNSVGFeOffset",
|
||||
];
|
||||
|
||||
struct SvgRenderer;
|
||||
|
||||
impl ComponentRenderer for SvgRenderer {
|
||||
// The SVG document is drawn from the shapes under its root.
|
||||
fn reads_children(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn render(
|
||||
&self,
|
||||
node: &ComponentNode,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Option<AnyElement> {
|
||||
if node.component != "RNSVGSvgView" {
|
||||
return Some(div().into_any_element());
|
||||
}
|
||||
// The frame is in unscaled coordinates and this element is painted
|
||||
// scaled, exactly as `ComponentRenderer::render` documents, so the box
|
||||
// has to be scaled the same way core scales the element it would have
|
||||
// built. `size_full()` sized the image to its parent instead: the
|
||||
// document is rasterized at the window's scale factor, and an image
|
||||
// carries no intrinsic size, so every SVG stretched to fill whatever
|
||||
// surrounded it and painted over the text below it.
|
||||
let frame = node.frame;
|
||||
let (scale_x, scale_y) = node.scale;
|
||||
let width = frame.w * scale_x;
|
||||
let height = frame.h * scale_y;
|
||||
let scale = window.scale_factor();
|
||||
let image = raster::image_for(node, frame, scale, window, cx);
|
||||
// Chrome paints an inline SVG at a whole CSS pixel: a box laid out at
|
||||
// 2.5 is drawn at 3. The image is placed the same way, so a 15 point
|
||||
// icon centred in a 20 point button lands where the web app's does.
|
||||
Some(
|
||||
gpui::canvas(
|
||||
|bounds, _, _| bounds,
|
||||
move |_, bounds, window, _| {
|
||||
let snapped = gpui::Bounds::new(
|
||||
gpui::point(
|
||||
px((bounds.origin.x.as_f32() + 0.5).floor()),
|
||||
px((bounds.origin.y.as_f32() + 0.5).floor()),
|
||||
),
|
||||
bounds.size,
|
||||
);
|
||||
if let Some(image) = &image {
|
||||
let _ = window.paint_image(
|
||||
snapped,
|
||||
snapped,
|
||||
gpui::Corners::default(),
|
||||
image.clone(),
|
||||
0,
|
||||
false,
|
||||
);
|
||||
}
|
||||
},
|
||||
)
|
||||
.w(px(width))
|
||||
.h(px(height))
|
||||
.into_any_element(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rn_gpui_register() {
|
||||
for component in COMPONENTS {
|
||||
register_component_renderer(*component, Arc::new(SvgRenderer));
|
||||
}
|
||||
}
|
||||
|
||||
/// Where one coverage sample sits, so a shape can be drawn once per sample.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct Sample {
|
||||
/// The sample's offset from the pixel centre, in device pixels.
|
||||
pub(crate) dx: f32,
|
||||
pub(crate) dy: f32,
|
||||
/// Device pixels per CSS pixel.
|
||||
pub(crate) scale: f32,
|
||||
}
|
||||
|
||||
/// Chrome rasterises with four coverage samples per pixel at these offsets (in
|
||||
/// pixels from the centre): the standard Direct3D 4x pattern. A pixel is
|
||||
/// therefore never covered by anything but 0, 1/4, 1/2, 3/4 or all, and the edge
|
||||
/// of an icon takes the same five steps here.
|
||||
pub(crate) const SAMPLE_OFFSETS: [(f32, f32); 4] = [
|
||||
(-2.0 / 16.0, -6.0 / 16.0),
|
||||
(6.0 / 16.0, -2.0 / 16.0),
|
||||
(-6.0 / 16.0, 2.0 / 16.0),
|
||||
(2.0 / 16.0, 6.0 / 16.0),
|
||||
];
|
||||
|
||||
pub(crate) fn svg_document(node: &ComponentNode, frame: RectF, sample: Option<Sample>) -> String {
|
||||
let props = &node.props;
|
||||
let mut attributes = format!(
|
||||
"xmlns=\"http://www.w3.org/2000/svg\" width=\"{}\" height=\"{}\"",
|
||||
svg_number(frame.w),
|
||||
svg_number(frame.h)
|
||||
);
|
||||
if sample.is_some() {
|
||||
attributes.push_str(" shape-rendering=\"crispEdges\"");
|
||||
}
|
||||
for (name, attribute) in [
|
||||
("color", "color"),
|
||||
("fill", "fill"),
|
||||
("stroke", "stroke"),
|
||||
("strokeWidth", "stroke-width"),
|
||||
("fillOpacity", "fill-opacity"),
|
||||
("strokeOpacity", "stroke-opacity"),
|
||||
("opacity", "opacity"),
|
||||
] {
|
||||
if let Some(value) = props.get(name).filter(|value| !value.is_null()) {
|
||||
if let Some(value) = svg_value(value) {
|
||||
attributes.push_str(&format!(" {attribute}=\"{}\"", svg_escape(&value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(cap) = props
|
||||
.get("strokeLinecap")
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
{
|
||||
let cap = ["butt", "round", "square"]
|
||||
.get(cap as usize)
|
||||
.copied()
|
||||
.unwrap_or("butt");
|
||||
attributes.push_str(&format!(" stroke-linecap=\"{cap}\""));
|
||||
}
|
||||
if let Some(join) = props
|
||||
.get("strokeLinejoin")
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
{
|
||||
let join = ["miter", "round", "bevel"]
|
||||
.get(join as usize)
|
||||
.copied()
|
||||
.unwrap_or("miter");
|
||||
attributes.push_str(&format!(" stroke-linejoin=\"{join}\""));
|
||||
}
|
||||
let view_box = match (
|
||||
props.get("vbWidth").and_then(serde_json::Value::as_f64),
|
||||
props.get("vbHeight").and_then(serde_json::Value::as_f64),
|
||||
) {
|
||||
(Some(width), Some(height)) if width > 0.0 && height > 0.0 => {
|
||||
let min_x = props
|
||||
.get("minX")
|
||||
.and_then(serde_json::Value::as_f64)
|
||||
.unwrap_or(0.0);
|
||||
let min_y = props
|
||||
.get("minY")
|
||||
.and_then(serde_json::Value::as_f64)
|
||||
.unwrap_or(0.0);
|
||||
let align = props.get("align").and_then(serde_json::Value::as_str);
|
||||
let mode = match props
|
||||
.get("meetOrSlice")
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
.unwrap_or(0)
|
||||
{
|
||||
1 => "slice",
|
||||
2 => "none",
|
||||
_ => "meet",
|
||||
};
|
||||
Some((min_x, min_y, width, height, align, mode))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
match (view_box, sample) {
|
||||
(Some((min_x, min_y, width, height, align, mode)), sample) => {
|
||||
// The sample's shift in user units: how many of them one device
|
||||
// pixel covers depends on how the view box is fitted to the frame.
|
||||
let (mut min_x, mut min_y) = (min_x, min_y);
|
||||
if let Some(sample) = sample {
|
||||
let fit_x = f64::from(frame.w * sample.scale) / width;
|
||||
let fit_y = f64::from(frame.h * sample.scale) / height;
|
||||
let (unit_x, unit_y) = match (align, mode) {
|
||||
(Some("none"), _) | (Some(_), "none") => (fit_x, fit_y),
|
||||
(_, "slice") => (fit_x.max(fit_y), fit_x.max(fit_y)),
|
||||
_ => (fit_x.min(fit_y), fit_x.min(fit_y)),
|
||||
};
|
||||
min_x += f64::from(sample.dx) / unit_x;
|
||||
min_y += f64::from(sample.dy) / unit_y;
|
||||
}
|
||||
attributes.push_str(&format!(
|
||||
" viewBox=\"{} {} {} {}\"",
|
||||
svg_number(min_x as f32),
|
||||
svg_number(min_y as f32),
|
||||
svg_number(width as f32),
|
||||
svg_number(height as f32)
|
||||
));
|
||||
if let Some(align) = align {
|
||||
attributes.push_str(&format!(
|
||||
" preserveAspectRatio=\"{} {}\"",
|
||||
svg_escape(align),
|
||||
mode
|
||||
));
|
||||
}
|
||||
}
|
||||
(None, Some(sample)) => {
|
||||
// Without a view box the user unit is the CSS pixel; one is added
|
||||
// only to carry the shift.
|
||||
attributes.push_str(&format!(
|
||||
" viewBox=\"{} {} {} {}\"",
|
||||
svg_number(sample.dx / sample.scale),
|
||||
svg_number(sample.dy / sample.scale),
|
||||
svg_number(frame.w),
|
||||
svg_number(frame.h)
|
||||
));
|
||||
}
|
||||
(None, None) => {}
|
||||
}
|
||||
let mut document = format!("<svg {attributes}>");
|
||||
for child in &node.children {
|
||||
svg_node(child, &mut document);
|
||||
}
|
||||
document.push_str("</svg>");
|
||||
document
|
||||
}
|
||||
|
||||
fn svg_node(node: &ComponentNode, output: &mut String) {
|
||||
let props = &node.props;
|
||||
let element = match node.component.as_str() {
|
||||
"RNSVGCircle" => "circle",
|
||||
"RNSVGEllipse" => "ellipse",
|
||||
"RNSVGLine" => "line",
|
||||
"RNSVGPath" => "path",
|
||||
"RNSVGRect" => "rect",
|
||||
"RNSVGGroup" => "g",
|
||||
"RNSVGDefs" => "defs",
|
||||
"RNSVGClipPath" => "clipPath",
|
||||
"RNSVGLinearGradient" => "linearGradient",
|
||||
"RNSVGRadialGradient" => "radialGradient",
|
||||
"RNSVGMask" => "mask",
|
||||
"RNSVGSymbol" => "symbol",
|
||||
"RNSVGPattern" => "pattern",
|
||||
"RNSVGMarker" => "marker",
|
||||
"RNSVGText" => "text",
|
||||
"RNSVGTSpan" => "tspan",
|
||||
"RNSVGTextPath" => "textPath",
|
||||
"RNSVGUse" => "use",
|
||||
"RNSVGFilter" => "filter",
|
||||
"RNSVGFeBlend" => "feBlend",
|
||||
"RNSVGFeColorMatrix" => "feColorMatrix",
|
||||
"RNSVGFeComposite" => "feComposite",
|
||||
"RNSVGFeFlood" => "feFlood",
|
||||
"RNSVGFeGaussianBlur" => "feGaussianBlur",
|
||||
"RNSVGFeMerge" => "feMerge",
|
||||
"RNSVGFeOffset" => "feOffset",
|
||||
_ => "",
|
||||
};
|
||||
if element.is_empty() {
|
||||
for child in &node.children {
|
||||
svg_node(child, output);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let mut attributes = String::new();
|
||||
for name in [
|
||||
"id",
|
||||
"name",
|
||||
"href",
|
||||
"d",
|
||||
"x",
|
||||
"y",
|
||||
"x1",
|
||||
"y1",
|
||||
"x2",
|
||||
"y2",
|
||||
"cx",
|
||||
"cy",
|
||||
"r",
|
||||
"rx",
|
||||
"ry",
|
||||
"width",
|
||||
"height",
|
||||
"color",
|
||||
"fill",
|
||||
"stroke",
|
||||
"strokeWidth",
|
||||
"fillOpacity",
|
||||
"strokeOpacity",
|
||||
"strokeDasharray",
|
||||
"strokeDashoffset",
|
||||
"strokeMiterlimit",
|
||||
"opacity",
|
||||
"fontSize",
|
||||
"fontWeight",
|
||||
"fontFamily",
|
||||
"textLength",
|
||||
"dominantBaseline",
|
||||
"offset",
|
||||
"stopColor",
|
||||
"stopOpacity",
|
||||
] {
|
||||
if !svg_property_is_set(props, name) {
|
||||
continue;
|
||||
}
|
||||
if let Some(value) = props.get(name) {
|
||||
let attribute = match name {
|
||||
"strokeWidth" => "stroke-width",
|
||||
"fillOpacity" => "fill-opacity",
|
||||
"strokeOpacity" => "stroke-opacity",
|
||||
"strokeDasharray" => "stroke-dasharray",
|
||||
"strokeDashoffset" => "stroke-dashoffset",
|
||||
"strokeMiterlimit" => "stroke-miterlimit",
|
||||
"fontSize" => "font-size",
|
||||
"fontWeight" => "font-weight",
|
||||
"fontFamily" => "font-family",
|
||||
"dominantBaseline" => "dominant-baseline",
|
||||
"stopColor" => "stop-color",
|
||||
"stopOpacity" => "stop-opacity",
|
||||
other => other,
|
||||
};
|
||||
let value = if value.is_null() && matches!(name, "fill" | "stroke") {
|
||||
Some("none".to_owned())
|
||||
} else {
|
||||
svg_value(value)
|
||||
};
|
||||
if let Some(value) = value {
|
||||
attributes.push_str(&format!(" {attribute}=\"{}\"", svg_escape(&value)));
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(rule) = props
|
||||
.get("fillRule")
|
||||
.filter(|_| svg_property_is_set(props, "fillRule"))
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
{
|
||||
attributes.push_str(&format!(
|
||||
" fill-rule=\"{}\"",
|
||||
if rule == 0 { "evenodd" } else { "nonzero" }
|
||||
));
|
||||
}
|
||||
if let Some(cap) = props
|
||||
.get("strokeLinecap")
|
||||
.filter(|_| svg_property_is_set(props, "strokeLinecap"))
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
{
|
||||
let cap = ["butt", "round", "square"]
|
||||
.get(cap as usize)
|
||||
.copied()
|
||||
.unwrap_or("butt");
|
||||
attributes.push_str(&format!(" stroke-linecap=\"{cap}\""));
|
||||
}
|
||||
if let Some(join) = props
|
||||
.get("strokeLinejoin")
|
||||
.filter(|_| svg_property_is_set(props, "strokeLinejoin"))
|
||||
.and_then(serde_json::Value::as_i64)
|
||||
{
|
||||
let join = ["miter", "round", "bevel"]
|
||||
.get(join as usize)
|
||||
.copied()
|
||||
.unwrap_or("miter");
|
||||
attributes.push_str(&format!(" stroke-linejoin=\"{join}\""));
|
||||
}
|
||||
if let Some(matrix) = props.get("matrix").and_then(serde_json::Value::as_array) {
|
||||
let component = |index: usize| matrix.get(index).and_then(serde_json::Value::as_f64);
|
||||
let values = match matrix.len() {
|
||||
6 => [0, 1, 2, 3, 4, 5],
|
||||
16.. => [0, 1, 4, 5, 12, 13],
|
||||
_ => [usize::MAX; 6],
|
||||
}
|
||||
.map(component);
|
||||
if let [Some(a), Some(b), Some(c), Some(d), Some(e), Some(f)] = values {
|
||||
attributes.push_str(&format!(" transform=\"matrix({a} {b} {c} {d} {e} {f})\""));
|
||||
}
|
||||
}
|
||||
if let Some(id) = props
|
||||
.get("name")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.filter(|id| !id.is_empty())
|
||||
{
|
||||
attributes.push_str(&format!(" id=\"{}\"", svg_escape(id)));
|
||||
}
|
||||
if let Some(filter) = props
|
||||
.get("filter")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.filter(|filter| !filter.is_empty())
|
||||
{
|
||||
attributes.push_str(&format!(" filter=\"url(#{})\"", svg_escape(filter)));
|
||||
}
|
||||
if element == "filter" {
|
||||
attributes.push_str(" color-interpolation-filters=\"sRGB\"");
|
||||
}
|
||||
if element.starts_with("fe") || element == "filter" {
|
||||
attributes.push_str(&filter_primitive_attributes(props));
|
||||
}
|
||||
output.push('<');
|
||||
output.push_str(element);
|
||||
output.push_str(&attributes);
|
||||
output.push('>');
|
||||
if element == "feMerge" {
|
||||
for id in props
|
||||
.get("nodes")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(serde_json::Value::as_str)
|
||||
{
|
||||
output.push_str(&format!("<feMergeNode in=\"{}\"/>", svg_escape(id)));
|
||||
}
|
||||
}
|
||||
for child in &node.children {
|
||||
svg_node(child, output);
|
||||
}
|
||||
output.push_str("</");
|
||||
output.push_str(element);
|
||||
output.push('>');
|
||||
}
|
||||
|
||||
fn filter_primitive_attributes(props: &serde_json::Value) -> String {
|
||||
let mut attributes = String::new();
|
||||
let text = |name: &str| match props.get(name) {
|
||||
Some(serde_json::Value::String(value)) if !value.is_empty() => Some(value.clone()),
|
||||
Some(value) if value.is_array() || value.is_number() => svg_value(value),
|
||||
_ => None,
|
||||
};
|
||||
for (name, attribute) in [
|
||||
("in1", "in"),
|
||||
("in2", "in2"),
|
||||
("result", "result"),
|
||||
("mode", "mode"),
|
||||
("type", "type"),
|
||||
("values", "values"),
|
||||
("operator1", "operator"),
|
||||
("k1", "k1"),
|
||||
("k2", "k2"),
|
||||
("k3", "k3"),
|
||||
("k4", "k4"),
|
||||
("dx", "dx"),
|
||||
("dy", "dy"),
|
||||
("edgeMode", "edgeMode"),
|
||||
("floodOpacity", "flood-opacity"),
|
||||
("filterUnits", "filterUnits"),
|
||||
("primitiveUnits", "primitiveUnits"),
|
||||
] {
|
||||
if let Some(value) = text(name) {
|
||||
attributes.push_str(&format!(" {attribute}=\"{}\"", svg_escape(&value)));
|
||||
}
|
||||
}
|
||||
if let Some(color) = props.get("floodColor").and_then(svg_value) {
|
||||
attributes.push_str(&format!(" flood-color=\"{}\"", svg_escape(&color)));
|
||||
}
|
||||
let deviation = |name: &str| props.get(name).and_then(serde_json::Value::as_f64);
|
||||
if let Some(x) = deviation("stdDeviationX") {
|
||||
let y = deviation("stdDeviationY").unwrap_or(x);
|
||||
attributes.push_str(&format!(" stdDeviation=\"{x} {y}\""));
|
||||
}
|
||||
attributes
|
||||
}
|
||||
|
||||
fn svg_property_is_set(props: &serde_json::Value, name: &str) -> bool {
|
||||
if !matches!(
|
||||
name,
|
||||
"fill"
|
||||
| "fillOpacity"
|
||||
| "fillRule"
|
||||
| "stroke"
|
||||
| "strokeWidth"
|
||||
| "strokeOpacity"
|
||||
| "strokeLinecap"
|
||||
| "strokeLinejoin"
|
||||
| "strokeDasharray"
|
||||
| "strokeDashoffset"
|
||||
| "strokeMiterlimit"
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
props
|
||||
.get("propList")
|
||||
.and_then(serde_json::Value::as_array)
|
||||
.is_none_or(|properties| {
|
||||
properties
|
||||
.iter()
|
||||
.any(|property| property.as_str() == Some(name))
|
||||
})
|
||||
}
|
||||
|
||||
fn svg_value(value: &serde_json::Value) -> Option<String> {
|
||||
match value.get("type").and_then(serde_json::Value::as_i64) {
|
||||
Some(1) => {
|
||||
return value
|
||||
.get("brushRef")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.map(|id| format!("url(#{id})"))
|
||||
}
|
||||
Some(2) => return Some("currentColor".to_owned()),
|
||||
Some(3) => return Some("context-fill".to_owned()),
|
||||
Some(4) => return Some("context-stroke".to_owned()),
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if let Some(number) = value.as_f64() {
|
||||
if number < 0.0 && number.fract() == 0.0 {
|
||||
return Some(format!("#{:06x}", (number as i64 as u32) & 0x00ff_ffff));
|
||||
}
|
||||
if (0x0100_0000..=0xffff_ffff).contains(&(number as u64)) {
|
||||
let argb = number as u32;
|
||||
return Some(format!(
|
||||
"rgba({}, {}, {}, {})",
|
||||
(argb >> 16) & 0xff,
|
||||
(argb >> 8) & 0xff,
|
||||
argb & 0xff,
|
||||
((argb >> 24) & 0xff) as f32 / 255.0
|
||||
));
|
||||
}
|
||||
return Some(svg_number(number as f32));
|
||||
}
|
||||
if let Some(value) = value.as_str() {
|
||||
return Some(value.to_owned());
|
||||
}
|
||||
if let Some(value) = value.as_array() {
|
||||
return Some(
|
||||
value
|
||||
.iter()
|
||||
.filter_map(svg_value)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
);
|
||||
}
|
||||
value
|
||||
.get("payload")
|
||||
.and_then(svg_value)
|
||||
.or_else(|| value.get("value").and_then(svg_value))
|
||||
}
|
||||
|
||||
fn svg_escape(value: &str) -> String {
|
||||
value
|
||||
.replace('&', "&")
|
||||
.replace('"', """)
|
||||
.replace('<', "<")
|
||||
.replace('>', ">")
|
||||
}
|
||||
|
||||
fn svg_number(value: f32) -> String {
|
||||
if value.fract() == 0.0 {
|
||||
format!("{value:.0}")
|
||||
} else {
|
||||
value.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod svg_paint_tests {
|
||||
use super::{svg_property_is_set, svg_value};
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn brushes_keep_current_color_and_reference_semantics() {
|
||||
assert_eq!(
|
||||
svg_value(&json!({"type": 2})).as_deref(),
|
||||
Some("currentColor")
|
||||
);
|
||||
assert_eq!(
|
||||
svg_value(&json!({"type": 1, "brushRef": "gradient"})).as_deref(),
|
||||
Some("url(#gradient)")
|
||||
);
|
||||
assert_eq!(
|
||||
svg_value(&json!({"type": 0, "payload": 0xff64d2ff_u32})).as_deref(),
|
||||
Some("rgba(100, 210, 255, 1)")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_black_fill_does_not_override_inherited_none() {
|
||||
let child = json!({"fill": {"type": 0, "payload": 0xff000000_u32}, "strokeLinecap": 0, "propList": []});
|
||||
assert!(!svg_property_is_set(&child, "fill"));
|
||||
assert!(!svg_property_is_set(&child, "strokeLinecap"));
|
||||
let group = json!({"fill": null, "stroke": {"type": 2}, "propList": ["fill", "stroke"]});
|
||||
assert!(svg_property_is_set(&group, "fill"));
|
||||
assert!(svg_property_is_set(&group, "stroke"));
|
||||
}
|
||||
}
|
||||
@@ -1,477 +0,0 @@
|
||||
//! Turning a node into the image it is drawn as, and when to do it.
|
||||
//!
|
||||
//! A drawing is found by its [`Key`] in the shared cache, which builds no
|
||||
//! document. Only a miss builds the four documents (one per coverage sample) and
|
||||
//! rasterises them. A small image with nothing already on screen for its node is
|
||||
//! drawn right away, so a row that scrolls in has its icon on the first frame.
|
||||
//! Anything else is drawn on a background thread while the node keeps showing
|
||||
//! its previous image, if it has one, and is built again when the new one is
|
||||
//! ready.
|
||||
|
||||
use crate::cache::{self, Cache};
|
||||
use crate::key::{node_key, Key};
|
||||
use crate::{svg_document, Sample, SAMPLE_OFFSETS};
|
||||
use gpui::{App, Window};
|
||||
use react_native_gpui::extensions::{rendering_node, ComponentNode, NodeHandle};
|
||||
use rn_gpui_sys::ffi::RectF;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
use std::time::Instant;
|
||||
|
||||
/// Images up to this many device pixels are drawn on the main thread when their
|
||||
/// node has nothing to show yet: about 48 CSS pixels square at a scale of 2.
|
||||
const SYNC_MAX_PIXELS: i64 = 96 * 96;
|
||||
|
||||
/// Below this many device pixels the four samples are drawn one after the other:
|
||||
/// each takes less time than starting a thread for it.
|
||||
const PARALLEL_MIN_PIXELS: i64 = 128 * 128;
|
||||
|
||||
/// `RN_GPUI_SVG_TRACE=1` logs every drawing that is rasterised: its size, how
|
||||
/// long each coverage sample took and how long they took together. With
|
||||
/// `RN_GPUI_SVG_SYNC=1` every drawing is rasterised on the main thread.
|
||||
fn setting(name: &str) -> bool {
|
||||
std::env::var(name).is_ok_and(|value| value == "1")
|
||||
}
|
||||
|
||||
fn trace() -> bool {
|
||||
static TRACE: OnceLock<bool> = OnceLock::new();
|
||||
*TRACE.get_or_init(|| setting("RN_GPUI_SVG_TRACE"))
|
||||
}
|
||||
|
||||
fn force_sync() -> bool {
|
||||
static SYNC: OnceLock<bool> = OnceLock::new();
|
||||
*SYNC.get_or_init(|| setting("RN_GPUI_SVG_SYNC"))
|
||||
}
|
||||
|
||||
/// The image each node showed last, so a node whose drawing changes keeps
|
||||
/// showing it until the new one is ready.
|
||||
fn shown() -> &'static Mutex<HashMap<i32, Arc<gpui::RenderImage>>> {
|
||||
static SHOWN: OnceLock<Mutex<HashMap<i32, Arc<gpui::RenderImage>>>> = OnceLock::new();
|
||||
SHOWN.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
/// Drawings being rasterised, with the nodes waiting for each.
|
||||
fn pending() -> &'static Mutex<HashMap<Key, Vec<NodeHandle>>> {
|
||||
static PENDING: OnceLock<Mutex<HashMap<Key, Vec<NodeHandle>>>> = OnceLock::new();
|
||||
PENDING.get_or_init(|| Mutex::new(HashMap::new()))
|
||||
}
|
||||
|
||||
/// Drawings the renderer could not rasterise, which are not tried again.
|
||||
fn failed() -> &'static Mutex<HashSet<Key>> {
|
||||
static FAILED: OnceLock<Mutex<HashSet<Key>>> = OnceLock::new();
|
||||
FAILED.get_or_init(|| Mutex::new(HashSet::new()))
|
||||
}
|
||||
|
||||
fn lock<T>(mutex: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
|
||||
mutex
|
||||
.lock()
|
||||
.unwrap_or_else(|poisoned| poisoned.into_inner())
|
||||
}
|
||||
|
||||
fn has_filter(node: &ComponentNode) -> bool {
|
||||
node.component == "RNSVGFilter" || node.children.iter().any(has_filter)
|
||||
}
|
||||
|
||||
/// The image in the cache for `key`, or the result of `make`, which is stored.
|
||||
pub fn get_or_make(
|
||||
cache: &Mutex<Cache>,
|
||||
key: Key,
|
||||
make: impl FnOnce() -> Option<Arc<gpui::RenderImage>>,
|
||||
) -> Option<Arc<gpui::RenderImage>> {
|
||||
if let Some(image) = lock(cache).get(&key) {
|
||||
return Some(image);
|
||||
}
|
||||
let image = make()?;
|
||||
lock(cache).insert(key, image.clone());
|
||||
Some(image)
|
||||
}
|
||||
|
||||
/// The image `node` is drawn as now, or what it shows meanwhile.
|
||||
pub fn image_for(
|
||||
node: &ComponentNode,
|
||||
frame: RectF,
|
||||
scale: f32,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> Option<Arc<gpui::RenderImage>> {
|
||||
let key = node_key(node, frame, scale);
|
||||
let remember = |image: &Arc<gpui::RenderImage>| {
|
||||
lock(shown()).insert(node.tag, image.clone());
|
||||
};
|
||||
if let Some(image) = lock(cache::global()).get(&key) {
|
||||
remember(&image);
|
||||
return Some(image);
|
||||
}
|
||||
let previous = lock(shown()).get(&node.tag).cloned();
|
||||
if lock(failed()).contains(&key) {
|
||||
return previous;
|
||||
}
|
||||
let pixels = (frame.w * scale).round() as i64 * (frame.h * scale).round() as i64;
|
||||
let small = pixels <= SYNC_MAX_PIXELS && !has_filter(node);
|
||||
if force_sync() || (previous.is_none() && small) {
|
||||
let renderer = cx.svg_renderer();
|
||||
let image = get_or_make(cache::global(), key, || {
|
||||
sampled_image(&renderer, node, frame, scale)
|
||||
});
|
||||
match &image {
|
||||
Some(image) => remember(image),
|
||||
None => {
|
||||
lock(failed()).insert(key);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
request(key, node.clone(), frame, scale, window, cx);
|
||||
previous
|
||||
}
|
||||
|
||||
/// Rasterises `node` on a background thread, once for every node that asks for
|
||||
/// the same drawing, and builds the nodes again when it is done.
|
||||
fn request(
|
||||
key: Key,
|
||||
node: ComponentNode,
|
||||
frame: RectF,
|
||||
scale: f32,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
let Some(handle) = rendering_node() else {
|
||||
return;
|
||||
};
|
||||
{
|
||||
let mut pending = lock(pending());
|
||||
if let Some(waiting) = pending.get_mut(&key) {
|
||||
waiting.push(handle);
|
||||
return;
|
||||
}
|
||||
pending.insert(key, vec![handle]);
|
||||
}
|
||||
let renderer = cx.svg_renderer();
|
||||
let work = cx
|
||||
.background_executor()
|
||||
.spawn(async move { sampled_image(&renderer, &node, frame, scale) });
|
||||
window
|
||||
.spawn(cx, async move |cx| {
|
||||
let image = work.await;
|
||||
match &image {
|
||||
Some(image) => lock(cache::global()).insert(key, image.clone()),
|
||||
None => {
|
||||
lock(failed()).insert(key);
|
||||
}
|
||||
}
|
||||
let waiting = lock(pending()).remove(&key).unwrap_or_default();
|
||||
let _ = cx.update(|_, cx| {
|
||||
for handle in &waiting {
|
||||
handle.notify(cx);
|
||||
}
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
/// Draws the document at the window's scale, once per coverage sample without
|
||||
/// antialiasing, and averages the samples. The samples are rasterised side by
|
||||
/// side and combined in a fixed order, so the pixels do not depend on timing.
|
||||
pub fn sampled_image(
|
||||
renderer: &gpui::SvgRenderer,
|
||||
node: &ComponentNode,
|
||||
frame: RectF,
|
||||
scale: f32,
|
||||
) -> Option<Arc<gpui::RenderImage>> {
|
||||
let width = (frame.w * scale).round() as i32;
|
||||
let height = (frame.h * scale).round() as i32;
|
||||
if width < 1 || height < 1 {
|
||||
return None;
|
||||
}
|
||||
let size = gpui::size(gpui::DevicePixels(width), gpui::DevicePixels(height));
|
||||
let started = Instant::now();
|
||||
let render = |(dx, dy): (f32, f32)| -> Option<(Vec<u8>, f64)> {
|
||||
let sample_started = Instant::now();
|
||||
let document = svg_document(node, frame, Some(Sample { dx, dy, scale }));
|
||||
let parsed = renderer.parse_svg(document.as_bytes()).ok()?;
|
||||
let rendered = renderer
|
||||
.render_parsed(&parsed, gpui::SvgSize::ExactSize(size))
|
||||
.ok()?;
|
||||
rendered.as_bytes(0).map(|bytes| {
|
||||
(
|
||||
bytes.to_vec(),
|
||||
sample_started.elapsed().as_secs_f64() * 1000.0,
|
||||
)
|
||||
})
|
||||
};
|
||||
let samples: Vec<Option<(Vec<u8>, f64)>> =
|
||||
if i64::from(width) * i64::from(height) < PARALLEL_MIN_PIXELS {
|
||||
SAMPLE_OFFSETS
|
||||
.iter()
|
||||
.map(|offset| render(*offset))
|
||||
.collect()
|
||||
} else {
|
||||
std::thread::scope(|scope| {
|
||||
let workers: Vec<_> = SAMPLE_OFFSETS
|
||||
.iter()
|
||||
.map(|offset| scope.spawn(move || render(*offset)))
|
||||
.collect();
|
||||
workers
|
||||
.into_iter()
|
||||
.map(|worker| worker.join().ok().flatten())
|
||||
.collect()
|
||||
})
|
||||
};
|
||||
if trace() {
|
||||
let times: Vec<String> = samples
|
||||
.iter()
|
||||
.map(|sample| {
|
||||
sample
|
||||
.as_ref()
|
||||
.map_or("failed".into(), |(_, ms)| format!("{ms:.1}"))
|
||||
})
|
||||
.collect();
|
||||
eprintln!(
|
||||
"[svg] {width}x{height} samples_ms=[{}] wall_ms={:.1}",
|
||||
times.join(", "),
|
||||
started.elapsed().as_secs_f64() * 1000.0
|
||||
);
|
||||
}
|
||||
let samples: Vec<Option<Vec<u8>>> = samples
|
||||
.into_iter()
|
||||
.map(|sample| sample.map(|(bytes, _)| bytes))
|
||||
.collect();
|
||||
average(&samples, width as u32, height as u32)
|
||||
}
|
||||
|
||||
/// Averages the samples: BGRA with straight alpha, colour weighted by alpha.
|
||||
fn average(samples: &[Option<Vec<u8>>], width: u32, height: u32) -> Option<Arc<gpui::RenderImage>> {
|
||||
let length = width as usize * height as usize * 4;
|
||||
let mut sum = vec![0f32; length];
|
||||
for bytes in samples {
|
||||
let bytes = bytes.as_ref()?;
|
||||
if bytes.len() != length {
|
||||
return None;
|
||||
}
|
||||
for (total, pixel) in sum.chunks_exact_mut(4).zip(bytes.chunks_exact(4)) {
|
||||
let alpha = f32::from(pixel[3]);
|
||||
total[0] += f32::from(pixel[0]) * alpha;
|
||||
total[1] += f32::from(pixel[1]) * alpha;
|
||||
total[2] += f32::from(pixel[2]) * alpha;
|
||||
total[3] += alpha;
|
||||
}
|
||||
}
|
||||
let count = samples.len() as f32;
|
||||
let mut out = Vec::with_capacity(sum.len());
|
||||
for total in sum.chunks_exact(4) {
|
||||
if total[3] <= 0.0 {
|
||||
out.extend_from_slice(&[0, 0, 0, 0]);
|
||||
continue;
|
||||
}
|
||||
let blue = (total[0] / total[3]).round().clamp(0.0, 255.0);
|
||||
let green = (total[1] / total[3]).round().clamp(0.0, 255.0);
|
||||
let red = (total[2] / total[3]).round().clamp(0.0, 255.0);
|
||||
// Half coverage is 127.5 of 255, which the alpha channel cannot hold.
|
||||
// Chrome blends the exact half, and the result then rounds away from
|
||||
// the ink's own side: toward light under dark ink and toward dark
|
||||
// under light ink, so the alpha rounds the other way to match.
|
||||
let alpha = total[3] / count;
|
||||
let alpha = if (alpha.fract() - 0.5).abs() < 1e-3 {
|
||||
let luma = 0.299 * red + 0.587 * green + 0.114 * blue;
|
||||
if luma < 128.0 {
|
||||
alpha.floor()
|
||||
} else {
|
||||
alpha.ceil()
|
||||
}
|
||||
} else {
|
||||
alpha.round()
|
||||
};
|
||||
out.extend_from_slice(&[
|
||||
blue as u8,
|
||||
green as u8,
|
||||
red as u8,
|
||||
alpha.clamp(0.0, 255.0) as u8,
|
||||
]);
|
||||
}
|
||||
let buffer = image::ImageBuffer::from_raw(width, height, out)?;
|
||||
Some(Arc::new(gpui::RenderImage::new(
|
||||
smallvec::SmallVec::from_const([image::Frame::new(buffer)]),
|
||||
)))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
fn image() -> Arc<gpui::RenderImage> {
|
||||
let buffer = image::ImageBuffer::from_raw(2, 2, vec![0u8; 16]).expect("a buffer");
|
||||
Arc::new(gpui::RenderImage::new(smallvec::SmallVec::from_const([
|
||||
image::Frame::new(buffer),
|
||||
])))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_hit_makes_nothing_and_a_miss_makes_once() {
|
||||
let cache = Mutex::new(Cache::new(1 << 20));
|
||||
let made = AtomicUsize::new(0);
|
||||
let make = || {
|
||||
made.fetch_add(1, Ordering::SeqCst);
|
||||
Some(image())
|
||||
};
|
||||
let key = crate::key::test_key(1);
|
||||
let first = get_or_make(&cache, key, make).unwrap();
|
||||
assert_eq!(made.load(Ordering::SeqCst), 1);
|
||||
// The second lookup is a hit: the closure, which is where a document
|
||||
// string would be built, is not called.
|
||||
let second = get_or_make(&cache, key, || panic!("a hit must not build anything")).unwrap();
|
||||
assert!(Arc::ptr_eq(&first, &second));
|
||||
assert_eq!(made.load(Ordering::SeqCst), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drawing_that_cannot_be_made_is_not_cached() {
|
||||
let cache = Mutex::new(Cache::new(1 << 20));
|
||||
let key = crate::key::test_key(2);
|
||||
assert!(get_or_make(&cache, key, || None).is_none());
|
||||
assert_eq!(lock(&cache).len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn averaging_four_equal_samples_gives_the_sample() {
|
||||
let pixels = vec![10u8, 20, 30, 255, 0, 0, 0, 0];
|
||||
let samples = vec![
|
||||
Some(pixels.clone()),
|
||||
Some(pixels.clone()),
|
||||
Some(pixels.clone()),
|
||||
Some(pixels),
|
||||
];
|
||||
let averaged = average(&samples, 2, 1).unwrap();
|
||||
assert_eq!(
|
||||
averaged.as_bytes(0).unwrap(),
|
||||
&[10, 20, 30, 255, 0, 0, 0, 0]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_missing_sample_gives_no_image() {
|
||||
assert!(average(&[Some(vec![0; 4]), None], 1, 1).is_none());
|
||||
}
|
||||
|
||||
fn node(
|
||||
component: &str,
|
||||
props: serde_json::Value,
|
||||
children: Vec<ComponentNode>,
|
||||
) -> ComponentNode {
|
||||
ComponentNode {
|
||||
component: component.into(),
|
||||
tag: 1,
|
||||
frame: RectF {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
w: 24.0,
|
||||
h: 24.0,
|
||||
},
|
||||
scale: (1.0, 1.0),
|
||||
props: Arc::new(props),
|
||||
parent: None,
|
||||
children,
|
||||
}
|
||||
}
|
||||
|
||||
fn drawings() -> Vec<ComponentNode> {
|
||||
use serde_json::json;
|
||||
let fill = json!({"type": 0, "payload": 0xff336699_u32});
|
||||
let shape = |component: &str, props: serde_json::Value| {
|
||||
let mut full = json!({"fill": fill, "propList": ["fill"]});
|
||||
full.as_object_mut()
|
||||
.unwrap()
|
||||
.extend(props.as_object().unwrap().clone());
|
||||
node(component, full, vec![])
|
||||
};
|
||||
let root = |children| {
|
||||
node(
|
||||
"RNSVGSvgView",
|
||||
json!({"vbWidth": 24, "vbHeight": 24, "align": "xMidYMid", "meetOrSlice": 0}),
|
||||
children,
|
||||
)
|
||||
};
|
||||
let blurred = node(
|
||||
"RNSVGGroup",
|
||||
json!({"filter": "f", "propList": []}),
|
||||
vec![shape(
|
||||
"RNSVGRect",
|
||||
json!({"x": 6, "y": 6, "width": 12, "height": 12}),
|
||||
)],
|
||||
);
|
||||
let filter = node(
|
||||
"RNSVGDefs",
|
||||
json!({}),
|
||||
vec![node(
|
||||
"RNSVGFilter",
|
||||
json!({"name": "f", "x": 0, "y": 0, "width": 24, "height": 24, "filterUnits": "userSpaceOnUse", "primitiveUnits": "userSpaceOnUse"}),
|
||||
vec![node(
|
||||
"RNSVGFeGaussianBlur",
|
||||
json!({"stdDeviationX": 2, "stdDeviationY": 2}),
|
||||
vec![],
|
||||
)],
|
||||
)],
|
||||
);
|
||||
vec![
|
||||
root(vec![shape("RNSVGPath", json!({"d": "M2 2L22 2L12 22Z"}))]),
|
||||
root(vec![shape(
|
||||
"RNSVGCircle",
|
||||
json!({"cx": 12, "cy": 12, "r": 7.3}),
|
||||
)]),
|
||||
root(vec![shape(
|
||||
"RNSVGRect",
|
||||
json!({"x": 3.3, "y": 4.1, "width": 15, "height": 9, "rx": 3, "matrix": [0.94, -0.34, 0.34, 0.94, 0, 0]}),
|
||||
)]),
|
||||
root(vec![filter, blurred]),
|
||||
]
|
||||
}
|
||||
|
||||
/// The four samples one after the other, as the module drew them before.
|
||||
fn sequential(
|
||||
renderer: &gpui::SvgRenderer,
|
||||
node: &ComponentNode,
|
||||
frame: RectF,
|
||||
scale: f32,
|
||||
) -> Option<Arc<gpui::RenderImage>> {
|
||||
let width = (frame.w * scale).round() as i32;
|
||||
let height = (frame.h * scale).round() as i32;
|
||||
let size = gpui::size(gpui::DevicePixels(width), gpui::DevicePixels(height));
|
||||
let samples: Vec<Option<Vec<u8>>> = SAMPLE_OFFSETS
|
||||
.iter()
|
||||
.map(|&(dx, dy)| {
|
||||
let document = svg_document(node, frame, Some(Sample { dx, dy, scale }));
|
||||
let parsed = renderer.parse_svg(document.as_bytes()).ok()?;
|
||||
let rendered = renderer
|
||||
.render_parsed(&parsed, gpui::SvgSize::ExactSize(size))
|
||||
.ok()?;
|
||||
rendered.as_bytes(0).map(<[u8]>::to_vec)
|
||||
})
|
||||
.collect();
|
||||
average(&samples, width as u32, height as u32)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rasterising_the_samples_side_by_side_gives_the_same_pixels() {
|
||||
let renderer = gpui::SvgRenderer::new(Arc::new(()));
|
||||
// 24 and 48 point frames are drawn in turn, and 80 at a scale of 2 is
|
||||
// large enough to be drawn in parallel.
|
||||
for (index, drawing) in drawings().iter().enumerate() {
|
||||
for (side, scale) in [(24.0, 1.0), (24.0, 2.0), (80.0, 2.0)] {
|
||||
let frame = RectF {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
w: side,
|
||||
h: side,
|
||||
};
|
||||
let together = sampled_image(&renderer, drawing, frame, scale).expect("drawn");
|
||||
let apart = sequential(&renderer, drawing, frame, scale).expect("drawn");
|
||||
assert_eq!(
|
||||
together.as_bytes(0).unwrap(),
|
||||
apart.as_bytes(0).unwrap(),
|
||||
"drawing {index} at scale {scale}"
|
||||
);
|
||||
assert!(together.as_bytes(0).unwrap().iter().any(|byte| *byte != 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user