Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@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 "IconButton.h" |
| 21 | |
| 22 | #import "NSColor+RingTheme.h" |
| 23 | |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 24 | @interface IconButton() |
| 25 | @property (nonatomic, strong) NSTrackingArea *trackingArea; |
| 26 | @end |
| 27 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 28 | @implementation IconButton |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 29 | @synthesize trackingArea; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 30 | |
| 31 | -(void) awakeFromNib { |
| 32 | if (!self.bgColor) { |
| 33 | self.bgColor = [NSColor ringBlue]; |
| 34 | } |
| 35 | |
| 36 | if (!self.cornerRadius) { |
| 37 | self.cornerRadius = @(NSWidth(self.frame) / 2); |
| 38 | } |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 39 | |
| 40 | if (self.imageInsets == 0) |
Nicolas Jager | eccf8ae | 2016-02-05 16:34:00 -0500 | [diff] [blame] | 41 | self.imageInsets = 8.0f; |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 42 | |
| 43 | self.pressed = NO; |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 44 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | -(void) setPressed:(BOOL)newVal |
| 48 | { |
| 49 | _pressed = newVal; |
| 50 | [self setNeedsDisplay:YES]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | - (void)drawRect:(NSRect)dirtyRect |
| 54 | { |
| 55 | [super drawRect:dirtyRect]; |
| 56 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 57 | NSColor* backgroundColor; |
| 58 | NSColor* backgroundStrokeColor; |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 59 | NSColor* tintColor = self.imageColor ? self.imageColor : [NSColor whiteColor]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 60 | |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 61 | if (self.bgColor == [NSColor clearColor]) { |
| 62 | backgroundColor = self.bgColor ; |
| 63 | backgroundStrokeColor = self.bgColor; |
| 64 | if(!self.isEnabled) { |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame^] | 65 | if (self.buttonDisableColor) { |
| 66 | tintColor = self.buttonDisableColor; |
| 67 | } else { |
| 68 | tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3]; |
| 69 | } |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | else if (!self.isEnabled) { |
| 73 | backgroundColor = [self.bgColor colorWithAlphaComponent:0.7]; |
| 74 | backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7]; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame^] | 75 | if (self.buttonDisableColor) { |
| 76 | tintColor = self.buttonDisableColor; |
| 77 | } else { |
| 78 | tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3]; |
| 79 | } |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 80 | } else if (self.mouseDown || self.isPressed) { |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 81 | if (self.highlightColor) { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 82 | backgroundColor = self.highlightColor; |
| 83 | backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1]; |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 84 | } else { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 85 | backgroundColor = [self.bgColor darkenColorByValue:0.3]; |
| 86 | backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4]; |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 87 | } |
Kateryna Kostiuk | 958cd07 | 2017-07-14 15:56:35 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | else { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 91 | backgroundColor = self.bgColor; |
| 92 | backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame^] | 95 | backgroundStrokeColor = NSColor.clearColor; |
| 96 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 97 | //// Subframes |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 98 | NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5, |
| 99 | NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5, |
| 100 | floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333), |
| 101 | floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333)); |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 102 | |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 103 | //// Group |
| 104 | { |
| 105 | //// Oval Drawing |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 106 | NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect: |
| 107 | NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5), |
| 108 | NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5), |
| 109 | floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5), |
| 110 | floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5)) |
| 111 | xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 112 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 113 | [backgroundColor setFill]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 114 | [ovalPath fill]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 115 | [backgroundStrokeColor setStroke]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 116 | [ovalPath setLineWidth: 0.5]; |
| 117 | [ovalPath stroke]; |
| 118 | |
| 119 | [NSGraphicsContext saveGraphicsState]; |
| 120 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 121 | NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 122 | [path addClip]; |
| 123 | |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 124 | [self setImagePosition:NSImageOverlaps]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 125 | auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets); |
Alexandre Lision | 266fca0 | 2015-09-28 14:47:05 -0400 | [diff] [blame] | 126 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 127 | [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 128 | fromRect:NSZeroRect |
| 129 | operation:NSCompositeSourceOver |
| 130 | fraction:1.0 |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 131 | respectFlipped:YES |
| 132 | hints:nil]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 133 | |
| 134 | [NSGraphicsContext restoreGraphicsState]; |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 135 | |
| 136 | NSRect rect2; |
| 137 | NSDictionary *att = nil; |
| 138 | |
| 139 | NSMutableParagraphStyle *style = |
| 140 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 141 | [style setLineBreakMode:NSLineBreakByWordWrapping]; |
| 142 | [style setAlignment:NSCenterTextAlignment]; |
| 143 | att = [[NSDictionary alloc] initWithObjectsAndKeys: |
| 144 | style, NSParagraphStyleAttributeName, |
| 145 | [NSColor whiteColor], |
| 146 | NSForegroundColorAttributeName, nil]; |
| 147 | |
| 148 | rect.size = [[self title] sizeWithAttributes:att]; |
| 149 | rect.origin.x = floor( NSMidX([self bounds]) - rect.size.width / 2 ); |
| 150 | rect.origin.y = floor( NSMidY([self bounds]) - rect.size.height / 2 ); |
| 151 | [[self title] drawInRect:rect withAttributes:att]; |
Alexandre Lision | 6da08a8 | 2015-09-24 17:09:24 -0400 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | -(void)mouseDown:(NSEvent *)theEvent |
| 156 | { |
| 157 | self.mouseDown = TRUE; |
| 158 | [self setNeedsDisplay:YES]; |
| 159 | |
| 160 | [super mouseDown:theEvent]; |
| 161 | |
| 162 | self.mouseDown = FALSE; |
| 163 | [self setNeedsDisplay:YES]; |
| 164 | } |
| 165 | |
| 166 | @end |