From 8fe67b73dad4ec0f3792ae84ed64c1762d7b8daa Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 22 Sep 2010 13:11:32 +0200 Subject: [PATCH] implement _setCurrentValueIsPlaceholder in CPTextField CPKeyValueBinding will sent _setCurrentValueIsPlaceholder: to indicate to an object that current value is a placeholder and should be displayed as such. This commit implements the method for CPTextField and adds a unit test to test the behavior. --- AppKit/CPTextField.j | 26 ++++++++++++++++++++++++ Tests/AppKit/CPKeyValueBindingTest.j | 30 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/AppKit/CPTextField.j b/AppKit/CPTextField.j index 3afc353b1..0f5b00ebc 100644 --- a/AppKit/CPTextField.j +++ b/AppKit/CPTextField.j @@ -86,6 +86,8 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder"); CPColor _textFieldBackgroundColor; id _placeholderString; + id _originalPlaceholderString; + BOOL _currentValueIsPlaceholder; id _delegate; @@ -780,6 +782,30 @@ CPTextFieldStatePlaceholder = CPThemeState("placeholder"); return _placeholderString; } +- (void)_setCurrentValueIsPlaceholder:(BOOL)isPlaceholder +{ + if (isPlaceholder) + { + // Save the original placeholder value so we can restore it later + // Only do this if the placeholder is not already overridden because the bindings logic might call this method + // several times and we don't want the bindings placeholder to ever become the original placeholder + if (!_currentValueIsPlaceholder) + _originalPlaceholderString = [self placeholderString]; + + // Set the current string value as the current placeholder and clear the string value + [self setPlaceholderString:[self stringValue]]; + [self setStringValue:@""]; + } + else + { + // Restore the original placeholder, the actual textfield value is already correct + // because it was set using setValue:forKey: + [self setPlaceholderString:_originalPlaceholderString]; + } + + _currentValueIsPlaceholder = isPlaceholder; +} + /*! Size to fit has two behavior, depending on if the receiver is an editable text field or not. diff --git a/Tests/AppKit/CPKeyValueBindingTest.j b/Tests/AppKit/CPKeyValueBindingTest.j index 5face5a7a..5055cbad0 100644 --- a/Tests/AppKit/CPKeyValueBindingTest.j +++ b/Tests/AppKit/CPKeyValueBindingTest.j @@ -190,6 +190,29 @@ [self assert:'value' equals:testView.lastKey]; } +- (void)testTextField +{ + var textField = [[CPTextField alloc] initWithFrame:CGRectMakeZero()]; + [textField setPlaceholderString:@"cheese"]; + + + content = [ + [BindingTester testerWithCheese:@"yellow"], + [BindingTester testerWithCheese:@"green"] + ]; + arrayController = [[CPArrayController alloc] initWithContent:content]; + + [arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndexesInRange:CPMakeRange(0, 2)]]; + + var options = [CPDictionary dictionaryWithJSObject:{CPMultipleValuesPlaceholderBindingOption:@"Multiple Values"}]; + [textField bind:@"value" toObject:arrayController withKeyPath:@"selection.cheese" options:options]; + + [self assert:@"Multiple Values" equals:[textField placeholderString]]; + + [arrayController setSelectionIndex:0]; + [self assert:@"cheese" equals:[textField placeholderString]]; +} + - (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changes context:(id)aContext { CPLog(@"here: "+aKeyPath+" value: "+[anObject valueForKey:aKeyPath]); @@ -202,6 +225,13 @@ id cheese; } ++ (id)testerWithCheese:(id)aCheese +{ + var tester = [[self alloc] init]; + [tester setCheese:aCheese]; + return tester; +} + - (void)setCheese:(id)aCheese { cheese = aCheese;