ソースを参照

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.
Nico Weber 1 年間 前
コミット
f02b84d34d

+ 20 - 2
Meta/Lagom/Contrib/MacPDF/MacPDFView.mm

@@ -66,9 +66,27 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> 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<NSKeyValueChangeKey, id>*)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<Gfx::Bitmap> 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<Gfx::Bitmap> bitmap_p)
 - (void)drawRect:(NSRect)rect
 {
     [self ensureCachedBitmapIsUpToDate];
-    [_cachedBitmap drawInRect:self.bounds];
+    [_cachedBitmap drawInRect:self.safeAreaRect];
 }
 
 #pragma mark - Keyboard handling

+ 5 - 2
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