diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 455e2225a3a8..40ff272b1b17 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -757,6 +757,10 @@ void Window::PreviewFile(const std::string& path, mate::Arguments* args) { window_->PreviewFile(path, display_name); } +void Window::CloseFilePreview() { + window_->CloseFilePreview(); +} + void Window::SetParentWindow(v8::Local value, mate::Arguments* args) { if (IsModal()) { @@ -861,6 +865,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("isFullScreen", &Window::IsFullscreen) .SetMethod("setAspectRatio", &Window::SetAspectRatio) .SetMethod("previewFile", &Window::PreviewFile) + .SetMethod("closeFilePreview", &Window::CloseFilePreview) #if !defined(OS_WIN) .SetMethod("setParentWindow", &Window::SetParentWindow) #endif diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 5819c7b2f2a4..9606a5e69b56 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -171,6 +171,7 @@ class Window : public mate::TrackableObject, bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); void PreviewFile(const std::string& path, mate::Arguments* args); + void CloseFilePreview(); void SetParentWindow(v8::Local value, mate::Arguments* args); v8::Local GetParentWindow() const; std::vector> GetChildWindows() const; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 92419d357eb3..64934762c387 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -381,6 +381,9 @@ void NativeWindow::PreviewFile(const std::string& path, const std::string& display_name) { } +void NativeWindow::CloseFilePreview() { +} + void NativeWindow::RequestToClosePage() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index b5e2cf3bac31..4c1d0a95a1be 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -179,8 +179,11 @@ class NativeWindow : public base::SupportsUserData, double GetAspectRatio(); gfx::Size GetAspectRatioExtraSize(); virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size); + + // File preview APIs. virtual void PreviewFile(const std::string& path, const std::string& display_name); + virtual void CloseFilePreview(); base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 7c4d2b4eef2f..3f781a785096 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -57,6 +57,7 @@ class NativeWindowMac : public NativeWindow, override; void PreviewFile(const std::string& path, const std::string& display_name) override; + void CloseFilePreview() override; bool IsMovable() override; void SetMinimizable(bool minimizable) override; bool IsMinimizable() override; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 5ba0700a1863..0e7aadb4a86c 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -1004,6 +1004,12 @@ void NativeWindowMac::PreviewFile(const std::string& path, [window_ previewFileAtPath:path_ns withName:name_ns]; } +void NativeWindowMac::CloseFilePreview() { + if ([QLPreviewPanel sharedPreviewPanelExists]) { + [[QLPreviewPanel sharedPreviewPanel] close]; + } +} + void NativeWindowMac::SetMovable(bool movable) { [window_ setMovable:movable]; } diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 2a6c2c57e34b..a7cdafbbd798 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -682,6 +682,10 @@ height areas you have within the overall content view. Uses [Quick Look][quick-look] to preview a file at a given path. +#### `win.closeFilePreview()` _macOS_ + +Closes the currently open [Quick Look][quick-look] panel. + #### `win.setBounds(bounds[, animate])` * `bounds` [Rectangle](structures/rectangle.md) diff --git a/spec/api-browser-window-spec.js b/spec/api-browser-window-spec.js index 034461a4bf38..08f5ce94912e 100644 --- a/spec/api-browser-window-spec.js +++ b/spec/api-browser-window-spec.js @@ -1718,6 +1718,17 @@ describe('browser-window module', function () { }) }) + describe('previewFile', function () { + it('opens the path in Quick Look on macOS', function () { + if (process.platform !== 'darwin') return this.skip() + + assert.doesNotThrow(function () { + w.previewFile(__filename) + w.closeFilePreview() + }) + }) + }) + describe('offscreen rendering', function () { this.timeout(10000)