Skip to content
Snippets Groups Projects
Commit 194f21ec authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk
Browse files

fix: account selection


Change-Id: I2f09fb3acfb45c827705cfa0d63544961444c3c9
Reviewed-by: default avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent df68076a
No related branches found
No related tags found
No related merge requests found
...@@ -60,6 +60,7 @@ Boolean menuIsOpen; ...@@ -60,6 +60,7 @@ Boolean menuIsOpen;
Boolean menuNeedsUpdate; Boolean menuNeedsUpdate;
NSMenu* accountsMenu; NSMenu* accountsMenu;
NSMenuItem* selectedMenuItem; NSMenuItem* selectedMenuItem;
NSMutableDictionary* menuItemsTags;
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::NewAccountModel*) accMdl delegate:(id <ChooseAccountDelegate> )mainWindow -(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil model:(lrc::api::NewAccountModel*) accMdl delegate:(id <ChooseAccountDelegate> )mainWindow
{ {
...@@ -85,6 +86,7 @@ NSMenuItem* selectedMenuItem; ...@@ -85,6 +86,7 @@ NSMenuItem* selectedMenuItem;
[accountsMenu setDelegate:self]; [accountsMenu setDelegate:self];
accountSelectionButton.menu = accountsMenu; accountSelectionButton.menu = accountsMenu;
[accountSelectionButton setAutoenablesItems:NO]; [accountSelectionButton setAutoenablesItems:NO];
menuItemsTags = [[NSMutableDictionary alloc] init];
[self update]; [self update];
QObject::connect(accMdl_, QObject::connect(accMdl_,
...@@ -122,11 +124,12 @@ NSMenuItem* selectedMenuItem; ...@@ -122,11 +124,12 @@ NSMenuItem* selectedMenuItem;
QObject::connect(accMdl_, QObject::connect(accMdl_,
&lrc::api::NewAccountModel::accountStatusChanged, &lrc::api::NewAccountModel::accountStatusChanged,
[self] (const std::string& accountID) { [self] (const std::string& accountID) {
[self updateMenuItemForAccount:accountID];
if([self selectedAccount].id == accountID) { if([self selectedAccount].id == accountID) {
[self update]; // update account state
return; [self updatePhoto];
[self setPopUpButtonSelection];
} }
[self updateMenuItemForAccount:accountID];
}); });
} }
...@@ -138,9 +141,7 @@ NSMenuItem* selectedMenuItem; ...@@ -138,9 +141,7 @@ NSMenuItem* selectedMenuItem;
auto accountList = accMdl_->getAccountList(); auto accountList = accMdl_->getAccountList();
if (!accountList.empty()) { if (!accountList.empty()) {
const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0)); const auto& fallbackAccount = accMdl_->getAccountInfo(accountList.at(0));
if (accountList.size() == 1) {
[accountSelectionManager_ setSavedAccount:fallbackAccount]; [accountSelectionManager_ setSavedAccount:fallbackAccount];
}
return fallbackAccount; return fallbackAccount;
} else { } else {
NSException* noAccEx = [NSException NSException* noAccEx = [NSException
...@@ -153,13 +154,11 @@ NSMenuItem* selectedMenuItem; ...@@ -153,13 +154,11 @@ NSMenuItem* selectedMenuItem;
} }
-(void) updateMenuItemForAccount: (const std::string&) accountID { -(void) updateMenuItemForAccount: (const std::string&) accountID {
AccountMenuItemView *itemView = NSMenuItem *item =[accountsMenu itemWithTag:[menuItemsTags[@(accountID.c_str())] intValue]];
[accountsMenu itemWithTag:[@(accountID.c_str()) intValue]].view; if(!item) {return;}
if(!itemView) { AccountMenuItemView *itemView =item.view;
return; if(!itemView) {return;}
} [self configureView:itemView forAccount:accountID forMenuItem: item];
[self configureView:itemView forAccount:accountID];
} }
-(void) updateMenu { -(void) updateMenu {
...@@ -175,12 +174,11 @@ NSMenuItem* selectedMenuItem; ...@@ -175,12 +174,11 @@ NSMenuItem* selectedMenuItem;
action:NULL action:NULL
keyEquivalent:@""]; keyEquivalent:@""];
menuBarItem.attributedTitle = [self attributedItemTitleForAccount:account];
AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero]; AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
[self configureView:itemView forAccount:accId]; [self configureView:itemView forAccount:accId forMenuItem: menuBarItem];
if([@(accId.c_str()) intValue] != 0) { int itemTag = arc4random_uniform(1000);
[menuBarItem setTag:[@(accId.c_str()) intValue]]; menuItemsTags[@(accId.c_str())] = [NSNumber numberWithInt: itemTag];
} [menuBarItem setTag:itemTag];
[menuBarItem setView:itemView]; [menuBarItem setView:itemView];
[accountsMenu addItem:menuBarItem]; [accountsMenu addItem:menuBarItem];
} }
...@@ -204,8 +202,9 @@ NSMenuItem* selectedMenuItem; ...@@ -204,8 +202,9 @@ NSMenuItem* selectedMenuItem;
[accountStatus setHidden:accList.empty()]; [accountStatus setHidden:accList.empty()];
} }
-(void) configureView: (AccountMenuItemView *) itemView forAccount:(const std::string&) accountId { -(void) configureView: (AccountMenuItemView *) itemView forAccount:(const std::string&) accountId forMenuItem:(NSMenuItem *) item {
auto& account = accMdl_->getAccountInfo(accountId); auto& account = accMdl_->getAccountInfo(accountId);
item.attributedTitle = [self attributedItemTitleForAccount:account];
[itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())]; [itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())];
NSString* userNameString = [self nameForAccount: account]; NSString* userNameString = [self nameForAccount: account];
[itemView.userNameLabel setStringValue:userNameString]; [itemView.userNameLabel setStringValue:userNameString];
...@@ -323,7 +322,7 @@ NSMenuItem* selectedMenuItem; ...@@ -323,7 +322,7 @@ NSMenuItem* selectedMenuItem;
if(account.profileInfo.type == lrc::api::profile::Type::INVALID){ if(account.profileInfo.type == lrc::api::profile::Type::INVALID){
return; return;
} }
[accountSelectionButton selectItemWithTitle:[self itemTitleForAccount:account]]; [accountSelectionButton selectItemWithTag:[menuItemsTags[@(account.id.c_str())] intValue]];
} }
@catch (NSException *ex) { @catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment