mirror of
https://github.com/cappuccino/cappuccino.git
synced 2026-10-30 20:07:36 +00:00
Slightly faster _couldBeKeyEquivalent (1% of runtime in 3000 calls). Performance test included.
This commit is contained in:
1 parent
17669fd09d
commit
5ed475eedd
2 files changed
+89
-14
No files matched your search
+13
-14
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
@import <Foundation/CPObject.j>
|
||||
@import "CPText.j"
|
||||
|
||||
#include "CoreGraphics/CGGeometry.h"
|
||||
|
||||
@@ -539,22 +540,20 @@ var _CPEventPeriodicEventPeriod = 0,
|
||||
|
||||
- (BOOL)_couldBeKeyEquivalent
|
||||
{
|
||||
// FIXME: More cases? Space?
|
||||
return _type === CPKeyDown &&
|
||||
((_modifierFlags & (CPCommandKeyMask | CPControlKeyMask) &&
|
||||
[_characters length] > 0) ||
|
||||
[self _hasActionCharacter]);
|
||||
}
|
||||
if (_type !== CPKeyDown)
|
||||
return NO;
|
||||
|
||||
- (BOOL)_hasActionCharacter
|
||||
{
|
||||
var characters = [self characters],
|
||||
characterCount = [characters length];
|
||||
var characterCount = _characters.length;
|
||||
|
||||
if (!characterCount)
|
||||
return NO;
|
||||
|
||||
if (_modifierFlags & (CPCommandKeyMask | CPControlKeyMask))
|
||||
return YES;
|
||||
|
||||
for(var i=0; i<characterCount; i++)
|
||||
{
|
||||
var c = [characters characterAtIndex:i];
|
||||
switch(c)
|
||||
switch(_characters.charAt(i))
|
||||
{
|
||||
case CPBackspaceCharacter:
|
||||
case CPDeleteCharacter:
|
||||
@@ -568,10 +567,10 @@ var _CPEventPeriodicEventPeriod = 0,
|
||||
case CPRightArrowFunctionKey:
|
||||
case CPDownArrowFunctionKey:
|
||||
return YES;
|
||||
default:
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
// FIXME: More cases? Space?
|
||||
return NO;
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
@import <AppKit/CPApplication.j>
|
||||
@import <AppKit/CPWindow.j>
|
||||
@import <AppKit/CPEvent.j>
|
||||
@import <AppKit/CPButton.j>
|
||||
|
||||
[CPApplication sharedApplication];
|
||||
|
||||
@implementation CPKeyEquivalentPerformance : OJTestCase
|
||||
|
||||
- (void)testKeyEquivalentSpeed
|
||||
{
|
||||
var REPEATS = 1000,
|
||||
theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(0,0,200,150)
|
||||
styleMask:CPWindowNotSizable],
|
||||
contentView = [theWindow contentView],
|
||||
subView1 = [[CPView alloc] initWithFrame:CGRectMakeZero()],
|
||||
subView2 = [[CPView alloc] initWithFrame:CGRectMakeZero()],
|
||||
button1 = [CPButton buttonWithTitle:"when"],
|
||||
button2 = [CPButton buttonWithTitle:"you have eliminated"],
|
||||
button3 = [CPButton buttonWithTitle:"the impossible"];
|
||||
|
||||
[contentView addSubview:subView1];
|
||||
[contentView addSubview:subView2];
|
||||
[subView1 addSubview:button1];
|
||||
[subView2 addSubview:button2];
|
||||
[subView2 addSubview:button3];
|
||||
|
||||
[button1 setTarget:self];
|
||||
[button1 setAction:@selector(clicked:)];
|
||||
[button1 setKeyEquivalent:"a"];
|
||||
[button1 setKeyEquivalentModifierMask:CPControlKeyMask];
|
||||
button1.clicks = 0;
|
||||
|
||||
[button2 setTarget:self];
|
||||
[button2 setAction:@selector(clicked:)];
|
||||
[button2 setKeyEquivalent:"a"];
|
||||
[button2 setKeyEquivalentModifierMask:CPAlternateKeyMask|CPCommandKeyMask];
|
||||
button2.clicks = 0;
|
||||
|
||||
[button3 setTarget:self];
|
||||
[button3 setAction:@selector(clicked:)];
|
||||
[button3 setKeyEquivalent:"A"];
|
||||
[button3 setKeyEquivalentModifierMask:CPControlKeyMask];
|
||||
button3.clicks = 0;
|
||||
|
||||
var start = (new Date).getTime();
|
||||
|
||||
for (var i=0; i<REPEATS; i++)
|
||||
{
|
||||
[theWindow sendEvent:[CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:CPControlKeyMask
|
||||
timestamp:nil windowNumber:nil context:nil
|
||||
characters:"a" charactersIgnoringModifiers:"a" isARepeat:NO keyCode:0]];
|
||||
[theWindow sendEvent:[CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:CPAlternateKeyMask|CPCommandKeyMask
|
||||
timestamp:nil windowNumber:nil context:nil
|
||||
characters:"a" charactersIgnoringModifiers:"a" isARepeat:NO keyCode:0]];
|
||||
[theWindow sendEvent:[CPEvent keyEventWithType:CPKeyDown location:CGPointMakeZero() modifierFlags:CPControlKeyMask|CPShiftKeyMask
|
||||
timestamp:nil windowNumber:nil context:nil
|
||||
characters:"a" charactersIgnoringModifiers:"a" isARepeat:NO keyCode:0]];
|
||||
}
|
||||
|
||||
var end = (new Date).getTime();
|
||||
|
||||
[self assert:REPEATS equals:button1.clicks message:"button1"];
|
||||
[self assert:REPEATS equals:button2.clicks message:"button2"];
|
||||
[self assert:REPEATS equals:button3.clicks message:"button3"];
|
||||
|
||||
CPLog.warn("testKeyEquivalentSpeed: "+(end-start)+"ms");
|
||||
|
||||
}
|
||||
|
||||
- (void)clicked:(id)sender
|
||||
{
|
||||
sender.clicks++;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user