Skip to content

core: Expose dragging state on TextSelectionEvent - #4203

Open
M4n5ter wants to merge 1 commit into
longbridge:mainfrom
M4n5ter:pr/text-selection-dragging
Open

M4n5ter wants to merge 1 commit into
longbridge:mainfrom
M4n5ter:pr/text-selection-dragging

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Expose dragging: bool on TextSelectionEvent (and TextViewSelectionEvent / JS onSelectionChange) so applications anchoring floating action bars or popovers to pageBounds can defer showing them until the user finishes dragging instead of flickering or jumping under the cursor.

  • Semantics: dragging is true while an active pointer or selection-handle drag is extending a non-empty selection (!text.is_empty()), then flips to false once the drag ends (pointer up, cancel, window deactivation, or active handle dismissal) or the selection clears.
  • Full backend parity: Implemented across all 8 backends (macOS, Windows, Linux/GTK4, iOS, Android, Web, GPUI, TUI) and Headless (nsh-testing), including:
    • Deduplication keyed on dragging alongside text, bounds, and endpoints so releasing a drag emits dragging: false even when the selected text and box are unchanged.
    • Consistent text.is_empty() => !dragging invariant across every backend (including empty-copyText attachments).
    • Mid-drag selectable={false} teardown on macOS/Linux, window-scoped deactivation cancel on GPUI (both main AppView and secondary WindowView), split-touch / handle-dismiss single-source-of-truth tracking on Android + JNI eventMap key interning, and primary-pointer / .nsh-area drag ownership on Web.
    • JS host fallback defaulting omitted dragging to false so listeners always receive a boolean.
    • Headless selection_down ignoring nested Input drags without affecting programmatic ref.selectAll(), and Headless::done() flushing queued events once on error exit so trailing release errors stay attributed to the drag step.

Public API

  • Rust (nsh-core): Added pub dragging: bool to TextSelectionEvent (already #[non_exhaustive], defaults to false).
  • Rust (nsh-base): Added pub dragging: bool to TextViewSelectionEvent (already #[non_exhaustive], defaults to false) and plumbed e.dragging in TextView.
  • JS / TS: Added dragging: boolean to onSelectionChange on selectable <View> and <TextView> (crates/nsh-core/src/js/host.rs, TextViewSelectionEvent JsObject derive).
  • Docs: Updated EN/ZH text-selection.md and text-view.md.

Breaking Changes

None. Both TextSelectionEvent and TextViewSelectionEvent are already #[non_exhaustive] and derive Default; JS listeners receive an additional dragging: boolean property (defaulting to false for any payload omitting it).

Verification

  • macOS (real AppKit): cargo clippy -p nsh-core -p nsh-js -p nsh-base -p nsh-testing -p nsh-platform-macos -p nsh-platform-tui --features nsh-platform-macos/dev --tests -- -D warnings; NSH_TEXT_VIEW_SELECTION_CHECK_ONLY=1 cargo test -p nsh-platform-macos --test native -- --ignored (asserts real AppKit dragging: true -> false across LeftMouseDown/LeftMouseDragged/LeftMouseUp).
  • Core / JS / Base / Headless / TUI: cargo test -p nsh-core --lib selected_text, public_api_is_typed; cargo test -p nsh-js --lib declarations (20/20); cargo test -p nsh-base --test base text_view_selection_change_forwards_dragging_state; cargo test -p nsh-testing --lib selection_drag_reports_dragging_until_release (drag_and_hold/drop_held, nested Input, ref.selectAll()); cargo test -p nsh-testing --features bundle --test testing select (5/5); cargo test -p nsh-platform-tui --test tui a_selection_area_reports_dragging_until_mouse_up.
  • Web: node --test crates/nsh-platform-web/web/nsh.test.mjs (201/201, exercising real pointerDown/areaSelects/editable/textAt/areaGap/areaOf, concurrent pointer types, and pointerUp); cargo check --target wasm32-unknown-unknown -p nsh-platform-web --tests.
  • GPUI & iOS: cargo clippy --manifest-path crates/gpui/Cargo.toml -p nsh-platform-gpui --features runtime-shaders -- -D warnings; cargo clippy --target aarch64-apple-ios -p nsh-platform-ios --features embed,dev --no-deps -- -D warnings.
  • Linux (Arch Linux x86_64 GTK4): cargo clippy -p nsh-platform-linux --tests -- -D warnings; xvfb-run -a cargo test -p nsh-platform-linux --lib selectable_view (3/3).
  • Windows (Windows 11 VM): cargo clippy -p nsh-platform-windows --tests -- -D warnings; cargo test -p nsh-platform-windows --lib selectable_view (2/2).
  • Compat: Extended tests/compat/cases/base/base-selectable-view.tsx and script.txt (strict e.dragging === false check on omitted payloads + true -> false on both <View selectable> and <TextView selectable>).

Original contribution authored by @M4n5ter and contributed directly under NSH's Apache-2.0 license.

@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

💡 Needs an owner decision on the new public field TextSelectionEvent::dragging.

crates/nsh-core/src/element/events.rs:153, also exposed as JS onSelectionChange.dragging. Precedent exists in AppKit (setSelectedRange:affinity:stillSelecting:); UIKit and the Web selectionchange event have none, apps there wait for pointer-up.
TextSelectionEvent is a public struct without #[non_exhaustive], so downstream struct literals stop compiling; this needs a Breaking Changes note.
Implementation looks consistent: core parsing defaults to false (events.rs:609), dedup includes dragging, tests cover headless, TUI, Web and macOS. It is ~1k lines across 8 backends for one overlay case, hence the scope call.

Diff-only review at 868b13f.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@M4n5ter

M4n5ter commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Two quick clarifications for the maintainer call:

  • Not a breaking Rust change: both TextSelectionEvent (crates/nsh-core/src/element/events.rs:125) and TextViewSelectionEvent (crates/nsh-base/src/text_view/view.rs:308) are already #[non_exhaustive], so downstream crates cannot construct struct literals or exhaustively destructure them.
  • Why apps cannot rely on pointer-up: native text selection drags (and iOS/Android selection handles) own the pointer gesture and frequently finish outside the selectable view's bounds or inside native handle windows, so view-level pointer-up handlers either do not fire or cannot distinguish an active drag from keyboard/double-click/programmatic selection. event.dragging lets selection overlays wait for release uniformly across mouse, touch handles, and keyboard selection.

@M4n5ter
M4n5ter force-pushed the pr/text-selection-dragging branch from 868b13f to 3823bd2 Compare December 3, 2026 14:39
@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

💡 The new dragging field is wired consistently across backends; adding it to the public API is a maintainer call.

  • New public API: TextSelectionEvent::dragging (crates/nsh-core/src/element/events.rs) and TextViewSelectionEvent::dragging (crates/nsh-base/src/text_view/view.rs); in JS onSelectionChange → dragging, with crates/nsh-js/src/host.rs defaulting to false when a backend omits it.
  • Precedent: AppKit has stillSelecting:; UIKit and the Web selectionchange event have nothing like it. Both structs are #[non_exhaustive], so it is not breaking in Rust.
  • Scope: 33 files across macOS, iOS, Android, Windows, Linux, GPUI, TUI and Web, with tests, compat cases and docs in both languages. Payload parsing defaults to false and is unit-tested.
  • Open question: whether apps need this from the framework or can use pointer-up plus a debounce. The author notes native selection handles do not report pointer-up to apps.
  • CI: not run (fork workflows are waiting for maintainer approval).

Reviewed at 3823bd2 (diff only, not built).

🤖 Noop (reviewer) · AI agent acting for @huacnlee

Expose a `dragging: bool` field on `TextSelectionEvent` (and
`TextViewSelectionEvent` / JS `onSelectionChange`) that is `true` while
a pointer or selection-handle drag is actively extending a non-empty
selection and flips to `false` once the drag ends or the selection
clears.

This allows applications anchoring floating action bars or popovers to
`pageBounds` to defer showing the overlay until the user finishes
dragging instead of flickering or jumping under the cursor.

Contributed by the author from https://github.com/apache/maka (#7504)
under NSH's Apache-2.0 license.
@M4n5ter
M4n5ter force-pushed the pr/text-selection-dragging branch from 3823bd2 to 06aef46 Compare December 4, 2026 00:52

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants