mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:17:36 +00:00
MacPDF: Start moving window-related things into MacPDFWindowController
- MacPDFWindowController is now the xib file's owner - _pdfView moves over - MacPDFWindowController is now the MacPDFViewDelegate and responsible for updating the window's title - Due to MacPDFWindowController now being the xib file's owner, windowControllerDidLoadNib: is no longer called automatically, so call a custom windowIsReady method manually instead No behavior change.
This commit is contained in:
parent
67f6baead0
commit
dcf40892b8
5 changed files with 76 additions and 47 deletions
|
@ -8,16 +8,14 @@
|
||||||
|
|
||||||
#include "CocoaWrapper.h"
|
#include "CocoaWrapper.h"
|
||||||
|
|
||||||
#import "MacPDFView.h"
|
#import "MacPDFWindowController.h"
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@interface MacPDFDocument : NSDocument <MacPDFViewDelegate>
|
@interface MacPDFDocument : NSDocument
|
||||||
{
|
|
||||||
IBOutlet MacPDFView* _pdfView;
|
|
||||||
}
|
|
||||||
|
|
||||||
- (IBAction)showGoToPageDialog:(id)sender;
|
- (PDF::Document*)pdf;
|
||||||
|
- (void)windowIsReady;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -15,11 +15,17 @@
|
||||||
{
|
{
|
||||||
NSData* _data; // Strong, _doc refers to it.
|
NSData* _data; // Strong, _doc refers to it.
|
||||||
RefPtr<PDF::Document> _doc;
|
RefPtr<PDF::Document> _doc;
|
||||||
|
MacPDFWindowController* _windowController;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
@implementation MacPDFDocument
|
@implementation MacPDFDocument
|
||||||
|
|
||||||
|
- (PDF::Document*)pdf
|
||||||
|
{
|
||||||
|
return _doc;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)promptForPassword:(NSWindow*)window
|
- (void)promptForPassword:(NSWindow*)window
|
||||||
{
|
{
|
||||||
auto alert = [[NSAlert alloc] init];
|
auto alert = [[NSAlert alloc] init];
|
||||||
|
@ -67,25 +73,21 @@
|
||||||
// FIXME: show error?
|
// FIXME: show error?
|
||||||
NSLog(@"failed to load 2: %@", @(err.error().message().characters()));
|
NSLog(@"failed to load 2: %@", @(err.error().message().characters()));
|
||||||
} else {
|
} else {
|
||||||
[_pdfView setDocument:_doc->make_weak_ptr()];
|
[_windowController pdfDidInitialize];
|
||||||
[self pageChanged];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)makeWindowControllers
|
- (void)makeWindowControllers
|
||||||
{
|
{
|
||||||
[self addWindowController:[[MacPDFWindowController alloc] initWithDocument:self]];
|
_windowController = [[MacPDFWindowController alloc] initWithDocument:self];
|
||||||
|
[self addWindowController:_windowController];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)windowControllerDidLoadNib:(NSWindowController*)aController
|
- (void)windowIsReady
|
||||||
{
|
{
|
||||||
[super windowControllerDidLoadNib:aController];
|
|
||||||
|
|
||||||
[_pdfView setDelegate:self];
|
|
||||||
|
|
||||||
if (_doc) {
|
if (_doc) {
|
||||||
if (auto handler = _doc->security_handler(); handler && !handler->has_user_password()) {
|
if (auto handler = _doc->security_handler(); handler && !handler->has_user_password()) {
|
||||||
[self promptForPassword:aController.window];
|
[self promptForPassword:_windowController.window];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[self initializePDF];
|
[self initializePDF];
|
||||||
|
@ -142,34 +144,4 @@
|
||||||
return NO;
|
return NO;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (IBAction)showGoToPageDialog:(id)sender
|
|
||||||
{
|
|
||||||
auto alert = [[NSAlert alloc] init];
|
|
||||||
alert.messageText = @"Page Number";
|
|
||||||
[alert addButtonWithTitle:@"Go"];
|
|
||||||
[alert addButtonWithTitle:@"Cancel"];
|
|
||||||
|
|
||||||
auto textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 24)];
|
|
||||||
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
|
|
||||||
formatter.numberStyle = NSNumberFormatterNoStyle; // Integers only.
|
|
||||||
[textField setFormatter:formatter];
|
|
||||||
[textField setIntValue:[_pdfView page]];
|
|
||||||
|
|
||||||
alert.accessoryView = textField;
|
|
||||||
alert.window.initialFirstResponder = textField;
|
|
||||||
|
|
||||||
NSWindow* window = _pdfView.window;
|
|
||||||
[alert beginSheetModalForWindow:window
|
|
||||||
completionHandler:^(NSModalResponse response) {
|
|
||||||
if (response == NSAlertFirstButtonReturn)
|
|
||||||
[self->_pdfView goToPage:[textField intValue]];
|
|
||||||
}];
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)pageChanged
|
|
||||||
{
|
|
||||||
[_pdfView.window setSubtitle:
|
|
||||||
[NSString stringWithFormat:@"Page %d of %d", [_pdfView page], _doc -> get_page_count()]];
|
|
||||||
}
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<objects>
|
<objects>
|
||||||
<customObject id="-2" userLabel="File's Owner" customClass="MacPDFDocument">
|
<customObject id="-2" userLabel="File's Owner" customClass="MacPDFWindowController">
|
||||||
<connections>
|
<connections>
|
||||||
<outlet property="_pdfView" destination="gIp-Ho-8D9" id="bcT-vU-foe"/>
|
<outlet property="_pdfView" destination="gIp-Ho-8D9" id="bcT-vU-foe"/>
|
||||||
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
|
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
|
||||||
|
|
|
@ -8,13 +8,18 @@
|
||||||
|
|
||||||
#include "CocoaWrapper.h"
|
#include "CocoaWrapper.h"
|
||||||
|
|
||||||
|
#import "MacPDFView.h"
|
||||||
|
#include <LibPDF/Document.h>
|
||||||
|
|
||||||
NS_ASSUME_NONNULL_BEGIN
|
NS_ASSUME_NONNULL_BEGIN
|
||||||
|
|
||||||
@class MacPDFDocument;
|
@class MacPDFDocument;
|
||||||
|
|
||||||
@interface MacPDFWindowController : NSWindowController
|
@interface MacPDFWindowController : NSWindowController <MacPDFViewDelegate>
|
||||||
|
|
||||||
- (instancetype)initWithDocument:(MacPDFDocument*)document;
|
- (instancetype)initWithDocument:(MacPDFDocument*)document;
|
||||||
|
- (IBAction)showGoToPageDialog:(id)sender;
|
||||||
|
- (void)pdfDidInitialize;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,15 @@
|
||||||
|
|
||||||
#import "MacPDFWindowController.h"
|
#import "MacPDFWindowController.h"
|
||||||
|
|
||||||
|
#import "MacPDFDocument.h"
|
||||||
|
|
||||||
|
@interface MacPDFWindowController ()
|
||||||
|
{
|
||||||
|
MacPDFDocument* _pdfDocument;
|
||||||
|
IBOutlet MacPDFView* _pdfView;
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation MacPDFWindowController
|
@implementation MacPDFWindowController
|
||||||
|
|
||||||
- (instancetype)initWithDocument:(MacPDFDocument*)document
|
- (instancetype)initWithDocument:(MacPDFDocument*)document
|
||||||
|
@ -13,8 +22,53 @@
|
||||||
if (self = [super initWithWindowNibName:@"MacPDFDocument" owner:self]; !self)
|
if (self = [super initWithWindowNibName:@"MacPDFDocument" owner:self]; !self)
|
||||||
return nil;
|
return nil;
|
||||||
|
|
||||||
|
_pdfDocument = document;
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)windowDidLoad
|
||||||
|
{
|
||||||
|
[super windowDidLoad];
|
||||||
|
[_pdfView setDelegate:self];
|
||||||
|
[_pdfDocument windowIsReady];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)pdfDidInitialize
|
||||||
|
{
|
||||||
|
[_pdfView setDocument:_pdfDocument.pdf->make_weak_ptr()];
|
||||||
|
[self pageChanged];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (IBAction)showGoToPageDialog:(id)sender
|
||||||
|
{
|
||||||
|
auto alert = [[NSAlert alloc] init];
|
||||||
|
alert.messageText = @"Page Number";
|
||||||
|
[alert addButtonWithTitle:@"Go"];
|
||||||
|
[alert addButtonWithTitle:@"Cancel"];
|
||||||
|
|
||||||
|
auto textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 24)];
|
||||||
|
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
|
||||||
|
formatter.numberStyle = NSNumberFormatterNoStyle; // Integers only.
|
||||||
|
[textField setFormatter:formatter];
|
||||||
|
[textField setIntValue:[_pdfView page]];
|
||||||
|
|
||||||
|
alert.accessoryView = textField;
|
||||||
|
alert.window.initialFirstResponder = textField;
|
||||||
|
|
||||||
|
NSWindow* window = _pdfView.window;
|
||||||
|
[alert beginSheetModalForWindow:window
|
||||||
|
completionHandler:^(NSModalResponse response) {
|
||||||
|
if (response == NSAlertFirstButtonReturn)
|
||||||
|
[self->_pdfView goToPage:[textField intValue]];
|
||||||
|
}];
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma mark - MacPDFViewDelegate
|
||||||
|
|
||||||
|
- (void)pageChanged
|
||||||
|
{
|
||||||
|
[_pdfView.window setSubtitle:
|
||||||
|
[NSString stringWithFormat:@"Page %d of %d", [_pdfView page], _pdfDocument.pdf->get_page_count()]];
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue