diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index d5f1964517..88b0459c26 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -729,6 +729,13 @@ void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) { window_->SetAspectRatio(aspect_ratio, extra_size); } +void Window::PreviewFile(const std::string& path, mate::Arguments* args) { + std::string display_name; + if (!args->GetNext(&display_name)) + display_name = path; + window_->PreviewFile(path, display_name); +} + void Window::SetParentWindow(v8::Local value, mate::Arguments* args) { if (IsModal()) { @@ -825,6 +832,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setFullScreen", &Window::SetFullScreen) .SetMethod("isFullScreen", &Window::IsFullscreen) .SetMethod("setAspectRatio", &Window::SetAspectRatio) + .SetMethod("previewFile", &Window::PreviewFile) #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 317f5b51ec..ba1a999b2b 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -170,6 +170,7 @@ class Window : public mate::TrackableObject, void SetMenuBarVisibility(bool visible); bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); + void PreviewFile(const std::string& path, mate::Arguments* args); 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 1cf5a23b6e..6c32e79e19 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -374,6 +374,10 @@ void NativeWindow::SetAspectRatio(double aspect_ratio, aspect_ratio_extraSize_ = extra_size; } +void NativeWindow::PreviewFile(const std::string& path, + const std::string& display_name) { +} + 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 d8c022d606..3d190ba7d9 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -176,6 +176,8 @@ class NativeWindow : public base::SupportsUserData, double GetAspectRatio(); gfx::Size GetAspectRatioExtraSize(); virtual void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size); + virtual void PreviewFile(const std::string& path, + const std::string& display_name); base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 4fd7bcc3c2..979d69d1a2 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -55,6 +55,8 @@ class NativeWindowMac : public NativeWindow, void SetMovable(bool movable) override; void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size) override; + void PreviewFile(const std::string& path, const std::string& display_name) + 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 98e8d4b656..95423e0851 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -4,6 +4,7 @@ #include "atom/browser/native_window_mac.h" +#include #include #include "atom/browser/window_list.h" @@ -277,7 +278,29 @@ bool ScopedDisableResize::disable_resize_ = false; @end -@interface AtomNSWindow : EventDispatchingWindow { +@interface AtomPreviewItem : NSObject + +@property (nonatomic, retain) NSURL* previewItemURL; +@property (nonatomic, retain) NSString* previewItemTitle; + +- (id)initWithURL:(NSURL*)url title:(NSString*)title; + +@end + +@implementation AtomPreviewItem + +- (id)initWithURL:(NSURL*)url title:(NSString*)title { + self = [super init]; + if (self) { + self.previewItemURL = url; + self.previewItemTitle = title; + } + return self; +} + +@end + +@interface AtomNSWindow : EventDispatchingWindow { @private atom::NativeWindowMac* shell_; bool enable_larger_than_screen_; @@ -287,6 +310,7 @@ bool ScopedDisableResize::disable_resize_ = false; @property BOOL disableAutoHideCursor; @property BOOL disableKeyOrMainWindow; @property NSPoint windowButtonsOffset; +@property (nonatomic, retain) AtomPreviewItem* quickLookItem; - (void)setShell:(atom::NativeWindowMac*)shell; - (void)setEnableLargerThanScreen:(bool)enable; @@ -444,6 +468,36 @@ bool ScopedDisableResize::disable_resize_ = false; return [[self contentView] superview]; } +// Quicklook methods + +- (BOOL)acceptsPreviewPanelControl:(QLPreviewPanel*)panel { + return YES; +} + +- (void)beginPreviewPanelControl:(QLPreviewPanel*)panel { + panel.delegate = self; + panel.dataSource = self; +} + +- (void)endPreviewPanelControl:(QLPreviewPanel*)panel { + panel.delegate = nil; + panel.dataSource = nil; +} + +- (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel*)panel { + return 1; +} + +- (id )previewPanel:(QLPreviewPanel*)panel previewItemAtIndex:(NSInteger)index { + return [self quickLookItem]; +} + +- (void)previewFileAtPath:(NSString*)path withName:(NSString*) fileName { + NSURL* url = [[[NSURL alloc] initFileURLWithPath:path] autorelease]; + [self setQuickLookItem:[[[AtomPreviewItem alloc] initWithURL:url title:fileName] autorelease]]; + [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil]; +} + @end @interface ControlRegionView : NSView @@ -899,6 +953,13 @@ void NativeWindowMac::SetAspectRatio(double aspect_ratio, [window_ setResizeIncrements:NSMakeSize(1.0, 1.0)]; } +void NativeWindowMac::PreviewFile(const std::string& path, + const std::string& display_name) { + NSString* path_ns = [NSString stringWithUTF8String:path.c_str()]; + NSString* name_ns = [NSString stringWithUTF8String:display_name.c_str()]; + [window_ previewFileAtPath:path_ns withName:name_ns]; +} + void NativeWindowMac::SetMovable(bool movable) { [window_ setMovable:movable]; } diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index 345419eed2..059c9f152d 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -664,6 +664,17 @@ the player itself we would call this function with arguments of 16/9 and are within the content view--only that they exist. Just sum any extra width and height areas you have within the overall content view. +#### `win.previewFile(path[, displayName])` _macOS_ + +* `path` String - The absolute path to the file to preview with QuickLook. This + is important as Quick Look uses the file name and file extension on the path + to determine the content type of the file to open. +* `displayName` String (Optional) - The name of the file to display on the + Quick Look modal view. This is purely visual and does not affect the content + type of the file. Defaults to `path`. + +Uses [Quick Look][quick-look] to preview a file at a given path. + #### `win.setBounds(bounds[, animate])` * `bounds` [Rectangle](structures/rectangle.md) @@ -1182,3 +1193,4 @@ Returns `BrowserWindow` - The parent window. Returns `BrowserWindow[]` - All child windows. [window-levels]: https://developer.apple.com/reference/appkit/nswindow/1664726-window_levels +[quick-look]: https://en.wikipedia.org/wiki/Quick_Look diff --git a/electron.gyp b/electron.gyp index 413d6c5580..0552006e69 100644 --- a/electron.gyp +++ b/electron.gyp @@ -509,6 +509,7 @@ 'libraries': [ '$(SDKROOT)/System/Library/Frameworks/Carbon.framework', '$(SDKROOT)/System/Library/Frameworks/QuartzCore.framework', + '$(SDKROOT)/System/Library/Frameworks/Quartz.framework', ], }, 'mac_bundle': 1,