78c234c461aefbca929d4dc39a84e18e2729f794
[vlc.git] / modules / gui / macosx / VLCHUDButtonCell.m
1 /*****************************************************************************
2  * VLCHUDButtonCell.m: Custom button cell UI for dark HUD Panels
3  *****************************************************************************
4  * Copyright (C) 2016 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Marvin Scholz <epirat07 -at- gmail -dot- com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #import "VLCHUDButtonCell.h"
25
26 @implementation VLCHUDButtonCell
27
28 - (instancetype) initWithCoder:(NSCoder *)coder
29 {
30     self = [super initWithCoder:coder];
31     if (self) {
32         _disabledGradient  = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:1.0f]
33                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:1.0f]];
34         _normalGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.251f green:0.251f blue:0.255f alpha:0.7f]
35                                                            endingColor:[NSColor colorWithDeviceRed:0.118f green:0.118f blue:0.118f alpha:0.7f]];
36         _highlightGradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
37                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
38         _pushedGradient    = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithDeviceRed:0.451f green:0.451f blue:0.455f alpha:1.0f]
39                                                            endingColor:[NSColor colorWithDeviceRed:0.318f green:0.318f blue:0.318f alpha:1.0f]];
40         _enabledTextColor  = [NSColor whiteColor];
41         _disabledTextColor = [NSColor grayColor];
42     }
43     return self;
44 }
45
46 - (void) drawBezelWithFrame:(NSRect)frame inView:(NSView *)controlView
47 {
48     // Set frame to the correct size
49     frame.size.height = self.cellSize.height;
50
51     // Inset rect to have enough room for the stroke
52     frame = NSInsetRect(frame, 1, 1);
53     if (self.bezelStyle == NSRoundRectBezelStyle) {
54         [self drawRoundRectButtonBezelInRect:frame];
55     } else {
56         [super drawBezelWithFrame:frame inView:controlView];
57     }
58 }
59
60 - (NSRect)drawTitle:(NSAttributedString *)title withFrame:(NSRect)frame inView:(NSView *)controlView
61 {
62     NSMutableAttributedString *coloredTitle = [[NSMutableAttributedString alloc]
63                                                initWithAttributedString:title];
64     if (self.isEnabled) {
65         [coloredTitle addAttribute:NSForegroundColorAttributeName
66                              value:_enabledTextColor
67                              range:NSMakeRange(0, coloredTitle.length)];
68     } else {
69         [coloredTitle addAttribute:NSForegroundColorAttributeName
70                              value:_disabledTextColor
71                              range:NSMakeRange(0, coloredTitle.length)];
72     }
73
74     return [super drawTitle:coloredTitle withFrame:frame inView:controlView];
75 }
76
77 - (void) drawRoundRectButtonBezelInRect:(NSRect)rect
78 {
79     NSBezierPath *path;
80     if (self.controlSize == NSMiniControlSize) {
81         rect = NSInsetRect(rect, 1.0, 2.0);
82         path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:3.0 yRadius:3.0];
83     } else {
84         path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8.0 yRadius:8.0];
85     }
86     if (self.highlighted) {
87         [_pushedGradient drawInBezierPath:path angle:90.0f];
88     } else if (!self.enabled) {
89         [_disabledGradient drawInBezierPath:path angle:90.0f];
90     } else {
91         [_normalGradient drawInBezierPath:path angle:90.0f];
92     }
93     [[NSColor colorWithCalibratedWhite:1.0 alpha:1.0] setStroke];
94     [path setLineWidth:1.0];
95     [path stroke];
96 }
97
98 @end