@@ -31,6 +31,7 @@ library flutter_single_instance;
31
31
32
32
export 'package:window_manager/window_manager.dart' show windowManager;
33
33
34
+ import 'dart:async' ;
34
35
import 'dart:convert' ;
35
36
import 'dart:io' ;
36
37
import 'package:flutter/foundation.dart' ;
@@ -81,6 +82,17 @@ abstract class FlutterSingleInstance {
81
82
/// Defaults to [kDebugMode] .
82
83
static bool debugMode = kDebugMode;
83
84
85
+ /// Called before this instance is focused with metadata provided by the calling instance.
86
+ ///
87
+ /// ```dart
88
+ /// FlutterSingleInstance.onFocus = (metadata) {
89
+ /// print("Focused instance with metadata: $metadata"); // "Focused instance with metadata: {hello: world}"
90
+ /// };
91
+ ///
92
+ /// FlutterSingleInstance().focus({"hello": "world"});
93
+ /// ```
94
+ static FutureOr <void > Function (Map <String , dynamic >)? onFocus;
95
+
84
96
/// Retrieves the process name of the given [pid] .
85
97
/// Returns [null] if the process does not exist.
86
98
Future <String ?> getProcessName (int pid);
@@ -190,9 +202,10 @@ abstract class FlutterSingleInstance {
190
202
return _server! .port! ;
191
203
}
192
204
193
- /// Focuses the running instance of the app and
194
- /// returns `null` if the operation was successful or an error message if it failed.
195
- Future <String ?> focus () async {
205
+ /// Focuses the running instance of the app and returns `null` if the operation was successful or an error message if it failed.
206
+ ///
207
+ /// The [metadata] parameter is passed to the focused instance's [onFocus] callback.
208
+ Future <String ?> focus ([Object ? metadata]) async {
196
209
if (_instance == null ) return "No instance to focus" ;
197
210
if (_server != null ) return "This is the first instance" ;
198
211
@@ -213,9 +226,15 @@ abstract class FlutterSingleInstance {
213
226
),
214
227
);
215
228
229
+ if (metadata == null ) {
230
+ metadata = < String , dynamic > {};
231
+ }
232
+ final json = jsonEncode (metadata);
233
+ final binary = utf8.encode (json);
234
+
216
235
final client = FocusServiceClient (channel);
217
236
218
- final response = await client.focus (FocusRequest ());
237
+ final response = await client.focus (FocusRequest (metadata : binary ));
219
238
220
239
if (response.success) {
221
240
logger.finest ("Instance focused" );
0 commit comments