diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index c0c63673a32b..3519bff93f5b 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -398,6 +398,11 @@ It creates a new `BrowserWindow` with native properties as set by the `options`. * `bypassHeatCheck` - Bypass code caching heuristics but with lazy compilation * `bypassHeatCheckAndEagerCompile` - Same as above except compilation is eager. Default policy is `code`. + * `enablePreferredSizeMode` Boolean (optional) - Whether to enable + preferred size mode. The preferred size is the minimum size needed to + contain the layout of the document—without requiring scrolling. Enabling + this will cause the `preferred-size-changed` event to be emitted on the + `WebContents` when the preferred size changes. Default is `false`. When setting minimum or maximum window size with `minWidth`/`maxWidth`/ `minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 5d12a9f04952..28202256c953 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -837,6 +837,19 @@ Emitted when `remote.getCurrentWebContents()` is called in the renderer process. Calling `event.preventDefault()` will prevent the object from being returned. Custom value can be returned by setting `event.returnValue`. +#### Event: 'preferred-size-changed' + +Returns: + +* `event` Event +* `preferredSize` [Size](structures/size.md) - The minimum size needed to + contain the layout of the document—without requiring scrolling. + +Emitted when the `WebContents` preferred size has changed. + +This event will only be emitted when `enablePreferredSizeMode` is set to `true` +in `webPreferences`. + ### Instance Methods #### `contents.loadURL(url[, options])` diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index a3cb00fb80c2..7f086832fd03 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -1173,6 +1173,11 @@ void WebContents::DidStartLoading() { void WebContents::DidStopLoading() { Emit("did-stop-loading"); + + auto* web_preferences = WebContentsPreferences::From(web_contents()); + if (web_preferences && + web_preferences->IsEnabled(options::kEnablePreferredSizeMode)) + web_contents()->GetRenderViewHost()->EnablePreferredSizeMode(); } bool WebContents::EmitNavigationEvent( @@ -2903,6 +2908,11 @@ v8::Local WebContents::TakeHeapSnapshot( return handle; } +void WebContents::UpdatePreferredSize(content::WebContents* web_contents, + const gfx::Size& pref_size) { + Emit("preferred-size-changed", pref_size); +} + // static v8::Local WebContents::FillObjectTemplate( v8::Isolate* isolate, diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index d053803e4f7d..11e8a85921ec 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -551,6 +551,8 @@ class WebContents : public gin::Wrappable, content::JavaScriptDialogManager* GetJavaScriptDialogManager( content::WebContents* source) override; void OnAudioStateChanged(bool audible) override; + void UpdatePreferredSize(content::WebContents* web_contents, + const gfx::Size& pref_size) override; // content::WebContentsObserver: void BeforeUnloadFired(bool proceed, diff --git a/shell/browser/web_contents_preferences.cc b/shell/browser/web_contents_preferences.cc index f25b06a301cb..ba0a27cdbb5c 100644 --- a/shell/browser/web_contents_preferences.cc +++ b/shell/browser/web_contents_preferences.cc @@ -143,6 +143,7 @@ WebContentsPreferences::WebContentsPreferences( SetDefaultBoolIfUndefined(options::kTextAreasAreResizable, true); SetDefaultBoolIfUndefined(options::kWebGL, true); SetDefaultBoolIfUndefined(options::kEnableWebSQL, true); + SetDefaultBoolIfUndefined(options::kEnablePreferredSizeMode, false); bool webSecurity = true; SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity); // If webSecurity was explicitly set to false, let's inherit that into diff --git a/shell/common/options_switches.cc b/shell/common/options_switches.cc index 723cb353cee7..510896db52ac 100644 --- a/shell/common/options_switches.cc +++ b/shell/common/options_switches.cc @@ -191,6 +191,8 @@ const char kEnableRemoteModule[] = "enableRemoteModule"; const char kEnableWebSQL[] = "enableWebSQL"; +const char kEnablePreferredSizeMode[] = "enablePreferredSizeMode"; + } // namespace options namespace switches { diff --git a/shell/common/options_switches.h b/shell/common/options_switches.h index 01b44e242ebb..0dd8fc8351ef 100644 --- a/shell/common/options_switches.h +++ b/shell/common/options_switches.h @@ -87,6 +87,7 @@ extern const char kTextAreasAreResizable[]; extern const char kWebGL[]; extern const char kNavigateOnDragDrop[]; extern const char kEnableWebSQL[]; +extern const char kEnablePreferredSizeMode[]; #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) extern const char kSpellcheck[];