From 2650e64bd4da6bdd2c6ba9bb6a0eda14e225aad0 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 15 Jan 2024 10:50:48 -0500 Subject: [PATCH] MacPDF: Add a "Debug" menu with a "Show Clipping Paths" entry ...and hook it up. I opened MainMenu.xib in Xcode, added a new "Submenu Menu Item" from the Library (cmd-shift-l), added a User Defined "toggleShowClippingPaths:" action on First Responder and connected the menu item's action to that action. (I first tried duplicating the existing Window menu and editing that, but the Window menu is marked as `systemMenu="window"` in the xib and I couldn't find a way to undo that in Xcode. So the Debug menu first acted as a second Window menu.) I made "Debug" a toplevel menu to make it consistent with Ladybird.app for now, but I'll probably make it a submenu of "View" in the future. --- Meta/Lagom/Contrib/MacPDF/MacPDFView.h | 2 ++ Meta/Lagom/Contrib/MacPDF/MacPDFView.mm | 24 +++++++++++++-- Meta/Lagom/Contrib/MacPDF/MainMenu.xib | 41 ++++++++++++++++--------- 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.h b/Meta/Lagom/Contrib/MacPDF/MacPDFView.h index f96c2d9016..298a7297a9 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.h +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.h @@ -26,4 +26,6 @@ - (IBAction)goToNextPage:(id)sender; - (IBAction)goToPreviousPage:(id)sender; +- (IBAction)toggleShowClippingPaths:(id)sender; + @end diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm index cc5d4c7d54..6e8d8a76c0 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm @@ -16,10 +16,11 @@ NSBitmapImageRep* _cachedBitmap; int _page_index; __weak id _delegate; + PDF::RenderingPreferences _preferences; } @end -static PDF::PDFErrorOr> render(PDF::Document& document, int page_index, NSSize size) +static PDF::PDFErrorOr> render(PDF::Document& document, int page_index, NSSize size, PDF::RenderingPreferences const& preferences) { auto page = TRY(document.get_page(page_index)); @@ -29,7 +30,7 @@ static PDF::PDFErrorOr> render(PDF::Document& documen auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size)); - auto errors = PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {}); + auto errors = PDF::Renderer::render(document, page, bitmap, Color::White, preferences); if (errors.is_error()) { for (auto const& error : errors.error().errors()) NSLog(@"warning: %@", @(error.message().characters())); @@ -129,7 +130,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) 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, _preferences); !bitmap_or.is_error()) _cachedBitmap = ns_from_gfx(bitmap_or.value()); } @@ -158,6 +159,23 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) [self goToPage:current_page - 1]; } +- (BOOL)validateMenuItem:(NSMenuItem*)item +{ + if ([item action] == @selector(toggleShowClippingPaths:)) { + [item setState:_preferences.show_clipping_paths ? NSControlStateValueOn : NSControlStateValueOff]; + return _doc ? YES : NO; + } + return NO; +} + +- (IBAction)toggleShowClippingPaths:(id)sender +{ + if (_doc) { + _preferences.show_clipping_paths = !_preferences.show_clipping_paths; + [self invalidateCachedBitmap]; + } +} + - (void)keyDown:(NSEvent*)event { // Calls moveLeft: or moveRight: below. diff --git a/Meta/Lagom/Contrib/MacPDF/MainMenu.xib b/Meta/Lagom/Contrib/MacPDF/MainMenu.xib index 32ab8ed996..9ddfae8b46 100644 --- a/Meta/Lagom/Contrib/MacPDF/MainMenu.xib +++ b/Meta/Lagom/Contrib/MacPDF/MainMenu.xib @@ -1,7 +1,7 @@ - + - + @@ -660,26 +660,39 @@ - + - + - - - - - - + - + - - + + + + + + + + + + + + + - + + + + + + + +