From 4ba02a87ed2e785f598d859d588b3aee3255e7a5 Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Sat, 1 Dec 2018 16:22:57 -0800 Subject: [PATCH] Fix scrollbars in Mojave dark mode not rendering properly Dark mode scrollbars's background are rendered in a translucent color so that it would overlay on top of the background nearly, even though we are using legacy scrollbars with a dedicated space. This is unlike light mode scrollbars which render with a concrete color. This means if the background has uncleared rendering it would result in some oddities. Just fix it by making sure we first render the scroll track with the current background color before drawing the system overlay. Known issues: - The scroll knob will look quite light and hard to see if the background color is bright. Consider this OK for now as users who use dark mode will likely have a dark-ish background color in Vim. - If both vertical and horizontal scrollbars are enabled, the corners will look black or sometimes filled with rendering artifacts. Will fix this later. One way to fix is to always fill the Vim view with a background color but that seems to slow things down a little bit because setNeedsDisplay seems to be called too much without a rect, so need to fix that first. - Vertical scrollbars' positions are sometimes set incorrectly. That's a separate bug and will be addressed later. - MacVim currently doesn't support overlay scrollbars which have been introduced in macOS since 10.7. Investigate that option too. --- src/MacVim/MMVimView.m | 51 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/MacVim/MMVimView.m b/src/MacVim/MMVimView.m index 16765ff972..fa5baeb759 100644 --- a/src/MacVim/MMVimView.m +++ b/src/MacVim/MMVimView.m @@ -494,6 +494,23 @@ enum { - (void)setDefaultColorsBackground:(NSColor *)back foreground:(NSColor *)fore { [textView setDefaultColorsBackground:back foreground:fore]; + + CALayer *backedLayer = [self layer]; + if (backedLayer) { + // This would only trigger in 10.14 where all views are layer-backed. + // + // Note: This doesn't do much now. Should fix this class to use + // updateLayer: instead of drawRect: at a later time, which would draw + // the background color automatically. When we do that we can remove the + // hack at drawKnobSlotInRect: since it would overlay properly without + // needing to manually draw the background color itself. + [backedLayer setBackgroundColor:[back CGColor]]; + } + + for (NSUInteger i = 0, count = [scrollbars count]; i < count; ++i) { + MMScroller *sb = [scrollbars objectAtIndex:i]; + [sb setNeedsDisplay:YES]; + } } @@ -801,10 +818,15 @@ enum { } } - // HACK: If there is no bottom or right scrollbar the resize indicator will - // cover the bottom-right corner of the text view so tell NSWindow not to - // draw it in this situation. - [[self window] setShowsResizeIndicator:(rightSbVisible||botSbVisible)]; + if (NSAppKitVersionNumber < NSAppKitVersionNumber10_7) { + // HACK: If there is no bottom or right scrollbar the resize indicator will + // cover the bottom-right corner of the text view so tell NSWindow not to + // draw it in this situation. + // + // Note: This API is ignored from 10.7 onward and is now deprecated. This + // should be removed if we want to drop support for 10.6. + [[self window] setShowsResizeIndicator:(rightSbVisible||botSbVisible)]; + } } - (NSUInteger)representedIndexOfTabViewItem:(NSTabViewItem *)tvi @@ -973,6 +995,27 @@ enum { return self; } +- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag +{ + // Dark mode scrollbars draw a translucent knob slot overlaid on top of + // whatever background the view has, even when we are using legacy + // scrollbars with a dedicated space. This means we need to draw the + // background with some colors first, or else it would look really black, or + // show through rendering artifacts (e.g. if guioption 'k' is on, and you + // turn off the bar bar, the artiacts will show through in the overlay). + // + // Note: This should ideally be done on MMVimView itself by setting a background + // color. This would be fixed at a later time by telling the view to just + // use the background color form the backed CALayer (mandated since Mojave + // 10.14). + MMVimView *vimView = [self target]; + NSColor *defaultBackgroundColor = [[vimView textView] defaultBackgroundColor]; + [defaultBackgroundColor setFill]; + NSRectFill(slotRect); + + [super drawKnobSlotInRect:slotRect highlight:flag]; +} + - (int32_t)scrollerId { return identifier;