Re: Subclassing NSToolBar
Re: Subclassing NSToolBar
- Subject: Re: Subclassing NSToolBar
- From: Brian Webster <email@hidden>
- Date: Thu, 4 Oct 2001 12:56:17 -0500
On Thursday, October 4, 2001, at 08:26 AM, cocoa-dev-
email@hidden wrote:
I'd like to have a custom view that behaves like NSToolBarView, so that
when you drag its items around, you get that nice smooth animation as
things shift to make room for what you're dragging. I'm trying to get
an idea of whether it's quicker to re-implement or to subclass
NSToolBar
to do what I want.
First of all, NSToolBarView isn't published API, so I'd have to
snarf it
with class-dump. Secondly, when playing around with the toolbar in
Mail, I can see that there are times when there's more than one icon
sliding. Thirdly, I have a need for a vertical orientation as well as
the existing horizontal mode.
From this description of what you want, I think it would be
better to make your own class rather than trying to leverage
stuff off of NSToolbar. It'd probably be much more trouble than
it's worth (especially the vertically oriented option), not to
mention having rely on some private APIs.
Creating the animation shouldn't be too incredibly hard. Doing
it without the animation is pretty easy, yes? Just calculate
the new position of each item after the insertion and move it
there. You might want to implement this first to get the basic
position calculations working correctly. To put in the
animation, instead of setting each item's position immediately,
have a second position for each item, let's call it the item's
"target position".
Upon entry, instead of setting each item's actual position, as
we do when there is no animation, instead set its target
position. Then set up an NSTimer to fire how ever often you
want the animation to occur. On each firing, loop through the
items and compare the item's actual position to its target
position. If they are not equal, move the item's actual
position by some increment toward its target position. This
will move multiple items gradually to where they need to be.
Whenever the dragged item moves around into a different position
on the toolbar, just reset all the item's target positions again.
Does that make sense?
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster