kkostiuk | 3a2c9f7 | 2021-01-27 10:30:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | |
| 20 | #import "NSColor+RingTheme.h" |
| 21 | #import "CustomBackgroundView.h" |
| 22 | |
| 23 | #import <QuartzCore/QuartzCore.h> |
| 24 | |
| 25 | @implementation CustomBackgroundView |
| 26 | |
| 27 | |
| 28 | - (void)drawRect:(NSRect)dirtyRect |
| 29 | { |
| 30 | [super drawRect:dirtyRect]; |
| 31 | NSColor *backgroundColor = self.backgroundColor ? self.backgroundColor : [NSColor colorWithCalibratedRed: 0 green: 0 blue: 0 alpha: 0.8]; |
| 32 | NSColor *backgroundStrokeColor = [NSColor clearColor]; |
| 33 | NSColor *tintColor = self.imageColor ? self.imageColor : [NSColor whiteColor]; |
| 34 | |
| 35 | NSBezierPath* path; |
| 36 | |
| 37 | switch (self.backgroundType) { |
| 38 | case RECTANGLE: { |
| 39 | path = [NSBezierPath bezierPathWithRoundedRect:dirtyRect xRadius:0 yRadius:0]; |
| 40 | break; |
| 41 | } |
| 42 | case RECTANGLE_WITH_ROUNDED_RIGHT_CORNER: { |
| 43 | path = [[NSBezierPath alloc] init]; |
| 44 | NSPoint bottomLeft = dirtyRect.origin; |
| 45 | NSPoint topLeft = CGPointMake(dirtyRect.origin.x, dirtyRect.size.height); |
| 46 | NSPoint topRight = CGPointMake(dirtyRect.size.width, dirtyRect.size.height); |
| 47 | NSPoint bottomRight = CGPointMake(dirtyRect.size.width * 0.5, dirtyRect.origin.y); |
| 48 | NSPoint middle = CGPointMake(dirtyRect.size.width, dirtyRect.size.height * 0.5); |
| 49 | NSPoint controlPoint1 = CGPointMake(dirtyRect.size.width * 0.96, dirtyRect.size.height * 0.01); |
| 50 | NSPoint controlPoint2 = CGPointMake(dirtyRect.size.width * 0.98, dirtyRect.size.height * 0.2); |
| 51 | [path setLineWidth:1.0]; |
| 52 | [path moveToPoint:topLeft]; |
| 53 | [path lineToPoint:bottomLeft]; |
| 54 | |
| 55 | [path lineToPoint:bottomRight]; |
| 56 | [path curveToPoint:middle controlPoint1:controlPoint1 controlPoint2:controlPoint2]; |
| 57 | [path lineToPoint:topRight]; |
| 58 | [path closePath]; |
| 59 | break; |
| 60 | } |
| 61 | case CUSP: { |
| 62 | path = [[NSBezierPath alloc] init]; |
| 63 | NSPoint bottomLeft = CGPointMake(dirtyRect.origin.x, dirtyRect.size.height * 0.5); |
| 64 | NSPoint topLeft = CGPointMake(dirtyRect.origin.x, dirtyRect.size.height); |
| 65 | NSPoint topRight = CGPointMake(dirtyRect.size.width, dirtyRect.size.height); |
| 66 | NSPoint controlPoint1 = CGPointMake(dirtyRect.size.width * 0.1, dirtyRect.size.height * 0.7); |
| 67 | NSPoint controlPoint2 = CGPointMake(dirtyRect.size.width * 0.01, dirtyRect.size.height * 0.9); |
| 68 | [path setLineWidth:1.0]; |
| 69 | [path moveToPoint:topLeft]; |
| 70 | [path lineToPoint:bottomLeft]; |
| 71 | |
| 72 | [path curveToPoint:topRight controlPoint1:controlPoint1 controlPoint2:controlPoint2]; |
| 73 | [path closePath]; |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | [backgroundColor set]; |
| 79 | [path fill]; |
| 80 | [[NSColor clearColor] set]; |
| 81 | [path stroke]; |
| 82 | |
| 83 | if (self.backgroundType == CUSP) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | NSRect rectImage = NSInsetRect(dirtyRect, 4, 4); |
| 88 | rectImage.size.width = rectImage.size.height; |
| 89 | |
| 90 | [[NSColor image: self.image tintedWithColor:tintColor] drawInRect:rectImage |
| 91 | fromRect:NSZeroRect |
| 92 | operation:NSCompositeSourceOver |
| 93 | fraction:1.0 |
| 94 | respectFlipped:YES |
| 95 | hints:nil]; |
| 96 | } |
| 97 | |
| 98 | @end |