macosx: Correct drawing of Volume slider in RTL mode
[vlc.git] / modules / gui / macosx / VLCVolumeSliderCell.m
1 /*****************************************************************************
2  * VLCVolumeSliderCell.m
3  *****************************************************************************
4  * Copyright (C) 2017 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
25 #import "VLCVolumeSliderCell.h"
26
27 @interface VLCVolumeSliderCell () {
28     BOOL _isRTL;
29 }
30 @end
31
32 @implementation VLCVolumeSliderCell
33
34 - (instancetype)init
35 {
36     self = [super init];
37     if (self) {
38         [self setSliderStyleLight];
39         _isRTL = ([self userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft);
40     }
41     return self;
42 }
43
44 - (instancetype)initWithCoder:(NSCoder *)coder
45 {
46     self = [super initWithCoder:coder];
47     if (self) {
48         [self setSliderStyleLight];
49         _isRTL = ([self userInterfaceLayoutDirection] == NSUserInterfaceLayoutDirectionRightToLeft);
50     }
51     return self;
52 }
53
54 - (void)setSliderStyleLight
55 {
56     // Color Declarations
57     _gradientColor = [NSColor colorWithCalibratedRed: 0.663 green: 0.663 blue: 0.663 alpha: 1];
58     _gradientColor2 = [NSColor colorWithCalibratedRed: 0.749 green: 0.749 blue: 0.753 alpha: 1];
59     _trackStrokeColor = [NSColor colorWithCalibratedRed: 0.619 green: 0.624 blue: 0.623 alpha: 1];
60     _filledTrackColor = [NSColor colorWithCalibratedRed: 0.55 green: 0.55 blue: 0.55 alpha: 1];
61     _knobFillColor = [NSColor colorWithCalibratedRed: 1 green: 1 blue: 1 alpha: 1];
62     _activeKnobFillColor = [NSColor colorWithCalibratedRed: 0.95 green: 0.95 blue: 0.95 alpha: 1];
63     _shadowColor = [NSColor colorWithCalibratedRed: 0.32 green: 0.32 blue: 0.32 alpha: 1];
64     _knobStrokeColor = [NSColor colorWithCalibratedRed: 0.592 green: 0.596 blue: 0.596 alpha: 1];
65
66     // Gradient Declarations
67     _trackGradient = [[NSGradient alloc] initWithColorsAndLocations:
68                       _gradientColor, 0.0,
69                       [_gradientColor blendedColorWithFraction:0.5 ofColor:_gradientColor2], 0.60,
70                       _gradientColor2, 1.0, nil];
71
72     // Shadow Declarations
73     _knobShadow = [[NSShadow alloc] init];
74     _knobShadow.shadowColor = _shadowColor;
75     _knobShadow.shadowOffset = NSMakeSize(0, 0);
76     _knobShadow.shadowBlurRadius = 2;
77
78     _highlightBackground = [NSColor colorWithCalibratedRed:0.20 green:0.55 blue:0.91 alpha:1.0];
79     NSColor *highlightAccent = [NSColor colorWithCalibratedRed:0.4588235294 green:0.7254901961 blue:0.9882352941 alpha:1.0];
80     _highlightGradient = [[NSGradient alloc] initWithColors:@[
81                                                               _highlightBackground,
82                                                               highlightAccent,
83                                                               _highlightBackground
84                                                               ]];
85 }
86
87 - (void)setSliderStyleDark
88 {
89     // Color Declarations
90     _gradientColor = [NSColor colorWithCalibratedRed: 0.24 green: 0.24 blue: 0.24 alpha: 1];
91     _gradientColor2 = [NSColor colorWithCalibratedRed: 0.15 green: 0.15 blue: 0.15 alpha: 1];
92     _trackStrokeColor = [NSColor colorWithCalibratedRed: 0.23 green: 0.23 blue: 0.23 alpha: 1];
93     _filledTrackColor = [NSColor colorWithCalibratedRed: 0.15 green: 0.15 blue: 0.15 alpha: 1];
94     _knobFillColor = [NSColor colorWithCalibratedRed:0.7 green:0.7 blue:0.7 alpha: 1];
95     _activeKnobFillColor = [NSColor colorWithCalibratedRed: 0.95 green: 0.95 blue: 0.95 alpha: 1];
96     _shadowColor = [NSColor colorWithCalibratedRed: 0.32 green: 0.32 blue: 0.32 alpha: 1];
97     _knobStrokeColor = [NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:1];
98
99     NSColor* knobGradientColor = [NSColor colorWithSRGBRed: 0.15 green: 0.15 blue: 0.15 alpha: 1];
100     NSColor* knobGradientColor2 = [NSColor colorWithSRGBRed: 0.30 green: 0.30 blue: 0.30 alpha: 1];
101
102     // Gradient Declarations
103     _trackGradient = [[NSGradient alloc] initWithColorsAndLocations:
104                       _gradientColor, 0.0,
105                       [_gradientColor blendedColorWithFraction:0.5 ofColor:_gradientColor2], 0.60,
106                       _gradientColor2, 1.0, nil];
107
108     _knobGradient = [[NSGradient alloc] initWithStartingColor:knobGradientColor
109                                                   endingColor:knobGradientColor2];
110
111
112     // Shadow Declarations
113     _knobShadow = [[NSShadow alloc] init];
114     _knobShadow.shadowColor = _shadowColor;
115     _knobShadow.shadowOffset = NSMakeSize(0, 0);
116     _knobShadow.shadowBlurRadius = 2;
117
118     _highlightBackground = [NSColor colorWithCalibratedRed:0.20 green:0.55 blue:0.91 alpha:1.0];
119     NSColor *highlightAccent = [NSColor colorWithCalibratedRed:0.4588235294 green:0.7254901961 blue:0.9882352941 alpha:1.0];
120     _highlightGradient = [[NSGradient alloc] initWithColors:@[
121                                                               _highlightBackground,
122                                                               highlightAccent,
123                                                               _highlightBackground
124                                                               ]];
125 }
126
127 - (void)drawKnob:(NSRect)knobRect
128 {
129     // Draw knob
130     NSBezierPath* knobPath = [NSBezierPath bezierPathWithOvalInRect:NSInsetRect(knobRect, 5.0, 5.0)];
131     if (self.isHighlighted) {
132         if (_knobGradient) {
133             [_knobGradient drawInBezierPath:knobPath angle:270];
134         } else {
135             [_activeKnobFillColor setFill];
136         }
137     } else {
138         if (_knobGradient) {
139             [_knobGradient drawInBezierPath:knobPath angle:90];
140         } else {
141             [_knobFillColor setFill];
142         }
143     }
144
145     if (!_knobGradient)
146         [knobPath fill];
147
148     [_knobStrokeColor setStroke];
149     knobPath.lineWidth = 0.5;
150
151     [NSGraphicsContext saveGraphicsState];
152     if (self.isHighlighted)
153         [_knobShadow set];
154     [knobPath stroke];
155     [NSGraphicsContext restoreGraphicsState];
156 }
157
158 - (double)myNormalizedDouble
159 {
160     double min = [self minValue];
161     double max = [self maxValue];
162     double current = [self doubleValue];
163
164     max -= min;
165     current -= min;
166
167     return current / max;
168 }
169
170 - (NSRect)knobRectFlipped:(BOOL)flipped
171 {
172     double val = [self myNormalizedDouble];
173     NSRect rect = NSMakeRect((NSWidth(_trackRect) - self.knobThickness) * val, -1, self.knobThickness, self.knobThickness);
174     return [[self controlView] backingAlignedRect:rect options:NSAlignAllEdgesNearest];
175 }
176
177 - (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped
178 {
179     // Inset rect
180     rect = NSInsetRect(rect, 1.0, 1.0);
181
182     // Empty Track Drawing
183     NSBezierPath* emptyTrackPath = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:1 yRadius:1];
184
185     // Calculate filled track
186     NSRect leadingTrackRect = rect;
187     NSRect knobRect = [self knobRectFlipped:NO];
188     CGFloat sliderCenter = knobRect.origin.x  + (self.knobThickness / 2);
189
190     leadingTrackRect.size.width = sliderCenter;
191
192     // Filled Track Drawing
193     CGFloat leadingTrackCornerRadius = 2;
194     NSBezierPath* leadingTrackPath = [NSBezierPath bezierPathWithRoundedRect:leadingTrackRect
195                                                                      xRadius:leadingTrackCornerRadius
196                                                                      yRadius:leadingTrackCornerRadius];
197
198     if (_isRTL) {
199         // In RTL mode, first fill the whole slider,
200         // then only redraw the empty part.
201
202         // Empty part drawing
203         [_filledTrackColor setFill];
204         [emptyTrackPath fill];
205
206         // Filled part drawing
207         [_trackGradient drawInBezierPath:leadingTrackPath angle:-90];
208     } else {
209         // Empty part drawing
210         [_trackGradient drawInBezierPath:emptyTrackPath angle:-90];
211
212         // Filled part drawing
213         [_filledTrackColor setFill];
214         [leadingTrackPath fill];
215     }
216
217     [_trackStrokeColor setStroke];
218     emptyTrackPath.lineWidth = 1;
219     [emptyTrackPath stroke];
220 }
221
222 @end