Shadows Around Document Views Under Elastic Scrolling On Lion

To break a trend, here’s something technical.

If you run Safari on Lion and pull down past the top of the page you’ll see a blue linen background and a shadow atop the content view. This is because of the new elastic scrolling that Lion has inherited from iOS. By default NSScrollView does not provide this behaviour for you. Moreover it’s not obvious how to implement it.

Creating a custom NSScrollView will fail — NSScrollView doesn’t report a different frame when a document view is offset by an elastic scroll. Neither does it report a different frame for it’s scroll view under those circumstances. This means that we can’t subclass NSScrollView or NSClipView and implement a -drawRect method that will achieve the shadowed content view we’re after.

Here’s how you do it.

Override the -drawRect method of your ScrollViews’ DocumentView. Save the graphics state. Create a new clip path based on outsetting the view’s bounds intersected by the bounds of the superview. Set the new clip path then draw the shadow in the affected regions. Finally, restore the graphics state.

Here’s a simple example:

-(void) drawRect:(NSRect)dirtyRect; { [NSGraphicsContext saveGraphicsState];

NSBezierPath *newClipPath = [NSBezierPath bezierPathWithRect: NSIntersectionRect( NSInsetRect( [self bounds], -5, -5 ), [[self superview] bounds] )];
[newClipPath setClip];

NSBezierPath *outerPath = [NSBezierPath bezierPathWithRect: [self bounds]];

NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor: [NSColor blackColor]];
[shadow setShadowBlurRadius: 5];
[shadow set];

[outerPath stroke];
[shadow release];

[NSGraphicsContext restoreGraphicsState];

}

The trick is knowing how things work — the NSClipView that is the super view of your DocumentView has set up a clipping region inside the renderer. It has assumed you’ll not draw outside your bounds. If you want to draw shadows around your DocumentView to look nice during elastic scrolling you’ll need to draw outside your bounds. That’s how you do it.

As a caveat — do not use -setClip unless you know what you’re doing. Use -addClip to decrease the rendering space which is almost always what you want to do. If you find yourself calling -setClip and you’re not 100% sure you know what you’re doing you’ll find that you’ve ended up drawing all over the toolbar and any other UI you’ve got surrounding the view you’ve decided to get cute with.

About Steve

I’m not the sort of person that has personal heroes. That’s what I thought before last night.

My story is similar to many of those in this community of my generation: I learnt to program on some iteration of the Apple ][ by typing in BASIC programs from books and magazines (mostly games. I wanted to play games and that was one of the few avenues available to me). I followed my love of software (though I spent much of the nineties on OS/2 and Windows NT) until eventually coming back to the Mac when Apple bought NeXT and Steve re-entered the fold. Steven Frank, Gus Mueller and many others have already told the story I’d have told of how I became enthralled with what I do. As time progressed I moved to writing first Macintosh and then iPhone software. Steve even used software I’d worked on during a demonstration at a WWDC. I’ve friends who have met him, I’ve friends who have worked with him, I’ve friends who have spent far too much time sitting outside his office while he picked his favourite desktop picture. There are a million humanizing stories about Steve Jobs that have yet to be told but they’re for those who knew him to share.

When I heard that Steve Jobs had died one of my first thoughts was, “Who do I work to impress now?”

That’s when I realized I’d had a hero figure all this time.

SecondConf 2011

I was flattered to be invited to speak at SecondConf this year. Billed as a, “three-day, Chicago-style, single-track conference” SecondConf should properly be known as, “the successor to the incomparable C4”. Following in the format prescribed by that earlier cult conference, SecondConf delivers the same spirit, intelligence and whimsy. Also, the Chicago style pizza.

The conference went off without a hitch, the organization and preparation were top notch, our hosts were gracious while still driving the event forward, and my fellow speakers were only overshadowed by the calibre of the attendees at hand. Writing a piece about a conference you’ve just attended tempts you to pick out highlights. I think that’d be a mistake though — there was far too much great content and far too many high quality after-conference chats to single anything out in particular. I met a bunch of great people and enjoyed the company of many friends I’ve made over the years I’ve been involved in this business.

I had the good fortune of attending C4 a few times. I met some terrific people there — Lucas Newman and Mike Lee while they were both still at Delicious Monster, Jessie, Maja and Terry too, who’ve all since moved upwards and onwards. With a crowd smaller than WWDC, C4 afforded me the opportunity to spend time with Chris, Gus, Brent, Daniel, my Rogue Amoeba compatriots and alumni, and many, many more. I met my Çingleton co-conspirator Scott Morrison at a C4. Matt Drance was there too, which was a pretty shitty thing for him to do. Still, C4 was a goldmine of intelligent talks, great people, and an invaluable experience for everyone who ever attended.

SecondConf is a worthy successor — don’t miss it next year.