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:
Nico Weber 2023-10-03 08:24:45 -04:00 committed by Andreas Kling
parent 67f6baead0
commit dcf40892b8
5 changed files with 76 additions and 47 deletions

View file

@ -8,16 +8,14 @@
#include "CocoaWrapper.h"
#import "MacPDFView.h"
#import "MacPDFWindowController.h"
NS_ASSUME_NONNULL_BEGIN
@interface MacPDFDocument : NSDocument <MacPDFViewDelegate>
{
IBOutlet MacPDFView* _pdfView;
}
@interface MacPDFDocument : NSDocument
- (IBAction)showGoToPageDialog:(id)sender;
- (PDF::Document*)pdf;
- (void)windowIsReady;
@end

View file

@ -15,11 +15,17 @@
{
NSData* _data; // Strong, _doc refers to it.
RefPtr<PDF::Document> _doc;
MacPDFWindowController* _windowController;
}
@end
@implementation MacPDFDocument
- (PDF::Document*)pdf
{
return _doc;
}
- (void)promptForPassword:(NSWindow*)window
{
auto alert = [[NSAlert alloc] init];
@ -67,25 +73,21 @@
// FIXME: show error?
NSLog(@"failed to load 2: %@", @(err.error().message().characters()));
} else {
[_pdfView setDocument:_doc->make_weak_ptr()];
[self pageChanged];
[_windowController pdfDidInitialize];
}
}
- (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 (auto handler = _doc->security_handler(); handler && !handler->has_user_password()) {
[self promptForPassword:aController.window];
[self promptForPassword:_windowController.window];
return;
}
[self initializePDF];
@ -142,34 +144,4 @@
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

View file

@ -5,7 +5,7 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="MacPDFDocument">
<customObject id="-2" userLabel="File's Owner" customClass="MacPDFWindowController">
<connections>
<outlet property="_pdfView" destination="gIp-Ho-8D9" id="bcT-vU-foe"/>
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>

View file

@ -8,13 +8,18 @@
#include "CocoaWrapper.h"
#import "MacPDFView.h"
#include <LibPDF/Document.h>
NS_ASSUME_NONNULL_BEGIN
@class MacPDFDocument;
@interface MacPDFWindowController : NSWindowController
@interface MacPDFWindowController : NSWindowController <MacPDFViewDelegate>
- (instancetype)initWithDocument:(MacPDFDocument*)document;
- (IBAction)showGoToPageDialog:(id)sender;
- (void)pdfDidInitialize;
@end

View file

@ -6,6 +6,15 @@
#import "MacPDFWindowController.h"
#import "MacPDFDocument.h"
@interface MacPDFWindowController ()
{
MacPDFDocument* _pdfDocument;
IBOutlet MacPDFView* _pdfView;
}
@end
@implementation MacPDFWindowController
- (instancetype)initWithDocument:(MacPDFDocument*)document
@ -13,8 +22,53 @@
if (self = [super initWithWindowNibName:@"MacPDFDocument" owner:self]; !self)
return nil;
_pdfDocument = document;
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