From f02b84d34d43b25668553eb5b3d9f6d365f1b6ed Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 3 Oct 2023 11:34:01 -0400 Subject: [PATCH] MacPDF: Give sidebar the more modern look This Just Works with NSToolbarSidebarTrackingSeparatorItemIdentifier, as long as your window is has NSWindowStyleMaskFullSizeContentView in its style mask. If it doesn't, things behave pretty weirdly and at least in the docs I looked at, this requirement wasn't documented at all :/ Anyways, switch MacPDFView to use safeAreaRect instead of bounds now that we use NSWindowStyleMaskFullSizeContentView so that we don't draw parts of the PDF under the title bar. Also be careful to invalidate the PDF view if safeAreaRect changes, so that the page is redrawn when toolbar visibility gets toggled. --- Meta/Lagom/Contrib/MacPDF/MacPDFView.mm | 22 +++++++++++++++++-- .../Contrib/MacPDF/MacPDFWindowController.mm | 7 ++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm index 42111fd19c..9b9fcc6088 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFView.mm @@ -66,9 +66,27 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) _doc = move(doc); _page_index = 0; + [self addObserver:self + forKeyPath:@"safeAreaRect" + options:NSKeyValueObservingOptionNew + context:nil]; + [self invalidateCachedBitmap]; } +- (void)observeValueForKeyPath:(NSString*)keyPath + ofObject:(id)object + change:(NSDictionary*)change + context:(void*)context +{ + // AppKit by default doesn't invalidate a view if safeAreaRect changes but the view's bounds don't change. + // This happens for example when toggling the visibility of the toolbar with a full-size content view. + // We do want a repaint in this case. + VERIFY([keyPath isEqualToString:@"safeAreaRect"]); + VERIFY(object == self); + [self setNeedsDisplay:YES]; +} + - (void)goToPage:(int)page { if (!_doc) @@ -107,7 +125,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) if (!_doc || _doc->get_page_count() == 0) return; - NSSize pixel_size = [self convertSizeToBacking:self.bounds.size]; + NSSize pixel_size = [self convertSizeToBacking:self.safeAreaRect.size]; if (NSEqualSizes([_cachedBitmap size], pixel_size)) return; @@ -118,7 +136,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr bitmap_p) - (void)drawRect:(NSRect)rect { [self ensureCachedBitmapIsUpToDate]; - [_cachedBitmap drawInRect:self.bounds]; + [_cachedBitmap drawInRect:self.safeAreaRect]; } #pragma mark - Keyboard handling diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm index 6e54bbed40..1812d17ae4 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm @@ -19,7 +19,7 @@ - (instancetype)initWithDocument:(MacPDFDocument*)document { - auto const style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; + auto const style_mask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | NSWindowStyleMaskFullSizeContentView; NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 600, 800) styleMask:style_mask backing:NSBackingStoreBuffered @@ -123,7 +123,10 @@ { // NSToolbarToggleSidebarItemIdentifier sends toggleSidebar: along the responder chain, // which NSSplitViewController conveniently implements. - return @[ NSToolbarToggleSidebarItemIdentifier ]; + return @[ + NSToolbarToggleSidebarItemIdentifier, + NSToolbarSidebarTrackingSeparatorItemIdentifier, + ]; } - (NSToolbarItem*)toolbar:(NSToolbar*)toolbar