text_document/events.rs
1//! Document event types and subscription handle.
2
3use std::sync::Arc;
4use std::sync::atomic::AtomicBool;
5
6use crate::inner::{CallbackEntry, TextDocumentInner};
7
8/// Events emitted by a [`TextDocument`](crate::TextDocument).
9///
10/// Subscribe via [`TextDocument::on_change`](crate::TextDocument::on_change) (callback-based)
11/// or poll via [`TextDocument::poll_events`](crate::TextDocument::poll_events) (frame-loop).
12///
13/// These events carry enough information for a UI to do incremental updates —
14/// repaint only the affected region, not the entire document.
15#[derive(Debug, Clone, PartialEq)]
16pub enum DocumentEvent {
17 /// Text content changed at a specific region.
18 ///
19 /// Emitted by: `insert_text`, `delete_char`, `delete_previous_char`,
20 /// `remove_selected_text`, `insert_formatted_text`, `insert_block`,
21 /// `insert_html`, `insert_markdown`, `insert_fragment`, `insert_image`.
22 ContentsChanged {
23 position: usize,
24 chars_removed: usize,
25 chars_added: usize,
26 blocks_affected: usize,
27 },
28
29 /// Formatting changed without text content change.
30 FormatChanged {
31 position: usize,
32 length: usize,
33 /// Distinguishes block-level changes (relayout needed) from
34 /// character-level changes (reshaping only).
35 kind: crate::flow::FormatChangeKind,
36 },
37
38 /// Only paint-level highlight attributes changed (colors, underline
39 /// decorations) on a paint-only highlighter. The shaping input
40 /// (`fragments`) is unchanged, so the layout engine can recolor the
41 /// cached layout without reshaping or reflowing.
42 ///
43 /// `position` / `length` are document-absolute character offsets. For
44 /// whole-document rehighlights both are 0; for single-block
45 /// rehighlights they bound the affected block.
46 HighlightPaintChanged { position: usize, length: usize },
47
48 /// Block count changed. Carries the new count.
49 BlockCountChanged(usize),
50
51 /// Flow elements were inserted at the given index in the main
52 /// frame's `child_order`.
53 ///
54 /// This is a performance optimization — the layout engine can
55 /// update incrementally instead of re-querying
56 /// [`TextDocument::flow()`](crate::TextDocument::flow).
57 FlowElementsInserted { flow_index: usize, count: usize },
58
59 /// Flow elements were removed starting at the given index in the
60 /// main frame's `child_order`.
61 FlowElementsRemoved { flow_index: usize, count: usize },
62
63 /// The document was completely replaced (import, clear).
64 DocumentReset,
65
66 /// Undo/redo was performed or availability changed.
67 UndoRedoChanged { can_undo: bool, can_redo: bool },
68
69 /// The modified flag changed.
70 ModificationChanged(bool),
71
72 /// A long operation progressed.
73 LongOperationProgress {
74 operation_id: String,
75 percent: f64,
76 message: String,
77 },
78
79 /// A long operation completed or failed.
80 LongOperationFinished {
81 operation_id: String,
82 success: bool,
83 error: Option<String>,
84 },
85}
86
87/// Handle to a document event subscription.
88///
89/// Events are delivered as long as this handle is alive.
90/// Drop it to unsubscribe. No explicit unsubscribe method needed.
91pub struct Subscription {
92 alive: Arc<AtomicBool>,
93}
94
95impl Drop for Subscription {
96 fn drop(&mut self) {
97 self.alive
98 .store(false, std::sync::atomic::Ordering::Relaxed);
99 }
100}
101
102/// Register a callback with the document inner, returning a Subscription handle.
103pub(crate) fn subscribe_inner<F>(inner: &mut TextDocumentInner, callback: F) -> Subscription
104where
105 F: Fn(DocumentEvent) + Send + Sync + 'static,
106{
107 let alive = Arc::new(AtomicBool::new(true));
108 inner.callbacks.push(CallbackEntry {
109 alive: Arc::downgrade(&alive),
110 callback: Arc::new(callback),
111 });
112 Subscription { alive }
113}