opt: hide main window when using shortcut

This commit is contained in:
Kingtous
2022-11-26 11:40:13 +08:00
parent 03c1395565
commit f343478016
5 changed files with 52 additions and 27 deletions

View File

@@ -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].

View File

@@ -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";

View File

@@ -420,7 +420,17 @@ class _DesktopHomePageState extends State<DesktopHomePage>
// 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<DesktopHomePage>
rustDeskWinManager.unregisterActiveWindow(call.arguments["id"]);
}
});
Future.delayed(Duration.zero, () {
checkArguments();
});
_uniLinksSubscription = listenUniLinks();
}

View File

@@ -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);
});
}