// Created by Abhishek Banthia on 11/4/15. // Copyright (c) 2015 Abhishek Banthia All rights reserved. // // Copyright (c) 2015, Abhishek Banthia // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation and/or // other materials provided with the distribution. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #import "StatusItemView.h" @implementation StatusItemView @synthesize statusItem = _statusItem; @synthesize image = _image; @synthesize alternateImage = _alternateImage; @synthesize isHighlighted = _isHighlighted; @synthesize action = _action; @synthesize target = _target; #pragma mark - - (id)initWithStatusItem:(NSStatusItem *)statusItem { CGFloat itemWidth = [statusItem length]; CGFloat itemHeight = [[NSStatusBar systemStatusBar] thickness]; NSRect itemRect = NSMakeRect(0.0, 0.0, itemWidth, itemHeight); self = [super initWithFrame:itemRect]; if (self != nil) { _statusItem = statusItem; _statusItem.view = self; } return self; } #pragma mark - - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; // Set up dark mode for icon if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"] isEqual: @"Dark"]) { self.image = [NSImage imageNamed:@"StatusHighlighted"]; } else { self.image = self.isHighlighted ? [NSImage imageNamed:@"StatusHighlighted"] : [NSImage imageNamed:@"Status"]; } [self.statusItem drawStatusBarBackgroundInRect:dirtyRect withHighlight:self.isHighlighted]; NSImage *icon = self.image; NSSize iconSize = [icon size]; NSRect bounds = self.bounds; CGFloat iconX = roundf((NSWidth(bounds) - iconSize.width) / 2); CGFloat iconY = roundf((NSHeight(bounds) - iconSize.height) / 2); NSPoint iconPoint = NSMakePoint(iconX, iconY); [icon drawAtPoint:iconPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; } #pragma mark - #pragma mark Mouse tracking - (void)mouseDown:(NSEvent *)theEvent { [NSApp sendAction:self.action to:self.target from:self]; } #pragma mark - #pragma mark Accessors - (void)setHighlighted:(BOOL)newFlag { if (_isHighlighted == newFlag) return; _isHighlighted = newFlag; [self setNeedsDisplay:YES]; } #pragma mark - - (void)setImage:(NSImage *)newImage { if (_image != newImage) { _image = newImage; [self setNeedsDisplay:YES]; } } - (void)setAlternateImage:(NSImage *)newImage { if (_alternateImage != newImage) { _alternateImage = newImage; if (self.isHighlighted) { [self setNeedsDisplay:YES]; } } } #pragma mark - - (NSRect)globalRect { NSRect frame = [self frame]; return [self.window convertRectToScreen:frame]; } @end