mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
MacPDF: Change current page with arrow left/right
This commit is contained in:
parent
c4c4ead9d2
commit
ef91ced481
1 changed files with 40 additions and 6 deletions
|
@ -20,13 +20,12 @@
|
||||||
RefPtr<Core::MappedFile> _file;
|
RefPtr<Core::MappedFile> _file;
|
||||||
RefPtr<PDF::Document> _doc;
|
RefPtr<PDF::Document> _doc;
|
||||||
NSBitmapImageRep* _rep;
|
NSBitmapImageRep* _rep;
|
||||||
|
int _page_index;
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
static PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render(PDF::Document& document, NSSize size)
|
static PDF::PDFErrorOr<NonnullRefPtr<Gfx::Bitmap>> render(PDF::Document& document, int page_index, NSSize size)
|
||||||
{
|
{
|
||||||
NSLog(@"num pages %@", @(document.get_page_count()));
|
|
||||||
int page_index = 0;
|
|
||||||
auto page = TRY(document.get_page(page_index));
|
auto page = TRY(document.get_page(page_index));
|
||||||
|
|
||||||
Gfx::IntSize page_size;
|
Gfx::IntSize page_size;
|
||||||
|
@ -79,16 +78,24 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)pageChanged
|
||||||
|
{
|
||||||
|
if (!_doc || _doc->get_page_count() == 0)
|
||||||
|
return;
|
||||||
|
NSSize pixel_size = [self convertSizeToBacking:self.bounds.size];
|
||||||
|
if (auto bitmap_or = render(*_doc, _page_index, pixel_size); !bitmap_or.is_error())
|
||||||
|
_rep = ns_from_gfx(bitmap_or.value());
|
||||||
|
}
|
||||||
|
|
||||||
- (void)drawRect:(NSRect)rect
|
- (void)drawRect:(NSRect)rect
|
||||||
{
|
{
|
||||||
static bool did_load = false;
|
static bool did_load = false;
|
||||||
if (!did_load) {
|
if (!did_load) {
|
||||||
NSSize pixel_size = [self convertSizeToBacking:self.bounds.size];
|
_page_index = 0;
|
||||||
did_load = true;
|
did_load = true;
|
||||||
if (auto doc_or = [self load]; !doc_or.is_error()) {
|
if (auto doc_or = [self load]; !doc_or.is_error()) {
|
||||||
_doc = doc_or.value();
|
_doc = doc_or.value();
|
||||||
if (auto bitmap_or = render(*_doc, pixel_size); !bitmap_or.is_error())
|
[self pageChanged];
|
||||||
_rep = ns_from_gfx(bitmap_or.value());
|
|
||||||
} else {
|
} else {
|
||||||
NSLog(@"failed to load: %@", @(doc_or.error().message().characters()));
|
NSLog(@"failed to load: %@", @(doc_or.error().message().characters()));
|
||||||
}
|
}
|
||||||
|
@ -96,4 +103,31 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
|
||||||
[_rep drawInRect:self.bounds];
|
[_rep drawInRect:self.bounds];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)acceptsFirstResponder
|
||||||
|
{
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)keyDown:(NSEvent*)event
|
||||||
|
{
|
||||||
|
[self interpretKeyEvents:@[ event ]];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (IBAction)moveLeft:(id)sender
|
||||||
|
{
|
||||||
|
if (_page_index > 0) {
|
||||||
|
_page_index--;
|
||||||
|
[self pageChanged];
|
||||||
|
[self setNeedsDisplay:YES];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (IBAction)moveRight:(id)sender
|
||||||
|
{
|
||||||
|
if (_page_index < _doc->get_page_count() - 1) {
|
||||||
|
_page_index++;
|
||||||
|
[self pageChanged];
|
||||||
|
[self setNeedsDisplay:YES];
|
||||||
|
}
|
||||||
|
}
|
||||||
@end
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue