blob: a516954547f6cb1208f133867d389783457e2de6 [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
25- (instancetype)initWithFrame:(CGRect)frame
26{
27 self = [super initWithFrame:frame];
28 if (self) {
29 [self loadFromNib];
30 }
31 return self;
32}
33
34- (void)loadFromNib
35{
36 NSView *viewFromXib = nil;
37 NSArray *objectsFromXib = nil;
38 [[NSBundle mainBundle] loadNibNamed:@"AccountMenuItemView" owner:self topLevelObjects:&objectsFromXib];
39 for (id item in objectsFromXib) {
40 if ([item isKindOfClass:[NSView class]]) {
41 viewFromXib = item;
42 break;
43 }
44 }
45 if (viewFromXib != nil) {
46 self.frame = viewFromXib.frame;
47 self.containerView = viewFromXib;
48 [self addSubview:self.containerView];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040049 [self.accountAvatar setWantsLayer:YES];
50 self.accountAvatar.layer.cornerRadius = self.accountAvatar.frame.size.width * 0.5;
51 self.accountAvatar.layer.masksToBounds = YES;
52 [self.accountStatus setWantsLayer:YES];
Kateryna Kostiukef66f972018-11-02 17:10:37 -040053 [self.accountAvatar.layer setBackgroundColor:[[NSColor disabledControlTextColor] CGColor]];
Kateryna Kostiukdc563242019-08-19 12:05:54 -040054#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -040055 if (@available(macOS 10.14, *)) {
56 self.createNewAccountImage.contentTintColor = [NSColor clearColor];
57 }
Kateryna Kostiukdc563242019-08-19 12:05:54 -040058#endif
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040059 }
60}
61
62- (BOOL)acceptsFirstMouse:(NSEvent *)event {
63 return YES;
64}
65
66-(void) mouseUp:(NSEvent *)theEvent {
67 NSMenu *menu = self.enclosingMenuItem.menu;
68 [menu cancelTracking];
69 [menu performActionForItemAtIndex:[menu indexOfItem:self.enclosingMenuItem]];
70 [super mouseUp:theEvent];
71}
72
73- (void) drawRect: (NSRect) rect {
74 NSMenuItem *menuItem = ([self enclosingMenuItem]);
75 BOOL isHighlighted = [menuItem isHighlighted];
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040076 NSColor* highlightBackground = @available(macOS 10.14, *) ? [NSColor controlColor] : [NSColor whiteColor];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040077 if (isHighlighted) {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040078 [self.backgroundView setFillColor: highlightBackground];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040079 } else {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -040080 [self.backgroundView setFillColor:[NSColor windowBackgroundColor]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040081 }
Kateryna Kostiukd7e2e102018-10-01 13:33:38 -040082 [super drawRect: rect];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040083}
84
85@end