Skip to content

Add cursor and selection rendering for document editor#6

Merged
kevincarlson merged 1 commit into
mainfrom
claude/cursor-selection-rendering-NH9gs
Apr 27, 2026
Merged

Add cursor and selection rendering for document editor#6
kevincarlson merged 1 commit into
mainfrom
claude/cursor-selection-rendering-NH9gs

Conversation

@kevincarlson

Copy link
Copy Markdown
Member

Summary

This PR implements cursor positioning, selection highlighting, and hit testing for the Loki document editor. It introduces the editing layer infrastructure to track cursor state, resolve cursor positions to visual coordinates, and render the cursor line and selection highlights on the canvas.

Key Changes

  • New editing module (loki-text/src/editing/):

    • cursor.rs: Defines CursorState (anchor/focus positions) and DocumentPosition (page/paragraph/byte offset) types
    • hit_test.rs: Implements canvas-coordinate to document-position hit testing using Strategy C (calculated from known layout values)
  • Cursor state management in editor.rs:

    • Added cursor_state signal to track anchor and focus positions
    • Implemented onmousedown, onmousemove, and onmouseup handlers for click-to-place and drag-to-select
    • Integrated canvas origin calculation (flex-centered pages) and scroll offset tracking
    • Wired cursor state to WgpuSurface component via props
  • Cursor rendering pipeline:

    • Extended DocumentState to carry optional cursor_state
    • Added resolve_cursor_paint() function to convert CursorState to CursorPaint for a specific page
    • Implemented build_selection_rects() for single-line and multi-line selection highlighting (intra-paragraph MVP)
    • Modified paint_single_page() to accept and render cursor/selection data via new paint_cursor() function
  • Layout integration:

    • Updated layout_document() calls to set preserve_for_editing: true when cursor is active (retains Parley data for hit testing)
    • Falls back to preserve_for_editing: false in read-only mode for lighter memory footprint
  • New types in loki-vello:

    • SelectionRect: Paragraph-local highlight rectangle
    • CursorPaint: Container for cursor rect and selection rects for a single paragraph
    • paint_cursor(): Renders 2-point-wide cursor line (accent color) and semi-transparent blue selection highlights
  • Loro bridge stub (loki-doc-model):

    • Added derive_loro_cursor() function (returns None in MVP; full implementation blocked on block-ID lookup table from layout engine)

Implementation Details

  • Coordinate spaces: Hit testing converts window-relative client coordinates → canvas-local CSS pixels → layout points (72 dpi), accounting for page margins and paragraph origins
  • Multi-page support: Hit test correctly identifies which page was clicked and rejects clicks in inter-page gaps
  • Selection MVP: Only intra-paragraph selection is supported; cross-paragraph selection produces no rects
  • Editing mode guard: All cursor state modifications are guarded on EditorMode::Editing; read-only mode shows no cursor
  • Comprehensive tests: Added unit tests for cursor paint resolution, selection rect generation, and hit testing across single and multi-page layouts

https://claude.ai/code/session_01HqYxT4iW8phi45WQUbifpt

- Add DocumentPosition + CursorState types (loki-text/src/editing/)
- Add hit_test_document with Strategy C coordinate transform (client coords
  via canvas_origin computed from layout constants; offset_x/use_mounted
  are unimplemented in dioxus-native-dom-0.7.4)
- Add CursorPaint, SelectionRect, paint_cursor to loki-vello/src/scene.rs;
  update paint_single_page to accept Option<&CursorPaint>
- Add resolve_cursor_paint + build_selection_rects in document_source.rs;
  wire cursor_state through DocumentState → WgpuSurface → editor.rs
- Add derive_loro_cursor stub in loro_bridge.rs (TODO: mapping from
  page/paragraph index to Loro block requires layout engine lookup table)
- Wire onmousedown/onmousemove/onmouseup handlers in editor.rs with
  TODO(cursor-layout-access) placeholder for PaginatedLayout access
- Add 3 cursor paint resolution tests; fix wgpu_surface_integration.rs

https://claude.ai/code/session_01HqYxT4iW8phi45WQUbifpt
@kevincarlson kevincarlson merged commit f704505 into main Apr 27, 2026
1 check passed
@kevincarlson kevincarlson deleted the claude/cursor-selection-rendering-NH9gs branch April 27, 2026 06:10
@AppThere AppThere locked as resolved and limited conversation to collaborators Apr 27, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants