From c256a43139e6bb32b33f2115d5e2ef2c69671efb Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 22 Feb 2018 14:59:39 +0900 Subject: [PATCH] Move ready-to-show to api::BrowserWindow --- atom/browser/api/atom_api_browser_window.cc | 25 ++++++++++++++++----- atom/browser/api/atom_api_browser_window.h | 12 +++++++++- atom/browser/native_window.cc | 21 ----------------- atom/browser/native_window.h | 4 ---- atom/browser/native_window_observer.h | 3 --- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/atom/browser/api/atom_api_browser_window.cc b/atom/browser/api/atom_api_browser_window.cc index c9efa64487..fd980a57c2 100644 --- a/atom/browser/api/atom_api_browser_window.cc +++ b/atom/browser/api/atom_api_browser_window.cc @@ -77,7 +77,8 @@ v8::Local ToBuffer(v8::Isolate* isolate, void* val, int size) { BrowserWindow::BrowserWindow(v8::Isolate* isolate, v8::Local wrapper, - const mate::Dictionary& options) { + const mate::Dictionary& options) + : weak_factory_(this) { mate::Handle web_contents; // Use options.webPreferences in WebContents. @@ -176,6 +177,24 @@ BrowserWindow::~BrowserWindow() { base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, window_.release()); } +void BrowserWindow::DidFirstVisuallyNonEmptyPaint() { + if (window_->IsVisible()) + return; + + // When there is a non-empty first paint, resize the RenderWidget to force + // Chromium to draw. + const auto view = web_contents()->GetRenderWidgetHostView(); + view->Show(); + view->SetSize(window_->GetContentSize()); + + // Emit the ReadyToShow event in next tick in case of pending drawing work. + base::ThreadTaskRunnerHandle::Get()->PostTask( + FROM_HERE, + base::Bind([](base::WeakPtr self) { + self->Emit("ready-to-show"); + }, GetWeakPtr())); +} + void BrowserWindow::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } @@ -232,10 +251,6 @@ void BrowserWindow::OnWindowHide() { Emit("hide"); } -void BrowserWindow::OnReadyToShow() { - Emit("ready-to-show"); -} - void BrowserWindow::OnWindowMaximize() { Emit("maximize"); } diff --git a/atom/browser/api/atom_api_browser_window.h b/atom/browser/api/atom_api_browser_window.h index c1a91d51e8..e0d0569ae3 100644 --- a/atom/browser/api/atom_api_browser_window.h +++ b/atom/browser/api/atom_api_browser_window.h @@ -15,6 +15,7 @@ #include "atom/browser/native_window_observer.h" #include "atom/common/api/atom_api_native_image.h" #include "atom/common/key_weak_map.h" +#include "base/memory/weak_ptr.h" #include "content/public/browser/web_contents_observer.h" #include "native_mate/handle.h" #include "native_mate/persistent_dictionary.h" @@ -60,6 +61,9 @@ class BrowserWindow : public mate::TrackableObject, const mate::Dictionary& options); ~BrowserWindow() override; + // content::WebContentsObserver: + void DidFirstVisuallyNonEmptyPaint() override; + // NativeWindowObserver: void WillCloseWindow(bool* prevent_default) override; void WillDestroyNativeObject() override; @@ -69,7 +73,6 @@ class BrowserWindow : public mate::TrackableObject, void OnWindowFocus() override; void OnWindowShow() override; void OnWindowHide() override; - void OnReadyToShow() override; void OnWindowMaximize() override; void OnWindowUnmaximize() override; void OnWindowMinimize() override; @@ -98,11 +101,16 @@ class BrowserWindow : public mate::TrackableObject, void OnWindowMessage(UINT message, WPARAM w_param, LPARAM l_param) override; #endif + base::WeakPtr GetWeakPtr() { + return weak_factory_.GetWeakPtr(); + } + private: void Init(v8::Isolate* isolate, v8::Local wrapper, const mate::Dictionary& options, mate::Handle web_contents); + // APIs for NativeWindow. void Close(); void Focus(); @@ -250,6 +258,8 @@ class BrowserWindow : public mate::TrackableObject, std::unique_ptr window_; + base::WeakPtrFactory weak_factory_; + DISALLOW_COPY_AND_ASSIGN(BrowserWindow); }; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index a34c4648da..3e488d9ecd 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -672,22 +672,6 @@ void NativeWindow::BeforeUnloadDialogCancelled() { window_unresposive_closure_.Cancel(); } -void NativeWindow::DidFirstVisuallyNonEmptyPaint() { - if (IsVisible()) - return; - - // When there is a non-empty first paint, resize the RenderWidget to force - // Chromium to draw. - const auto view = web_contents()->GetRenderWidgetHostView(); - view->Show(); - view->SetSize(GetContentSize()); - - // Emit the ReadyToShow event in next tick in case of pending drawing work. - base::ThreadTaskRunnerHandle::Get()->PostTask( - FROM_HERE, - base::Bind(&NativeWindow::NotifyReadyToShow, GetWeakPtr())); -} - bool NativeWindow::OnMessageReceived(const IPC::Message& message, content::RenderFrameHost* rfh) { bool handled = true; @@ -731,9 +715,4 @@ void NativeWindow::NotifyWindowUnresponsive() { } } -void NativeWindow::NotifyReadyToShow() { - for (NativeWindowObserver& observer : observers_) - observer.OnReadyToShow(); -} - } // namespace atom diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index d1cfa7cc35..9c1ccb2f42 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -320,7 +320,6 @@ class NativeWindow : public base::SupportsUserData, // content::WebContentsObserver: void RenderViewCreated(content::RenderViewHost* render_view_host) override; void BeforeUnloadDialogCancelled() override; - void DidFirstVisuallyNonEmptyPaint() override; bool OnMessageReceived(const IPC::Message& message, content::RenderFrameHost* rfh) override; @@ -331,9 +330,6 @@ class NativeWindow : public base::SupportsUserData, // Dispatch unresponsive event to observers. void NotifyWindowUnresponsive(); - // Dispatch ReadyToShow event to observers. - void NotifyReadyToShow(); - // Whether window has standard frame. bool has_frame_; diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index f21fbcc570..43eef69ed8 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -55,9 +55,6 @@ class NativeWindowObserver { // Called when window is hidden. virtual void OnWindowHide() {} - // Called when window is ready to show. - virtual void OnReadyToShow() {} - // Called when window state changed. virtual void OnWindowMaximize() {} virtual void OnWindowUnmaximize() {}