1+ /// Debug-only Flutter runtime inspector for SimDeck.
2+ ///
3+ /// Connects to the SimDeck server over WebSocket and publishes the live
4+ /// Flutter widget hierarchy along with render bounds, diagnostics properties,
5+ /// semantics metadata, and source locations when widget creation tracking is
6+ /// enabled.
7+ ///
8+ /// Call [startSimDeckFlutterInspector] during app startup in debug builds to
9+ /// begin publishing; call [stopSimDeckFlutterInspector] to tear it down.
10+ library ;
11+
112import 'dart:async' ;
213import 'dart:convert' ;
314import 'dart:io' as io;
@@ -16,6 +27,19 @@ const int _maxHierarchyDepth = 64;
1627
1728SimDeckFlutterInspector ? _sharedInspector;
1829
30+ /// Starts the shared [SimDeckFlutterInspector] and connects to the SimDeck
31+ /// server.
32+ ///
33+ /// Safe to call once at app startup in debug builds. Subsequent calls return
34+ /// the existing inspector instance without reconnecting.
35+ ///
36+ /// - [host] / [port] / [path] / [secure] configure the SimDeck WebSocket
37+ /// endpoint. Defaults target `ws://127.0.0.1:4310/api/inspector/connect` .
38+ /// - [reconnect] keeps trying to reconnect when the SimDeck server is not yet
39+ /// available or drops the connection.
40+ /// - [sourceRoot] is the absolute path to the Flutter app's source directory.
41+ /// When Flutter reports relative source locations, the inspector resolves
42+ /// them against this root so consumers see absolute file paths.
1943SimDeckFlutterInspector startSimDeckFlutterInspector ({
2044 String host = '127.0.0.1' ,
2145 String path = '/api/inspector/connect' ,
@@ -42,13 +66,17 @@ SimDeckFlutterInspector startSimDeckFlutterInspector({
4266 return inspector;
4367}
4468
69+ /// Stops the shared inspector started by [startSimDeckFlutterInspector] and
70+ /// releases its WebSocket and semantics resources.
4571void stopSimDeckFlutterInspector () {
4672 _sharedInspector? .stop ();
4773 _sharedInspector = null ;
4874}
4975
76+ /// Configuration for a [SimDeckFlutterInspector] connection.
5077@immutable
5178class SimDeckFlutterInspectorOptions {
79+ /// Creates options for connecting the inspector to the SimDeck server.
5280 const SimDeckFlutterInspectorOptions ({
5381 this .host = '127.0.0.1' ,
5482 this .path = '/api/inspector/connect' ,
@@ -58,18 +86,41 @@ class SimDeckFlutterInspectorOptions {
5886 this .sourceRoot = '' ,
5987 });
6088
89+ /// Host of the SimDeck server. Defaults to the loopback address.
6190 final String host;
91+
92+ /// WebSocket path the inspector connects to on the SimDeck server.
6293 final String path;
94+
95+ /// Port of the SimDeck server.
6396 final int port;
97+
98+ /// Whether the inspector should keep retrying the WebSocket connection
99+ /// after a failure or drop.
64100 final bool reconnect;
101+
102+ /// Whether to use a TLS WebSocket (`wss` ) and HTTPS for polling.
65103 final bool secure;
104+
105+ /// Absolute path to the Flutter app's source root.
106+ ///
107+ /// Used to turn relative source locations reported by Flutter's widget
108+ /// creation tracking into absolute paths.
66109 final String sourceRoot;
67110}
68111
112+ /// Runtime inspector that publishes the live Flutter widget hierarchy to
113+ /// SimDeck over WebSocket and responds to inspector commands.
114+ ///
115+ /// Most apps should use [startSimDeckFlutterInspector] / [stopSimDeckFlutterInspector]
116+ /// to manage a single shared instance rather than constructing this directly.
69117class SimDeckFlutterInspector {
118+ /// Creates an inspector with the given [options] . Use [start] to begin
119+ /// publishing.
70120 SimDeckFlutterInspector ([SimDeckFlutterInspectorOptions ? options])
71121 : options = options ?? const SimDeckFlutterInspectorOptions ();
72122
123+ /// Connection options the inspector was created with.
73124 final SimDeckFlutterInspectorOptions options;
74125 final Expando <String > _ids = Expando <String >('simdeckFlutterInspectorId' );
75126 final Expando <Object > _sourceLocations = Expando <Object >(
@@ -86,6 +137,11 @@ class SimDeckFlutterInspector {
86137 Map <String , Object ?>? _metadata;
87138 SemanticsHandle ? _semanticsHandle;
88139
140+ /// Starts publishing to SimDeck.
141+ ///
142+ /// Opens the WebSocket connection, begins polling for inspector commands,
143+ /// and ensures Flutter semantics are enabled while the inspector is active.
144+ /// Calling [start] on an already-running inspector restarts it.
89145 void start () {
90146 stop ();
91147 _started = true ;
@@ -94,6 +150,8 @@ class SimDeckFlutterInspector {
94150 _startPolling ();
95151 }
96152
153+ /// Stops publishing and releases the WebSocket, polling timer, and
154+ /// semantics handle. Safe to call when the inspector is not running.
97155 void stop () {
98156 _reconnectTimer? .cancel ();
99157 _reconnectTimer = null ;
@@ -1078,10 +1136,20 @@ class SimDeckFlutterInspector {
10781136 }
10791137}
10801138
1139+ /// JSON-RPC style error raised when an inspector request cannot be served.
1140+ ///
1141+ /// The [code] follows the inspector protocol's negative numeric error space
1142+ /// (e.g. `-32601` unknown method, `-32004` unknown view id). The [message] is
1143+ /// suitable for surfacing to inspector clients.
10811144class SimDeckFlutterInspectorFailure implements Exception {
1145+ /// Creates a failure with the given protocol [code] and human-readable
1146+ /// [message] .
10821147 const SimDeckFlutterInspectorFailure (this .code, this .message);
10831148
1149+ /// Inspector protocol error code.
10841150 final int code;
1151+
1152+ /// Human-readable error message returned to the inspector client.
10851153 final String message;
10861154
10871155 @override
0 commit comments