1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:37:44 +00:00

MacPDF: Fill bitmap cache on demand in -[LagomPDFView drawRect]

Removes one needless paint on document restore, and makes sure
that window resizing invalidates the cache (since this now checks
the bitmap's size).
This commit is contained in:
Nico Weber 2023-09-24 10:43:41 -04:00 committed by Andreas Kling
parent 2799dedb1b
commit 97dbdbc9b0

View file

@ -14,7 +14,7 @@
@interface LagomPDFView () @interface LagomPDFView ()
{ {
WeakPtr<PDF::Document> _doc; WeakPtr<PDF::Document> _doc;
NSBitmapImageRep* _rep; NSBitmapImageRep* _cachedBitmap;
int _page_index; int _page_index;
} }
@end @end
@ -60,33 +60,45 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
@implementation LagomPDFView @implementation LagomPDFView
// Called from LagomPDFDocument
- (void)setDocument:(WeakPtr<PDF::Document>)doc - (void)setDocument:(WeakPtr<PDF::Document>)doc
{ {
NSLog(@"doc set"); NSLog(@"doc set");
_doc = move(doc); _doc = move(doc);
_page_index = 0; _page_index = 0;
// FIXME: We do this again in restoreStateWithCoder:, which is wasteful. [self invalidateCachedBitmap];
// Compute bitmap lazily.
[self pageChanged];
} }
- (void)pageChanged #pragma mark Drawing
- (void)invalidateCachedBitmap
{
_cachedBitmap = nil;
[self setNeedsDisplay:YES];
}
- (void)ensureCachedBitmapIsUpToDate
{ {
if (!_doc || _doc->get_page_count() == 0) if (!_doc || _doc->get_page_count() == 0)
return; return;
NSSize pixel_size = [self convertSizeToBacking:self.bounds.size]; NSSize pixel_size = [self convertSizeToBacking:self.bounds.size];
if (NSEqualSizes([_cachedBitmap size], pixel_size))
return;
if (auto bitmap_or = render(*_doc, _page_index, pixel_size); !bitmap_or.is_error()) if (auto bitmap_or = render(*_doc, _page_index, pixel_size); !bitmap_or.is_error())
_rep = ns_from_gfx(bitmap_or.value()); _cachedBitmap = ns_from_gfx(bitmap_or.value());
} }
- (void)drawRect:(NSRect)rect - (void)drawRect:(NSRect)rect
{ {
if (!_doc) [self ensureCachedBitmapIsUpToDate];
return; [_cachedBitmap drawInRect:self.bounds];
[_rep drawInRect:self.bounds];
} }
#pragma mark Keyboard handling
- (BOOL)acceptsFirstResponder - (BOOL)acceptsFirstResponder
{ {
return YES; return YES;
@ -94,29 +106,32 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
- (void)keyDown:(NSEvent*)event - (void)keyDown:(NSEvent*)event
{ {
// Calls moveLeft: or moveRight: below.
[self interpretKeyEvents:@[ event ]]; [self interpretKeyEvents:@[ event ]];
} }
// Called on left arrow.
- (IBAction)moveLeft:(id)sender - (IBAction)moveLeft:(id)sender
{ {
if (_page_index > 0) { if (_page_index > 0) {
_page_index--; _page_index--;
[self invalidateRestorableState]; [self invalidateRestorableState];
[self pageChanged]; [self invalidateCachedBitmap];
[self setNeedsDisplay:YES];
} }
} }
// Called on right arrow.
- (IBAction)moveRight:(id)sender - (IBAction)moveRight:(id)sender
{ {
if (_page_index < _doc->get_page_count() - 1) { if (_page_index < _doc->get_page_count() - 1) {
_page_index++; _page_index++;
[self invalidateRestorableState]; [self invalidateRestorableState];
[self pageChanged]; [self invalidateCachedBitmap];
[self setNeedsDisplay:YES];
} }
} }
#pragma mark State restoration
- (void)encodeRestorableStateWithCoder:(NSCoder*)coder - (void)encodeRestorableStateWithCoder:(NSCoder*)coder
{ {
[coder encodeInt:_page_index forKey:@"PageIndex"]; [coder encodeInt:_page_index forKey:@"PageIndex"];
@ -129,8 +144,8 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
int page_index = [coder decodeIntForKey:@"PageIndex"]; int page_index = [coder decodeIntForKey:@"PageIndex"];
_page_index = min(max(0, page_index), _doc->get_page_count() - 1); _page_index = min(max(0, page_index), _doc->get_page_count() - 1);
NSLog(@"encodeRestorableStateWithCoder restored %d", _page_index); NSLog(@"encodeRestorableStateWithCoder restored %d", _page_index);
[self pageChanged]; [self invalidateCachedBitmap];
[self setNeedsDisplay:YES];
} }
} }
@end @end