Browse Source

Adding easy way to preview files

Pierre Laurac 8 years ago
parent
commit
526debb5ab

+ 2 - 2
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<v8::Value> value,

+ 1 - 1
atom/browser/api/atom_api_window.h

@@ -170,7 +170,7 @@ class Window : public mate::TrackableObject<Window>,
   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<v8::Value> value, mate::Arguments* args);
   v8::Local<v8::Value> GetParentWindow() const;
   std::vector<v8::Local<v8::Object>> GetChildWindows() const;

+ 1 - 1
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() {

+ 1 - 1
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<NativeWindow> GetWeakPtr() {
     return weak_factory_.GetWeakPtr();

+ 2 - 1
atom/browser/native_window_mac.h

@@ -9,6 +9,7 @@
 
 #include <string>
 #include <vector>
+#include <Quartz/Quartz.h>
 
 #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;

+ 65 - 7
atom/browser/native_window_mac.mm

@@ -5,6 +5,7 @@
 #include "atom/browser/native_window_mac.h"
 
 #include <string>
+#include <Quartz/Quartz.h>
 
 #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 <QLPreviewItem>
+
+@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<QLPreviewPanelDataSource, QLPreviewPanelDelegate> {
  @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 <QLPreviewItem>)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:pathStr.c_str() 
+                                      encoding:[NSString defaultCStringEncoding]];
+
+  NSString *name = [NSString stringWithCString:nameStr.c_str() 
+                                      encoding:[NSString defaultCStringEncoding]];
 
-  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];
+  [window_ previewFileAtPath:path withName:name];
 }
 
 void NativeWindowMac::SetMovable(bool movable) {

+ 1 - 0
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,