Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 1 | /* |
| 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 Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 54 | @property NSVisualEffectView* effect_view; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 55 | @end |
| 56 | |
| 57 | @implementation HoverTableRowView |
| 58 | |
| 59 | @dynamic mouseInside; |
| 60 | |
| 61 | - (instancetype)init |
| 62 | { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 63 | [self configureView]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | - (id)initWithFrame:(CGRect)aRect |
| 67 | { |
| 68 | self = [super initWithFrame:aRect]; |
| 69 | |
| 70 | if (self) { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 71 | [self configureView]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 72 | } |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 73 | return self; |
| 74 | } |
| 75 | |
| 76 | - (id)initWithCoder:(NSCoder*)aDecoder |
| 77 | { |
| 78 | self = [super initWithCoder:aDecoder]; |
| 79 | if (self) { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 80 | [self configureView]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 81 | } |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 82 | return self; |
| 83 | } |
| 84 | |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 85 | - (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 Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 92 | } |
| 93 | |
Kateryna Kostiuk | 4db6109 | 2019-10-17 16:57:40 -0400 | [diff] [blame] | 94 | -(void)setBlurType:(int)type { |
| 95 | [self.effect_view setMaterial: static_cast<NSVisualEffectMaterial>(type)]; |
| 96 | } |
| 97 | |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 98 | - (void)setMouseInside:(BOOL)value { |
| 99 | if (mouseInside != value) { |
| 100 | mouseInside = value; |
| 101 | [self setNeedsDisplay:YES]; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | - (BOOL)mouseInside { |
| 106 | return mouseInside; |
| 107 | } |
| 108 | |
| 109 | - (void)ensureTrackingArea { |
| 110 | if (trackingArea == nil) { |
| 111 | trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect |
| 112 | options:NSTrackingInVisibleRect |
| 113 | | NSTrackingActiveAlways |
| 114 | | NSTrackingMouseEnteredAndExited owner:self userInfo:nil]; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | - (void)updateTrackingAreas { |
| 119 | [super updateTrackingAreas]; |
| 120 | [self ensureTrackingArea]; |
| 121 | if (![[self trackingAreas] containsObject:trackingArea]) { |
| 122 | [self addTrackingArea:trackingArea]; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | - (void)mouseEntered:(NSEvent *)theEvent { |
| 127 | self.mouseInside = YES; |
| 128 | } |
| 129 | |
| 130 | - (void)mouseExited:(NSEvent *)theEvent { |
| 131 | self.mouseInside = NO; |
| 132 | } |
| 133 | |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 134 | -(void) drawSelection: (BOOL) isSelected { |
| 135 | [self.effect_view setHidden: !isSelected]; |
| 136 | [self.effect_view setMaterial: isSelected ? static_cast<NSVisualEffectMaterial>(4) : static_cast<NSVisualEffectMaterial>(6)]; |
| 137 | } |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 138 | - (void)drawBackgroundInRect:(NSRect)dirtyRect { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 139 | [self.effect_view setHidden:YES]; |
| 140 | if(!@available(macOS 10.14, *)) { |
| 141 | [super drawBackgroundInRect:dirtyRect]; |
| 142 | } |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 143 | |
Kateryna Kostiuk | 4cbcbf4 | 2020-12-03 15:02:28 -0500 | [diff] [blame] | 144 | if ((self.mouseInside || self.selected) && self.highlightable) { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 145 | [self.effect_view setHidden:NO]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | - (NSRect)separatorRect { |
| 150 | NSRect separatorRect = self.bounds; |
| 151 | separatorRect.origin.y = NSMaxY(separatorRect) - 1; |
| 152 | separatorRect.size.height = 1; |
| 153 | return separatorRect; |
| 154 | } |
| 155 | |
| 156 | // Only called if the table is set with a horizontal grid |
| 157 | - (void)drawSeparatorInRect:(NSRect)dirtyRect { |
| 158 | // Use a common shared method of drawing the separator |
| 159 | DrawSeparatorInRect([self separatorRect]); |
| 160 | } |
| 161 | |
| 162 | // Only called if the 'selected' property is yes. |
| 163 | - (void)drawSelectionInRect:(NSRect)dirtyRect { |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 164 | [self.effect_view setHidden:NO]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // 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. |
| 168 | - (NSBackgroundStyle)interiorBackgroundStyle { |
| 169 | return NSBackgroundStyleLight; |
| 170 | } |
| 171 | |
| 172 | - (void)setFrame:(NSRect)frameRect { |
| 173 | [super setFrame:frameRect]; |
| 174 | // We need to invalidate more things when live-resizing since we fill with a gradient and stroke |
| 175 | if ([self inLiveResize]) { |
| 176 | // Redraw everything if we are using a gradient |
| 177 | if (self.selected || self.mouseInside) { |
| 178 | [self setNeedsDisplay:YES]; |
| 179 | } else { |
| 180 | // Redraw our horizontal grid line, which is a gradient |
| 181 | [self setNeedsDisplayInRect:[self separatorRect]]; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Kateryna Kostiuk | 394f74c | 2018-10-05 15:45:26 -0400 | [diff] [blame] | 186 | - (void)prepareForReuse { |
| 187 | [self.effect_view setHidden:YES]; |
| 188 | self.mouseInside = NO; |
| 189 | [super prepareForReuse]; |
| 190 | } |
| 191 | |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 192 | @end |
| 193 | |
| 194 | void DrawSeparatorInRect(NSRect rect) { |
Anthony LĂ©onard | 22f7127 | 2017-08-04 10:26:12 -0400 | [diff] [blame] | 195 | static NSColor *color = [NSColor colorWithSRGBRed:.90 green:.90 blue:.90 alpha:1]; |
| 196 | [color drawSwatchInRect:rect]; |
| 197 | |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 198 | } |