blob: 45aa00e5e9b63cff16aab47412cef765bd4e492f [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 Lision6da08a82015-09-24 17:09:24 -040037}
38
39- (void)drawRect:(NSRect)dirtyRect
40{
41 [super drawRect:dirtyRect];
42
Alexandre Lision0f66bd32016-01-18 11:30:45 -050043 NSColor* backgroundColor;
44 NSColor* backgroundStrokeColor;
45 NSColor* tintColor = [NSColor whiteColor];
Alexandre Lision6da08a82015-09-24 17:09:24 -040046
Alexandre Lision0f66bd32016-01-18 11:30:45 -050047 if (self.mouseDown || self.isHighlighted) {
Alexandre Lision266fca02015-09-28 14:47:05 -040048 if (self.highlightColor) {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049 backgroundColor = self.highlightColor;
50 backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1];
Alexandre Lision266fca02015-09-28 14:47:05 -040051 } else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050052 backgroundColor = [self.bgColor darkenColorByValue:0.3];
53 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4];
Alexandre Lision266fca02015-09-28 14:47:05 -040054 }
Alexandre Lision0f66bd32016-01-18 11:30:45 -050055 } else if (!self.isEnabled) {
56 backgroundColor = [self.bgColor colorWithAlphaComponent:0.7];
57 backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7];
58 tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3];
Alexandre Lision6da08a82015-09-24 17:09:24 -040059 } else {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050060 backgroundColor = self.bgColor;
61 backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1];
Alexandre Lision6da08a82015-09-24 17:09:24 -040062 }
63
64 //// Subframes
Alexandre Lision0f66bd32016-01-18 11:30:45 -050065 NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5,
66 NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5,
67 floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333),
68 floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333));
Alexandre Lision6da08a82015-09-24 17:09:24 -040069
Alexandre Lision6da08a82015-09-24 17:09:24 -040070 //// Group
71 {
72 //// Oval Drawing
Alexandre Lision0f66bd32016-01-18 11:30:45 -050073 NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect:
74 NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5),
75 NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5),
76 floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5),
77 floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5))
78 xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]];
Alexandre Lision6da08a82015-09-24 17:09:24 -040079
Alexandre Lision0f66bd32016-01-18 11:30:45 -050080 [backgroundColor setFill];
Alexandre Lision6da08a82015-09-24 17:09:24 -040081 [ovalPath fill];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050082 [backgroundStrokeColor setStroke];
Alexandre Lision6da08a82015-09-24 17:09:24 -040083 [ovalPath setLineWidth: 0.5];
84 [ovalPath stroke];
85
86 [NSGraphicsContext saveGraphicsState];
87
Alexandre Lision0f66bd32016-01-18 11:30:45 -050088 NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect];
Alexandre Lision6da08a82015-09-24 17:09:24 -040089 [path addClip];
90
91 [self setImagePosition:NSImageOnly];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050092 auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets);
Alexandre Lision266fca02015-09-28 14:47:05 -040093
Alexandre Lision0f66bd32016-01-18 11:30:45 -050094 [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect
Alexandre Lision6da08a82015-09-24 17:09:24 -040095 fromRect:NSZeroRect
96 operation:NSCompositeSourceOver
97 fraction:1.0
Alexandre Lision0f66bd32016-01-18 11:30:45 -050098 respectFlipped:YES
99 hints:nil];
Alexandre Lision6da08a82015-09-24 17:09:24 -0400100
101 [NSGraphicsContext restoreGraphicsState];
102 }
103}
104
105-(void)mouseDown:(NSEvent *)theEvent
106{
107 self.mouseDown = TRUE;
108 [self setNeedsDisplay:YES];
109
110 [super mouseDown:theEvent];
111
112 self.mouseDown = FALSE;
113 [self setNeedsDisplay:YES];
114}
115
116@end