blob: 96c3271cff39b47a44142828a61af833b0d353e7 [file] [log] [blame]
Alexandre Lision6da08a82015-09-24 17:09:24 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision6da08a82015-09-24 17:09:24 -04003 * 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 Kostiuk958cd072017-07-14 15:56:35 -040024@interface IconButton()
25@property (nonatomic, strong) NSTrackingArea *trackingArea;
26@end
27
Alexandre Lision6da08a82015-09-24 17:09:24 -040028@implementation IconButton
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040029@synthesize trackingArea;
Alexandre Lision6da08a82015-09-24 17:09:24 -040030
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 Lision266fca02015-09-28 14:47:05 -040039
40 if (self.imageInsets == 0)
Nicolas Jagereccf8ae2016-02-05 16:34:00 -050041 self.imageInsets = 8.0f;
Alexandre Lision4baba4c2016-02-11 13:00:57 -050042
43 self.pressed = NO;
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040044
Alexandre Lision4baba4c2016-02-11 13:00:57 -050045}
46
47-(void) setPressed:(BOOL)newVal
48{
49 _pressed = newVal;
50 [self setNeedsDisplay:YES];
Alexandre Lision6da08a82015-09-24 17:09:24 -040051}
52
53- (void)drawRect:(NSRect)dirtyRect
54{
55 [super drawRect:dirtyRect];
56
Alexandre Lision0f66bd32016-01-18 11:30:45 -050057 NSColor* backgroundColor;
58 NSColor* backgroundStrokeColor;
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040059 NSColor* tintColor = self.imageColor ? self.imageColor : [NSColor whiteColor];
Alexandre Lision6da08a82015-09-24 17:09:24 -040060
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040061 if (self.bgColor == [NSColor clearColor]) {
62 backgroundColor = self.bgColor ;
63 backgroundStrokeColor = self.bgColor;
64 if(!self.isEnabled) {
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040065 if (self.buttonDisableColor) {
66 tintColor = self.buttonDisableColor;
67 } else {
68 tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3];
69 }
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040070 }
71 }
72 else if (!self.isEnabled) {
73 backgroundColor = [self.bgColor colorWithAlphaComponent:0.7];
74 backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040075 if (self.buttonDisableColor) {
76 tintColor = self.buttonDisableColor;
77 } else {
78 tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3];
79 }
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040080 } else if (self.mouseDown || self.isPressed) {
Alexandre Lision266fca02015-09-28 14:47:05 -040081 if (self.highlightColor) {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050082 backgroundColor = self.highlightColor;
83 backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1];
Alexandre Lision266fca02015-09-28 14:47:05 -040084 } else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050085 backgroundColor = [self.bgColor darkenColorByValue:0.3];
86 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4];
Alexandre Lision266fca02015-09-28 14:47:05 -040087 }
Kateryna Kostiuk958cd072017-07-14 15:56:35 -040088 }
89
90 else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050091 backgroundColor = self.bgColor;
92 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1];
Alexandre Lision6da08a82015-09-24 17:09:24 -040093 }
94
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040095 backgroundStrokeColor = NSColor.clearColor;
96
Alexandre Lision6da08a82015-09-24 17:09:24 -040097 //// Subframes
Alexandre Lision0f66bd32016-01-18 11:30:45 -050098 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 Lision6da08a82015-09-24 17:09:24 -0400102
Alexandre Lision6da08a82015-09-24 17:09:24 -0400103 //// Group
104 {
105 //// Oval Drawing
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500106 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 Lision6da08a82015-09-24 17:09:24 -0400112
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500113 [backgroundColor setFill];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400114 [ovalPath fill];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500115 [backgroundStrokeColor setStroke];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400116 [ovalPath setLineWidth: 0.5];
117 [ovalPath stroke];
118
119 [NSGraphicsContext saveGraphicsState];
120
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500121 NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400122 [path addClip];
123
Alexandre Lision62005312016-01-28 15:55:16 -0500124 [self setImagePosition:NSImageOverlaps];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500125 auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets);
Alexandre Lision266fca02015-09-28 14:47:05 -0400126
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500127 [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect
Alexandre Lision6da08a82015-09-24 17:09:24 -0400128 fromRect:NSZeroRect
129 operation:NSCompositeSourceOver
130 fraction:1.0
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500131 respectFlipped:YES
132 hints:nil];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400133
134 [NSGraphicsContext restoreGraphicsState];
Alexandre Lision62005312016-01-28 15:55:16 -0500135
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 Lision6da08a82015-09-24 17:09:24 -0400152 }
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