macOS: Don't draw custom VLCHUDCheckboxCell on 10.10+
[vlc.git] / modules / gui / macosx / VLCHUDCheckboxCell.m
index c9b7496..b9e8c93 100644 (file)
 //     POSSIBILITY OF SUCH DAMAGE.
 
 #import "VLCHUDCheckboxCell.h"
+#import "CompatibilityFixes.h"
 
 @implementation VLCHUDCheckboxCell
 
-- (instancetype) initWithCoder:(NSCoder *)coder
++ (void)load
+{
+    /* On 10.10+ we do not want custom drawing, therefore we swap out the implementation
+     * of the selectors below with their original implementations.
+     * Just calling super will not work, as the AppKit implementation for the NSButton
+     * checkbox checks if the drawing related selectors below are overriden, and if
+     * that is the case, will fall back to legacy drawing, without animation,
+     * without vibrancy and non-layer-based.
+     */
+    if (OSX_YOSEMITE_AND_HIGHER) {
+        swapoutOverride([VLCHUDCheckboxCell class], @selector(initWithCoder:));
+        swapoutOverride([VLCHUDCheckboxCell class], @selector(drawImage:withFrame:inView:));
+        swapoutOverride([VLCHUDCheckboxCell class], @selector(drawTitle:withFrame:inView:));
+    }
+}
+
+- (instancetype)initWithCoder:(NSCoder *)coder
 {
     self = [super initWithCoder:coder];
     if (self) {
+        /* Colors */
+        _cellTextColor         = [NSColor whiteColor];
+        _disabledCellTextColor = [NSColor colorWithCalibratedWhite:1.0f alpha:0.5f];
+        _strokeColor           = [NSColor whiteColor];
+        _disabledStrokeColor   = [NSColor colorWithCalibratedWhite:1.0f alpha:0.5f];
+
+        /* Gradients */
         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
@@ -50,7 +74,7 @@
     return self;
 }
 
-- (void) drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
+- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
 {
     // Set frame size correctly
     NSRect backgroundFrame = frame;
         } else {
             [_normalGradient drawInBezierPath:backgroundPath angle:90.0];
         }
-        [[NSColor whiteColor] setStroke];
+        [_strokeColor setStroke];
     } else {
-        [[NSColor colorWithCalibratedWhite:0.25 alpha:1.0] setStroke];
+        [_disabledStrokeColor setStroke];
     }
 
+    [NSGraphicsContext saveGraphicsState];
+    if ([super showsFirstResponder] && [[[self controlView] window] isKeyWindow] &&
+       ([self focusRingType] == NSFocusRingTypeDefault ||
+        [self focusRingType] == NSFocusRingTypeExterior)) {
+        NSSetFocusRingStyle(NSFocusRingOnly);
+    }
     [backgroundPath setLineWidth:1.0];
     [backgroundPath stroke];
+    [NSGraphicsContext restoreGraphicsState];
 
     // Now drawing tick
     if ([self intValue]) {
         [bezierPath setLineWidth: 1.5];
 
         if([self isEnabled]) {
-            [[NSColor whiteColor] setStroke];
+            [_strokeColor setStroke];
         } else {
-            [[NSColor colorWithCalibratedWhite:0.25 alpha:1.0] setStroke];
+            [_disabledStrokeColor setStroke];
         }
         [bezierPath stroke];
     }
 }
 
+- (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
+{
+    NSMutableAttributedString *newTitle = [title mutableCopy];
+
+    if([self isEnabled]) {
+        [newTitle addAttribute:NSForegroundColorAttributeName
+                         value:_cellTextColor
+                         range:NSMakeRange(0, [newTitle length])];
+    } else {
+        [newTitle addAttribute:NSForegroundColorAttributeName
+                         value:_disabledCellTextColor
+                         range:NSMakeRange(0, [newTitle length])];
+    }
+
+    return [super drawTitle: newTitle withFrame: frame inView: controlView];
+}
+
 @end