macOS: No custom HUD drawing on macOS 10.10+
[vlc.git] / modules / gui / macosx / VLCHUDButtonCell.m
1 //
2 //  VLCHUDButtonCell.m
3 //  BGHUDAppKit
4 //
5 //  Created by BinaryGod on 5/25/08.
6 //
7 //  Copyright (c) 2008, Tim Davis (BinaryMethod.com, binary.god@gmail.com)
8 //  All rights reserved.
9 //
10 //  Redistribution and use in source and binary forms, with or without modification,
11 //  are permitted provided that the following conditions are met:
12 //
13 //              Redistributions of source code must retain the above copyright notice, this
14 //      list of conditions and the following disclaimer.
15 //
16 //              Redistributions in binary form must reproduce the above copyright notice,
17 //      this list of conditions and the following disclaimer in the documentation and/or
18 //      other materials provided with the distribution.
19 //
20 //              Neither the name of the BinaryMethod.com nor the names of its contributors
21 //      may be used to endorse or promote products derived from this software without
22 //      specific prior written permission.
23 //
24 //      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND
25 //      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 //      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 //      IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 //      INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 //      BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
30 //      OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 //      WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 //      ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 //      POSSIBILITY OF SUCH DAMAGE.
34
35 #import "VLCHUDButtonCell.h"
36 #import "CompatibilityFixes.h"
37
38 @implementation VLCHUDButtonCell
39
40
41 + (void)load
42 {
43     /* On 10.10+ we do not want custom drawing, therefore we swap out the implementation
44      * of the selectors below with their original implementations.
45      * Just calling super is not enough, as it would still result in different drawing
46      * due to lack of vibrancy.
47      */
48     if (OSX_YOSEMITE_AND_HIGHER) {
49         swapoutOverride([VLCHUDButtonCell class], @selector(initWithCoder:));
50         swapoutOverride([VLCHUDButtonCell class], @selector(drawBezelWithFrame:inView:));
51         swapoutOverride([VLCHUDButtonCell class], @selector(drawTitle:withFrame:inView:));
52     }
53 }
54
55 - (instancetype)initWithCoder:(NSCoder *)coder
56 {
57     self = [super initWithCoder:coder];
58     if (self) {
59         _disabledGradient  = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
60                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
61         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:0.7f]
62                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:0.7f]];
63         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
64                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
65         _pushedGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
66                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
67         _enabledTextColor  = [NSColor whiteColor];
68         _disabledTextColor = [NSColor grayColor];
69     }
70     return self;
71 }
72
73 - (void)drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
74 {
75     // Set frame to the correct size
76     frame.size.height = self.cellSize.height;
77
78     // Inset rect to have enough room for the stroke
79     frame = NSInsetRect(frame, 1, 1);
80     if (self.bezelStyle == NSRoundRectBezelStyle) {
81         [self drawRoundRectButtonBezelInRect:frame];
82     } else {
83         [super drawBezelWithFrame:frame inView:controlView];
84     }
85 }
86
87 - (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
88 {
89     NSMutableAttributedString *coloredTitle = [[NSMutableAttributedString alloc]
90                                                initWithAttributedString:title];
91     if (self.isEnabled) {
92         [coloredTitle addAttribute:NSForegroundColorAttributeName
93                              value:_enabledTextColor
94                              range:NSMakeRange(0, coloredTitle.length)];
95     } else {
96         [coloredTitle addAttribute:NSForegroundColorAttributeName
97                              value:_disabledTextColor
98                              range:NSMakeRange(0, coloredTitle.length)];
99     }
100
101     return [super drawTitle:coloredTitle withFrame:frame inView:controlView];
102 }
103
104 - (void)drawRoundRectButtonBezelInRect:(NSRect)rect
105 {
106     NSBezierPath *path;
107     if (self.controlSize == NSMiniControlSize) {
108         rect = NSInsetRect(rect, 1.0, 2.0);
109         path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:3.0 yRadius:3.0];
110     } else {
111         path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8.0 yRadius:8.0];
112     }
113     if (self.highlighted) {
114         [_pushedGradient drawInBezierPath:path angle:90.0f];
115     } else if (!self.enabled) {
116         [_disabledGradient drawInBezierPath:path angle:90.0f];
117     } else {
118         [_normalGradient drawInBezierPath:path angle:90.0f];
119     }
120     [[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] setStroke];
121     [path setLineWidth:1.0];
122     [path stroke];
123 }
124
125 @end