diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h index 592ef77c0d..c7d752a042 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.h @@ -10,9 +10,14 @@ #import #undef FixedPoint +#import "LagomPDFView.h" + NS_ASSUME_NONNULL_BEGIN @interface LagomPDFDocument : NSDocument +{ + IBOutlet LagomPDFView* _pdfView; +} @end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm index 510754ee82..628b9e11a9 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.mm @@ -46,7 +46,9 @@ - (void)windowControllerDidLoadNib:(NSWindowController*)aController { [super windowControllerDidLoadNib:aController]; - // Add any code here that needs to be executed once the windowController has loaded the document's window. + if (_doc) { + [_pdfView setDocument:_doc->make_weak_ptr()]; + } } - (NSData*)dataOfType:(NSString*)typeName error:(NSError**)outError @@ -96,4 +98,10 @@ return YES; } ++ (BOOL)canConcurrentlyReadDocumentsOfType:(NSString*)typeName +{ + // Run readFromData:ofType:error: on background thread: + return YES; +} + @end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib index c8df7b7d7c..b0a6a2ebc7 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFDocument.xib @@ -8,6 +8,7 @@ + diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h index cf8c08d579..6d781ecb18 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.h @@ -12,7 +12,13 @@ #import #undef FixedPoint +#include +#include + @interface LagomPDFView : NSView { } + +- (void)setDocument:(WeakPtr)doc; + @end diff --git a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm index 4106864f16..b3cd452fbf 100644 --- a/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/LagomPDFView.mm @@ -15,8 +15,7 @@ @interface LagomPDFView () { - RefPtr _file; - RefPtr _doc; + WeakPtr _doc; NSBitmapImageRep* _rep; int _page_index; } @@ -63,18 +62,11 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) @implementation LagomPDFView -- (PDF::PDFErrorOr>)load +- (void)setDocument:(WeakPtr)doc { - auto source_root = DeprecatedString("/Users/thakis/src/serenity"); - Gfx::FontDatabase::set_default_fonts_lookup_path(DeprecatedString::formatted("{}/Base/res/fonts", source_root)); - - _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/pdf_reference_1-7.pdf"sv)); - // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/DC-008-Translation-2023-E.pdf"sv)); - // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/ISO_32000-2-2020_sponsored.pdf"sv)); - // _file = TRY(Core::MappedFile::map("/Users/thakis/Downloads/Text.pdf"sv)); - auto document = TRY(PDF::Document::create(_file->bytes())); - TRY(document->initialize()); - return document; + _doc = move(doc); + _page_index = min(24 - 1, _doc->get_page_count() - 1); + [self pageChanged]; } - (void)pageChanged @@ -88,17 +80,8 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) - (void)drawRect:(NSRect)rect { - static bool did_load = false; - if (!did_load) { - _page_index = 24 - 1; - did_load = true; - if (auto doc_or = [self load]; !doc_or.is_error()) { - _doc = doc_or.value(); - [self pageChanged]; - } else { - NSLog(@"failed to load: %@", @(doc_or.error().message().characters())); - } - } + if (!_doc) + return; [_rep drawInRect:self.bounds]; }