blob: 475343583a6e9297a280b6cbdc03432a274645c5 [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>
Anthony Léonard49cb2912017-11-13 16:15:39 -05004 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
5 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Kateryna Kostiuk13b76882017-03-30 09:18:44 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22#import "ChooseAccountVC.h"
23
24//Qt
25#import <QSize>
26#import <QtMacExtras/qmacfunctions.h>
27#import <QPixmap>
28
29//LRC
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040030#import <interfaces/pixmapmanipulatori.h>
Anthony Léonard49cb2912017-11-13 16:15:39 -050031#import <api/newaccountmodel.h>
32#import <api/account.h>
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040033
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040034//RING
35#import "views/AccountMenuItemView.h"
Kateryna Kostiuka16c9862017-05-03 13:30:14 -040036#import "AccountSelectionManager.h"
Anthony Léonard9bebf1d2017-12-21 14:33:51 -050037#import "RingWindowController.h"
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040038#import "utils.h"
39#import "views/NSColor+RingTheme.h"
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050040#import "views/RoundedTextField.h"
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040041
Kateryna Kostiuk04a56122018-08-27 10:51:27 -040042@interface NSMenu ()
43- (void) _setHasPadding: (BOOL) pad onEdge: (int) whatEdge;
44@end
45
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040046@interface ChooseAccountVC () <NSMenuDelegate>
47
48@end
49
50@implementation ChooseAccountVC {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040051
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040052 __unsafe_unretained IBOutlet NSImageView* profileImage;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050053 __unsafe_unretained IBOutlet RoundedTextField* accountStatus;
54 __unsafe_unretained IBOutlet NSTextField* selectedAccountTitle;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040055 __unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040056 lrc::api::NewAccountModel* accMdl_;
Anthony Léonard49cb2912017-11-13 16:15:39 -050057 AccountSelectionManager* accountSelectionManager_;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040058}
59Boolean menuIsOpen;
60Boolean menuNeedsUpdate;
61NSMenu* accountsMenu;
62NSMenuItem* selectedMenuItem;
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -040063NSMutableDictionary* menuItemsTags;
Anthony Léonard49cb2912017-11-13 16:15:39 -050064
Kateryna Kostiukef66f972018-11-02 17:10:37 -040065//-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::NewAccountModel*) accMdl delegate:(id <ChooseAccountDelegate> )mainWindow
66//{
67// accMdl_ = accMdl;
68// accountSelectionManager_ = [[AccountSelectionManager alloc] initWithAccountModel:accMdl_];
69// self.delegate = mainWindow;
70// return [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
71//}
72
73-(void) updateWithDelegate:(id <ChooseAccountDelegate> )mainWindow andModel:(lrc::api::NewAccountModel*) accMdl {
Anthony Léonard49cb2912017-11-13 16:15:39 -050074 accMdl_ = accMdl;
Kateryna Kostiukef66f972018-11-02 17:10:37 -040075 accountSelectionManager_ = [[AccountSelectionManager alloc] initWithAccountModel:accMdl_];
76 self.delegate = mainWindow;
77 [self initView];
Anthony Léonard49cb2912017-11-13 16:15:39 -050078}
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040079
Kateryna Kostiukef66f972018-11-02 17:10:37 -040080- (void)initView
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040081{
82 [profileImage setWantsLayer: YES];
83 profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
84 profileImage.layer.masksToBounds = YES;
Kateryna Kostiukef66f972018-11-02 17:10:37 -040085 profileImage.layer.backgroundColor = [[NSColor disabledControlTextColor] CGColor];
Kateryna Kostiuk065aaf22017-07-07 13:04:23 -040086
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040087 accountsMenu = [[NSMenu alloc] initWithTitle:@""];
Kateryna Kostiuk04a56122018-08-27 10:51:27 -040088 if ([accountsMenu respondsToSelector: @selector(_setHasPadding:onEdge:)])
89 {
90 [accountsMenu _setHasPadding: NO onEdge: 1];
91 [accountsMenu _setHasPadding: NO onEdge: 3];
92 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040093 [accountsMenu setDelegate:self];
94 accountSelectionButton.menu = accountsMenu;
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040095 [accountSelectionButton setAutoenablesItems:NO];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -040096 menuItemsTags = [[NSMutableDictionary alloc] init];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040097 [self update];
98
Kateryna Kostiukef66f972018-11-02 17:10:37 -040099
Anthony Léonard49cb2912017-11-13 16:15:39 -0500100 QObject::connect(accMdl_,
101 &lrc::api::NewAccountModel::accountAdded,
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400102 [self] (const std::string& accountID) {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400103 [self update];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400104 @try {
105 auto& account = [self selectedAccount];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400106 [self.delegate selectAccount:account currentRemoved: NO];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400107 }
108 @catch (NSException * e) {
109 NSLog(@"account selection failed");
110 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400111 });
Anthony Léonard49cb2912017-11-13 16:15:39 -0500112 QObject::connect(accMdl_,
113 &lrc::api::NewAccountModel::accountRemoved,
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400114 [self] (const std::string& accountID) {
115 if ([self selectedAccount].id.compare(accountID) == 0) {
116 [accountSelectionManager_ clearSelectedAccount];
117 }
118 @try {
119 auto& account = [self selectedAccount];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400120 [self.delegate selectAccount:account currentRemoved: YES];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400121 }
122 @catch (NSException * e) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400123 [self.delegate allAccountsDeleted];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400124 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400125 [self update];
126 });
Anthony Léonard49cb2912017-11-13 16:15:39 -0500127 QObject::connect(accMdl_,
128 &lrc::api::NewAccountModel::profileUpdated,
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400129 [self] (const std::string& accountID) {
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400130 [self update];
131 });
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400132 QObject::connect(accMdl_,
133 &lrc::api::NewAccountModel::accountStatusChanged,
134 [self] (const std::string& accountID) {
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400135 [self updateMenuItemForAccount:accountID];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400136 if([self selectedAccount].id == accountID) {
137 // update account state
138 [self updatePhoto];
139 [self setPopUpButtonSelection];
140 }
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400141 });
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400142}
143
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400144
Anthony Léonard49cb2912017-11-13 16:15:39 -0500145-(const lrc::api::account::Info&) selectedAccount
146{
Anthony Léonard72128c92017-12-26 16:48:39 -0500147 try {
148 return [accountSelectionManager_ savedAccount];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400149 } catch (NSException *ex) {
Anthony Léonard72128c92017-12-26 16:48:39 -0500150 auto accountList = accMdl_->getAccountList();
151 if (!accountList.empty()) {
152 const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0));
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400153 [accountSelectionManager_ setSavedAccount:fallbackAccount];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500154 return fallbackAccount;
Anthony Léonard72128c92017-12-26 16:48:39 -0500155 } else {
156 NSException* noAccEx = [NSException
157 exceptionWithName:@"NoAccountException"
158 reason:@"No account in AccountModel"
159 userInfo:nil];
160 @throw noAccEx;
Anthony Léonard49cb2912017-11-13 16:15:39 -0500161 }
162 }
Anthony Léonard49cb2912017-11-13 16:15:39 -0500163}
164
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400165-(void) updateMenuItemForAccount: (const std::string&) accountID {
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400166 NSMenuItem *item =[accountsMenu itemWithTag:[menuItemsTags[@(accountID.c_str())] intValue]];
167 if(!item) {return;}
168 AccountMenuItemView *itemView =item.view;
169 if(!itemView) {return;}
170 [self configureView:itemView forAccount:accountID forMenuItem: item];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400171}
172
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400173-(void) updateMenu {
174 [accountsMenu removeAllItems];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400175
Anthony Léonard49cb2912017-11-13 16:15:39 -0500176 auto accList = accMdl_->getAccountList();
177
178 for (std::string accId : accList) {
179 auto& account = accMdl_->getAccountInfo(accId);
180
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400181 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400182 initWithTitle:[self itemTitleForAccount:account]
183 action:NULL
184 keyEquivalent:@""];
185
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400186 AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400187 [self configureView:itemView forAccount:accId forMenuItem: menuBarItem];
188 int itemTag = arc4random_uniform(1000);
189 menuItemsTags[@(accId.c_str())] = [NSNumber numberWithInt: itemTag];
190 [menuBarItem setTag:itemTag];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400191 [menuBarItem setView:itemView];
192 [accountsMenu addItem:menuBarItem];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400193 }
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400194
195 // create "add a new account" menu item
196 NSMenuItem* menuBarItem = [[NSMenuItem alloc]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400197 initWithTitle:@"Add Account"
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400198 action:nil
199 keyEquivalent:@""];
200 AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
201 [itemView.accountAvatar setHidden:YES];
202 [itemView.accountStatus setHidden:YES];
203 [itemView.accountTypeLabel setHidden:YES];
204 [itemView.userNameLabel setHidden:YES];
205 [itemView.accountLabel setHidden:YES];
206 [itemView.createNewAccount setAction:@selector(createNewAccount:)];
207 [itemView.createNewAccount setTarget:self];
208 [menuBarItem setView: itemView];
209 [accountsMenu addItem: menuBarItem];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400210 [profileImage setHidden:accList.empty()];
211 [accountStatus setHidden:accList.empty()];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400212}
213
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400214-(void) configureView: (AccountMenuItemView *) itemView forAccount:(const std::string&) accountId forMenuItem:(NSMenuItem *) item {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400215 auto& account = accMdl_->getAccountInfo(accountId);
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400216 item.attributedTitle = [self attributedItemTitleForAccount:account];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400217 [itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())];
218 NSString* userNameString = [self nameForAccount: account];
219 [itemView.userNameLabel setStringValue:userNameString];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400220 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
221 NSImage *image = [[NSImage alloc] initWithData:imageData];
222 if(image) {
223 [itemView.accountAvatar setImage: image];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400224 } else {
225 [itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
226 }
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500227 itemView.accountStatus.bgColor = colorForAccountStatus(account.status);
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400228 switch (account.profileInfo.type) {
229 case lrc::api::profile::Type::SIP:
230 [itemView.accountTypeLabel setStringValue:@"SIP"];
231 break;
232 case lrc::api::profile::Type::RING:
Kateryna Kostiuk85a334e2018-12-03 15:54:19 -0500233 [itemView.accountTypeLabel setStringValue:@"Jami"];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400234 break;
235 default:
236 break;
237 }
238 [itemView.createNewAccount setHidden:YES];
239 [itemView.createNewAccountImage setHidden:YES];
240}
241
242
243- (void)createNewAccount:(id)sender {
244 [accountSelectionButton.menu cancelTrackingWithoutAnimation];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400245 [self.delegate createNewAccount];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400246}
247
Anthony Léonard49cb2912017-11-13 16:15:39 -0500248-(void) updatePhoto
249{
Anthony Léonard72128c92017-12-26 16:48:39 -0500250 @try {
251 auto& account = [self selectedAccount];
252 if(account.profileInfo.type == lrc::api::profile::Type::INVALID)
253 return;
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400254 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
255 NSImage *image = [[NSImage alloc] initWithData:imageData];
256 if(image) {
257 [profileImage setImage: image];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400258 } else {
259 [profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
260 }
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500261 accountStatus.bgColor = colorForAccountStatus(account.status);
262 [accountStatus setNeedsDisplay: YES];
Anthony Léonard72128c92017-12-26 16:48:39 -0500263 }
264 @catch (NSException *ex) {
265 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
266 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400267}
268
Anthony Léonard49cb2912017-11-13 16:15:39 -0500269-(NSString*) nameForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400270 return bestIDForAccount(account);
Anthony Léonard49cb2912017-11-13 16:15:39 -0500271}
272
273-(NSString*) itemTitleForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400274 NSString* alias = bestNameForAccount(account);
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400275 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiukdff4c0e2018-08-31 10:58:32 -0400276 if([alias isEqualToString:userNameString] || [userNameString length] == 0) {
277 return alias;
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400278 }
Kateryna Kostiukdff4c0e2018-08-31 10:58:32 -0400279 alias = [NSString stringWithFormat: @"%@\n", alias];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400280 return [alias stringByAppendingString:userNameString];
281}
282
Anthony Léonard49cb2912017-11-13 16:15:39 -0500283- (NSAttributedString*) attributedItemTitleForAccount:(const lrc::api::account::Info&) account {
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400284 NSString* alias = bestNameForAccount(account);
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400285 NSString* userNameString = [self nameForAccount: account];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500286 NSFont *fontAlias = [NSFont fontWithName:@"Helvetica Neue Light" size:16.0];
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400287 NSFont *fontUserName = [NSFont fontWithName:@"Helvetica Neue Light" size:13.0];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500288 NSColor *colorAlias = [NSColor textColor];
289 NSColor *colorAUserName = [NSColor labelColor];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400290 NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
291 paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400292 paragraphStyle.lineSpacing = 3;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500293 NSDictionary *aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
294 fontAlias,NSFontAttributeName,
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400295 colorAlias,NSForegroundColorAttributeName,
296 paragraphStyle,NSParagraphStyleAttributeName, nil];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500297 NSDictionary *userNameAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
298 fontUserName,NSFontAttributeName,
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400299 colorAUserName,NSForegroundColorAttributeName,
300 paragraphStyle,NSParagraphStyleAttributeName, nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400301
302 if([alias isEqualToString:userNameString] || [userNameString length] == 0) {
303 paragraphStyle.paragraphSpacingBefore = 20;
304 aliasAttrs = [NSDictionary dictionaryWithObjectsAndKeys:fontAlias,NSFontAttributeName,
305 colorAlias,NSForegroundColorAttributeName,
306 paragraphStyle,NSParagraphStyleAttributeName, nil];
307 return [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs];
308 }
309 alias = [NSString stringWithFormat: @"%@\n", alias];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400310 NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:alias attributes:aliasAttrs];
311 NSAttributedString* attributedStringSecond= [[NSAttributedString alloc] initWithString:userNameString attributes:userNameAttrs];
312 NSMutableAttributedString *result = [[NSMutableAttributedString alloc] init];
313 [result appendAttributedString:attributedString];
314 [result appendAttributedString:attributedStringSecond];
315 return result;
316}
317
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400318-(void) update {
319 if(menuIsOpen) {
320 return;
321 }
322 [self updateMenu];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400323 [self setPopUpButtonSelection];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400324 [self updatePhoto];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400325}
326
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400327-(void) setPopUpButtonSelection {
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400328 if(accountsMenu.itemArray.count == 0) {
329 [self.view setHidden:YES];
330 return;
331 }
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400332 [self.view setHidden:NO];
Anthony Léonard72128c92017-12-26 16:48:39 -0500333 @try {
334 auto& account = [self selectedAccount];
335 if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
336 return;
337 }
Kateryna Kostiuk194f21e2018-10-22 16:21:03 -0400338 [accountSelectionButton selectItemWithTag:[menuItemsTags[@(account.id.c_str())] intValue]];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500339 [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400340 }
Anthony Léonard72128c92017-12-26 16:48:39 -0500341 @catch (NSException *ex) {
342 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
343 }
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400344}
345
346#pragma mark - NSPopUpButton item selection
347
348- (IBAction)itemChanged:(id)sender {
Kateryna Kostiuk04a56122018-08-27 10:51:27 -0400349 NSInteger row = [(NSPopUpButton *)sender indexOfSelectedItem];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500350 auto accList = accMdl_->getAccountList();
351 if (row >= accList.size())
Kateryna Kostiuka16c9862017-05-03 13:30:14 -0400352 return;
Anthony Léonard49cb2912017-11-13 16:15:39 -0500353
354 auto& account = accMdl_->getAccountInfo(accList[row]);
355 [accountSelectionManager_ setSavedAccount:account];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400356 [self.delegate selectAccount:account currentRemoved: NO];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400357 [self updatePhoto];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500358 [selectedAccountTitle setAttributedStringValue: accountSelectionButton.attributedTitle];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400359}
360
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400361- (IBAction)openMenu:(id)sender {
362 [accountSelectionButton performClick:nil];
363}
364
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400365#pragma mark - NSMenuDelegate
366- (void)menuWillOpen:(NSMenu *)menu {
367 menuIsOpen = true;
368 // remember selected item to remove highlighting when menu is open
369 selectedMenuItem = [accountSelectionButton selectedItem];
370}
371- (void)menuDidClose:(NSMenu *)menu {
372
373 menuIsOpen = false;
374}
375
376- (void)menu:(NSMenu *)menu willHighlightItem:(nullable NSMenuItem *)item {
377 if (!selectedMenuItem || selectedMenuItem == item) {
378 return;
379 }
380 int index = [menu indexOfItem:selectedMenuItem];
381 [menu removeItemAtIndex:index];
382 [menu insertItem:selectedMenuItem atIndex:index];
383 [accountSelectionButton selectItemAtIndex:index];
384 selectedMenuItem = nil;
385}
386
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400387-(void) enable {
388 [accountSelectionButton setEnabled:YES];
389}
390-(void) disable {
391 [accountSelectionButton setEnabled:NO];
392}
393
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500394- (void)selectAccount:(NSString*)accountID {
395 auto accList = accMdl_->getAccountList();
396 if(std::find(accList.begin(), accList.end(), [accountID UTF8String]) != accList.end()) {
397 auto& account = accMdl_->getAccountInfo([accountID UTF8String]);
398 [accountSelectionManager_ setSavedAccount:account];
399 [self updatePhoto];
400 [self setPopUpButtonSelection];
401 }
402}
403
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400404@end