From 526debb5ab5bdf6d72a71a0fc4c6f7f7f53ba5b7 Mon Sep 17 00:00:00 2001 From: Pierre Laurac Date: Wed, 12 Oct 2016 17:21:56 -0700 Subject: [PATCH] Adding easy way to preview files --- atom/browser/api/atom_api_window.cc | 4 +- atom/browser/api/atom_api_window.h | 2 +- atom/browser/native_window.cc | 2 +- atom/browser/native_window.h | 2 +- atom/browser/native_window_mac.h | 3 +- atom/browser/native_window_mac.mm | 72 ++++++++++++++++++++++++++--- electron.gyp | 1 + 7 files changed, 73 insertions(+), 13 deletions(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index dccf6a77ce26..408f738c4b15 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -729,8 +729,8 @@ void Window::SetAspectRatio(double aspect_ratio, mate::Arguments* args) { window_->SetAspectRatio(aspect_ratio, extra_size); } -void Window::PreviewFile(const base::string16& filepath) { - window_->PreviewFile(filepath); +void Window::PreviewFile(const base::string16& filepath, const base::string16& filename) { + window_->PreviewFile(filepath, filename); } void Window::SetParentWindow(v8::Local value, diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index abd19f9d5bed..e92960f33950 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -170,7 +170,7 @@ class Window : public mate::TrackableObject, void SetMenuBarVisibility(bool visible); bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); - void PreviewFile(const base::string16& filepath); + void PreviewFile(const base::string16& filepath, const base::string16& filename); 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 b6ea285159a0..f9b762f54465 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -374,7 +374,7 @@ void NativeWindow::SetAspectRatio(double aspect_ratio, aspect_ratio_extraSize_ = extra_size; } -void NativeWindow::PreviewFile(const base::string16& filepath) { +void NativeWindow::PreviewFile(const base::string16& filepath, const base::string16& filename) { } void NativeWindow::RequestToClosePage() { diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index c15868c5f407..c10cb90725f9 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -176,7 +176,7 @@ 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 base::string16& filepath); + virtual void PreviewFile(const base::string16& filepath, const base::string16& filename); base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index 35649ae7d332..6e3f58670365 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -9,6 +9,7 @@ #include #include +#include #include "atom/browser/native_window.h" #include "base/mac/scoped_nsobject.h" @@ -55,7 +56,7 @@ class NativeWindowMac : public NativeWindow, void SetMovable(bool movable) override; void SetAspectRatio(double aspect_ratio, const gfx::Size& extra_size) override; - void PreviewFile(const base::string16& filepath) override; + void PreviewFile(const base::string16& filepath, const base::string16& filename) 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 1e18efcc713a..cf9e863b8617 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -5,6 +5,7 @@ #include "atom/browser/native_window_mac.h" #include +#include #include "atom/browser/window_list.h" #include "atom/common/color_util.h" @@ -280,7 +281,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 *)anURL title:(NSString *)aTitle; + +@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_; @@ -290,6 +313,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; @@ -447,6 +471,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 *)filepath withName:(NSString *) name { + NSURL * url = [[NSURL alloc] initFileURLWithPath:filepath]; + [self setQuickLookItem:[[AtomPreviewItem alloc] initWithURL:url title:name]]; + [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil]; +} + @end @interface ControlRegionView : NSView @@ -902,13 +956,17 @@ void NativeWindowMac::SetAspectRatio(double aspect_ratio, [window_ setResizeIncrements:NSMakeSize(1.0, 1.0)]; } -void NativeWindowMac::PreviewFile(const base::string16& filepath) { - std::string rtf = base::UTF16ToUTF8(filepath); +void NativeWindowMac::PreviewFile(const base::string16& filepath, const base::string16& filename) { + std::string pathStr = base::UTF16ToUTF8(filepath); + std::string nameStr = base::UTF16ToUTF8(filename); - NSString *path = [NSString stringWithCString:rtf.c_str() - encoding:[NSString defaultCStringEncoding]]; - NSAlert *alert = [NSAlert alertWithMessageText:path defaultButton:@"Close anyway" alternateButton:@"Cancel" otherButton:nil informativeTextWithFormat:@""]; - [alert runModal]; + NSString *path = [NSString stringWithCString:pathStr.c_str() + encoding:[NSString defaultCStringEncoding]]; + + NSString *name = [NSString stringWithCString:nameStr.c_str() + encoding:[NSString defaultCStringEncoding]]; + + [window_ previewFileAtPath:path withName:name]; } void NativeWindowMac::SetMovable(bool movable) { diff --git a/electron.gyp b/electron.gyp index 413d6c5580de..0552006e694d 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,