Skip to main content

CommonLibrary/IPC/
SkyEvent.rs

1//! # Sky Event Registry - single source of truth for Mountain → Sky/Wind events
2//!
3//! Mountain emits Tauri events on `sky://…` URIs to notify the webview of
4//! state changes that don't originate from a Wind-initiated `invoke` call.
5//! Historically each emit site used a free-text string literal and each Wind
6//! listener matched against its own free-text string - drift was invisible
7//! until runtime (the listener simply never fired).
8//!
9//! `SkyEvent` is the enumerated registry. Mountain callers dispatch on the
10//! variant; the wire string is produced by `AsStr()` and parsed by `FromStr`.
11//! The matching TypeScript const object lives at
12//! `Element/Wind/Source/IPC/SkyEvent.ts` - kept in sync by convention, same
13//! protocol as the `Channel` registry.
14//!
15//! ## Adding a new event
16//!
17//! 1. Add the variant here AND in `Element/Wind/Source/IPC/SkyEvent.ts`.
18//! 2. Emit from Mountain:
19//!    `ApplicationHandle.emit(SkyEvent::TerminalData.AsStr(), Payload)`.
20//! 3. Subscribe from Wind: `IPCService.events(SkyEvent.TerminalData)`.
21//!
22//! ## Why a declarative macro?
23//!
24//! Same rationale as `Channel`: the variant → wire-string mapping is pure
25//! data. `DefineSkyEvents!` expands it into enum body + `AsStr` + `All` +
26//! `FromStr` in one pass so adding an event is a single-line change that
27//! compilers can't forget.
28
29#![allow(non_camel_case_types)]
30
31macro_rules! DefineSkyEvents {
32
33	($($Variant:ident => $Wire:literal,)* $(,)?) => {
34
35		/// Enumerated Mountain → Sky/Wind event identifiers.
36		#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
37		pub enum SkyEvent {
38
39			$($Variant,)*
40		}
41
42		impl SkyEvent {
43
44			/// Wire string produced on the Tauri event transport.
45			pub fn AsStr(&self) -> &'static str {
46
47				match self {
48
49					$(Self::$Variant => $Wire,)*
50				}
51			}
52
53			/// Full set of events, in declaration order.
54			pub fn All() -> &'static [Self] {
55
56				&[$(Self::$Variant,)*]
57			}
58		}
59
60		impl ::std::fmt::Display for SkyEvent {
61
62			fn fmt(&self, Formatter:&mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
63
64				Formatter.write_str(self.AsStr())
65			}
66		}
67
68		impl ::std::str::FromStr for SkyEvent {
69
70			type Err = ::std::string::String;
71
72			fn from_str(Wire:&str) -> ::std::result::Result<Self, Self::Err> {
73
74				match Wire {
75
76					$($Wire => Ok(Self::$Variant),)*
77					_ => Err(format!("unknown Sky event: {}", Wire)),
78				}
79			}
80		}
81	};
82}
83
84DefineSkyEvents! {
85
86	// --- Configuration ---
87	ConfigurationChanged                          => "sky://configuration/changed",
88
89	// --- Debug ---
90	DebugDapMessage                               => "sky://debug/dap-message",
91
92	DebugRegister                                 => "sky://debug/register",
93
94	DebugStart                                    => "sky://debug/start",
95
96	DebugStop                                     => "sky://debug/stop",
97
98	// --- Decorations (set-ranges) ---
99	// Fired when an extension calls `editor.setDecorations(type, ranges)`.
100	// Payload: `{ decorationTypeKey, uri, rangesOrOptions }`.
101	// Sky listener applies to Monaco via ICodeEditorService.
102	DecorationSetRanges                           => "sky://decoration/set-ranges",
103
104	// --- Diagnostics ---
105	DiagnosticsChanged                            => "sky://diagnostics/changed",
106
107	// --- CustomEditor ---
108	CustomEditorSaved                             => "sky://customEditor/saved",
109
110	// --- Dialog ---
111	DialogOpen                                    => "sky://dialog/open",
112
113	DialogSave                                    => "sky://dialog/save",
114
115	// --- Documents ---
116	DocumentsOpen                                 => "sky://documents/open",
117
118	DocumentsRenamed                              => "sky://documents/renamed",
119
120	DocumentsSaved                                => "sky://documents/saved",
121
122	// --- Editor ---
123	EditorApplyEdits                              => "sky://editor/applyEdits",
124
125	// Fired when an extension calls `editor.edit(editBuilder => { ... })`.
126	// Payload: `{ uri, edits: [{ range, text }] }`.
127	EditorApplyTextEdits                          => "sky://editor/apply-text-edits",
128
129	// Fired by Sky when the active text editor's selection changes.
130	// Payload: `{ uri, selections: [{ start, end, active, anchor }] }`.
131	// Mountain stores it and forwards to Cocoon via `window.didChangeTextEditorSelection`.
132	EditorSelectionChanged                        => "sky://editor/selection-changed",
133
134	EditorOpenDocument                            => "sky://editor/openDocument",
135
136	EditorSaveAll                                 => "sky://editor/saveAll",
137
138	// Fired by Sky when a text editor becomes visible (tab activated).
139	// Payload: `{ uri, viewColumn, selections }`.
140	EditorActiveChanged                           => "sky://editor/active-changed",
141
142	// `editor.revealRange(range, revealType)` - scroll Monaco to the range.
143	// Payload: `{ uri, range, revealType }`.
144	EditorRevealRange                             => "sky://editor/revealRange",
145
146	// --- Extensions ---
147	ExtensionsInstalled                           => "sky://extensions/installed",
148
149	ExtensionsUninstalled                         => "sky://extensions/uninstalled",
150
151	// --- ExtHost ---
152	ExtHostDebugClose                             => "sky://exthost/debug-close",
153
154	ExtHostDebugReload                            => "sky://exthost/debug-reload",
155
156	// --- Input ---
157	InputBoxShow                                  => "sky://input-box/show",
158
159	// --- Language ---
160	LanguageConfigure                             => "sky://language/configure",
161
162	LanguagesSetDocumentLanguage                  => "sky://languages/setDocumentLanguage",
163
164	// --- Lifecycle ---
165	LifecyclePhaseChanged                         => "sky://lifecycle/phaseChanged",
166
167	LifecycleWillShutdown                         => "sky://lifecycle/willShutdown",
168
169	// --- Native ---
170	NativeOpenExternal                            => "sky://native/openExternal",
171
172	// --- Notifications ---
173	NotificationProgressBegin                     => "sky://notification/progress-begin",
174
175	NotificationProgressEnd                       => "sky://notification/progress-end",
176
177	NotificationProgressUpdate                    => "sky://notification/progress-update",
178
179	NotificationShow                              => "sky://notification/show",
180
181	// --- Output ---
182	OutputAppend                                  => "sky://output/append",
183
184	OutputClear                                   => "sky://output/clear",
185
186	OutputCreate                                  => "sky://output/create",
187
188	OutputDispose                                 => "sky://output/dispose",
189
190	OutputReplace                                 => "sky://output/replace",
191
192	OutputReveal                                  => "sky://output/reveal",
193
194	OutputShow                                    => "sky://output/show",
195
196	// --- Progress ---
197	ProgressBegin                                 => "sky://progress/begin",
198
199	ProgressComplete                              => "sky://progress/complete",
200
201	ProgressEnd                                   => "sky://progress/end",
202
203	ProgressReport                                => "sky://progress/report",
204
205	ProgressStart                                 => "sky://progress/start",
206
207	ProgressUpdate                                => "sky://progress/update",
208
209	// --- QuickPick ---
210	QuickPickShow                                 => "sky://quickpick/show",
211
212	// --- Source Control ---
213	SCMGroupChanged                               => "sky://scm/group/changed",
214
215	SCMProviderAdded                              => "sky://scm/provider/added",
216
217	SCMProviderChanged                            => "sky://scm/provider/changed",
218
219	SCMProviderRemoved                            => "sky://scm/provider/removed",
220
221	SCMRegister                                   => "sky://scm/register",
222
223	SCMUpdateGroup                                => "sky://scm/updateGroup",
224
225	// --- Status bar ---
226	// Canonical prefix is `sky://statusbar/` (no hyphen). The earlier
227	// `sky://status-bar/message` channel was an accidental fork produced by
228	// a separate emit site and has been consolidated onto
229	// `sky://statusbar/set-message`.
230	StatusBarCreate                               => "sky://statusbar/create",
231
232	StatusBarDispose                              => "sky://statusbar/dispose",
233
234	StatusBarDisposeEntry                         => "sky://statusbar/dispose-entry",
235
236	StatusBarDisposeMessage                       => "sky://statusbar/dispose-message",
237
238	StatusBarSetEntry                             => "sky://statusbar/set-entry",
239
240	StatusBarSetMessage                           => "sky://statusbar/set-message",
241
242	StatusBarUpdate                               => "sky://statusbar/update",
243
244	// --- Task ---
245	TaskExecute                                   => "sky://task/execute",
246
247	TaskTerminate                                 => "sky://task/terminate",
248
249	// --- Terminal ---
250	TerminalClosed                                => "sky://terminal/closed",
251
252	TerminalCreate                                => "sky://terminal/create",
253
254	TerminalData                                  => "sky://terminal/data",
255
256	TerminalExit                                  => "sky://terminal/exit",
257
258	TerminalHide                                  => "sky://terminal/hide",
259
260	TerminalOpened                                => "sky://terminal/opened",
261
262	TerminalProcessId                             => "sky://terminal/processId",
263
264	TerminalResize                                => "sky://terminal/resize",
265
266	TerminalShow                                  => "sky://terminal/show",
267
268	// --- Test ---
269	TestRegistered                                => "sky://test/registered",
270
271	TestRunStarted                                => "sky://test/run-started",
272
273	TestRunStatusChanged                          => "sky://test/run-status-changed",
274
275	// --- Theme ---
276	ThemeChange                                   => "sky://theme/change",
277
278	// --- Tree view ---
279	// Canonical prefix is `sky://tree-view/` (kebab-case). The earlier
280	// `sky://treeView/register` camelCase channel was a parallel emission
281	// from `CocoonService/TreeView.rs`; it has been collapsed into
282	// `TreeViewCreate`, which every handler already subscribes to.
283	TreeViewCreate                                => "sky://tree-view/create",
284
285	TreeViewDispose                               => "sky://tree-view/dispose",
286
287	TreeViewNodeExpanded                          => "sky://tree-view/node-expanded",
288
289	TreeViewRefresh                               => "sky://tree-view/refresh",
290
291	TreeViewRestoreState                          => "sky://tree-view/restore-state",
292
293	TreeViewReveal                                => "sky://tree-view/reveal",
294
295	TreeViewSelectionChanged                      => "sky://tree-view/selection-changed",
296
297	TreeViewSetBadge                              => "sky://tree-view/set-badge",
298
299	TreeViewSetMessage                            => "sky://tree-view/set-message",
300
301	TreeViewSetTitle                              => "sky://tree-view/set-title",
302
303	// --- UI ---
304	// `UIShow{InputBox,QuickPick}Request` are deprecated aliases. The
305	// Sky listener channels are `InputBoxShow` and `QuickPickShow`
306	// declared earlier in this enum. `UserInterfaceProvider.rs` now
307	// references those directly so the `UIShow*Request` channel names
308	// below remain reachable only from older code paths and tests.
309	UIShowInputBoxRequest                         => "sky://ui/show-input-box-request",
310
311	UIShowMessageRequest                          => "sky://ui/show-message-request",
312
313	UIShowQuickPickRequest                        => "sky://ui/show-quick-pick-request",
314
315	// --- Virtual file system ---
316	VFSFileChange                                 => "sky://vfs/fileChange",
317
318	// --- Webview ---
319	// Canonical form is kebab-case (`sky://webview/post-message`,
320	// `sky://webview/set-html`). The `…CamelCase` aliases existed because
321	// mod.rs emitted `sky://webview/postMessage` / `sky://webview/setHtml`
322	// inline; those emit sites have been migrated to the enum so Sky only
323	// ever sees the kebab-case form.
324	WebviewCreate                                 => "sky://webview/create",
325
326	WebviewCreated                                => "sky://webview/created",
327
328	WebviewDispose                                => "sky://webview/dispose",
329
330	WebviewDisposed                               => "sky://webview/disposed",
331
332	WebviewMessage                                => "sky://webview/message",
333
334	WebviewOptionsChanged                         => "sky://webview/options-changed",
335
336	WebviewPostMessage                            => "sky://webview/post-message",
337
338	WebviewRevealed                               => "sky://webview/revealed",
339
340	WebviewSetHTML                                => "sky://webview/set-html",
341
342	// --- Window ---
343	WindowShowTextDocument                        => "sky://window/showTextDocument",
344
345	// --- Workspace ---
346	WorkspaceApplyEdit                            => "sky://workspace/applyEdit",
347
348	WorkspacesChanged                             => "sky://workspaces/changed",
349}
350
351#[cfg(test)]
352mod Tests {
353
354	use std::str::FromStr;
355
356	use super::SkyEvent;
357
358	#[test]
359	fn RoundTrip() {
360		for Variant in SkyEvent::All() {
361			let Wire = Variant.AsStr();
362
363			let Parsed = SkyEvent::from_str(Wire).expect("round-trip");
364
365			assert_eq!(*Variant, Parsed, "{} failed round-trip", Wire);
366		}
367	}
368
369	#[test]
370	fn EveryWireStartsWithSkyScheme() {
371		for Variant in SkyEvent::All() {
372			assert!(
373				Variant.AsStr().starts_with("sky://"),
374				"{} does not use the sky:// scheme",
375				Variant.AsStr()
376			);
377		}
378	}
379
380	#[test]
381	fn RejectsUnknown() {
382		assert!(SkyEvent::from_str("mountain://nope").is_err());
383
384		assert!(SkyEvent::from_str("").is_err());
385	}
386
387	/// Guards against drift between this Rust enum and its TS mirror at
388	/// `Element/Wind/Source/IPC/SkyEvent.ts`. Both files are hand-edited,
389	/// so the test scrapes the TS literal array and asserts every wire
390	/// string here exists there, and vice versa. If this fails the two
391	/// tables disagree - add or remove from whichever side is missing.
392	#[test]
393	fn RustAndTypeScriptTablesAgree() {
394		use std::{collections::HashSet, path::PathBuf};
395
396		let TsPath = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../Wind/Source/IPC/SkyEvent.ts");
397
398		let Source = match std::fs::read_to_string(&TsPath) {
399			Ok(S) => S,
400
401			// In packaging contexts where Wind isn't checked out alongside
402			// Common we skip the cross-check silently rather than
403			// failing - the RoundTrip / UniqueWireStrings guards above
404			// still cover the Rust side on its own.
405			Err(_) => return,
406		};
407
408		let mut TsWires:HashSet<String> = HashSet::new();
409
410		for Line in Source.lines() {
411			if let Some(Start) = Line.find("\"sky://") {
412				let Tail = &Line[Start + 1..];
413
414				if let Some(End) = Tail.find('"') {
415					TsWires.insert(Tail[..End].to_string());
416				}
417			}
418		}
419
420		let RsWires:HashSet<String> = SkyEvent::All().iter().map(|V| V.AsStr().to_string()).collect();
421
422		let OnlyInRust:Vec<_> = RsWires.difference(&TsWires).collect();
423
424		let OnlyInTs:Vec<_> = TsWires.difference(&RsWires).collect();
425
426		assert!(
427			OnlyInRust.is_empty() && OnlyInTs.is_empty(),
428			"SkyEvent drift between Rust and TS:\n  only in Rust: {:?}\n  only in TS:   {:?}",
429			OnlyInRust,
430			OnlyInTs
431		);
432	}
433
434	#[test]
435	fn UniqueWireStrings() {
436		let mut Seen = std::collections::HashSet::new();
437
438		for Variant in SkyEvent::All() {
439			assert!(Seen.insert(Variant.AsStr()), "duplicate wire: {}", Variant.AsStr());
440		}
441	}
442}