diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5638aeb --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,153 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "protocol_handler", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler", + "cwd": "packages\\protocol_handler", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler (profile mode)", + "cwd": "packages\\protocol_handler", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler (release mode)", + "cwd": "packages\\protocol_handler", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "protocol_handler_android", + "cwd": "packages\\protocol_handler_android", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler_android (profile mode)", + "cwd": "packages\\protocol_handler_android", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler_android (release mode)", + "cwd": "packages\\protocol_handler_android", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "protocol_handler_ios", + "cwd": "packages\\protocol_handler_ios", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler_ios (profile mode)", + "cwd": "packages\\protocol_handler_ios", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler_ios (release mode)", + "cwd": "packages\\protocol_handler_ios", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "protocol_handler_macos", + "cwd": "packages\\protocol_handler_macos", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler_macos (profile mode)", + "cwd": "packages\\protocol_handler_macos", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler_macos (release mode)", + "cwd": "packages\\protocol_handler_macos", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "protocol_handler_platform_interface", + "cwd": "packages\\protocol_handler_platform_interface", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler_platform_interface (profile mode)", + "cwd": "packages\\protocol_handler_platform_interface", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler_platform_interface (release mode)", + "cwd": "packages\\protocol_handler_platform_interface", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "protocol_handler_windows", + "cwd": "packages\\protocol_handler_windows", + "request": "launch", + "type": "dart" + }, + { + "name": "protocol_handler_windows (profile mode)", + "cwd": "packages\\protocol_handler_windows", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "protocol_handler_windows (release mode)", + "cwd": "packages\\protocol_handler_windows", + "request": "launch", + "type": "dart", + "flutterMode": "release" + }, + { + "name": "example", + "cwd": "packages\\protocol_handler\\example", + "request": "launch", + "type": "dart" + }, + { + "name": "example (profile mode)", + "cwd": "packages\\protocol_handler\\example", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "example (release mode)", + "cwd": "packages\\protocol_handler\\example", + "request": "launch", + "type": "dart", + "flutterMode": "release" + } + ] +} \ No newline at end of file diff --git a/packages/protocol_handler_windows/lib/protocol_handler_windows.dart b/packages/protocol_handler_windows/lib/protocol_handler_windows.dart index a1c6c06..7e265b4 100644 --- a/packages/protocol_handler_windows/lib/protocol_handler_windows.dart +++ b/packages/protocol_handler_windows/lib/protocol_handler_windows.dart @@ -1,3 +1,3 @@ library protocol_handler_windows; -export 'src/protocol_handler_windows.dart'; +export 'src/protocol_handler_windows_impl.dart'; diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows.dart deleted file mode 100644 index 609831c..0000000 --- a/packages/protocol_handler_windows/lib/src/protocol_handler_windows.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'dart:io'; - -import 'package:protocol_handler_platform_interface/protocol_handler_platform_interface.dart'; -import 'package:win32_registry/win32_registry.dart'; - -class ProtocolHandlerWindows extends MethodChannelProtocolHandler { - /// The [ProtocolHandlerWindows] constructor. - ProtocolHandlerWindows() : super(); - - /// Registers this class as the default instance of [ProtocolHandlerWindows]. - static void registerWith() { - ProtocolHandlerPlatform.instance = ProtocolHandlerWindows(); - } - - @override - Future register(String scheme) async { - String appPath = Platform.resolvedExecutable; - - String protocolRegKey = 'Software\\Classes\\$scheme'; - RegistryValue protocolRegValue = const RegistryValue( - 'URL Protocol', - RegistryValueType.string, - '', - ); - String protocolCmdRegKey = 'shell\\open\\command'; - RegistryValue protocolCmdRegValue = RegistryValue( - '', - RegistryValueType.string, - '$appPath "%1"', - ); - - final regKey = Registry.currentUser.createKey(protocolRegKey); - regKey.createValue(protocolRegValue); - regKey.createKey(protocolCmdRegKey).createValue(protocolCmdRegValue); - } -} diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows_base.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_base.dart new file mode 100644 index 0000000..8e87c38 --- /dev/null +++ b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_base.dart @@ -0,0 +1,5 @@ +import 'package:protocol_handler_platform_interface/protocol_handler_platform_interface.dart'; + +abstract class ProtocolHandlerWindowsBase extends MethodChannelProtocolHandler { + Future register(String scheme, {String? friendlyAppName}); +} diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows_impl.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_impl.dart new file mode 100644 index 0000000..614558f --- /dev/null +++ b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_impl.dart @@ -0,0 +1,22 @@ +import 'package:protocol_handler_platform_interface/protocol_handler_platform_interface.dart'; + +import 'protocol_handler_windows_stub.dart' + if (dart.library.io) 'protocol_handler_windows_io.dart' + if (dart.library.html) 'protocol_handler_windows_web.dart'; + +class ProtocolHandlerWindows extends MethodChannelProtocolHandler { + final ProtocolHandlerWindowsImpl _impl; + + ProtocolHandlerWindows() : _impl = ProtocolHandlerWindowsImpl(); + + static void registerWith() { + ProtocolHandlerPlatform.instance = ProtocolHandlerWindowsImpl(); + } + + Future register(String scheme, {String? friendlyAppName}) async { + return await _impl.register( + scheme, + friendlyAppName: friendlyAppName, + ); + } +} diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows_io.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_io.dart new file mode 100644 index 0000000..e3fe408 --- /dev/null +++ b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_io.dart @@ -0,0 +1,44 @@ +import 'dart:io'; + +import 'package:protocol_handler_platform_interface/protocol_handler_platform_interface.dart'; +import 'package:protocol_handler_windows/src/protocol_handler_windows_base.dart'; +import 'package:win32_registry/win32_registry.dart'; + +class ProtocolHandlerWindowsImpl extends ProtocolHandlerWindowsBase { + /// The [ProtocolHandlerWindows] constructor. + ProtocolHandlerWindowsImpl() : super(); + + /// Registers this class as the default instance of [ProtocolHandlerWindowsImpl]. + static void registerWith() { + ProtocolHandlerPlatform.instance = ProtocolHandlerWindowsImpl(); + } + + @override + Future register(String scheme, {String? friendlyAppName}) async { + String appPath = Platform.resolvedExecutable; + + String protocolRegKey = 'Software\\Classes\\$scheme'; + RegistryValue protocolRegValue = const RegistryValue.string( + 'URL Protocol', + '', + ); + String protocolCmdRegKey = 'shell\\open\\command'; + RegistryValue protocolCmdRegValue = RegistryValue.string( + '', + '$appPath "%1"', + ); + + final regKey = Registry.currentUser.createKey(protocolRegKey); + regKey.createValue(protocolRegValue); + regKey.createKey(protocolCmdRegKey).createValue(protocolCmdRegValue); + + if (friendlyAppName != null) { + String applicationRegKey = 'Application'; + RegistryValue friendlyAppNameRegValue = RegistryValue.string( + 'ApplicationName', + friendlyAppName, + ); + regKey.createKey(applicationRegKey).createValue(friendlyAppNameRegValue); + } + } +} diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows_stub.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_stub.dart new file mode 100644 index 0000000..bc11f38 --- /dev/null +++ b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_stub.dart @@ -0,0 +1,12 @@ +import 'package:protocol_handler_windows/src/protocol_handler_windows_base.dart'; + +class ProtocolHandlerWindowsImpl extends ProtocolHandlerWindowsBase { + static void registerWith() { + // Stub implementation does nothing. + } + + @override + Future register(String scheme, {String? friendlyAppName}) async { + // Stub implementation does nothing. + } +} diff --git a/packages/protocol_handler_windows/lib/src/protocol_handler_windows_web.dart b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_web.dart new file mode 100644 index 0000000..5215d61 --- /dev/null +++ b/packages/protocol_handler_windows/lib/src/protocol_handler_windows_web.dart @@ -0,0 +1,13 @@ +import 'package:protocol_handler_windows/src/protocol_handler_windows_base.dart'; + +class ProtocolHandlerWindowsImpl extends ProtocolHandlerWindowsBase { + ProtocolHandlerWindowsImpl() : super(); + + static void registerWith() { + // Web implementation does nothing. + } + + Future register(String scheme, {String? friendlyAppName}) async { + // Web implementation does nothing. + } +} diff --git a/packages/protocol_handler_windows/pubspec.yaml b/packages/protocol_handler_windows/pubspec.yaml index 4d1408f..db1bfc3 100644 --- a/packages/protocol_handler_windows/pubspec.yaml +++ b/packages/protocol_handler_windows/pubspec.yaml @@ -11,7 +11,7 @@ dependencies: flutter: sdk: flutter protocol_handler_platform_interface: ^0.2.0 - win32_registry: ^1.0.2 + win32_registry: ^2.1.0 dev_dependencies: flutter_test: