Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
TwUI 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenbrichter committed Jul 1, 2011
1 parent 5d720b0 commit 2a704ca
Show file tree
Hide file tree
Showing 111 changed files with 18,289 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
*.DS_Store
xcuserdata
*.mode1v3
*.pbxuser
build
32 changes: 32 additions & 0 deletions ExampleProject/ConcordeExample/Example-Info.plist
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.example.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
7 changes: 7 additions & 0 deletions ExampleProject/ConcordeExample/Example-Prefix.pch
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'ConcordeExample' target in the 'ConcordeExample' project
//

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
25 changes: 25 additions & 0 deletions ExampleProject/ConcordeExample/ExampleAppDelegate.h
@@ -0,0 +1,25 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import <Cocoa/Cocoa.h>
#import "TUIKit.h"

@interface ExampleAppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
}

@end
53 changes: 53 additions & 0 deletions ExampleProject/ConcordeExample/ExampleAppDelegate.m
@@ -0,0 +1,53 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "ExampleAppDelegate.h"
#import "ExampleView.h"

@implementation ExampleAppDelegate

- (void)dealloc
{
[window release];
[super dealloc];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CGRect b = CGRectMake(0, 0, 500, 450);

window = [[NSWindow alloc] initWithContentRect:b
styleMask:NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setMinSize:NSMakeSize(300, 250)];
[window center];

/*
TUINSView is the bridge between the standard AppKit NSView-based heirarchy and the TUIView-based heirarchy
*/
TUINSView *tuiContainer = [[TUINSView alloc] initWithFrame:b];
[window setContentView:tuiContainer];
[tuiContainer release];

ExampleView *example = [[ExampleView alloc] initWithFrame:b];
tuiContainer.rootView = example;
[example release];

[window makeKeyAndOrderFront:nil];
}

@end
41 changes: 41 additions & 0 deletions ExampleProject/ConcordeExample/ExampleTabBar.h
@@ -0,0 +1,41 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "TUIKit.h"

@class ExampleTabBar;

@protocol ExampleTabBarDelegate <NSObject>
@required
- (void)tabBar:(ExampleTabBar *)tabBar didSelectTab:(NSInteger)index;
@end

/*
An example of how to build a custom UI control, in this case a simple tab bar
*/

@interface ExampleTabBar : TUIView
{
id<ExampleTabBarDelegate> delegate;
NSArray *tabViews;
}

- (id)initWithNumberOfTabs:(NSInteger)nTabs;

@property (nonatomic, assign) id<ExampleTabBarDelegate> delegate;
@property (nonatomic, readonly) NSArray *tabViews;

@end
103 changes: 103 additions & 0 deletions ExampleProject/ConcordeExample/ExampleTabBar.m
@@ -0,0 +1,103 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "ExampleTabBar.h"

@interface ExampleTab : TUIView
@end
@implementation ExampleTab

- (ExampleTabBar *)tabBar
{
return (ExampleTabBar *)self.superview;
}

- (void)mouseDown:(NSEvent *)event
{
[super mouseDown:event]; // always call super when overriding mouseXXX: methods - lots of plumbing happens in TUIView
[self setNeedsDisplay];
}

- (void)mouseUp:(NSEvent *)event
{
[super mouseUp:event];

// rather than a simple -setNeedsDisplay, let's fade it back out
[TUIView animateWithDuration:0.5 animations:^{
[self redraw]; // -redraw forces a .contents update immediately based on drawRect, and it happens inside an animation block, so CoreAnimation gives us a cross-fade for free
}];

if([self eventInside:event]) { // only perform the action if the mouse up happened inside our bounds - ignores mouse down, drag-out, mouse up
[[self tabBar].delegate tabBar:[self tabBar] didSelectTab:self.tag];
}
}

@end

@implementation ExampleTabBar

@synthesize delegate;
@synthesize tabViews;

- (id)initWithNumberOfTabs:(NSInteger)nTabs
{
if((self = [super initWithFrame:CGRectZero])) {
NSMutableArray *_tabViews = [NSMutableArray arrayWithCapacity:nTabs];
for(int i = 0; i < nTabs; ++i) {
ExampleTab *t = [[ExampleTab alloc] initWithFrame:CGRectZero];
t.tag = i;
t.layout = ^(TUIView *v) { // the layout of an individual tab is a function of the superview bounds, the number of tabs, and the current tab index
CGRect b = v.superview.bounds; // reference the passed-in 'v' rather than 't' to avoid a retain cycle
float width = b.size.width / nTabs;
float x = i * width;
return CGRectMake(roundf(x), 0, roundf(width), b.size.height);
};
[self addSubview:t];
[_tabViews addObject:t];
[t release];
}

tabViews = [[NSArray alloc] initWithArray:_tabViews];
}
return self;
}

- (void)dealloc
{
[tabViews release];
[super dealloc];
}

- (void)drawRect:(CGRect)rect
{
// draw tab bar background

CGRect b = self.bounds;
CGContextRef ctx = TUIGraphicsGetCurrentContext();

// gray gradient
CGFloat colorA[] = { 0.85, 0.85, 0.85, 1.0 };
CGFloat colorB[] = { 0.71, 0.71, 0.71, 1.0 };
CGContextDrawLinearGradientBetweenPoints(ctx, CGPointMake(0, b.size.height), colorA, CGPointMake(0, 0), colorB);

// top emboss
CGContextSetRGBFillColor(ctx, 1, 1, 1, 0.5);
CGContextFillRect(ctx, CGRectMake(0, b.size.height-2, b.size.width, 1));
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.3);
CGContextFillRect(ctx, CGRectMake(0, b.size.height-1, b.size.width, 1));
}

@end
26 changes: 26 additions & 0 deletions ExampleProject/ConcordeExample/ExampleTableViewCell.h
@@ -0,0 +1,26 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "TUIKit.h"

@interface ExampleTableViewCell : TUITableViewCell
{
TUITextRenderer *textRenderer;
}

@property (nonatomic, copy) NSAttributedString *attributedString;

@end
79 changes: 79 additions & 0 deletions ExampleProject/ConcordeExample/ExampleTableViewCell.m
@@ -0,0 +1,79 @@
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#import "ExampleTableViewCell.h"

@implementation ExampleTableViewCell

- (id)initWithStyle:(TUITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
textRenderer = [[TUITextRenderer alloc] init];

/*
Add the text renderer to the view so events get routed to it properly.
Text selection, dictionary popup, etc should just work.
You can add more than one.
The text renderer encapsulates an attributed string and a frame.
The attributed string in this case is set by setAttributedString:
which is configured by the table view delegate. The frame needs to be
set before it can be drawn, we do that in drawRect: below.
*/
self.textRenderers = [NSArray arrayWithObjects:textRenderer, nil];
}
return self;
}

- (void)dealloc
{
[textRenderer release];
[super dealloc];
}

- (NSAttributedString *)attributedString
{
return textRenderer.attributedString;
}

- (void)setAttributedString:(NSAttributedString *)attributedString
{
textRenderer.attributedString = attributedString;
[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
CGRect b = self.bounds;
CGContextRef ctx = TUIGraphicsGetCurrentContext();

// light gray background
CGContextSetRGBFillColor(ctx, .97, .97, .97, 1);
CGContextFillRect(ctx, b);

// text
CGRect textRect = CGRectOffset(b, 15, -15);
textRenderer.frame = textRect; // set the frame so it knows where to draw itself
[textRenderer draw];

// emboss
CGContextSetRGBFillColor(ctx, 1, 1, 1, 0.9); // light at the top
CGContextFillRect(ctx, CGRectMake(0, b.size.height-1, b.size.width, 1));
CGContextSetRGBFillColor(ctx, 0, 0, 0, 0.08); // dark at the bottom
CGContextFillRect(ctx, CGRectMake(0, 0, b.size.width, 1));
}

@end

0 comments on commit 2a704ca

Please sign in to comment.