Merge pull request #1980 from Kingtous/master

opt: add preventClose to handle close event manually
This commit is contained in:
RustDesk 2022-11-06 18:56:51 +08:00 committed by GitHub
commit 866224ecec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 13 deletions

View File

@ -33,7 +33,7 @@ class DesktopHomePage extends StatefulWidget {
const borderColor = Color(0xFF2F65BA); const borderColor = Color(0xFF2F65BA);
class _DesktopHomePageState extends State<DesktopHomePage> class _DesktopHomePageState extends State<DesktopHomePage>
with TrayListener, WindowListener, AutomaticKeepAliveClientMixin { with TrayListener, AutomaticKeepAliveClientMixin {
final _leftPaneScrollController = ScrollController(); final _leftPaneScrollController = ScrollController();
@override @override
@ -41,14 +41,6 @@ class _DesktopHomePageState extends State<DesktopHomePage>
var updateUrl = ''; var updateUrl = '';
StreamSubscription? _uniLinksSubscription; StreamSubscription? _uniLinksSubscription;
@override
void onWindowClose() async {
super.onWindowClose();
// hide window on close
await windowManager.hide();
rustDeskWinManager.unregisterActiveWindow(0);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context); super.build(context);
@ -425,7 +417,6 @@ class _DesktopHomePageState extends State<DesktopHomePage>
// disable this tray because we use tray function provided by rust now // disable this tray because we use tray function provided by rust now
// initTray(); // initTray();
trayManager.addListener(this); trayManager.addListener(this);
windowManager.addListener(this);
rustDeskWinManager.registerActiveWindowListener(onActiveWindowChanged); rustDeskWinManager.registerActiveWindowListener(onActiveWindowChanged);
rustDeskWinManager.registerActiveWindow(0); rustDeskWinManager.registerActiveWindow(0);
rustDeskWinManager.setMethodHandler((call, fromWindowId) async { rustDeskWinManager.setMethodHandler((call, fromWindowId) async {
@ -471,9 +462,9 @@ class _DesktopHomePageState extends State<DesktopHomePage>
@override @override
void dispose() { void dispose() {
// destoryTray(); // destoryTray();
rustDeskWinManager.unregisterActiveWindowListener(onActiveWindowChanged); // fix: disable unregister to prevent from receiving events from other windows
// rustDeskWinManager.unregisterActiveWindowListener(onActiveWindowChanged);
trayManager.removeListener(this); trayManager.removeListener(this);
windowManager.removeListener(this);
_uniLinksSubscription?.cancel(); _uniLinksSubscription?.cancel();
super.dispose(); super.dispose();
} }

View File

@ -498,6 +498,21 @@ class WindowActionPanelState extends State<WindowActionPanel>
super.onWindowUnmaximize(); super.onWindowUnmaximize();
} }
@override
void onWindowClose() async {
// hide window on close
if (widget.isMainWindow) {
await windowManager.hide();
rustDeskWinManager.unregisterActiveWindow(0);
} else {
widget.onClose?.call();
WindowController.fromWindowId(windowId!).hide();
rustDeskWinManager
.call(WindowType.Main, kWindowEventHide, {"id": windowId!});
}
super.onWindowClose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Row( return Row(

View File

@ -140,6 +140,8 @@ void runMultiWindow(
String title, String title,
) async { ) async {
await initEnv(appType); await initEnv(appType);
// set prevent close to true, we handle close event manually
WindowController.fromWindowId(windowId!).setPreventClose(true);
late Widget widget; late Widget widget;
switch (appType) { switch (appType) {
case kAppTypeDesktopRemote: case kAppTypeDesktopRemote:

View File

@ -166,6 +166,7 @@ class RustDeskMultiWindowManager {
// no such window already // no such window already
return; return;
} }
await WindowController.fromWindowId(wId).setPreventClose(false);
await WindowController.fromWindowId(wId).close(); await WindowController.fromWindowId(wId).close();
} on Error { } on Error {
return; return;
@ -206,6 +207,12 @@ class RustDeskMultiWindowManager {
} }
/// Remove active window which has [`windowId`]
///
/// [Avaliability]
/// This function should only be called from main window.
/// For other windows, please post a unregister(hide) event to main window handler:
/// `rustDeskWinManager.call(WindowType.Main, kWindowEventHide, {"id": windowId!});`
void unregisterActiveWindow(int windowId) { void unregisterActiveWindow(int windowId) {
if (!_activeWindows.contains(windowId)) { if (!_activeWindows.contains(windowId)) {
// ignore // ignore

View File

@ -64,7 +64,7 @@ dependencies:
desktop_multi_window: desktop_multi_window:
git: git:
url: https://github.com/Kingtous/rustdesk_desktop_multi_window url: https://github.com/Kingtous/rustdesk_desktop_multi_window
ref: 39a8a5b8aed059a89a1694ed2dffe69e31da2ac1 ref: 8d9db30df28eddfdb11ded4aaba6f0fe96547960
freezed_annotation: ^2.0.3 freezed_annotation: ^2.0.3
tray_manager: tray_manager:
git: git: