mirror of
https://github.com/LinwoodDev/Butterfly.git
synced 2026-03-13 09:20:32 +08:00
32 lines
771 B
Dart
32 lines
771 B
Dart
import 'dart:async';
|
|
|
|
import 'package:butterfly/api/close.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:web/web.dart';
|
|
|
|
CloseSubscription onPreventClose(
|
|
BuildContext context,
|
|
OnCloseCallback onClose,
|
|
) {
|
|
return WebCloseSubscription(onClose);
|
|
}
|
|
|
|
class WebCloseSubscription extends CloseSubscription {
|
|
final StreamSubscription _subscription;
|
|
|
|
WebCloseSubscription(OnCloseCallback onClose)
|
|
: _subscription =
|
|
const EventStreamProvider<BeforeUnloadEvent>(
|
|
'onbeforeunload',
|
|
).forTarget(window).listen((event) {
|
|
final message = onClose();
|
|
if (message == null) return;
|
|
event.returnValue = message;
|
|
});
|
|
|
|
@override
|
|
void dispose() {
|
|
_subscription.cancel();
|
|
}
|
|
}
|