VLCTintedImageButtonCell.m 2.91 KB
Newer Older
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*****************************************************************************
 * VLCTintedImageButtonCell.m
 *****************************************************************************
 * Copyright (C) 2017 VLC authors and VideoLAN
 * $Id$
 *
 * Authors: Marvin Scholz <epirat07 at gmail dot 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 2 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 "VLCTintedImageButtonCell.h"
#import "CompatibilityFixes.h"

@interface VLCTintedImageButtonCell () {
    NSMutableDictionary *_imageCache;
}
@end


@implementation VLCTintedImageButtonCell

+ (void)load
{
    /* On 10.10+ we do not want custom drawing, therefore we swap out the implementation
     * of the selectors below with their original implementations.
     * Just calling super is not enough here, as the button would still draw in a different
     * way, non vibrant with weird highlighting behaviour.
     */
    if (OSX_YOSEMITE_AND_HIGHER) {
        swapoutOverride([VLCTintedImageButtonCell class], @selector(initWithCoder:));
        swapoutOverride([VLCTintedImageButtonCell class], @selector(drawImage:withFrame:inView:));
    }
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _imageCache = [NSMutableDictionary dictionary];
        _imageTintColor = [NSColor whiteColor];
    }
    return self;
}

- (NSImage *)image:(NSImage*)image tintedWithColor:(NSColor *)tint
{
    image = [image copy];
61
    [image setTemplate:NO];
62 63 64
    if (tint) {
        [image lockFocus];
        NSRect imageRect = {NSZeroPoint, [image size]};
65
        [tint setFill];
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        NSRectFillUsingOperation(imageRect, NSCompositeSourceAtop);
        [image unlockFocus];
    }
    return image;
}

- (NSImage*)tintedImage:(NSImage*)image
{
    NSNumber *key = @((NSInteger)image);
    if (![_imageCache objectForKey:key]) {
        NSImage *tintedImg = [self image:image tintedWithColor:_imageTintColor];
        [_imageCache setObject:tintedImg forKey:key];
    }
    return [_imageCache objectForKey:key];
}

- (void)drawImage:(NSImage *)image withFrame:(NSRect)frame inView:(NSView *)controlView
{
84
    image = [self tintedImage:image];
85 86 87 88
    [super drawImage:image withFrame:frame inView:controlView];
}

@end