From 790228a8a02cce8069c71f982cd0a711dee92eb2 Mon Sep 17 00:00:00 2001 From: Hitesh Kumar Saini Date: Wed, 25 May 2022 17:06:42 +0530 Subject: [PATCH] fix: flutter_native_view window showing before window upon restore from taskbar icon --- core/native_view_core.cc | 29 ++++++++++++++++------------- core/native_view_core.h | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/core/native_view_core.cc b/core/native_view_core.cc index 65faf5d..26509bd 100644 --- a/core/native_view_core.cc +++ b/core/native_view_core.cc @@ -100,17 +100,6 @@ std::optional NativeViewCore::WindowProc(HWND hwnd, UINT message, break; } case WM_SIZE: { - // Keeping the |native_view_container_| hidden when minimizing the app & - // showing it again only when the app is restored. - // ---- INTENTIONALLY COMMENTED OUT ---- - // if (last_wm_size_wparam_ == SIZE_MINIMIZED) { - // std::thread([=]() { - // std::this_thread::sleep_for( - // std::chrono::milliseconds(kNativeViewPositionAndShowDelay)); - // ::ShowWindow(native_view_container_, SW_SHOWNOACTIVATE); - // }).detach(); - // } - // Handle Windows's minimize & maximize animations properly. // Since |SetWindowPos| & other Win32 APIs on |native_view_container_| // do not re-produce the same DWM animations like actual user @@ -126,14 +115,18 @@ std::optional NativeViewCore::WindowProc(HWND hwnd, UINT message, // equivalent window screenshot will result in a totally seamless // experience. if (wparam != SIZE_RESTORED || last_wm_size_wparam_ == SIZE_MINIMIZED || - last_wm_size_wparam_ == SIZE_MAXIMIZED) { + last_wm_size_wparam_ == SIZE_MAXIMIZED || + was_window_hidden_due_to_minimize_) { + was_window_hidden_due_to_minimize_ = false; + // Minimize condition is handled separately inside |WM_WINDOWPOSCHANGED| + // case, since we don't want to cause unnecessary redraws (& show/hide) + // when user is resizing the window by dragging the window border. SetWindowComposition(window_, 0, 0); ::ShowWindow(native_view_container_, SW_HIDE); last_thread_time_ = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()) .count(); - // A possibly-failed attempt to avoid race-condition & jitter. std::thread( [=](uint64_t time) { if (time < last_thread_time_) { @@ -164,6 +157,16 @@ std::optional NativeViewCore::WindowProc(HWND hwnd, UINT message, ::SetWindowPos(native_view_container_, window_, window_rect.left, window_rect.top, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE); + // |window_| is minimized. + if (window_rect.left < 0 && window_rect.top < 0 && + window_rect.right < 0 && window_rect.bottom < 0) { + // Hide |native_view_container_| to prevent showing + // |native_view_container_| before |window_| placement + // i.e when restoring window after clicking the taskbar icon. + SetWindowComposition(window_, 0, 0); + ::ShowWindow(native_view_container_, SW_HIDE); + was_window_hidden_due_to_minimize_ = true; + } } break; } diff --git a/core/native_view_core.h b/core/native_view_core.h index 0dfd67e..a7cfcde 100644 --- a/core/native_view_core.h +++ b/core/native_view_core.h @@ -65,6 +65,7 @@ class NativeViewCore { std::map native_views_ = {}; uint64_t last_thread_time_ = 0; WPARAM last_wm_size_wparam_ = SIZE_RESTORED; + bool was_window_hidden_due_to_minimize_ = false; static std::unique_ptr instance_; static std::optional proc_id_; };