Skip to content

Commit

Permalink
Fix windows deep link
Browse files Browse the repository at this point in the history
  • Loading branch information
0xConsumer committed Jan 10, 2024
1 parent 6dcae6c commit 3673689
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 31 deletions.
16 changes: 4 additions & 12 deletions lib/features/window/notifier/window_notifier.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import 'dart:io';
import 'dart:ui';

import 'package:hiddify/core/app_info/app_info_provider.dart';
import 'package:hiddify/core/model/constants.dart';
import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
import 'package:hiddify/utils/utils.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:tray_manager/tray_manager.dart';
import 'package:window_manager/window_manager.dart';
import 'package:windows_single_instance/windows_single_instance.dart';

part 'window_notifier.g.dart';

Expand All @@ -21,18 +17,14 @@ class WindowNotifier extends _$WindowNotifier with AppLogger {
Future<void> build() async {
if (!PlatformUtils.isDesktop) return;

if (Platform.isWindows) {
loggy.debug("ensuring single instance");
await WindowsSingleInstance.ensureSingleInstance([], "app.hiddify.com");
}
// if (Platform.isWindows) {
// loggy.debug("ensuring single instance");
// await WindowsSingleInstance.ensureSingleInstance([], "HiddifyNext");
// }

await windowManager.ensureInitialized();
await windowManager.setMinimumSize(minimumWindowSize);
await windowManager.setSize(defaultWindowSize);

final appInfo = await ref.watch(appInfoProvider.future);
await windowManager
.setTitle("${Constants.appName} v${appInfo.presentVersion}");
}

Future<void> open({bool focus = true}) async {
Expand Down
8 changes: 0 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1674,14 +1674,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.7"
windows_single_instance:
dependency: "direct main"
description:
name: windows_single_instance
sha256: "50d5dcd6bec90b4a5ed588b1822b1aad21b39fc96da843e61c734b3caccfd2fc"
url: "https://pub.dev"
source: hosted
version: "1.0.1"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ dependencies:
win32: ^5.2.0
qr_flutter: ^4.1.0
flutter_displaymode: ^0.6.0
windows_single_instance: ^1.0.1
flutter_loggy_dio: ^3.0.1
dio_smart_retry: ^6.0.0
cupertino_http: ^1.2.0
Expand Down
3 changes: 0 additions & 3 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <url_launcher_windows/url_launcher_windows.h>
#include <vclibs/vclibs_plugin.h>
#include <window_manager/window_manager_plugin.h>
#include <windows_single_instance/windows_single_instance_plugin.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
ProtocolHandlerPluginRegisterWithRegistrar(
Expand All @@ -36,6 +35,4 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("VclibsPlugin"));
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
WindowsSingleInstancePluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowsSingleInstancePlugin"));
}
1 change: 0 additions & 1 deletion windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
url_launcher_windows
vclibs
window_manager
windows_single_instance
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down
23 changes: 17 additions & 6 deletions windows/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@

int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
_In_ wchar_t *command_line, _In_ int show_command) {
HWND hwnd = ::FindWindow(L"FLUTTER_RUNNER_WIN32_WINDOW", L"Hiddify Next");
if (hwnd != NULL) {
DispatchToProtocolHandler(hwnd);
HANDLE hMutexInstance = CreateMutex(NULL, TRUE, L"HiddifyMutex");
HWND handle = FindWindowA(NULL, "Hiddify Next");

::ShowWindow(hwnd, SW_NORMAL);
::SetForegroundWindow(hwnd);
return EXIT_FAILURE;
if (GetLastError() == ERROR_ALREADY_EXISTS) {
flutter::DartProject project(L"data");
std::vector<std::string> command_line_arguments = GetCommandLineArguments();
project.set_dart_entrypoint_arguments(std::move(command_line_arguments));
FlutterWindow window(project);
if (window.SendAppLinkToInstance(L"Hiddify Next")) {
return false;
}

WINDOWPLACEMENT place = {sizeof(WINDOWPLACEMENT)};
GetWindowPlacement(handle, &place);
ShowWindow(handle, SW_NORMAL);
return 0;
}

// Attach to console when present (e.g., 'flutter run') or create a
// new console when running with a debugger.
if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) {
Expand Down Expand Up @@ -49,5 +59,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
}

::CoUninitialize();
ReleaseMutex(hMutexInstance);
return EXIT_SUCCESS;
}
44 changes: 44 additions & 0 deletions windows/runner/win32_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <flutter_windows.h>

#include "resource.h"
#include <protocol_handler/protocol_handler_plugin.h>

namespace {

Expand Down Expand Up @@ -123,6 +124,11 @@ Win32Window::~Win32Window() {
bool Win32Window::Create(const std::wstring& title,
const Point& origin,
const Size& size) {
if (SendAppLinkToInstance(title))
{
return false;
}

Destroy();

const wchar_t* window_class =
Expand Down Expand Up @@ -155,6 +161,44 @@ bool Win32Window::Show() {
return ShowWindow(window_handle_, SW_SHOWNORMAL);
}


bool Win32Window::SendAppLinkToInstance(const std::wstring &title)
{
// Find our exact window
HWND hwnd = ::FindWindow(kWindowClassName, title.c_str());

if (hwnd)
{
// Dispatch new link to current window
DispatchToProtocolHandler(hwnd);

// (Optional) Restore our window to front in same state
WINDOWPLACEMENT place = {sizeof(WINDOWPLACEMENT)};
GetWindowPlacement(hwnd, &place);

switch (place.showCmd)
{
case SW_SHOWMAXIMIZED:
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
break;
case SW_SHOWMINIMIZED:
ShowWindow(hwnd, SW_RESTORE);
break;
default:
ShowWindow(hwnd, SW_NORMAL);
break;
}

SetWindowPos(0, HWND_TOP, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE);
SetForegroundWindow(hwnd);

// Window has been found, don't create another one.
return true;
}

return false;
}

// static
LRESULT CALLBACK Win32Window::WndProc(HWND const window,
UINT const message,
Expand Down
2 changes: 2 additions & 0 deletions windows/runner/win32_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class Win32Window {
// |Show| is called. Returns true if the window was created successfully.
bool Create(const std::wstring& title, const Point& origin, const Size& size);

bool SendAppLinkToInstance(const std::wstring &title);

// Show the current window. Returns true if the window was successfully shown.
bool Show();

Expand Down

0 comments on commit 3673689

Please sign in to comment.