Fix windows deep link

This commit is contained in:
problematicconsumer
2024-01-10 17:28:08 +03:30
parent 6dcae6cff1
commit 3673689e32
8 changed files with 67 additions and 31 deletions

View File

@@ -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(
@@ -36,6 +35,4 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("VclibsPlugin"));
WindowManagerPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowManagerPlugin"));
WindowsSingleInstancePluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("WindowsSingleInstancePlugin"));
}

View File

@@ -12,7 +12,6 @@ list(APPEND FLUTTER_PLUGIN_LIST
url_launcher_windows
vclibs
window_manager
windows_single_instance
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST

View File

@@ -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()) {
@@ -49,5 +59,6 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
}
::CoUninitialize();
ReleaseMutex(hMutexInstance);
return EXIT_SUCCESS;
}

View File

@@ -4,6 +4,7 @@
#include <flutter_windows.h>
#include "resource.h"
#include <protocol_handler/protocol_handler_plugin.h>
namespace {
@@ -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 =
@@ -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,

View File

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