1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:37:46 +00:00

MacPDF: Add a "Go to Page..." menu item

xib changes:
* Add a "Go" toplevel submenu
* Put a "Go to Page..." menu item in it
* Add showGoToPageDialog: to first responder
* Bind action of new menu item to that

The dialog is just a janky NSAlert for now.
This commit is contained in:
Nico Weber 2023-09-24 21:42:51 -04:00 committed by Andreas Kling
parent fe4fe5ceff
commit bd19fcc039
5 changed files with 58 additions and 10 deletions

View file

@ -636,6 +636,19 @@
</items>
</menu>
</menuItem>
<menuItem title="Go" id="XZ6-XO-pVc">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Go" id="u8F-oH-oMu">
<items>
<menuItem title="Go to Page…" keyEquivalent="g" id="Ou1-5M-LzJ">
<modifierMask key="keyEquivalentModifierMask" option="YES" command="YES"/>
<connections>
<action selector="showGoToPageDialog:" target="-1" id="hmq-Sy-pJC"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="Window" id="aUF-d1-5bR">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Window" systemMenu="window" id="Td7-aD-5lo">

View file

@ -19,6 +19,8 @@ NS_ASSUME_NONNULL_BEGIN
IBOutlet LagomPDFView* _pdfView;
}
- (IBAction)showGoToPageDialog:(id)sender;
@end
NS_ASSUME_NONNULL_END

View file

@ -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

View file

@ -20,5 +20,6 @@
}
- (void)setDocument:(WeakPtr<PDF::Document>)doc;
- (void)goToPage:(int)page;
@end

View file

@ -70,6 +70,20 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> 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