diff --git a/atom/browser/api/atom_api_browser_window.cc b/atom/browser/api/atom_api_browser_window.cc index def0640862..04b3e4f1c3 100644 --- a/atom/browser/api/atom_api_browser_window.cc +++ b/atom/browser/api/atom_api_browser_window.cc @@ -307,6 +307,10 @@ void BrowserWindow::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } +void BrowserWindow::RequestPreferredWidth(int* width) { + *width = web_contents()->GetPreferredSize().width(); +} + void BrowserWindow::OnCloseButtonClicked(bool* prevent_default) { // When user tries to close the window by clicking the close button, we do // not close the window immediately, instead we try to close the web page diff --git a/atom/browser/api/atom_api_browser_window.h b/atom/browser/api/atom_api_browser_window.h index 768d975e46..8d0a31b6ee 100644 --- a/atom/browser/api/atom_api_browser_window.h +++ b/atom/browser/api/atom_api_browser_window.h @@ -81,6 +81,7 @@ class BrowserWindow : public mate::TrackableObject, // NativeWindowObserver: void WillCloseWindow(bool* prevent_default) override; + void RequestPreferredWidth(int* width) override; void OnCloseButtonClicked(bool* prevent_default) override; void OnWindowClosed() override; void OnWindowEndSession() override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 1222151248..0dbb3eb0ed 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -402,6 +402,11 @@ void NativeWindow::PreviewFile(const std::string& path, void NativeWindow::CloseFilePreview() { } +void NativeWindow::NotifyWindowRequestPreferredWith(int* width) { + for (NativeWindowObserver& observer : observers_) + observer.RequestPreferredWidth(width); +} + void NativeWindow::NotifyWindowCloseButtonClicked() { // First ask the observers whether we want to close. bool prevent_default = false; diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 7f12f5a23b..3badaf641b 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -243,6 +243,7 @@ class NativeWindow : public base::SupportsUserData, // Public API used by platform-dependent delegates and observers to send UI // related notifications. + void NotifyWindowRequestPreferredWith(int* width); void NotifyWindowCloseButtonClicked(); void NotifyWindowClosed(); void NotifyWindowEndSession(); diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index f4ceaafd12..b71b3f23d5 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -218,16 +218,16 @@ bool ScopedDisableResize::disable_resize_ = false; if ([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) return frame; - content::WebContents* web_contents = shell_->web_contents(); - if (!web_contents) + // Get preferred width from observers. Usually the page width. + int preferred_width = 0; + shell_->NotifyWindowRequestPreferredWith(&preferred_width); + if (preferred_width <= 0) return frame; - CGFloat page_width = static_cast( - web_contents->GetPreferredSize().width()); - NSRect window_frame = [window frame]; - // Never shrink from the current size on zoom. - CGFloat zoomed_width = std::max(page_width, NSWidth(window_frame)); + NSRect window_frame = [window frame]; + CGFloat zoomed_width = std::max(static_cast(preferred_width), + NSWidth(window_frame)); // |frame| determines our maximum extents. We need to set the origin of the // frame -- and only move it left if necessary. diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index 27f5cb92f8..4dfd352109 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -34,6 +34,9 @@ class NativeWindowObserver { // Called when the window is gonna closed. virtual void WillCloseWindow(bool* prevent_default) {} + // Called when the window wants to know the preferred width. + virtual void RequestPreferredWidth(int* width) {} + // Called when closed button is clicked. virtual void OnCloseButtonClicked(bool* prevent_default) {}