blob: cddf2e50c496d44b14a89e8742694e691e75f5fb [file] [log] [blame]
Kateryna Kostiuk13b76882017-03-30 09:18:44 -04001/*
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 Kostiuk757489b2019-12-02 15:29:29 -050025NSTrackingArea *trackingArea;
26
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040027- (instancetype)initWithFrame:(CGRect)frame
28{
29 self = [super initWithFrame:frame];
30 if (self) {
31 [self loadFromNib];
32 }
33 return self;
34}
35
Kateryna Kostiuk757489b2019-12-02 15:29:29 -050036- (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 Kostiuk13b76882017-03-30 09:18:44 -040067- (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 Kostiukd73f9602018-07-24 13:51:28 -040082 [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 Kostiukef66f972018-11-02 17:10:37 -040086 [self.accountAvatar.layer setBackgroundColor:[[NSColor disabledControlTextColor] CGColor]];
Kateryna Kostiuk757489b2019-12-02 15:29:29 -050087 [self.backgroundView setFillColor:[NSColor windowBackgroundColor]];
Kateryna Kostiukf362e2b2020-09-10 19:00:52 -040088 NSColor *color = [self checkIsDarkMode] ? [NSColor lightGrayColor] : [NSColor darkGrayColor];
89 self.rendezVousIndicator.image = [NSColor image: [NSImage imageNamed:@"ic_group.png"] tintedWithColor: color];
Kateryna Kostiuk757489b2019-12-02 15:29:29 -050090 [self createTrackingArea];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040091 }
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 Kostiuk757489b2019-12-02 15:29:29 -0500105- (void)mouseExited:(NSEvent *)event {
106 [self.backgroundView setFillColor:[NSColor windowBackgroundColor]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400107}
108
Kateryna Kostiuk757489b2019-12-02 15:29:29 -0500109- (void)mouseEntered:(NSEvent *)event {
110 NSColor* highlightBackground = @available(macOS 10.14, *) ? [NSColor controlColor] : [NSColor whiteColor];
111 [self.backgroundView setFillColor: highlightBackground];
112}
113
Kateryna Kostiukf362e2b2020-09-10 19:00:52 -0400114-(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 Kostiuk757489b2019-12-02 15:29:29 -0500129
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400130@end