From f343478016514481df79c6859688c69b15365877 Mon Sep 17 00:00:00 2001 From: Kingtous Date: Sat, 26 Nov 2022 11:40:13 +0800 Subject: [PATCH] opt: hide main window when using shortcut --- flutter/lib/common.dart | 34 +++++++++++++------ flutter/lib/consts.dart | 1 + .../lib/desktop/pages/desktop_home_page.dart | 15 +++++--- flutter/lib/main.dart | 11 ++++-- flutter/pubspec.lock | 18 +++++----- 5 files changed, 52 insertions(+), 27 deletions(-) diff --git a/flutter/lib/common.dart b/flutter/lib/common.dart index 483aba384..fed58e999 100644 --- a/flutter/lib/common.dart +++ b/flutter/lib/common.dart @@ -6,6 +6,7 @@ import 'dart:typed_data'; import 'package:back_button_interceptor/back_button_interceptor.dart'; import 'package:desktop_multi_window/desktop_multi_window.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -321,7 +322,7 @@ void window_on_top(int? id) { windowManager.restore(); windowManager.show(); windowManager.focus(); - rustDeskWinManager.registerActiveWindow(0); + rustDeskWinManager.registerActiveWindow(kWindowMainId); } else { WindowController.fromWindowId(id) ..focus() @@ -1227,41 +1228,50 @@ StreamSubscription? listenUniLinks() { return sub; } -void checkArguments() { +/// Returns true if we successfully handle the startup arguments. +bool checkArguments() { // check connect args final connectIndex = bootArgs.indexOf("--connect"); if (connectIndex == -1) { - return; + return false; } String? arg = bootArgs.length < connectIndex + 1 ? null : bootArgs[connectIndex + 1]; if (arg != null) { if (arg.startsWith(kUniLinksPrefix)) { - parseRustdeskUri(arg); + return parseRustdeskUri(arg); } else { + // remove "--connect xxx" in the `bootArgs` array + bootArgs.removeAt(connectIndex); + bootArgs.removeAt(connectIndex); // fallback to peer id - rustDeskWinManager.newRemoteDesktop(arg); - bootArgs.removeAt(connectIndex); - bootArgs.removeAt(connectIndex); + Future.delayed(Duration.zero, () { + rustDeskWinManager.newRemoteDesktop(arg); + }); + return true; } } + return false; } /// Parse `rustdesk://` unilinks /// +/// Returns true if we successfully handle the uri provided. /// [Functions] /// 1. New Connection: rustdesk://connection/new/your_peer_id -void parseRustdeskUri(String uriPath) { +bool parseRustdeskUri(String uriPath) { final uri = Uri.tryParse(uriPath); if (uri == null) { print("uri is not valid: $uriPath"); - return; + return false; } - callUniLinksUriHandler(uri); + return callUniLinksUriHandler(uri); } /// uri handler -void callUniLinksUriHandler(Uri uri) { +/// +/// Returns true if we successfully handle the uri provided. +bool callUniLinksUriHandler(Uri uri) { debugPrint("uni links called: $uri"); // new connection if (uri.authority == "connection" && uri.path.startsWith("/new/")) { @@ -1269,7 +1279,9 @@ void callUniLinksUriHandler(Uri uri) { Future.delayed(Duration.zero, () { rustDeskWinManager.newRemoteDesktop(peerId); }); + return true; } + return false; } /// Connect to a peer with [id]. diff --git a/flutter/lib/consts.dart b/flutter/lib/consts.dart index 4d3cd8cd9..ae0d334e2 100644 --- a/flutter/lib/consts.dart +++ b/flutter/lib/consts.dart @@ -20,6 +20,7 @@ const String kTabLabelHomePage = "Home"; const String kTabLabelSettingPage = "Settings"; const String kWindowPrefix = "wm_"; +const int kWindowMainId = 0; // the executable name of the portable version const String kEnvPortableExecutable = "RUSTDESK_APPNAME"; diff --git a/flutter/lib/desktop/pages/desktop_home_page.dart b/flutter/lib/desktop/pages/desktop_home_page.dart index f7b07cf3a..4be64eee0 100644 --- a/flutter/lib/desktop/pages/desktop_home_page.dart +++ b/flutter/lib/desktop/pages/desktop_home_page.dart @@ -420,7 +420,17 @@ class _DesktopHomePageState extends State // initTray(); trayManager.addListener(this); rustDeskWinManager.registerActiveWindowListener(onActiveWindowChanged); - rustDeskWinManager.registerActiveWindow(0); + // main window may be hidden because of the initial uni link or arguments. + // note that we must wrap this active window registration in future because + // we must ensure the execution is after `windowManager.hide/show()`. + Future.delayed(Duration.zero, () { + windowManager.isVisible().then((visibility) { + if (visibility) { + rustDeskWinManager.registerActiveWindow(kWindowMainId); + } + }); + }); + rustDeskWinManager.setMethodHandler((call, fromWindowId) async { debugPrint( "[Main] call ${call.method} with args ${call.arguments} from window $fromWindowId"); @@ -455,9 +465,6 @@ class _DesktopHomePageState extends State rustDeskWinManager.unregisterActiveWindow(call.arguments["id"]); } }); - Future.delayed(Duration.zero, () { - checkArguments(); - }); _uniLinksSubscription = listenUniLinks(); } diff --git a/flutter/lib/main.dart b/flutter/lib/main.dart index 44db1436e..8af2a477f 100644 --- a/flutter/lib/main.dart +++ b/flutter/lib/main.dart @@ -118,13 +118,18 @@ void runMainApp(bool startService) async { gFFI.serverModel.startService(); } runApp(App()); + // check the startup argument, if we successfully handle the argument, we keep the main window hidden. + if (checkArguments()) { + windowManager.hide(); + } else { + windowManager.show(); + windowManager.focus(); + } // set window option WindowOptions windowOptions = getHiddenTitleBarWindowOptions(); windowManager.waitUntilReadyToShow(windowOptions, () async { restoreWindowPosition(WindowType.Main); - await windowManager.show(); - await windowManager.focus(); - await windowManager.setOpacity(1); + windowManager.setOpacity(1); }); } diff --git a/flutter/pubspec.lock b/flutter/pubspec.lock index 738567b22..29726a365 100644 --- a/flutter/pubspec.lock +++ b/flutter/pubspec.lock @@ -35,7 +35,7 @@ packages: name: archive url: "https://pub.flutter-io.cn" source: hosted - version: "3.3.4" + version: "3.3.5" args: dependency: transitive description: @@ -257,8 +257,8 @@ packages: dependency: "direct main" description: path: "." - ref: "8ee8eb59cabf6ac83a13fe002de7d4a231263a58" - resolved-ref: "8ee8eb59cabf6ac83a13fe002de7d4a231263a58" + ref: cb086219bd4760a95a483cb14c1791d2a39ca5a0 + resolved-ref: cb086219bd4760a95a483cb14c1791d2a39ca5a0 url: "https://github.com/Kingtous/rustdesk_desktop_multi_window" source: git version: "0.1.0" @@ -462,7 +462,7 @@ packages: name: frontend_server_client url: "https://pub.flutter-io.cn" source: hosted - version: "3.1.0" + version: "3.2.0" get: dependency: "direct main" description: @@ -735,7 +735,7 @@ packages: name: path_provider_android url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.21" + version: "2.0.22" path_provider_ios: dependency: transitive description: @@ -1054,14 +1054,14 @@ packages: name: url_launcher url: "https://pub.flutter-io.cn" source: hosted - version: "6.1.6" + version: "6.1.7" url_launcher_android: dependency: transitive description: name: url_launcher_android url: "https://pub.flutter-io.cn" source: hosted - version: "6.0.21" + version: "6.0.22" url_launcher_ios: dependency: transitive description: @@ -1124,7 +1124,7 @@ packages: name: video_player url: "https://pub.flutter-io.cn" source: hosted - version: "2.4.7" + version: "2.4.8" video_player_android: dependency: transitive description: @@ -1215,7 +1215,7 @@ packages: name: win32 url: "https://pub.flutter-io.cn" source: hosted - version: "3.1.1" + version: "3.1.2" win32_registry: dependency: transitive description: