浏览代码

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
共有 2 个文件被更改,包括 25 次插入4 次删除
  1. 20 2
      Meta/Lagom/Contrib/MacPDF/MacPDFView.mm
  2. 5 2
      Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm

+ 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);
     _doc = move(doc);
     _page_index = 0;
     _page_index = 0;
 
 
+    [self addObserver:self
+           forKeyPath:@"safeAreaRect"
+              options:NSKeyValueObservingOptionNew
+              context:nil];
+
     [self invalidateCachedBitmap];
     [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
 - (void)goToPage:(int)page
 {
 {
     if (!_doc)
     if (!_doc)
@@ -107,7 +125,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
     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.safeAreaRect.size];
     if (NSEqualSizes([_cachedBitmap size], pixel_size))
     if (NSEqualSizes([_cachedBitmap size], pixel_size))
         return;
         return;
 
 
@@ -118,7 +136,7 @@ static NSBitmapImageRep* ns_from_gfx(NonnullRefPtr<Gfx::Bitmap> bitmap_p)
 - (void)drawRect:(NSRect)rect
 - (void)drawRect:(NSRect)rect
 {
 {
     [self ensureCachedBitmapIsUpToDate];
     [self ensureCachedBitmapIsUpToDate];
-    [_cachedBitmap drawInRect:self.bounds];
+    [_cachedBitmap drawInRect:self.safeAreaRect];
 }
 }
 
 
 #pragma mark - Keyboard handling
 #pragma mark - Keyboard handling

+ 5 - 2
Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm

@@ -19,7 +19,7 @@
 
 
 - (instancetype)initWithDocument:(MacPDFDocument*)document
 - (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)
     NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 600, 800)
                                                    styleMask:style_mask
                                                    styleMask:style_mask
                                                      backing:NSBackingStoreBuffered
                                                      backing:NSBackingStoreBuffered
@@ -123,7 +123,10 @@
 {
 {
     // NSToolbarToggleSidebarItemIdentifier sends toggleSidebar: along the responder chain,
     // NSToolbarToggleSidebarItemIdentifier sends toggleSidebar: along the responder chain,
     // which NSSplitViewController conveniently implements.
     // which NSSplitViewController conveniently implements.
-    return @[ NSToolbarToggleSidebarItemIdentifier ];
+    return @[
+        NSToolbarToggleSidebarItemIdentifier,
+        NSToolbarSidebarTrackingSeparatorItemIdentifier,
+    ];
 }
 }
 
 
 - (NSToolbarItem*)toolbar:(NSToolbar*)toolbar
 - (NSToolbarItem*)toolbar:(NSToolbar*)toolbar