blob: 5ce29ef217360d2b64f9233129652899014ac085 [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
Alexandre Lision6da08a82015-09-24 17:09:24 -040024@implementation IconButton
25
26-(void) awakeFromNib {
27 if (!self.bgColor) {
28 self.bgColor = [NSColor ringBlue];
29 }
30
31 if (!self.cornerRadius) {
32 self.cornerRadius = @(NSWidth(self.frame) / 2);
33 }
Alexandre Lision266fca02015-09-28 14:47:05 -040034
35 if (self.imageInsets == 0)
Nicolas Jagereccf8ae2016-02-05 16:34:00 -050036 self.imageInsets = 8.0f;
Alexandre Lision4baba4c2016-02-11 13:00:57 -050037
38 self.pressed = NO;
39}
40
41-(void) setPressed:(BOOL)newVal
42{
43 _pressed = newVal;
44 [self setNeedsDisplay:YES];
Alexandre Lision6da08a82015-09-24 17:09:24 -040045}
46
47- (void)drawRect:(NSRect)dirtyRect
48{
49 [super drawRect:dirtyRect];
50
Alexandre Lision0f66bd32016-01-18 11:30:45 -050051 NSColor* backgroundColor;
52 NSColor* backgroundStrokeColor;
53 NSColor* tintColor = [NSColor whiteColor];
Alexandre Lision6da08a82015-09-24 17:09:24 -040054
Alexandre Lision4baba4c2016-02-11 13:00:57 -050055 if (self.mouseDown || self.isPressed) {
Alexandre Lision266fca02015-09-28 14:47:05 -040056 if (self.highlightColor) {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050057 backgroundColor = self.highlightColor;
58 backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1];
Alexandre Lision266fca02015-09-28 14:47:05 -040059 } else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050060 backgroundColor = [self.bgColor darkenColorByValue:0.3];
61 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4];
Alexandre Lision266fca02015-09-28 14:47:05 -040062 }
Alexandre Lision0f66bd32016-01-18 11:30:45 -050063 } else if (!self.isEnabled) {
64 backgroundColor = [self.bgColor colorWithAlphaComponent:0.7];
65 backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7];
66 tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3];
Alexandre Lision6da08a82015-09-24 17:09:24 -040067 } else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050068 backgroundColor = self.bgColor;
69 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1];
Alexandre Lision6da08a82015-09-24 17:09:24 -040070 }
71
72 //// Subframes
Alexandre Lision0f66bd32016-01-18 11:30:45 -050073 NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5,
74 NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5,
75 floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333),
76 floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333));
Alexandre Lision6da08a82015-09-24 17:09:24 -040077
Alexandre Lision6da08a82015-09-24 17:09:24 -040078 //// Group
79 {
80 //// Oval Drawing
Alexandre Lision0f66bd32016-01-18 11:30:45 -050081 NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect:
82 NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5),
83 NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5),
84 floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5),
85 floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5))
86 xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]];
Alexandre Lision6da08a82015-09-24 17:09:24 -040087
Alexandre Lision0f66bd32016-01-18 11:30:45 -050088 [backgroundColor setFill];
Alexandre Lision6da08a82015-09-24 17:09:24 -040089 [ovalPath fill];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050090 [backgroundStrokeColor setStroke];
Alexandre Lision6da08a82015-09-24 17:09:24 -040091 [ovalPath setLineWidth: 0.5];
92 [ovalPath stroke];
93
94 [NSGraphicsContext saveGraphicsState];
95
Alexandre Lision0f66bd32016-01-18 11:30:45 -050096 NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect];
Alexandre Lision6da08a82015-09-24 17:09:24 -040097 [path addClip];
98
99 [self setImagePosition:NSImageOnly];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500100 auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets);
Alexandre Lision266fca02015-09-28 14:47:05 -0400101
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500102 [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect
Alexandre Lision6da08a82015-09-24 17:09:24 -0400103 fromRect:NSZeroRect
104 operation:NSCompositeSourceOver
105 fraction:1.0
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500106 respectFlipped:YES
107 hints:nil];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400108
109 [NSGraphicsContext restoreGraphicsState];
110 }
111}
112
113-(void)mouseDown:(NSEvent *)theEvent
114{
115 self.mouseDown = TRUE;
116 [self setNeedsDisplay:YES];
117
118 [super mouseDown:theEvent];
119
120 self.mouseDown = FALSE;
121 [self setNeedsDisplay:YES];
122}
123
124@end