IconButton.mm 3.59 KB
Newer Older
Alexandre Lision's avatar
Alexandre Lision committed
1 2 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
/*
 *  Copyright (C) 2015 Savoir-faire Linux Inc.
 *  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);
    }
Alexandre Lision's avatar
Alexandre Lision committed
34 35 36

    if (self.imageInsets == 0)
        self.imageInsets = 5.0f;
Alexandre Lision's avatar
Alexandre Lision committed
37 38 39 40 41 42 43 44 45 46
}

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

    NSColor* backgroundColor6;
    NSColor* backgroundStrokeColor5;

    if (self.mouseDown || self.state == NSOnState) {
Alexandre Lision's avatar
Alexandre Lision committed
47 48 49 50 51 52 53 54
        if (self.highlightColor) {
            backgroundColor6 = self.highlightColor;
            backgroundStrokeColor5 = [self.highlightColor darkenColorByValue:0.1];
        } else {
            backgroundColor6 = [self.bgColor darkenColorByValue:0.3];
            backgroundStrokeColor5 = [self.bgColor darkenColorByValue:0.4];
        }

Alexandre Lision's avatar
Alexandre Lision committed
55 56 57 58 59 60 61 62 63 64 65
    } else {
        backgroundColor6 = self.bgColor;
        backgroundStrokeColor5 = [self.bgColor darkenColorByValue:0.1];
    }

    //// Subframes
    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));

    //// Group
    {
        //// Oval Drawing
Alexandre Lision's avatar
Alexandre Lision committed
66
        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]];
Alexandre Lision's avatar
Alexandre Lision committed
67 68 69 70 71 72 73 74 75 76 77 78 79

        [backgroundColor6 setFill];
        [ovalPath fill];
        [backgroundStrokeColor5 setStroke];
        [ovalPath setLineWidth: 0.5];
        [ovalPath stroke];

        [NSGraphicsContext saveGraphicsState];

        NSBezierPath *path = [NSBezierPath bezierPathWithRect:dirtyRect];
        [path addClip];

        [self setImagePosition:NSImageOnly];
Alexandre Lision's avatar
Alexandre Lision committed
80 81
        auto rect2 = NSInsetRect(dirtyRect, self.imageInsets, self.imageInsets);

Alexandre Lision's avatar
Alexandre Lision committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

        [[NSColor image:self.image tintedWithColor:[NSColor whiteColor]]
                drawInRect:rect2
                 fromRect:NSZeroRect
                operation:NSCompositeSourceOver
                 fraction:1.0
                respectFlipped:YES
                         hints:nil];

        [NSGraphicsContext restoreGraphicsState];
    }
}

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

    [super mouseDown:theEvent];

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

@end