Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions lib/src/windows_single_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,30 @@ class WindowsSingleInstance {
/// `bringWindowToFront`: Should your active window become visible\
/// `onSecondWindow`: Callback function that is called when a second window is attempted to be opened.
/// `exitFunction`: An alternate function to exit the app, if not provided, the app will be exited using `exit(0)`.
static Future ensureSingleInstance(
List<String> arguments,
String pipeName, {
Function(List<String>)? onSecondWindow,
bool bringWindowToFront = true,
Future<void> Function()? exitFunction,
}) async {
if (!Platform.isWindows) return;
/// __Returns__\
/// `true` if this is the only running instance of the app;\
/// `false` if another instance already exists and this invocation is terminating.
static Future<bool> ensureSingleInstance(
List<String> arguments,
String pipeName, {
Function(List<String>)? onSecondWindow,
bool bringWindowToFront = true,
Future<void> Function()? exitFunction,
}) async {
if (!Platform.isWindows) return true;
final fullPipeName = "\\\\.\\pipe\\$pipeName";
final bool isSingleInstance = await _channel.invokeMethod('isSingleInstance', <String, Object>{
"pipe": pipeName,
});
if (!isSingleInstance) {
_writePipeData(fullPipeName, arguments);
await (exitFunction?.call() ?? Future.value(exit(0)));
return;
return false;
}

// No callback so don't bother starting pipe
if (onSecondWindow == null && bringWindowToFront == false) {
return;
return true;
}

final reader = ReceivePort()
Expand All @@ -145,6 +148,7 @@ class WindowsSingleInstance {
}
});
await Isolate.spawn(_startReadPipeIsolate, {"port": reader.sendPort, "pipe": fullPipeName});
return true;
}

static void _bringWindowToFront() {
Expand Down
12 changes: 6 additions & 6 deletions lib/src/windows_single_instance.mock.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class WindowsSingleInstance {
WindowsSingleInstance._();

static Future ensureSingleInstance(
List<String> arguments,
String pipeName, {
Function(List<String>)? onSecondWindow,
bool bringWindowToFront = true,
static Future<bool> ensureSingleInstance(
List<String> arguments,
String pipeName, {
Function(List<String>)? onSecondWindow,
bool bringWindowToFront = true,
Future<void> Function()? exitFunction,
}) async {
}) async {
throw UnimplementedError("windows_single_instance not supported on this platform");
}
}