wip: uni links

This commit is contained in:
kingtous 2022-10-18 10:29:33 +08:00 committed by Kingtous
parent e50271cbb6
commit 5fff68011a
6 changed files with 82 additions and 0 deletions

View File

@ -15,6 +15,8 @@ import 'package:flutter_hbb/models/peer_model.dart';
import 'package:flutter_hbb/utils/multi_window_manager.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni_links/uni_links.dart';
import 'package:uni_links_desktop/uni_links_desktop.dart';
import 'package:window_manager/window_manager.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:window_size/window_size.dart' as window_size;
@ -1178,6 +1180,48 @@ Future<bool> restoreWindowPosition(WindowType type, {int? windowId}) async {
return false;
}
/// Initialize uni links for macos/windows
///
/// [Availability]
/// initUniLinks should only be used on macos/windows.
/// we use dbus for linux currently.
Future<void> initUniLinks() async {
if (!Platform.isWindows && !Platform.isMacOS) {
return;
}
if (Platform.isWindows) {
registerProtocol('rustdesk');
}
// check cold boot
try {
final initialLink = await getInitialLink();
// TODO: parse link
print("${initialLink}");
} on PlatformException {
// Handle exception by warning the user their action did not succeed
// return?
}
}
StreamSubscription listenUniLinks() {
if (Platform.isWindows || Platform.isMacOS) {
final sub = uriLinkStream.listen((Uri? uri) {
if (uri != null) {
callUniLinksUriHandler(uri);
} else {
print("uni listen error: uri is empty.");
}
}, onError: (err) {
print("uni links error: $err");
});
return sub;
} else {
// return empty stream subscription for uniform logic
final stream = Stream.empty();
return stream.listen((event) {/*ignore*/});
}
}
void checkArguments() {
// check connect args
final connectIndex = bootArgs.indexOf("--connect");
@ -1208,6 +1252,11 @@ void parseRustdeskUri(String uriPath) {
print("uri is not valid: $uriPath");
return;
}
callUniLinksUriHandler(uri);
}
/// uri handler
void callUniLinksUriHandler(Uri uri) {
// new connection
if (uri.authority == "connection" && uri.path.startsWith("/new/")) {
final peerId = uri.path.substring("/new/".length);

View File

@ -37,6 +37,7 @@ class _DesktopHomePageState extends State<DesktopHomePage>
@override
bool get wantKeepAlive => true;
var updateUrl = '';
StreamSubscription? _uniLinksSubscription;
@override
void onWindowClose() async {
@ -455,12 +456,14 @@ class _DesktopHomePageState extends State<DesktopHomePage>
Future.delayed(Duration.zero, () {
checkArguments();
});
_uniLinksSubscription = listenUniLinks();
}
@override
void dispose() {
trayManager.removeListener(this);
windowManager.removeListener(this);
_uniLinksSubscription?.cancel();
super.dispose();
}
}

View File

@ -1,4 +1,5 @@
import 'dart:convert';
import 'dart:io';
import 'package:desktop_multi_window/desktop_multi_window.dart';
import 'package:flutter/material.dart';
@ -13,6 +14,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni_links_desktop/uni_links_desktop.dart';
import 'package:window_manager/window_manager.dart';
// import 'package:window_manager/window_manager.dart';
@ -89,6 +91,8 @@ Future<void> initEnv(String appType) async {
}
void runMainApp(bool startService) async {
// register uni links
initUniLinks();
await initEnv(kAppTypeMain);
// trigger connection status updater
await bind.mainCheckConnectStatus();

View File

@ -28,5 +28,18 @@
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string></string>
<key>CFBundleURLSchemes</key>
<array>
<string>rustdesk</string>
</array>
</dict>
</array>
</dict>
</plist>

View File

@ -95,6 +95,8 @@ dependencies:
# git:
# url: https://github.com/Kingtous/flutter_improved_scrolling
# ref: 62f09545149f320616467c306c8c5f71714a18e6
uni_links: ^0.5.1
uni_links_desktop: ^0.1.3
dev_dependencies:
icons_launcher: ^2.0.4

View File

@ -7,6 +7,8 @@
#include "utils.h"
// #include <bitsdojo_window_windows/bitsdojo_window_plugin.h>
#include <uni_links_desktop/uni_links_desktop_plugin.h>
typedef char** (*FUNC_RUSTDESK_CORE_MAIN)(int*);
typedef void (*FUNC_RUSTDESK_FREE_ARGS)( char**, int);
@ -14,6 +16,15 @@ typedef void (*FUNC_RUSTDESK_FREE_ARGS)( char**, int);
int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command)
{
// uni links dispatch
HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"rustdesk");
if (hwnd != NULL) {
DispatchToUniLinksDesktop(hwnd);
::ShowWindow(hwnd, SW_NORMAL);
::SetForegroundWindow(hwnd);
return EXIT_FAILURE;
}
HINSTANCE hInstance = LoadLibraryA("librustdesk.dll");
if (!hInstance)
{