From d9287850d6e35a7e3dfe9af5d8ba6366a89e2fe7 Mon Sep 17 00:00:00 2001 From: arce lai Date: Tue, 3 Sep 2024 19:40:47 +0800 Subject: [PATCH] feat(native): signle-window-instance (#330) Co-authored-by: Eduardo M. --- macos/Runner/AppDelegate.swift | 13 +++++++++++++ windows/runner/main.cpp | 6 ++++++ 2 files changed, 19 insertions(+) diff --git a/macos/Runner/AppDelegate.swift b/macos/Runner/AppDelegate.swift index f685a298..d2e77384 100644 --- a/macos/Runner/AppDelegate.swift +++ b/macos/Runner/AppDelegate.swift @@ -4,6 +4,19 @@ import FlutterMacOS @main class AppDelegate: FlutterAppDelegate { override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { + return false + } + + override func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { + if !flag { + for window in NSApp.windows { + if !window.isVisible { + window.setIsVisible(true) + } + window.makeKeyAndOrderFront(self) + NSApp.activate(ignoringOtherApps: true) + } + } return true } diff --git a/windows/runner/main.cpp b/windows/runner/main.cpp index ea8f8431..1d8019a1 100644 --- a/windows/runner/main.cpp +++ b/windows/runner/main.cpp @@ -10,6 +10,12 @@ auto bdw = bitsdojo_window_configure(BDW_CUSTOM_FRAME | BDW_HIDE_ON_STARTUP); 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"Sidekick"); + if (hwnd != NULL) { + ::ShowWindow(hwnd, SW_NORMAL); + ::SetForegroundWindow(hwnd); + return EXIT_FAILURE; + } // 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())