blob: 8484cba2b4a5a08c54225561daa26d140af78096 [file] [log] [blame]
Alexandre Lision4e280d62015-09-09 15:56:30 -04001/*
2 File: HoverTableRowView.m
3 Abstract: Custom NSTableRowView class for custom drawing and mouse tracking feedback via NSTrackingArea
4 Version: 1.1
5
6 Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
7 Inc. ("Apple") in consideration of your agreement to the following
8 terms, and your use, installation, modification or redistribution of
9 this Apple software constitutes acceptance of these terms. If you do
10 not agree with these terms, please do not use, install, modify or
11 redistribute this Apple software.
12
13 In consideration of your agreement to abide by the following terms, and
14 subject to these terms, Apple grants you a personal, non-exclusive
15 license, under Apple's copyrights in this original Apple software (the
16 "Apple Software"), to use, reproduce, modify and redistribute the Apple
17 Software, with or without modifications, in source and/or binary forms;
18 provided that if you redistribute the Apple Software in its entirety and
19 without modifications, you must retain this notice and the following
20 text and disclaimers in all such redistributions of the Apple Software.
21 Neither the name, trademarks, service marks or logos of Apple Inc. may
22 be used to endorse or promote products derived from the Apple Software
23 without specific prior written permission from Apple. Except as
24 expressly stated in this notice, no other rights or licenses, express or
25 implied, are granted by Apple herein, including but not limited to any
26 patent rights that may be infringed by your derivative works or by other
27 works in which the Apple Software may be incorporated.
28
29 The Apple Software is provided by Apple on an "AS IS" basis. APPLE
30 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34
35 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42 POSSIBILITY OF SUCH DAMAGE.
43
44 Copyright (C) 2012 Apple Inc. All Rights Reserved.
45
46 */
47
48#import "HoverTableRowView.h"
49
50#import "NSColor+RingTheme.h"
51
52@interface HoverTableRowView ()
53@property BOOL mouseInside;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040054@property NSVisualEffectView* effect_view;
Alexandre Lision4e280d62015-09-09 15:56:30 -040055@end
56
57@implementation HoverTableRowView
58
59@dynamic mouseInside;
60
61- (instancetype)init
62{
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040063 [self configureView];
Alexandre Lision4e280d62015-09-09 15:56:30 -040064}
65
66- (id)initWithFrame:(CGRect)aRect
67{
68 self = [super initWithFrame:aRect];
69
70 if (self) {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040071 [self configureView];
Alexandre Lision4e280d62015-09-09 15:56:30 -040072 }
Alexandre Lision4e280d62015-09-09 15:56:30 -040073 return self;
74}
75
76- (id)initWithCoder:(NSCoder*)aDecoder
77{
78 self = [super initWithCoder:aDecoder];
79 if (self) {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040080 [self configureView];
Alexandre Lision4e280d62015-09-09 15:56:30 -040081 }
Alexandre Lision4e280d62015-09-09 15:56:30 -040082 return self;
83}
84
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040085- (void)configureView {
86 self.highlightable = YES;
87 self.effect_view = [[NSVisualEffectView alloc] initWithFrame: [self bounds]];
88 [self.effect_view setAutoresizingMask:NSViewHeightSizable | NSViewWidthSizable];
89 [self addSubview: self.effect_view positioned: NSWindowBelow relativeTo:nil];
90 [self.effect_view setHidden:YES];
91 [self.effect_view setMaterial: static_cast<NSVisualEffectMaterial>(6)];
Alexandre Lision4e280d62015-09-09 15:56:30 -040092}
93
94- (void)setMouseInside:(BOOL)value {
95 if (mouseInside != value) {
96 mouseInside = value;
97 [self setNeedsDisplay:YES];
98 }
99}
100
101- (BOOL)mouseInside {
102 return mouseInside;
103}
104
105- (void)ensureTrackingArea {
106 if (trackingArea == nil) {
107 trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
108 options:NSTrackingInVisibleRect
109 | NSTrackingActiveAlways
110 | NSTrackingMouseEnteredAndExited owner:self userInfo:nil];
111 }
112}
113
114- (void)updateTrackingAreas {
115 [super updateTrackingAreas];
116 [self ensureTrackingArea];
117 if (![[self trackingAreas] containsObject:trackingArea]) {
118 [self addTrackingArea:trackingArea];
119 }
120}
121
122- (void)mouseEntered:(NSEvent *)theEvent {
123 self.mouseInside = YES;
124}
125
126- (void)mouseExited:(NSEvent *)theEvent {
127 self.mouseInside = NO;
128}
129
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400130-(void) drawSelection: (BOOL) isSelected {
131 [self.effect_view setHidden: !isSelected];
132 [self.effect_view setMaterial: isSelected ? static_cast<NSVisualEffectMaterial>(4) : static_cast<NSVisualEffectMaterial>(6)];
133}
Alexandre Lision4e280d62015-09-09 15:56:30 -0400134- (void)drawBackgroundInRect:(NSRect)dirtyRect {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400135 [self.effect_view setHidden:YES];
136 if(!@available(macOS 10.14, *)) {
137 [super drawBackgroundInRect:dirtyRect];
138 }
Alexandre Lision4e280d62015-09-09 15:56:30 -0400139
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400140 if ((self.mouseInside) && self.highlightable) {
141 [self.effect_view setHidden:NO];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400142 }
143}
144
145- (NSRect)separatorRect {
146 NSRect separatorRect = self.bounds;
147 separatorRect.origin.y = NSMaxY(separatorRect) - 1;
148 separatorRect.size.height = 1;
149 return separatorRect;
150}
151
152// Only called if the table is set with a horizontal grid
153- (void)drawSeparatorInRect:(NSRect)dirtyRect {
154 // Use a common shared method of drawing the separator
155 DrawSeparatorInRect([self separatorRect]);
156}
157
158// Only called if the 'selected' property is yes.
159- (void)drawSelectionInRect:(NSRect)dirtyRect {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400160 [self.effect_view setHidden:NO];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400161}
162
163// interiorBackgroundStyle is normaly "dark" when the selection is drawn (self.selected == YES) and we are in a key window (self.emphasized == YES). However, we always draw a light selection, so we override this method to always return a light color.
164- (NSBackgroundStyle)interiorBackgroundStyle {
165 return NSBackgroundStyleLight;
166}
167
168- (void)setFrame:(NSRect)frameRect {
169 [super setFrame:frameRect];
170 // We need to invalidate more things when live-resizing since we fill with a gradient and stroke
171 if ([self inLiveResize]) {
172 // Redraw everything if we are using a gradient
173 if (self.selected || self.mouseInside) {
174 [self setNeedsDisplay:YES];
175 } else {
176 // Redraw our horizontal grid line, which is a gradient
177 [self setNeedsDisplayInRect:[self separatorRect]];
178 }
179 }
180}
181
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400182- (void)prepareForReuse {
183 [self.effect_view setHidden:YES];
184 self.mouseInside = NO;
185 [super prepareForReuse];
186}
187
Alexandre Lision4e280d62015-09-09 15:56:30 -0400188@end
189
190void DrawSeparatorInRect(NSRect rect) {
Anthony LĂ©onard22f71272017-08-04 10:26:12 -0400191 static NSColor *color = [NSColor colorWithSRGBRed:.90 green:.90 blue:.90 alpha:1];
192 [color drawSwatchInRect:rect];
193
Alexandre Lision4e280d62015-09-09 15:56:30 -0400194}