summaryrefslogtreecommitdiffstats
path: root/macosx/DockTextField.m
blob: 5ce10da5e71f4222eec49f5d48f43a95d7df2399 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*  DockTextField.m $
 
 This file is part of the HandBrake source code.
 Homepage: <http://handbrake.fr/>.
 It may be used under the terms of the GNU General Public License. */

#import "DockTextField.h"

#define DOCK_TEXTFIELD_ALPHA 0.8
#define DOCK_TEXTFIELD_FONTSIZE 28.0

@implementation DockTextField

@synthesize textToDisplay = _textToDisplay;
@synthesize startColor = _startColor;
@synthesize endColor = _endColor;

- (instancetype)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [[self cell] setBezelStyle:NSRoundedBezelStyle];
        _textToDisplay = @"";
        [self changeGradientColors:[NSColor grayColor] endColor:[NSColor blackColor]];
    }
    
    return self;
}

- (void)changeGradientColors:(NSColor*)startColor endColor:(NSColor*)endColor
{
    self.startColor = [startColor colorWithAlphaComponent:DOCK_TEXTFIELD_ALPHA];
    self.endColor = [endColor colorWithAlphaComponent:DOCK_TEXTFIELD_ALPHA];
}

- (void)drawRect:(NSRect)dirtyRect
{
    if (self.isHidden)
        return;

    NSFont *font;
    if ([[NSFont class] respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
        // On macOS 10.11+ the monospaced digit system is available.
        font = [NSFont monospacedDigitSystemFontOfSize:DOCK_TEXTFIELD_FONTSIZE weight:NSFontWeightBold];
    } else {
        // macOS 10.10- use the default system font.
        font = [NSFont boldSystemFontOfSize:DOCK_TEXTFIELD_FONTSIZE];
    }

    NSRect blackOutlineFrame = NSMakeRect(0.0, 0.0, [self bounds].size.width, [self bounds].size.height-1.0);
    double radius = self.bounds.size.height / 2;

    NSGradient *gradient = [[NSGradient alloc] initWithStartingColor:self.startColor endingColor:self.endColor];
    [gradient drawInBezierPath:[NSBezierPath bezierPathWithRoundedRect:blackOutlineFrame xRadius:radius yRadius:radius] angle:90];
    
    NSMutableDictionary *drawStringAttributes = [[NSMutableDictionary alloc] init];
	[drawStringAttributes setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
    [drawStringAttributes setValue:font forKey:NSFontAttributeName];
	NSShadow *stringShadow = [[NSShadow alloc] init];
	[stringShadow setShadowColor:[NSColor blackColor]];
	NSSize shadowSize;
	shadowSize.width = 2;
	shadowSize.height = -2;
	[stringShadow setShadowOffset:shadowSize];
	[stringShadow setShadowBlurRadius:6];
	[drawStringAttributes setValue:stringShadow forKey:NSShadowAttributeName];
	
    NSString *MRString = _textToDisplay;
	NSString *budgetString = [NSString stringWithFormat:@"%@", MRString];
	NSSize stringSize = [budgetString sizeWithAttributes:drawStringAttributes];
	NSPoint centerPoint;
	centerPoint.x = (dirtyRect.size.width / 2) - (stringSize.width / 2);
	centerPoint.y = dirtyRect.size.height / 2 - (stringSize.height / 2) - 2;
	[budgetString drawAtPoint:centerPoint withAttributes:drawStringAttributes];
}

@end