Skip to content

Commit

Permalink
Merge pull request #7592 from frontapp/master
Browse files Browse the repository at this point in the history
Preview file with QuickLook
  • Loading branch information
kevinsawicki committed Oct 26, 2016
2 parents 0f5bfad + d85c4da commit 9e26664
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 1 deletion.
8 changes: 8 additions & 0 deletions atom/browser/api/atom_api_window.cc
Expand Up @@ -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<v8::Value> value,
mate::Arguments* args) {
if (IsModal()) {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions atom/browser/api/atom_api_window.h
Expand Up @@ -170,6 +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 std::string& path, mate::Arguments* args);
void SetParentWindow(v8::Local<v8::Value> value, mate::Arguments* args);
v8::Local<v8::Value> GetParentWindow() const;
std::vector<v8::Local<v8::Object>> GetChildWindows() const;
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/native_window.cc
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/native_window.h
Expand Up @@ -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<NativeWindow> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
Expand Down
2 changes: 2 additions & 0 deletions atom/browser/native_window_mac.h
Expand Up @@ -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;
Expand Down
63 changes: 62 additions & 1 deletion atom/browser/native_window_mac.mm
Expand Up @@ -4,6 +4,7 @@

#include "atom/browser/native_window_mac.h"

#include <Quartz/Quartz.h>
#include <string>

#include "atom/browser/window_list.h"
Expand Down Expand Up @@ -277,7 +278,29 @@ - (NSRect)window:(NSWindow*)window

@end

@interface AtomNSWindow : EventDispatchingWindow {
@interface AtomPreviewItem : NSObject <QLPreviewItem>

@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<QLPreviewPanelDataSource, QLPreviewPanelDelegate> {
@private
atom::NativeWindowMac* shell_;
bool enable_larger_than_screen_;
Expand All @@ -287,6 +310,7 @@ @interface AtomNSWindow : EventDispatchingWindow {
@property BOOL disableAutoHideCursor;
@property BOOL disableKeyOrMainWindow;
@property NSPoint windowButtonsOffset;
@property (nonatomic, retain) AtomPreviewItem* quickLookItem;

- (void)setShell:(atom::NativeWindowMac*)shell;
- (void)setEnableLargerThanScreen:(bool)enable;
Expand Down Expand Up @@ -444,6 +468,36 @@ - (NSView*)frameView {
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*)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
Expand Down Expand Up @@ -899,6 +953,13 @@ static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
[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];
}
Expand Down
12 changes: 12 additions & 0 deletions docs/api/browser-window.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions electron.gyp
Expand Up @@ -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,
Expand Down

0 comments on commit 9e26664

Please sign in to comment.