From 28b75fa9f7370a244cff707fdc0e5785c91a523c Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 9 Aug 2022 09:01:06 +0800 Subject: [PATCH] switch window, close subwindow Signed-off-by: 21pages --- flutter/lib/common.dart | 7 +++++++ flutter/lib/desktop/pages/connection_tab_page.dart | 5 +++++ flutter/lib/desktop/pages/desktop_home_page.dart | 8 ++++++++ flutter/lib/desktop/pages/file_manager_tab_page.dart | 5 +++++ flutter/lib/desktop/widgets/tabbar_widget.dart | 4 +++- flutter/lib/utils/multi_window_manager.dart | 3 +-- 6 files changed, 29 insertions(+), 3 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 861b6b645..ef53b2c41 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:get/instance_manager.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:window_manager/window_manager.dart'; import 'models/model.dart'; import 'models/platform_model.dart'; @@ -109,6 +110,12 @@ backToHome() { Navigator.popUntil(globalKey.currentContext!, ModalRoute.withName("/")); } +void window_on_top() { + windowManager.restore(); + windowManager.show(); + windowManager.focus(); +} + typedef DialogBuilder = CustomAlertDialog Function( StateSetter setState, void Function([dynamic]) close); diff --git a/flutter/lib/desktop/pages/connection_tab_page.dart b/flutter/lib/desktop/pages/connection_tab_page.dart index f98c7d720..92a3938f5 100644 --- a/flutter/lib/desktop/pages/connection_tab_page.dart +++ b/flutter/lib/desktop/pages/connection_tab_page.dart @@ -8,6 +8,7 @@ import 'package:flutter_hbb/desktop/pages/remote_page.dart'; import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart'; import 'package:flutter_hbb/utils/multi_window_manager.dart'; import 'package:get/get.dart'; +import 'package:window_manager/window_manager.dart'; import '../../models/model.dart'; @@ -47,6 +48,7 @@ class _ConnectionTabPageState extends State "call ${call.method} with args ${call.arguments} from window ${fromWindowId}"); // for simplify, just replace connectionId if (call.method == "new_remote_desktop") { + window_on_top(); final args = jsonDecode(call.arguments); final id = args['id']; final indexOf = connectionIds.indexOf(id); @@ -111,5 +113,8 @@ class _ConnectionTabPageState extends State initialIndex = max(0, initialIndex - 1); tabController.value = TabController( length: connectionIds.length, vsync: this, initialIndex: initialIndex); + if (connectionIds.length == 0) { + windowManager.close(); + } } } diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index f02e0fdfd..6854027ee 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -9,6 +9,7 @@ import 'package:flutter_hbb/desktop/pages/connection_page.dart'; import 'package:flutter_hbb/desktop/widgets/titlebar_widget.dart'; import 'package:flutter_hbb/models/platform_model.dart'; import 'package:flutter_hbb/models/server_model.dart'; +import 'package:flutter_hbb/utils/multi_window_manager.dart'; import 'package:get/get.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -447,6 +448,13 @@ class _DesktopHomePageState extends State with TrayListener { void initState() { super.initState(); trayManager.addListener(this); + rustDeskWinManager.setMethodHandler((call, fromWindowId) async { + print( + "call ${call.method} with args ${call.arguments} from window ${fromWindowId}"); + if (call.method == "main_window_on_top") { + window_on_top(); + } + }); } @override diff --git a/flutter/lib/desktop/pages/file_manager_tab_page.dart b/flutter/lib/desktop/pages/file_manager_tab_page.dart index d06ed7444..c4888f37b 100644 --- a/flutter/lib/desktop/pages/file_manager_tab_page.dart +++ b/flutter/lib/desktop/pages/file_manager_tab_page.dart @@ -8,6 +8,7 @@ import 'package:flutter_hbb/desktop/widgets/tabbar_widget.dart'; import 'package:flutter_hbb/models/model.dart'; import 'package:flutter_hbb/utils/multi_window_manager.dart'; import 'package:get/get.dart'; +import 'package:window_manager/window_manager.dart'; /// File Transfer for multi tabs class FileManagerTabPage extends StatefulWidget { @@ -44,6 +45,7 @@ class _FileManagerTabPageState extends State "call ${call.method} with args ${call.arguments} from window ${fromWindowId}"); // for simplify, just replace connectionId if (call.method == "new_file_transfer") { + window_on_top(); final args = jsonDecode(call.arguments); final id = args['id']; final indexOf = connectionIds.indexOf(id); @@ -111,5 +113,8 @@ class _FileManagerTabPageState extends State initialIndex = max(0, initialIndex - 1); tabController.value = TabController( length: connectionIds.length, initialIndex: initialIndex, vsync: this); + if (connectionIds.length == 0) { + windowManager.close(); + } } } diff --git a/flutter/lib/desktop/widgets/tabbar_widget.dart b/flutter/lib/desktop/widgets/tabbar_widget.dart index e57334be3..1f6d1863c 100644 --- a/flutter/lib/desktop/widgets/tabbar_widget.dart +++ b/flutter/lib/desktop/widgets/tabbar_widget.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:flutter/material.dart'; import 'package:flutter_hbb/common.dart'; import 'package:flutter_hbb/consts.dart'; +import 'package:flutter_hbb/utils/multi_window_manager.dart'; import 'package:get/get.dart'; const Color _bgColor = Color.fromARGB(255, 231, 234, 237); @@ -184,7 +185,8 @@ class _AddButton extends StatelessWidget { return _Hoverable( onHover: (hover) => _hover.value = hover, onPressed: (pressed) => _pressed.value = pressed, - onTapUp: () => debugPrint('+'), // TODO + onTapUp: () => + rustDeskWinManager.call(WindowType.Main, "main_window_on_top", ""), child: Obx((() => Container( height: _kTabBarHeight, decoration: ShapeDecoration( diff --git a/flutter/lib/utils/multi_window_manager.dart b/flutter/lib/utils/multi_window_manager.dart index 979ebffd7..bea110dab 100644 --- a/flutter/lib/utils/multi_window_manager.dart +++ b/flutter/lib/utils/multi_window_manager.dart @@ -1,5 +1,4 @@ import 'dart:convert'; -import 'dart:ui'; import 'package:desktop_multi_window/desktop_multi_window.dart'; import 'package:flutter/services.dart'; @@ -97,7 +96,7 @@ class RustDeskMultiWindowManager { int? findWindowByType(WindowType type) { switch (type) { case WindowType.Main: - break; + return 0; case WindowType.RemoteDesktop: return _remoteDesktopWindowId; case WindowType.FileTransfer: