From e4bb456964ce5c622cd539fb3824dccdea4af329 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 29 May 2015 11:44:49 +0800 Subject: [PATCH] Pass element size when attaching --- atom/browser/api/atom_api_web_contents.cc | 3 +++ atom/browser/lib/guest-view-manager.coffee | 12 +++++----- .../lib/web-view/guest-view-internal.coffee | 4 ++-- atom/renderer/lib/web-view/web-view.coffee | 22 ++++++++++++------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index f82a65818a6a..639b875fa975 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -62,6 +62,8 @@ struct Converter { out->min_size.reset(new gfx::Size(size)); if (params.Get("max", &size)) out->max_size.reset(new gfx::Size(size)); + if (params.Get("elementSize", &size)) + out->normal_size.reset(new gfx::Size(size)); return true; } }; @@ -476,6 +478,7 @@ void WebContents::WillAttach(content::WebContents* embedder_web_contents, bool is_full_page_plugin) { embedder_web_contents_ = embedder_web_contents; element_instance_id_ = element_instance_id; + is_full_page_plugin_ = is_full_page_plugin; } void WebContents::Destroy() { diff --git a/atom/browser/lib/guest-view-manager.coffee b/atom/browser/lib/guest-view-manager.coffee index e59bdb42c009..ab8aa8a4ad98 100644 --- a/atom/browser/lib/guest-view-manager.coffee +++ b/atom/browser/lib/guest-view-manager.coffee @@ -41,7 +41,6 @@ createGuest = (embedder, params) -> guest = webContents.create isGuest: true guestInstanceId: id - storagePartitionId: params.storagePartitionId guestInstances[id] = {guest, embedder} # Destroy guest when the embedder is gone or navigated. @@ -58,9 +57,12 @@ createGuest = (embedder, params) -> delete @attachParams @viewInstanceId = params.instanceId - min = width: params.minwidth, height: params.minheight - max = width: params.maxwidth, height: params.maxheight - @setSize params.autosize, min, max + @setSize + enableAutoSize: params.autosize + min: + width: params.minwidth, height: params.minheight + max: + width: params.maxwidth, height: params.maxheight if params.src opts = {} @@ -123,7 +125,7 @@ destroyGuest = (embedder, id) -> delete reverseEmbedderElementsMap[id] delete embedderElementsMap[key] -ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', (event, type, params, requestId) -> +ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', (event, params, requestId) -> event.sender.send "ATOM_SHELL_RESPONSE_#{requestId}", createGuest(event.sender, params) ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_ATTACH_GUEST', (event, elementInstanceId, guestInstanceId, params) -> diff --git a/atom/renderer/lib/web-view/guest-view-internal.coffee b/atom/renderer/lib/web-view/guest-view-internal.coffee index 2abc47edffe6..cc0a7c928e38 100644 --- a/atom/renderer/lib/web-view/guest-view-internal.coffee +++ b/atom/renderer/lib/web-view/guest-view-internal.coffee @@ -55,9 +55,9 @@ module.exports = ipc.removeAllListeners "ATOM_SHELL_GUEST_VIEW_INTERNAL_IPC_MESSAGE-#{viewInstanceId}" ipc.removeAllListeners "ATOM_SHELL_GUEST_VIEW_INTERNAL_SIZE_CHANGED-#{viewInstanceId}" - createGuest: (type, params, callback) -> + createGuest: (params, callback) -> requestId++ - ipc.send 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', type, params, requestId + ipc.send 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', params, requestId ipc.once "ATOM_SHELL_RESPONSE_#{requestId}", callback attachGuest: (elementInstanceId, guestInstanceId, params) -> diff --git a/atom/renderer/lib/web-view/web-view.coffee b/atom/renderer/lib/web-view/web-view.coffee index 6a8e3cf05e58..72961052a055 100644 --- a/atom/renderer/lib/web-view/web-view.coffee +++ b/atom/renderer/lib/web-view/web-view.coffee @@ -88,7 +88,7 @@ class WebViewImpl return unless @guestInstanceId - guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildAttachParams() + guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildParams() onSizeChanged: (webViewEvent) -> newWidth = webViewEvent.newWidth @@ -121,9 +121,7 @@ class WebViewImpl @dispatchEvent webViewEvent createGuest: -> - params = - storagePartitionId: @attributes[webViewConstants.ATTRIBUTE_PARTITION].getValue() - guestViewInternal.createGuest 'webview', params, (guestInstanceId) => + guestViewInternal.createGuest @buildParams(), (guestInstanceId) => @attachWindow guestInstanceId dispatchEvent: (webViewEvent) -> @@ -156,21 +154,29 @@ class WebViewImpl onAttach: (storagePartitionId) -> @attributes[webViewConstants.ATTRIBUTE_PARTITION].setValue storagePartitionId - buildAttachParams: -> + buildParams: -> params = instanceId: @viewInstanceId userAgentOverride: @userAgentOverride for attributeName, attribute of @attributes params[attributeName] = attribute.getValue() + # When the WebView is not participating in layout (display:none) + # then getBoundingClientRect() would report a width and height of 0. + # However, in the case where the WebView has a fixed size we can + # use that value to initially size the guest so as to avoid a relayout of + # the on display:block. + css = window.getComputedStyle @webviewNode, null + elementRect = @webviewNode.getBoundingClientRect() + params.elementSize = + width: parseInt(elementRect.width) || parseInt(css.getPropertyValue('width')) + height: parseInt(elementRect.height) || parseInt(css.getPropertyValue('height')) params attachWindow: (guestInstanceId) -> @guestInstanceId = guestInstanceId - params = @buildAttachParams() - return true unless @internalInstanceId - guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, params + guestViewInternal.attachGuest @internalInstanceId, @guestInstanceId, @buildParams() # Registers browser plugin custom element. registerBrowserPluginElement = ->