Permalink
Browse files

Scroller controls selection (and vice-versa)

  • Loading branch information...
1 parent 997bd8d commit a9d965ca247a2cb5c280eb4d9f19807a98290dd6 @mattball committed Mar 27, 2009
Showing with 34 additions and 3 deletions.
  1. +3 −1 MBCoverFlowScroller.h
  2. +15 −1 MBCoverFlowScroller.m
  3. +16 −1 MBCoverFlowView.m
View
@@ -10,7 +10,9 @@
@interface MBCoverFlowScroller : NSScroller {
-
+ NSUInteger _numberOfIncrements;
}
+@property (nonatomic, assign) NSUInteger numberOfIncrements;
+
@end
View
@@ -21,6 +21,8 @@ - (NSRect)_drawingRectForPart:(NSScrollerPart)aPart;
@implementation MBCoverFlowScroller
+@synthesize numberOfIncrements=_numberOfIncrements;
+
- (void)drawRect:(NSRect)rect
{
[self drawKnobSlotInRect:[self rectForPart:NSScrollerKnobSlot] highlight:NO] ;
@@ -71,7 +73,9 @@ - (NSRect)rectForPart:(NSScrollerPart)aPart
rect.size.width = fmax(maxWidth * [self knobProportion], minWidth);
rect.origin.x = NSMaxX([self rectForPart:NSScrollerDecrementLine]) - 8.0 - 1.0;
- rect.origin.x += [self floatValue] * (maxWidth - rect.size.width);
+
+ float incrementWidth = (maxWidth - rect.size.width) / (self.numberOfIncrements);
+ rect.origin.x += [self integerValue] * incrementWidth;
return rect;
} else if (aPart == NSScrollerDecrementPage) {
@@ -83,6 +87,16 @@ - (NSRect)rectForPart:(NSScrollerPart)aPart
return NSZeroRect;
}
+- (NSInteger)integerValue
+{
+ return floor([self floatValue] * (self.numberOfIncrements));
+}
+
+- (void)setIntegerValue:(NSInteger)value
+{
+ [self setFloatValue:((float)value / (float)self.numberOfIncrements)+0.01];
+}
+
/* The documentation for NSScroller says to use -drawArrow:highlight:, but
* that's never called. -drawArrow:highlightPart: is.
*/
View
@@ -64,6 +64,8 @@ - (id)initWithFrame:(NSRect)frameRect
_scroller = [[MBCoverFlowScroller alloc] initWithFrame:NSMakeRect(10, 10, 400, 16)];
[_scroller setKnobProportion:1.0];
[_scroller setEnabled:YES];
+ [_scroller setTarget:self];
+ [_scroller setAction:@selector(scrollerWasClicked:)];
[self addSubview:_scroller];
_leftTransform = CATransform3DMakeRotation(-0.79, 0, -1, 0);
@@ -320,6 +322,7 @@ - (void)setContent:(NSArray *)newContents
}
}
+ [_scroller setNumberOfIncrements:([self.content count]-1)];
self.selectionIndex = self.selectionIndex;
}
@@ -371,7 +374,6 @@ - (void)setItemSize:(NSSize)newSize
- (void)setSelectionIndex:(NSInteger)newIndex
{
if (newIndex >= [[_scrollLayer sublayers] count] || newIndex < 0) {
- NSBeep();
return;
}
@@ -388,6 +390,7 @@ - (void)setSelectionIndex:(NSInteger)newIndex
// Scroll so the selected item is centered
[_scrollLayer scrollToPoint:CGPointMake([self _positionOfSelectedItem], layerFrame.origin.y)];
+ [_scroller setIntegerValue:self.selectionIndex];
}
- (void)setAccessoryController:(NSViewController *)aController
@@ -514,6 +517,18 @@ - (float)_positionOfSelectedItem
return floor(MBCoverFlowViewHorizontalMargin + .5*([_scrollLayer bounds].size.width - [self itemSize].width * [[_scrollLayer sublayers] count] - MBCoverFlowViewCellSpacing * ([[_scrollLayer sublayers] count] - 1))) + self.selectionIndex * ([self itemSize].width + MBCoverFlowViewCellSpacing) - .5 * [_scrollLayer bounds].size.width + .5 * [self itemSize].width;
}
+- (void)scrollerWasClicked:(NSScroller *)sender
+{
+ NSScrollerPart clickedPart = [sender hitPart];
+ if (clickedPart == NSScrollerIncrementLine) {
+ self.selectionIndex += 1;
+ } else if (clickedPart == NSScrollerDecrementLine) {
+ self.selectionIndex -= 1;
+ } else if (clickedPart == NSScrollerKnob) {
+ self.selectionIndex = [sender integerValue];
+ }
+}
+
#pragma mark -
#pragma mark Protocol Methods

0 comments on commit a9d965c

Please sign in to comment.