1/******************************************************************************
2 * Copyright (c) 2007-2012 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "InfoTabButtonCell.h"
24#import "NSApplicationAdditions.h"
25
26@implementation InfoTabButtonCell
27
28- (void) awakeFromNib
29{
30    if (![NSApp isOnMojaveOrBetter]) {
31        NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
32        [nc addObserver: self selector: @selector(updateControlTint:)
33            name: NSControlTintDidChangeNotification object: NSApp];
34    }
35
36    fSelected = NO;
37
38    //expects the icon to currently be set as the image
39    fIcon = [self image];
40}
41
42- (void) dealloc
43{
44    [[NSNotificationCenter defaultCenter] removeObserver: self];
45}
46
47- (void) setControlView: (NSView *) controlView
48{
49    const BOOL hadControlView = [self controlView] != nil;
50
51    [super setControlView: controlView];
52
53    if (!hadControlView)
54    {
55        [(NSMatrix *)[self controlView] setToolTip: [self title] forCell: self];
56        [self setSelectedTab: fSelected];
57    }
58}
59
60- (void) setSelectedTab: (BOOL) selected
61{
62    fSelected = selected;
63
64    [self reloadAppearance];
65}
66
67- (void) reloadAppearance
68{
69    if ([self controlView] == nil)
70        return;
71
72    NSInteger row, col;
73    [(NSMatrix *)[self controlView] getRow: &row column: &col ofCell: self];
74    NSRect tabRect = [(NSMatrix *)[self controlView] cellFrameAtRow: row column: col];
75    tabRect.origin.x = 0.0;
76    tabRect.origin.y = 0.0;
77
78    NSImage * tabImage = [[NSImage alloc] initWithSize: tabRect.size];
79
80    [tabImage lockFocus];
81
82    NSGradient * gradient;
83    if (fSelected)
84    {
85        NSColor * lightColor, * darkColor;
86        if (@available(macOS 10.14, *)) {
87            lightColor = [NSColor.controlAccentColor blendedColorWithFraction: 0.35 ofColor: [NSColor whiteColor]];
88            darkColor = [NSColor.controlAccentColor blendedColorWithFraction: 0.15 ofColor: [NSColor whiteColor]];
89        } else {
90            lightColor = [NSColor colorForControlTint: [NSColor currentControlTint]];
91            darkColor = [lightColor blendedColorWithFraction: 0.2 ofColor: [NSColor blackColor]];
92        }
93        gradient = [[NSGradient alloc] initWithStartingColor: lightColor endingColor: darkColor];
94    }
95    else
96    {
97        if ([NSApp isDarkMode]) {
98            NSColor * darkColor = [NSColor colorWithCalibratedRed: 60.0/255.0 green: 60.0/255.0 blue: 60.0/255.0 alpha: 1.0];
99            NSColor * lightColor = [NSColor colorWithCalibratedRed: 90.0/255.0 green: 90.0/255.0 blue: 90.0/255.0 alpha: 1.0];
100            gradient = [[NSGradient alloc] initWithStartingColor: lightColor endingColor: darkColor];
101        } else {
102            NSColor * lightColor = [NSColor colorWithCalibratedRed: 245.0/255.0 green: 245.0/255.0 blue: 245.0/255.0 alpha: 1.0];
103            NSColor * darkColor = [NSColor colorWithCalibratedRed: 215.0/255.0 green: 215.0/255.0 blue: 215.0/255.0 alpha: 1.0];
104            gradient = [[NSGradient alloc] initWithStartingColor: lightColor endingColor: darkColor];
105        }
106    }
107
108    if (@available(macOS 10.14, *)) {
109        [[NSColor separatorColor] set];
110    } else {
111        [[NSColor grayColor] set];
112    }
113    NSRectFill(NSMakeRect(0.0, 0.0, NSWidth(tabRect), 1.0));
114    NSRectFill(NSMakeRect(0.0, NSHeight(tabRect) - 1.0, NSWidth(tabRect), 1.0));
115    NSRectFill(NSMakeRect(NSWidth(tabRect) - 1.0, 1.0, NSWidth(tabRect) - 1.0, NSHeight(tabRect) - 2.0));
116
117    tabRect = NSMakeRect(0.0, 1.0, NSWidth(tabRect) - 1.0, NSHeight(tabRect) - 2.0);
118
119    [gradient drawInRect: tabRect angle: 270.0];
120
121    if (fIcon)
122    {
123        const NSSize iconSize = [fIcon size];
124
125        const NSRect iconRect = NSMakeRect(NSMinX(tabRect) + floor((NSWidth(tabRect) - iconSize.width) * 0.5),
126                                            NSMinY(tabRect) + floor((NSHeight(tabRect) - iconSize.height) * 0.5),
127                                            iconSize.width, iconSize.height);
128
129        [fIcon drawInRect: iconRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
130    }
131
132    [tabImage unlockFocus];
133
134    [self setImage: tabImage];
135}
136
137- (void) updateControlTint: (NSNotification *) notification
138{
139    NSAssert(![NSApp isOnMojaveOrBetter], @"should not be observing control tint color when accent color is available");
140
141    if (fSelected)
142        [self setSelectedTab: YES];
143}
144
145@end
146