Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015-2017 Savoir-faire Linux Inc. |
| 3 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "AccountMenuItemView.h" |
| 21 | #import "NSColor+RingTheme.h" |
| 22 | |
| 23 | @implementation AccountMenuItemView |
| 24 | |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 25 | NSTrackingArea *trackingArea; |
| 26 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 27 | - (instancetype)initWithFrame:(CGRect)frame |
| 28 | { |
| 29 | self = [super initWithFrame:frame]; |
| 30 | if (self) { |
| 31 | [self loadFromNib]; |
| 32 | } |
| 33 | return self; |
| 34 | } |
| 35 | |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 36 | - (void) createTrackingArea |
| 37 | { |
| 38 | NSTrackingAreaOptions options = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited); |
| 39 | |
| 40 | trackingArea = [[NSTrackingArea alloc] initWithRect: NSInsetRect(self.frame, 3, 3) |
| 41 | options:options |
| 42 | owner:self |
| 43 | userInfo:nil]; |
| 44 | |
| 45 | [self addTrackingArea:trackingArea]; |
| 46 | NSPoint mouseLocation = [[self window] mouseLocationOutsideOfEventStream]; |
| 47 | mouseLocation = [self convertPoint: mouseLocation |
| 48 | fromView: nil]; |
| 49 | |
| 50 | if (NSPointInRect(mouseLocation, [self bounds])) |
| 51 | { |
| 52 | [self mouseEntered: nil]; |
| 53 | } |
| 54 | else |
| 55 | { |
| 56 | [self mouseExited: nil]; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | - (void) updateTrackingAreas |
| 61 | { |
| 62 | [self removeTrackingArea:trackingArea]; |
| 63 | [self createTrackingArea]; |
| 64 | [super updateTrackingAreas]; |
| 65 | } |
| 66 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 67 | - (void)loadFromNib |
| 68 | { |
| 69 | NSView *viewFromXib = nil; |
| 70 | NSArray *objectsFromXib = nil; |
| 71 | [[NSBundle mainBundle] loadNibNamed:@"AccountMenuItemView" owner:self topLevelObjects:&objectsFromXib]; |
| 72 | for (id item in objectsFromXib) { |
| 73 | if ([item isKindOfClass:[NSView class]]) { |
| 74 | viewFromXib = item; |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | if (viewFromXib != nil) { |
| 79 | self.frame = viewFromXib.frame; |
| 80 | self.containerView = viewFromXib; |
| 81 | [self addSubview:self.containerView]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 82 | [self.accountAvatar setWantsLayer:YES]; |
| 83 | self.accountAvatar.layer.cornerRadius = self.accountAvatar.frame.size.width * 0.5; |
| 84 | self.accountAvatar.layer.masksToBounds = YES; |
| 85 | [self.accountStatus setWantsLayer:YES]; |
Kateryna Kostiuk | ef66f97 | 2018-11-02 17:10:37 -0400 | [diff] [blame] | 86 | [self.accountAvatar.layer setBackgroundColor:[[NSColor disabledControlTextColor] CGColor]]; |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 87 | [self.backgroundView setFillColor:[NSColor windowBackgroundColor]]; |
Kateryna Kostiuk | f362e2b | 2020-09-10 19:00:52 -0400 | [diff] [blame] | 88 | NSColor *color = [self checkIsDarkMode] ? [NSColor lightGrayColor] : [NSColor darkGrayColor]; |
| 89 | self.rendezVousIndicator.image = [NSColor image: [NSImage imageNamed:@"ic_group.png"] tintedWithColor: color]; |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 90 | [self createTrackingArea]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | - (BOOL)acceptsFirstMouse:(NSEvent *)event { |
| 95 | return YES; |
| 96 | } |
| 97 | |
| 98 | -(void) mouseUp:(NSEvent *)theEvent { |
| 99 | NSMenu *menu = self.enclosingMenuItem.menu; |
| 100 | [menu cancelTracking]; |
| 101 | [menu performActionForItemAtIndex:[menu indexOfItem:self.enclosingMenuItem]]; |
| 102 | [super mouseUp:theEvent]; |
| 103 | } |
| 104 | |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 105 | - (void)mouseExited:(NSEvent *)event { |
| 106 | [self.backgroundView setFillColor:[NSColor windowBackgroundColor]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 109 | - (void)mouseEntered:(NSEvent *)event { |
| 110 | NSColor* highlightBackground = @available(macOS 10.14, *) ? [NSColor controlColor] : [NSColor whiteColor]; |
| 111 | [self.backgroundView setFillColor: highlightBackground]; |
| 112 | } |
| 113 | |
Kateryna Kostiuk | f362e2b | 2020-09-10 19:00:52 -0400 | [diff] [blame] | 114 | -(void) viewDidChangeEffectiveAppearance { |
| 115 | NSColor *color = [self checkIsDarkMode] ? [NSColor lightGrayColor] : [NSColor darkGrayColor]; |
| 116 | self.rendezVousIndicator.image = [NSColor image: [NSImage imageNamed:@"ic_group.png"] tintedWithColor: color]; |
| 117 | [super viewDidChangeEffectiveAppearance]; |
| 118 | } |
| 119 | |
| 120 | -(BOOL)checkIsDarkMode { |
| 121 | NSAppearance *appearance = NSAppearance.currentAppearance; |
| 122 | if (@available(*, macOS 10.14)) { |
| 123 | NSString *interfaceStyle = [NSUserDefaults.standardUserDefaults valueForKey:@"AppleInterfaceStyle"]; |
| 124 | return [interfaceStyle isEqualToString:@"Dark"]; |
| 125 | } |
| 126 | return NO; |
| 127 | } |
| 128 | |
Kateryna Kostiuk | 757489b | 2019-12-02 15:29:29 -0500 | [diff] [blame] | 129 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 130 | @end |