Commit d73f960e authored by Kateryna Kostiuk's avatar Kateryna Kostiuk Committed by Andreas Traczyk

account: refactor account selection

Change-Id: Ib45a5388d6e6860970c1dd2a31709d0aaea123d2
Reviewed-by: Andreas Traczyk's avatarAndreas Traczyk <andreas.traczyk@savoirfairelinux.com>
parent ecaa3952
......@@ -37,6 +37,8 @@
#import "views/AccountMenuItemView.h"
#import "AccountSelectionManager.h"
#import "RingWindowController.h"
#import "utils.h"
#import "views/NSColor+RingTheme.h"
@interface ChooseAccountVC () <NSMenuDelegate>
......@@ -45,6 +47,7 @@
@implementation ChooseAccountVC {
__unsafe_unretained IBOutlet NSImageView* profileImage;
__unsafe_unretained IBOutlet NSTextField* accountStatus;
__unsafe_unretained IBOutlet NSPopUpButton* accountSelectionButton;
const lrc::api::NewAccountModel* accMdl_;
AccountSelectionManager* accountSelectionManager_;
......@@ -69,10 +72,12 @@ NSMenuItem* selectedMenuItem;
[profileImage setWantsLayer: YES];
profileImage.layer.cornerRadius = profileImage.frame.size.width / 2;
profileImage.layer.masksToBounds = YES;
profileImage.layer.backgroundColor = [[NSColor ringGreyLight] CGColor];
accountsMenu = [[NSMenu alloc] initWithTitle:@""];
[accountsMenu setDelegate:self];
accountSelectionButton.menu = accountsMenu;
[accountSelectionButton setAutoenablesItems:NO];
[self update];
QObject::connect(accMdl_,
......@@ -107,6 +112,11 @@ NSMenuItem* selectedMenuItem;
[self] (const std::string& accountID) {
[self update];
});
QObject::connect(accMdl_,
&lrc::api::NewAccountModel::accountStatusChanged,
[self] (const std::string& accountID) {
[self update];
});
}
-(const lrc::api::account::Info&) selectedAccount
......@@ -146,29 +156,65 @@ NSMenuItem* selectedMenuItem;
menuBarItem.attributedTitle = [self attributedItemTitleForAccount:account];
AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
[itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())];
NSString* userNameString = [self nameForAccount: account];
[itemView.userNameLabel setStringValue:userNameString];
switch (account.profileInfo.type) {
case lrc::api::profile::Type::SIP:
[itemView.accountTypeLabel setStringValue:@"SIP"];
break;
case lrc::api::profile::Type::RING:
[itemView.accountTypeLabel setStringValue:@"RING"];
break;
default:
break;
[self configureView:itemView forAccount:accId];
if([@(accId.c_str()) intValue] != 0) {
[menuBarItem setTag:[@(accId.c_str()) intValue]];
}
bool isAccountAlone = accList.size() == 1;
[[accountSelectionButton cell] setArrowPosition: (isAccountAlone)?NSPopUpNoArrow:NSPopUpArrowAtBottom];
[accountSelectionButton setEnabled:!isAccountAlone];
[menuBarItem setView:itemView];
[accountsMenu addItem:menuBarItem];
[accountsMenu addItem:[NSMenuItem separatorItem]];
}
// create "add a new account" menu item
NSMenuItem* menuBarItem = [[NSMenuItem alloc]
initWithTitle:@""
action:nil
keyEquivalent:@""];
AccountMenuItemView *itemView = [[AccountMenuItemView alloc] initWithFrame:CGRectZero];
[itemView.accountAvatar setHidden:YES];
[itemView.accountStatus setHidden:YES];
[itemView.accountTypeLabel setHidden:YES];
[itemView.userNameLabel setHidden:YES];
[itemView.accountLabel setHidden:YES];
[itemView.createNewAccount setAction:@selector(createNewAccount:)];
[itemView.createNewAccount setTarget:self];
[menuBarItem setView: itemView];
[accountsMenu addItem: menuBarItem];
[[accountSelectionButton itemAtIndex:[accountsMenu numberOfItems] -1] setEnabled:NO];
}
-(void) configureView: (AccountMenuItemView *) itemView forAccount:(const std::string&) accountId {
auto& account = accMdl_->getAccountInfo(accountId);
[itemView.accountLabel setStringValue:@(account.profileInfo.alias.c_str())];
NSString* userNameString = [self nameForAccount: account];
[itemView.userNameLabel setStringValue:userNameString];
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil);
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
[itemView.accountAvatar setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
} else {
[itemView.accountAvatar setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
}
BOOL accountNotRegistered = account.status == lrc::api::account::Status::REGISTERED ? NO : YES;
[itemView.accountStatus setHidden:accountNotRegistered];
switch (account.profileInfo.type) {
case lrc::api::profile::Type::SIP:
[itemView.accountTypeLabel setStringValue:@"SIP"];
break;
case lrc::api::profile::Type::RING:
[itemView.accountTypeLabel setStringValue:@"RING"];
break;
default:
break;
}
[itemView.createNewAccount setHidden:YES];
[itemView.createNewAccountImage setHidden:YES];
}
- (void)createNewAccount:(id)sender {
[accountSelectionButton.menu cancelTrackingWithoutAnimation];
[delegate createNewAccount];
}
-(void) updatePhoto
......@@ -181,7 +227,13 @@ NSMenuItem* selectedMenuItem;
QByteArray ba = QByteArray::fromStdString(account.profileInfo.avatar);
QVariant photo = GlobalInstances::pixmapManipulator().personPhoto(ba, nil);
[profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
if(QtMac::toNSImage(qvariant_cast<QPixmap>(photo))) {
[profileImage setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(photo))];
} else {
[profileImage setImage: [NSImage imageNamed:@"default_avatar_overlay.png"]];
}
BOOL accountNotRegistered = account.status == lrc::api::account::Status::REGISTERED ? NO : YES;
[accountStatus setHidden:accountNotRegistered];
}
@catch (NSException *ex) {
NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
......@@ -189,23 +241,22 @@ NSMenuItem* selectedMenuItem;
}
-(NSString*) nameForAccount:(const lrc::api::account::Info&) account {
auto name = account.registeredName;
return @(name.c_str());
return bestIDForAccount(account);
}
-(NSString*) itemTitleForAccount:(const lrc::api::account::Info&) account {
NSString* alias = @(account.profileInfo.alias.c_str());
NSString* alias = bestNameForAccount(account);
NSString* userNameString = [self nameForAccount: account];
if([userNameString length] > 0) {
if(![alias isEqualToString:userNameString]) {
alias = [NSString stringWithFormat: @"%@\n", alias];
}
return [alias stringByAppendingString:userNameString];
}
- (NSAttributedString*) attributedItemTitleForAccount:(const lrc::api::account::Info&) account {
NSString* alias = @(account.profileInfo.alias.c_str());
NSString* alias = bestNameForAccount(account);
NSString* userNameString = [self nameForAccount: account];
if([userNameString length] > 0){
if(![alias isEqualToString:userNameString]) {
alias = [NSString stringWithFormat: @"%@\n", alias];
}
NSFont *fontAlias = [NSFont userFontOfSize:14.0];
......
......@@ -79,4 +79,6 @@ namespace lrc {
*/
-(void)allAccountsDeleted;
- (void) createNewAccount;
@end
......@@ -55,10 +55,12 @@
#import "views/BackgroundView.h"
#import "ChooseAccountVC.h"
#import "utils.h"
#import "RingWizardWC.h"
@interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate>
@property (retain) MigrateRingAccountsWC* migrateWC;
@property RingWizardWC* wizard;
@end
......@@ -87,6 +89,7 @@ static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier";
@synthesize dataTransferModel, accountModel, behaviorController;
@synthesize wizard;
-(id) initWithWindowNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountModel:( lrc::api::NewAccountModel*)accountModel dataTransferModel:( lrc::api::DataTransferModel*)dataTransferModel behaviourController:( lrc::api::BehaviorController*) behaviorController
{
......@@ -448,4 +451,11 @@ NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarIte
return nil;
}
- (void) createNewAccount {
wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: self.accountModel];
[wizard showChooseWithCancelButton: YES andAdvanced: YES];
[self.window beginSheet:wizard.window completionHandler:nil];
[wizard showWindow:self];
}
@end
......@@ -46,6 +46,22 @@ static inline NSString* bestIDForConversation(const lrc::api::conversation::Info
return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
}
static inline NSString* bestIDForAccount(const lrc::api::account::Info& account)
{
if (!account.registeredName.empty()) {
return [@(account.registeredName.c_str()) removeEmptyLinesAtBorders];
}
return [@(account.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
}
static inline NSString* bestNameForAccount(const lrc::api::account::Info& account)
{
if (account.profileInfo.alias.empty()) {
return bestIDForAccount(account);
}
return @(account.profileInfo.alias.c_str());
}
static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
{
auto contact = model.owner.contactModel->getContact(conv.participants[0]);
......
......@@ -25,6 +25,10 @@
@property (nonatomic, strong) IBOutlet NSTextField* accountLabel;
@property (nonatomic, strong) IBOutlet NSTextField* userNameLabel;
@property (nonatomic, strong) IBOutlet NSTextField* accountTypeLabel;
@property (nonatomic, strong) IBOutlet NSBox* backgroundView;
@property (nonatomic, strong) IBOutlet NSImageView* accountAvatar;
@property (nonatomic, strong) IBOutlet NSTextField* accountStatus;
@property (nonatomic, strong) IBOutlet NSButton* createNewAccount;
@property (nonatomic, strong) IBOutlet NSImageView* createNewAccountImage;
@end
......@@ -46,6 +46,11 @@
self.frame = viewFromXib.frame;
self.containerView = viewFromXib;
[self addSubview:self.containerView];
[self.accountAvatar setWantsLayer:YES];
self.accountAvatar.layer.cornerRadius = self.accountAvatar.frame.size.width * 0.5;
self.accountAvatar.layer.masksToBounds = YES;
[self.accountStatus setWantsLayer:YES];
[self.accountAvatar.layer setBackgroundColor:[[NSColor ringGreyLight] CGColor]];
}
}
......@@ -64,11 +69,11 @@
NSMenuItem *menuItem = ([self enclosingMenuItem]);
BOOL isHighlighted = [menuItem isHighlighted];
if (isHighlighted) {
[[NSColor ringGreyHighlight] set];
[NSBezierPath fillRect:rect];
[self.backgroundView setFillColor:[NSColor controlLightHighlightColor]];
} else {
[super drawRect: rect];
[self.backgroundView setFillColor:[NSColor controlColor]];
}
[super drawRect: rect];
}
@end
This diff is collapsed.
......@@ -9,6 +9,7 @@
<customObject id="-2" userLabel="File's Owner" customClass="ChooseAccountVC">
<connections>
<outlet property="accountSelectionButton" destination="sMr-ym-Q7q" id="aWB-iE-k1f"/>
<outlet property="accountStatus" destination="3O0-W6-QCM" id="5i7-92-c94"/>
<outlet property="profileImage" destination="JSg-rZ-Xia" id="keh-R2-VI8"/>
<outlet property="view" destination="Hz6-mo-xeY" id="0bl-1N-x8E"/>
</connections>
......@@ -26,19 +27,42 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<imageView horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="JSg-rZ-Xia">
<rect key="frame" x="5" y="0.0" width="30" height="30"/>
<rect key="frame" x="13" y="0.0" width="30" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="V0B-Kr-2Kw"/>
<constraint firstAttribute="width" constant="30" id="b29-LU-Chc"/>
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="riX-D8-veh"/>
</imageView>
<textField horizontalHuggingPriority="750" verticalHuggingPriority="750" tag="500" translatesAutoresizingMaskIntoConstraints="NO" id="3O0-W6-QCM" userLabel="AccountStatus" customClass="RoundedTextField">
<rect key="frame" x="35" y="0.0" width="10" height="11"/>
<constraints>
<constraint firstAttribute="width" constant="6.5" id="4Ks-ia-6rg"/>
<constraint firstAttribute="height" constant="10.5" id="GGv-tJ-9Ps"/>
</constraints>
<textFieldCell key="cell" enabled="NO" sendsActionOnEndEditing="YES" alignment="center" id="Wdm-ve-lDX">
<font key="font" metaFont="system"/>
<color key="textColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="bgColor">
<color key="value" red="0.29803921570000003" green="0.85098039219999999" blue="0.3921568627" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="borderColor">
<color key="value" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderThickness">
<real key="value" value="0.5"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</textField>
<popUpButton translatesAutoresizingMaskIntoConstraints="NO" id="sMr-ym-Q7q">
<rect key="frame" x="38" y="6" width="159" height="19"/>
<constraints>
<constraint firstAttribute="width" constant="155" id="RHk-BK-YeF"/>
</constraints>
<popUpButtonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" lineBreakMode="truncatingTail" state="on" borderStyle="bezel" inset="2" selectedItem="CTF-me-3R0" id="AKJ-ja-nkI">
<popUpButtonCell key="cell" type="check" bezelStyle="regularSquare" imagePosition="left" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="bezel" inset="2" selectedItem="CTF-me-3R0" id="AKJ-ja-nkI">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system" size="16"/>
<menu key="menu" id="R46-PB-Vne">
......@@ -58,10 +82,12 @@
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="sMr-ym-Q7q" secondAttribute="trailing" constant="5" id="BD9-ZS-cZa"/>
<constraint firstItem="sMr-ym-Q7q" firstAttribute="leading" secondItem="JSg-rZ-Xia" secondAttribute="trailing" constant="5" id="L9t-7Q-Cw2"/>
<constraint firstItem="sMr-ym-Q7q" firstAttribute="leading" secondItem="JSg-rZ-Xia" secondAttribute="trailing" constant="-3" id="L9t-7Q-Cw2"/>
<constraint firstItem="JSg-rZ-Xia" firstAttribute="top" secondItem="HA7-fk-gse" secondAttribute="top" id="Rw2-Sd-7Hz"/>
<constraint firstItem="sMr-ym-Q7q" firstAttribute="centerY" secondItem="JSg-rZ-Xia" secondAttribute="centerY" id="S2w-ph-Hue"/>
<constraint firstAttribute="bottom" secondItem="JSg-rZ-Xia" secondAttribute="bottom" id="VyP-Jt-INt"/>
<constraint firstItem="3O0-W6-QCM" firstAttribute="trailing" secondItem="JSg-rZ-Xia" secondAttribute="trailing" id="ogd-CG-BuC"/>
<constraint firstItem="3O0-W6-QCM" firstAttribute="bottom" secondItem="JSg-rZ-Xia" secondAttribute="bottom" id="r5p-jD-VkY"/>
</constraints>
</view>
</box>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment