Skip to content

Commit

Permalink
Improve token field subclassing support.
Browse files Browse the repository at this point in the history
Token field now triggers autocompletion from textDidChange: which better meets the expectations of Cappuccino programmers, and is closer to how Cocoa does it.

Eliminated custom non-Cappuccino key press handler.
  • Loading branch information
aljungberg committed Jun 16, 2012
1 parent 429933f commit 5683af9
Showing 1 changed file with 17 additions and 35 deletions.
52 changes: 17 additions & 35 deletions AppKit/CPTokenField.j
Expand Up @@ -46,8 +46,6 @@ var CPTokenFieldDOMInputElement = nil,
CPTokenFieldFocusInput = NO,

CPTokenFieldBlurFunction = nil,
CPTokenFieldKeyUpFunction = nil,
CPTokenFieldKeyPressFunction = nil,
CPTokenFieldKeyDownFunction = nil;

#endif
Expand Down Expand Up @@ -610,36 +608,6 @@ var CPScrollDestinationNone = 0,
return true;
};

CPTokenFieldKeyPressFunction = function(aDOMEvent)
{
aDOMEvent = aDOMEvent || window.event;

var character = String.fromCharCode(aDOMEvent.keyCode || aDOMEvent.which),
owner = CPTokenFieldInputOwner;

if ([[owner tokenizingCharacterSet] characterIsMember:character])
{
if (aDOMEvent.preventDefault)
aDOMEvent.preventDefault();
if (aDOMEvent.stopPropagation)
aDOMEvent.stopPropagation();
aDOMEvent.cancelBubble = true;

[owner _autocompleteWithEvent:aDOMEvent];
[owner setNeedsLayout];

return true;
}

[CPTokenFieldInputOwner _delayedShowCompletions];
// If there was a selection, collapse it now since we're typing in a new token.
owner._selectedRange.length = 0;

// Force immediate layout in case word wrapping is now necessary.
[owner setNeedsLayout];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
};

CPTokenFieldKeyUpFunction = function()
{
if ([CPTokenFieldInputOwner stringValue] !== CPTokenFieldTextDidChangeValue)
Expand All @@ -663,12 +631,10 @@ var CPScrollDestinationNone = 0,
if (document.attachEvent)
{
CPTokenFieldDOMInputElement.attachEvent("on" + CPDOMEventKeyUp, CPTokenFieldKeyUpFunction);
CPTokenFieldDOMInputElement.attachEvent("on" + CPDOMEventKeyPress, CPTokenFieldKeyPressFunction);
}
else
{
CPTokenFieldDOMInputElement.addEventListener(CPDOMEventKeyUp, CPTokenFieldKeyUpFunction, NO);
CPTokenFieldDOMInputElement.addEventListener(CPDOMEventKeyPress, CPTokenFieldKeyPressFunction, NO);
}

//FIXME make this not onblur
Expand Down Expand Up @@ -706,7 +672,7 @@ var CPScrollDestinationNone = 0,
{
[[CPTokenFieldInputOwner _autocompleteMenu] selectNext];
}
else if (character === CPNewlineCharacter || character === CPCarriageReturnCharacter || character === CPTabCharacter)
else if (character === CPNewlineCharacter || character === CPCarriageReturnCharacter || character === CPTabCharacter || [_tokenizingCharacterSet characterIsMember:character])
{
// Only resign first responder if we weren't auto-completing
if (![self hasThemeState:CPThemeStateAutocompleting])
Expand Down Expand Up @@ -820,6 +786,22 @@ var CPScrollDestinationNone = 0,
}
}

- (void)textDidChange:(CPNotification)aNotification
{
if ([aNotification object] !== self)
return;

[super textDidChange:aNotification];

// For future reference: in Cocoa, textDidChange: appears to call [self complete:].
[self _delayedShowCompletions];
// If there was a selection, collapse it now since we're typing in a new token.
_selectedRange.length = 0;

// Force immediate layout in case word wrapping is now necessary.
[self setNeedsLayout];
}

// - (void)setTokenStyle: (NSTokenStyle) style;
// - (NSTokenStyle)tokenStyle;
//
Expand Down

0 comments on commit 5683af9

Please sign in to comment.