From dcf40892b8b2cabe1ee16e42667dacb36dc174b1 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 3 Oct 2023 08:24:45 -0400 Subject: [PATCH] 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. --- Meta/Lagom/Contrib/MacPDF/MacPDFDocument.h | 10 ++-- Meta/Lagom/Contrib/MacPDF/MacPDFDocument.mm | 50 ++++------------- Meta/Lagom/Contrib/MacPDF/MacPDFDocument.xib | 2 +- .../Contrib/MacPDF/MacPDFWindowController.h | 7 ++- .../Contrib/MacPDF/MacPDFWindowController.mm | 54 +++++++++++++++++++ 5 files changed, 76 insertions(+), 47 deletions(-) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.h b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.h index 737889e9bec..db439b0252e 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.h +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.h @@ -8,16 +8,14 @@ #include "CocoaWrapper.h" -#import "MacPDFView.h" +#import "MacPDFWindowController.h" NS_ASSUME_NONNULL_BEGIN -@interface MacPDFDocument : NSDocument -{ - IBOutlet MacPDFView* _pdfView; -} +@interface MacPDFDocument : NSDocument -- (IBAction)showGoToPageDialog:(id)sender; +- (PDF::Document*)pdf; +- (void)windowIsReady; @end diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.mm index 021701572e7..fa685404ca6 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.mm @@ -15,11 +15,17 @@ { NSData* _data; // Strong, _doc refers to it. RefPtr _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 diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.xib b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.xib index f6f129781e3..c2768cbc8f9 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.xib +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFDocument.xib @@ -5,7 +5,7 @@ - + diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h index 5861eddc594..745de894a62 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.h @@ -8,13 +8,18 @@ #include "CocoaWrapper.h" +#import "MacPDFView.h" +#include + NS_ASSUME_NONNULL_BEGIN @class MacPDFDocument; -@interface MacPDFWindowController : NSWindowController +@interface MacPDFWindowController : NSWindowController - (instancetype)initWithDocument:(MacPDFDocument*)document; +- (IBAction)showGoToPageDialog:(id)sender; +- (void)pdfDidInitialize; @end diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm index c75795a9dc3..e22d8c51836 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm @@ -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