Skip to content

Instantly share code, notes, and snippets.

@j4johnfox
Created October 9, 2009 04:35
Show Gist options
  • Save j4johnfox/205709 to your computer and use it in GitHub Desktop.
Save j4johnfox/205709 to your computer and use it in GitHub Desktop.
//
// MMSLoginToolbarItemView.j
// StepInsideWeb
//
// Created by John Fox on 10/8/09.
// Copyright GroupSmarts, LLC 2009. All rights reserved.
//
@import <AppKit/CPView.j>
@import "MMSAuthenticationController.j"
@implementation MMSLoginToolbarItemView : CPView
{
CPButton userIconButton @accessors;
CPTextField loginStatusLabel @accessors;
MMSUser sessionUser @accessors;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
var y = (CGRectGetMidY(aFrame) - 20);
userIconButton = [[CPButton alloc] initWithFrame:CGRectMake(3, y, 40, 40)];
[userIconButton setTarget:self];
[userIconButton setAction:@selector(loginOrOut:)];
[userIconButton setBordered:NO];
[userIconButton setButtonType:CPMomentaryLightButton];
[self addSubview:userIconButton];
var mainBundle = [CPBundle mainBundle];
var defaultPersonIcon = [[CPImage alloc] initWithContentsOfFile:[mainBundle pathForResource:@"defaultPersonIcon.png"] size:CPSizeMake(35, 35)];
[userIconButton setImage:defaultPersonIcon];
loginStatusLabel = [CPTextField labelWithTitle:CPLocalizedString("loginLabel", "")];
[self addSubview:loginStatusLabel];
var myFrame = CPRectCreateCopy([loginStatusLabel frame]);
myFrame.origin.x = ([userIconButton frame].origin.x +[userIconButton frame].size.width + 3);
myFrame.size.width = (aFrame.size.width - myFrame.origin.x);
myFrame.origin.y = ((aFrame.size.height / 2) - ([loginStatusLabel frame].size.height / 2));
[loginStatusLabel setFrame:myFrame];
return self;
}
- (void)setSessionUser:(MMSUser)aUser
{
log('here in setUser self is ' +self +' loginStatusLabel is ' +loginStatusLabel);
if (aUser)
{
var myStatus = [CPString stringWithFormat:CPLocalizedString("welcomeStatusFormat", ""),
[aUser firstName]];
log('loginStatusLabel is ' +loginStatusLabel +' myStatus is ' +myStatus);
[loginStatusLabel setStringValue:myStatus];
[loginStatusLabel setNeedsDisplay:YES];
}
else
{
[loginStatusLabel setStringValue:CPLocalizedString("loginLabel", "")];
}
sessionUser = aUser;
}
- (void)mouseDown:(CPEvent)theEvent
{
[self loginOrOut:nil];
}
- (void)loginOrOut:(id)sender
{
log('loginOrOut [[MMSAuthenticationController sharedController] sessionUser] is '
+[[MMSAuthenticationController sharedController] sessionUser]);
if ([[MMSAuthenticationController sharedController] sessionUser])
{
[[MMSAuthenticationController sharedController] logout:nil];
}
else
{
[self showLoginPanel:nil];
}
}
- (void)showLoginPanel:(id)sender
{
log('MMSToolbarItemView showLoginPanel');
[[MMSAuthenticationController sharedController] showLoginPanel:sender];
}
@end
@implementation MMSLoginToolbarItemView (CPCoding)
- (id)initWithCoder:(CPCoder)aCoder
{
log('here in MMSLoginToolbarItemView initWithCoder aCoder is ' +aCoder);
self = [super initWithCoder:aCoder];
log('after calling super initWithCoder');
if (self)
{
userIconButton = [aCoder decodeObjectForKey:"userIconButton"];
loginStatusLabel = [aCoder decodeObjectForKey:"loginStatusLabel"];
}
log('initWithCoder will return self');
return self;
}
- (void)encodeWithCoder:(CPCoder)aCoder
{
log('here in MMSLoginToolbarItemView encodeWithCoder userIconButton is ' +userIconButton +' loginStatusLabel is ' +loginStatusLabel);
log('aCoder is ' +aCoder);
// This will come out nil on the other side with decodeObjectForKey:
[aCoder encodeObject:userIconButton forKey:"userIconButton"];
[aCoder encodeObject:loginStatusLabel forKey:"loginStatusLabel"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment