Skip to content

Commit

Permalink
work on making state transitions actually do something
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Aug 2, 2011
1 parent f826a86 commit ffd8c7c
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 21 deletions.
35 changes: 30 additions & 5 deletions lib/UIKit/TUIButton+Content.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,27 +125,52 @@ - (TUIImage *)backgroundImageForState:(TUIControlState)state

- (NSString *)currentTitle
{
return [self titleForState:self.state];
NSString *title = [self titleForState:self.state];
if(title == nil) {
title = [self titleForState:TUIControlStateNormal];
}

return title;
}

- (TUIColor *)currentTitleColor
{
return [self titleColorForState:self.state];
TUIColor *color = [self titleColorForState:self.state];
if(color == nil) {
color = [self titleColorForState:TUIControlStateNormal];
}

return color;
}

- (TUIColor *)currentTitleShadowColor
{
return [self titleShadowColorForState:self.state];
TUIColor *color = [self titleShadowColorForState:self.state];
if(color == nil) {
color = [self titleShadowColorForState:TUIControlStateNormal];
}

return color;
}

- (TUIImage *)currentImage
{
return [self imageForState:self.state];
TUIImage *image = [self imageForState:self.state];
if(image == nil) {
image = [self imageForState:TUIControlStateNormal];
}

return image;
}

- (TUIImage *)currentBackgroundImage
{
return [self backgroundImageForState:self.state];
TUIImage *image = [self backgroundImageForState:self.state];
if(image == nil) {
image = [self backgroundImageForState:TUIControlStateNormal];
}

return image;
}

@end
1 change: 1 addition & 0 deletions lib/UIKit/TUIButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef enum {
struct {
unsigned int dimsInBackground:1;
unsigned int buttonType:8;
unsigned int firstDraw:1;
} _buttonFlags;
}

Expand Down
34 changes: 28 additions & 6 deletions lib/UIKit/TUIButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#import "TUIButton.h"
#import "TUILabel.h"
#import "TUINSView.h"
#import "TUIControl+Private.h"

@interface TUIButton ()

- (void)_update;

@end

@implementation TUIButton

Expand All @@ -31,6 +38,7 @@ - (id)initWithFrame:(CGRect)frame
self.opaque = NO; // won't matter unless image is set
_buttonFlags.buttonType = TUIButtonTypeCustom;
_buttonFlags.dimsInBackground = 1;
_buttonFlags.firstDraw = 1;
}
return self;
}
Expand Down Expand Up @@ -155,6 +163,11 @@ static CGRect ButtonRectCenteredInRect(CGRect a, CGRect b)

- (void)drawRect:(CGRect)r
{
if(_buttonFlags.firstDraw) {
[self _update];
_buttonFlags.firstDraw = 0;
}

CGRect bounds = self.bounds;

BOOL key = [self.nsWindow isKeyWindow];
Expand All @@ -164,11 +177,7 @@ - (void)drawRect:(CGRect)r
alpha = key?alpha:0.5;

TUIImage *backgroundImage = self.currentBackgroundImage;
if(!backgroundImage)
backgroundImage = [self backgroundImageForState:TUIControlStateNormal];
TUIImage *image = self.currentImage;
if(!image)
image = [self imageForState:TUIControlStateNormal];

[backgroundImage drawInRect:[self backgroundRectForBounds:bounds] blendMode:kCGBlendModeNormal alpha:1.0];

Expand Down Expand Up @@ -234,15 +243,28 @@ - (void)mouseDown:(NSEvent *)event
- (void)mouseUp:(NSEvent *)event
{
[super mouseUp:event];
if([event clickCount] < 2) {
// if([event clickCount] < 2) {
if([self eventInside:event]) {
if(![self didDrag]) {
[self sendActionsForControlEvents:TUIControlEventTouchUpInside];
}
} else {
[self sendActionsForControlEvents:TUIControlEventTouchUpOutside];
}
}
// }
}

- (void)_update {
_titleView.text = self.currentTitle;
_titleView.textColor = self.currentTitleColor;
}

- (void)_stateDidChange {
[super _stateDidChange];

[self _update];

[self setNeedsDisplay];
}

@end
7 changes: 5 additions & 2 deletions lib/UIKit/TUIControl+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
// Copyright 2011 Maybe Apps, LLC. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TUIControl.h"

@interface TUIControl_Private : NSObject
@interface TUIControl (Private)

- (void)_stateWillChange;
- (void)_stateDidChange;

@end
16 changes: 8 additions & 8 deletions lib/UIKit/TUIControl+Private.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

#import "TUIControl+Private.h"

@implementation TUIControl_Private
@implementation TUIControl (Private)

- (id)init
- (void)_stateWillChange
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}

- (void)_stateDidChange
{
}

@end
5 changes: 5 additions & 0 deletions lib/UIKit/TUIControl.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#import "TUIControl.h"
#import "TUIControl+Private.h"

@implementation TUIControl

Expand Down Expand Up @@ -64,14 +65,18 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)event
- (void)mouseDown:(NSEvent *)event
{
[super mouseDown:event];
[self _stateWillChange];
_controlFlags.tracking = 1;
[self _stateDidChange];
[self setNeedsDisplay];
}

- (void)mouseUp:(NSEvent *)event
{
[super mouseUp:event];
[self _stateWillChange];
_controlFlags.tracking = 0;
[self _stateDidChange];
[self setNeedsDisplay];
}

Expand Down

0 comments on commit ffd8c7c

Please sign in to comment.