mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 02:47:35 +00:00
MacPDF: Actually show contents of opened PDFs
xib change: connected _pdfView outlet to LagomPDFView instance.
This commit is contained in:
parent
0a01a2c82b
commit
819a602ee1
5 changed files with 28 additions and 25 deletions
|
@ -10,9 +10,14 @@
|
|||
#import <Cocoa/Cocoa.h>
|
||||
#undef FixedPoint
|
||||
|
||||
#import "LagomPDFView.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface LagomPDFDocument : NSDocument
|
||||
{
|
||||
IBOutlet LagomPDFView* _pdfView;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<objects>
|
||||
<customObject id="-2" userLabel="File's Owner" customClass="LagomPDFDocument">
|
||||
<connections>
|
||||
<outlet property="_pdfView" destination="gIp-Ho-8D9" id="bcT-vU-foe"/>
|
||||
<outlet property="window" destination="xOd-HO-29H" id="JIz-fz-R2o"/>
|
||||
</connections>
|
||||
</customObject>
|
||||
|
|
|
@ -12,7 +12,13 @@
|
|||
#import <Cocoa/Cocoa.h>
|
||||
#undef FixedPoint
|
||||
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <LibPDF/Document.h>
|
||||
|
||||
@interface LagomPDFView : NSView
|
||||
{
|
||||
}
|
||||
|
||||
- (void)setDocument:(WeakPtr<PDF::Document>)doc;
|
||||
|
||||
@end
|
||||
|
|
|
@ -15,8 +15,7 @@
|
|||
|
||||
@interface LagomPDFView ()
|
||||
{
|
||||
RefPtr<Core::MappedFile> _file;
|
||||
RefPtr<PDF::Document> _doc;
|
||||
WeakPtr<PDF::Document> _doc;
|
||||
NSBitmapImageRep* _rep;
|
||||
int _page_index;
|
||||
}
|
||||
|
@ -63,18 +62,11 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
|
|||
|
||||
@implementation LagomPDFView
|
||||
|
||||
- (PDF::PDFErrorOr<NonnullRefPtr<PDF::Document>>)load
|
||||
- (void)setDocument:(WeakPtr<PDF::Document>)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<Gfx::Bitmap> 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];
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue