feat: add webPreferences.enablePreferredSizeMode (#25874)

* feat: add preferredSizeMode preference

* docs: webPreferences.preferredSizeMode and event

* docs: better explain preferred size

* docs: small improvement

* refactor: preferredSizeMode -> enablePreferredSizeMode
This commit is contained in:
Samuel Maddock 2020-10-21 18:44:19 -04:00 committed by GitHub
parent 2aa5a1f494
commit 10a209ecba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 0 deletions

View file

@ -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 * `bypassHeatCheck` - Bypass code caching heuristics but with lazy compilation
* `bypassHeatCheckAndEagerCompile` - Same as above except compilation is eager. * `bypassHeatCheckAndEagerCompile` - Same as above except compilation is eager.
Default policy is `code`. 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`/ When setting minimum or maximum window size with `minWidth`/`maxWidth`/
`minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from `minHeight`/`maxHeight`, it only constrains the users. It won't prevent you from

View file

@ -837,6 +837,19 @@ Emitted when `remote.getCurrentWebContents()` is called in the renderer process.
Calling `event.preventDefault()` will prevent the object from being returned. Calling `event.preventDefault()` will prevent the object from being returned.
Custom value can be returned by setting `event.returnValue`. 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 ### Instance Methods
#### `contents.loadURL(url[, options])` #### `contents.loadURL(url[, options])`

View file

@ -1173,6 +1173,11 @@ void WebContents::DidStartLoading() {
void WebContents::DidStopLoading() { void WebContents::DidStopLoading() {
Emit("did-stop-loading"); 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( bool WebContents::EmitNavigationEvent(
@ -2903,6 +2908,11 @@ v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
return handle; return handle;
} }
void WebContents::UpdatePreferredSize(content::WebContents* web_contents,
const gfx::Size& pref_size) {
Emit("preferred-size-changed", pref_size);
}
// static // static
v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate( v8::Local<v8::ObjectTemplate> WebContents::FillObjectTemplate(
v8::Isolate* isolate, v8::Isolate* isolate,

View file

@ -551,6 +551,8 @@ class WebContents : public gin::Wrappable<WebContents>,
content::JavaScriptDialogManager* GetJavaScriptDialogManager( content::JavaScriptDialogManager* GetJavaScriptDialogManager(
content::WebContents* source) override; content::WebContents* source) override;
void OnAudioStateChanged(bool audible) override; void OnAudioStateChanged(bool audible) override;
void UpdatePreferredSize(content::WebContents* web_contents,
const gfx::Size& pref_size) override;
// content::WebContentsObserver: // content::WebContentsObserver:
void BeforeUnloadFired(bool proceed, void BeforeUnloadFired(bool proceed,

View file

@ -143,6 +143,7 @@ WebContentsPreferences::WebContentsPreferences(
SetDefaultBoolIfUndefined(options::kTextAreasAreResizable, true); SetDefaultBoolIfUndefined(options::kTextAreasAreResizable, true);
SetDefaultBoolIfUndefined(options::kWebGL, true); SetDefaultBoolIfUndefined(options::kWebGL, true);
SetDefaultBoolIfUndefined(options::kEnableWebSQL, true); SetDefaultBoolIfUndefined(options::kEnableWebSQL, true);
SetDefaultBoolIfUndefined(options::kEnablePreferredSizeMode, false);
bool webSecurity = true; bool webSecurity = true;
SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity); SetDefaultBoolIfUndefined(options::kWebSecurity, webSecurity);
// If webSecurity was explicitly set to false, let's inherit that into // If webSecurity was explicitly set to false, let's inherit that into

View file

@ -191,6 +191,8 @@ const char kEnableRemoteModule[] = "enableRemoteModule";
const char kEnableWebSQL[] = "enableWebSQL"; const char kEnableWebSQL[] = "enableWebSQL";
const char kEnablePreferredSizeMode[] = "enablePreferredSizeMode";
} // namespace options } // namespace options
namespace switches { namespace switches {

View file

@ -87,6 +87,7 @@ extern const char kTextAreasAreResizable[];
extern const char kWebGL[]; extern const char kWebGL[];
extern const char kNavigateOnDragDrop[]; extern const char kNavigateOnDragDrop[];
extern const char kEnableWebSQL[]; extern const char kEnableWebSQL[];
extern const char kEnablePreferredSizeMode[];
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
extern const char kSpellcheck[]; extern const char kSpellcheck[];