diff --git a/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib b/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib index 937e7cca86..7c6c8bb575 100644 --- a/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib +++ b/Meta/Lagom/Contrib/MacPDF/Base.lproj/MainMenu.xib @@ -636,6 +636,19 @@ + + + + + + + + + + + + + diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h index c7d752a042..a00cbc151f 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h @@ -19,6 +19,8 @@ NS_ASSUME_NONNULL_BEGIN IBOutlet LagomPDFView* _pdfView; } +- (IBAction)showGoToPageDialog:(id)sender; + @end NS_ASSUME_NONNULL_END diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm index e90c5cf351..fcd71e5c6f 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm @@ -142,4 +142,28 @@ return YES; } +- (IBAction)showGoToPageDialog:(id)sender +{ + auto alert = [[NSAlert alloc] init]; + alert.messageText = @"Page Number"; + [alert addButtonWithTitle:@"Go"]; + [alert addButtonWithTitle:@"Cancel"]; + + // FIXME: Pre-populate with current page. + auto textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 100, 24)]; + NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init]; + formatter.numberStyle = NSNumberFormatterNoStyle; // Integers only. + [textField setFormatter:formatter]; + + 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]]; + }]; +} + @end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h index 6d781ecb18..71a095a54c 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h @@ -20,5 +20,6 @@ } - (void)setDocument:(WeakPtr)doc; +- (void)goToPage:(int)page; @end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm index 80cfe4829b..ad9a8e554b 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm @@ -70,6 +70,20 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) [self invalidateCachedBitmap]; } +- (void)goToPage:(int)page +{ + if (!_doc) + return; + + int new_index = max(0, min(page - 1, _doc->get_page_count() - 1)); + if (new_index == _page_index) + return; + + _page_index = new_index; + [self invalidateRestorableState]; + [self invalidateCachedBitmap]; +} + #pragma mark Drawing - (void)invalidateCachedBitmap @@ -113,21 +127,15 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) // Called on left arrow. - (IBAction)moveLeft:(id)sender { - if (_page_index > 0) { - _page_index--; - [self invalidateRestorableState]; - [self invalidateCachedBitmap]; - } + int current_page = _page_index + 1; + [self goToPage:current_page - 1]; } // Called on right arrow. - (IBAction)moveRight:(id)sender { - if (_page_index < _doc->get_page_count() - 1) { - _page_index++; - [self invalidateRestorableState]; - [self invalidateCachedBitmap]; - } + int current_page = _page_index + 1; + [self goToPage:current_page + 1]; } #pragma mark State restoration