blob: e557b6f3f5584d29404ab0b9fadda267ad67d179 [file] [log] [blame]
Kateryna Kostiukd9039e92017-05-24 14:29:54 -04001/*
2 * Copyright (C) 2015-2017 Savoir-faire Linux Inc.
3 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19#import "RoundedTextField.h"
20#import "NSColor+RingTheme.h"
21
22@implementation RoundedTextField
23
24-(void) awakeFromNib {
25 if (!self.bgColor) {
26 self.bgColor = [NSColor controlColor];
27 }
28
29 if (!self.cornerRadius) {
30 self.cornerRadius = @(NSWidth(self.frame) / 2);
31 }
32
33 if(!self.borderColor) {
34 self.borderColor = [self.bgColor darkenColorByValue:0.1];
35 }
36
37 self.backgroundColor = [NSColor controlColor];
38}
39
40- (void)drawRect:(NSRect)dirtyRect {
41 [super drawRect:dirtyRect];
42
43 NSColor* backgroundColor = self.bgColor;
44 NSColor* borderColor = self.borderColor;
45
Kateryna Kostiukc14b5842017-06-14 16:31:21 -040046 NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5,
47 NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5,
48 floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333),
49 floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333));
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040050
Kateryna Kostiukc14b5842017-06-14 16:31:21 -040051 NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect:
52 NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5),
53 NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5),
54 floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5),
55 floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5))
56 xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040057 [backgroundColor setFill];
58 [ovalPath fill];
59 [borderColor setStroke];
Kateryna Kostiuk2efc5a92018-04-05 15:01:18 -040060 [ovalPath setLineWidth: 1.0];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040061 [ovalPath stroke];
62 NSDictionary *att = nil;
63
64 NSMutableParagraphStyle *style =
65 [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
66 [style setLineBreakMode:NSLineBreakByWordWrapping];
67 [style setAlignment:NSCenterTextAlignment];
Kateryna Kostiuk2efc5a92018-04-05 15:01:18 -040068 NSFont *font = [NSFont systemFontOfSize:10.0];
69
70 if (self.stringValue.length > 1) {
71 font = [NSFont systemFontOfSize:8.0];
72 }
73 if (self.stringValue.length > 2) {
74 font = [NSFont systemFontOfSize:6.0];
75 }
76
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040077 att = [[NSDictionary alloc] initWithObjectsAndKeys:
Kateryna Kostiuk2efc5a92018-04-05 15:01:18 -040078 font,NSFontAttributeName,
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040079 style, NSParagraphStyleAttributeName,
80 [self textColor],
81 NSForegroundColorAttributeName, nil];
Kateryna Kostiuk2efc5a92018-04-05 15:01:18 -040082 NSAttributedString *attrString =
83 [[NSAttributedString alloc] initWithString:[self stringValue]
84 attributes:att];
85 CGFloat stringHeight = attrString.size.height;
86 CGFloat originY = (group.size.height - stringHeight) / 2;
87 NSRect titleRect = CGRectMake(group.origin.x, originY, group.size.width, group.size.height);
88 [[self stringValue] drawInRect:titleRect withAttributes:att];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040089}
90
91@end