Re: clickCount question
Re: clickCount question
- Subject: Re: clickCount question
- From: James Quick <email@hidden>
- Date: Mon, 21 Jul 2003 16:49:08 -0400
On Monday, July 21, 2003, at 05:46 AM, Julien Jalon wrote:
On Monday, Jul 21, 2003, at 03:39 Europe/Paris, Koen van der Drift
wrote:
So I was thinking, one thing I learned from programming in Cocoa/ObjC
(and other frameworks) is that if you are struggling with the code,
probably the approach is wrong. Maybe I shouldn't test for
doubleClickThreshold within textViewDidChangeSelection? Is there a
possible other way to hook into the click event that will make it
easier to do this - maybe in the NSTextView itself?
This approach is right (but there might be a better solution to get
double click threshold). The only problem that you have is that you
want to discard the one-click event if there is a double-click event
(not exactly... you want to discard the selection change notification
for one-click event if there is a double-click). Since the framework
can't tell at first-click time that there will be a second (or a
third...) click, you have to use that sort of code.
While you could subclass your textField it would export
responsibilities to
that interface element that really don't belong there. You were right
to handle
this in the textViewDidChangeSelection: delegate method.
Julien, was on the track to realize that the time was a key element.
The solution depends not on being clever about what events are
occurring,
but in keeping track of the states which are of interest. The
condition you
are trying to avoid, is setting the value of the control to zero, if it
is part of
a doubleclick sequence. Since we don't know yet what we want the value
to be, but do know how long until we might now, a good technique here
is to schedule a message at a future time.
If it was a for a single click, it will be delivered as it ought.
If we sent a message, and later change our minds, so be it, cancel it.
Somewhere during your startup or initialization look for the preference
setting,
if you might use it elsewhere, otherwise simply set it the first time
the method
is called.
In your delegate method:
- (void)textViewDidChangeSelection:(NSNotification *)aNotification
{
static float doubleClickTime = 0.0;
static NSRange selectedRange;
static SEL takeIntValueFrom;
static NSDate *lastTime = nil;
// Set up locally saved state the first time we get called.
if (doubleClickTime = 0.0) {
NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];
doubleClickTime = [defaults floatForKey:
@"com.apple.mouse.doubleClickThreshold"];
takeIntValueFrom = @selector(takeIntValueFrom:);
selectedRange = 1; //don't cancelPrevious thi is our first time.
if (doubleClickTime == 0.0) {
doubleClickTime = 1.0; // If it didn't work, guess
}
}
// First check out what happened the last time we were called
// Should we cancel a pending message?
if (selectedRange.length == 0 && [lastTIme timeIntervalSinceNow] <
doubleClickTIme) {
[[lengthCell class] cancelPreviousPerformRequestsWithTarget:
lengthCell];
}
// Look at the time.
[lastTIme release];
lastTIme = [[NSDate date] retain];
// If the newly selected length is 0 schedule a future message.
// Otherwise just do it now.
selectedRange = [myTextView selectedRange];
if (selectedRange.range == 0) {
[lengthCell performSelector: takeIntValueFrom
withObject: [NSNumber numberWithInt:0]
afterDelay: doubleClickTime];
} else {
[lengthCell setIntValue: selectedRange.length];
}
}
Note that the state is all kept internal to the function, since I
assumed this
was the purpose it served, obviously it could be put elsewhere.
The kept state must, of course be in static variables, to preserve it
between invocations.
Note that this does impose overhead, but not very much.
A message sent on an empty selection cause by a single click arrive
shortly after the double click time passes.
When the selection changed due to the second click in a double click,
this
should be called in time to cancel the pending empty selection. Note
that this is not perfect and is untested. You may need to add a small
increment
to the click time to stifle the update when the user just sneaks in
under
wire, to account for latency between the clicks and this getting
called, etc.
I hope you found this useful.
jq
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.