IconButton.mm 4.15 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

#import "IconButton.h"

#import "NSColor+RingTheme.h"

@implementation IconButton

-(void) awakeFromNib {
    if (!self.bgColor) {
        self.bgColor = [NSColor ringBlue];
    }

    if (!self.cornerRadius) {
        self.cornerRadius = @(NSWidth(self.frame) / 2);
    }
34 35 36

    if (self.imageInsets == 0)
        self.imageInsets = 5.0f;
37 38 39 40 41 42
}

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

43 44 45
    NSColor* backgroundColor;
    NSColor* backgroundStrokeColor;
    NSColor* tintColor = [NSColor whiteColor];
46

47
    if (self.mouseDown || self.isHighlighted) {
48
        if (self.highlightColor) {
49 50
            backgroundColor = self.highlightColor;
            backgroundStrokeColor = [self.highlightColor darkenColorByValue:0.1];
51
        } else {
52 53
            backgroundColor = [self.bgColor darkenColorByValue:0.3];
            backgroundStrokeColor = [self.bgColor darkenColorByValue:0.4];
54
        }
55 56 57 58
    } else if (!self.isEnabled) {
        backgroundColor = [self.bgColor colorWithAlphaComponent:0.7];
        backgroundStrokeColor = [self.bgColor colorWithAlphaComponent:0.7];
        tintColor = [[NSColor grayColor] colorWithAlphaComponent:0.3];
59
    } else {
60 61
        backgroundColor = self.bgColor;
        backgroundStrokeColor = [self.bgColor darkenColorByValue:0.1];
62 63 64
    }

    //// Subframes
65 66 67 68
    NSRect group = NSMakeRect(NSMinX(dirtyRect) + floor(NSWidth(dirtyRect) * 0.03333) + 0.5,
                              NSMinY(dirtyRect) + floor(NSHeight(dirtyRect) * 0.03333) + 0.5,
                              floor(NSWidth(dirtyRect) * 0.96667) - floor(NSWidth(dirtyRect) * 0.03333),
                              floor(NSHeight(dirtyRect) * 0.96667) - floor(NSHeight(dirtyRect) * 0.03333));
69 70 71 72

    //// Group
    {
        //// Oval Drawing
73 74 75 76 77 78 79 80
        NSBezierPath* ovalPath = [NSBezierPath bezierPathWithRoundedRect:
                                  NSMakeRect(NSMinX(group) + floor(NSWidth(group) * 0.00000 + 0.5),
                                             NSMinY(group) + floor(NSHeight(group) * 0.00000 + 0.5),
                                             floor(NSWidth(group) * 1.00000 + 0.5) - floor(NSWidth(group) * 0.00000 + 0.5),
                                             floor(NSHeight(group) * 1.00000 + 0.5) - floor(NSHeight(group) * 0.00000 + 0.5))
                                                                 xRadius:[self.cornerRadius floatValue] yRadius:[self.cornerRadius floatValue]];

        [backgroundColor setFill];
81
        [ovalPath fill];
82
        [backgroundStrokeColor setStroke];
83 84 85 86 87
        [ovalPath setLineWidth: 0.5];
        [ovalPath stroke];

        [NSGraphicsContext saveGraphicsState];

88
        NSBezierPath* path = [NSBezierPath bezierPathWithRect:dirtyRect];
89 90 91
        [path addClip];

        [self setImagePosition:NSImageOnly];
92
        auto rect = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets);
93

94
        [[NSColor image:self.image tintedWithColor:tintColor] drawInRect:rect
95 96 97
                 fromRect:NSZeroRect
                operation:NSCompositeSourceOver
                 fraction:1.0
98 99
           respectFlipped:YES
                    hints:nil];
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

        [NSGraphicsContext restoreGraphicsState];
    }
}

-(void)mouseDown:(NSEvent *)theEvent
{
    self.mouseDown = TRUE;
    [self setNeedsDisplay:YES];

    [super mouseDown:theEvent];

    self.mouseDown = FALSE;
    [self setNeedsDisplay:YES];
}

@end