1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 20:17:44 +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:
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

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