fix: capturePage not resolving with hidden windows (#27883)

This commit is contained in:
Shelley Vohr 2021-03-09 05:51:44 -08:00 коммит произвёл GitHub
Родитель f6949dd197
Коммит d9997c303f
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 29 добавлений и 1 удалений

Просмотреть файл

@ -1370,7 +1370,7 @@ Returns `Boolean` - Whether the window's document has been edited.
Returns `Promise<NativeImage>` - Resolves with a [NativeImage](native-image.md) Returns `Promise<NativeImage>` - Resolves with a [NativeImage](native-image.md)
Captures a snapshot of the page within `rect`. Omitting `rect` will capture the whole visible page. Captures a snapshot of the page within `rect`. Omitting `rect` will capture the whole visible page. If the page is not visible, `rect` may be empty.
#### `win.loadURL(url[, options])` #### `win.loadURL(url[, options])`

Просмотреть файл

@ -2758,6 +2758,18 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
return handle; return handle;
} }
#if !defined(OS_MAC)
// If the view's renderer is suspended this may fail on Windows/Linux -
// bail if so. See CopyFromSurface in
// content/public/browser/render_widget_host_view.h.
auto* rfh = web_contents()->GetMainFrame();
if (rfh &&
rfh->GetVisibilityState() == blink::mojom::PageVisibilityState::kHidden) {
promise.Resolve(gfx::Image());
return handle;
}
#endif // defined(OS_MAC)
// Capture full page if user doesn't specify a |rect|. // Capture full page if user doesn't specify a |rect|.
const gfx::Size view_size = const gfx::Size view_size =
rect.IsEmpty() ? view->GetViewBounds().size() : rect.size(); rect.IsEmpty() ? view->GetViewBounds().size() : rect.size();

Просмотреть файл

@ -1338,6 +1338,22 @@ describe('BrowserWindow module', () => {
expect(image.isEmpty()).to.equal(true); expect(image.isEmpty()).to.equal(true);
}); });
it('resolves after the window is hidden', async () => {
const w = new BrowserWindow({ show: false });
w.loadFile(path.join(fixtures, 'pages', 'a.html'));
await emittedOnce(w, 'ready-to-show');
w.show();
const visibleImage = await w.capturePage();
expect(visibleImage.isEmpty()).to.equal(false);
w.hide();
const hiddenImage = await w.capturePage();
const isEmpty = process.platform !== 'darwin';
expect(hiddenImage.isEmpty()).to.equal(isEmpty);
});
it('preserves transparency', async () => { it('preserves transparency', async () => {
const w = new BrowserWindow({ show: false, transparent: true }); const w = new BrowserWindow({ show: false, transparent: true });
w.loadFile(path.join(fixtures, 'pages', 'theme-color.html')); w.loadFile(path.join(fixtures, 'pages', 'theme-color.html'));