New: give nil values the chance to be transformed to true via CPNegateBoolean (#3103)

This commit is contained in:
daboe01 authored and GitHub committed 2025-04-28 15:40:57 +02:00
1 parent c78a18dc3e
commit 2f563fb870
3 files changed
+26 -6

No files matched your search

+5 -1
View File
@@ -208,7 +208,11 @@ var CPBindingOperationAnd = 0,
options = [_info objectForKey:CPOptionsKey],
newValue = [destination valueForKeyPath:keyPath];
if (CPIsControllerMarker(newValue))
// give nil values the chance to be transformed to true via CPNegateBoolean (issue #1986)
if ((newValue == nil || CPIsControllerMarker(newValue)) && [options objectForKey:CPValueTransformerNameBindingOption] === CPNegateBooleanTransformerName)
[self setValue:[self transformValue:NO withOptions:options] forBinding:theBinding];
else if (CPIsControllerMarker(newValue))
{
[self raiseIfNotApplicable:newValue forKeyPath:keyPath options:options];
+14 -5
View File
@@ -81,7 +81,8 @@
var content =
[
@{ @"state": YES },
@{ @"state": NO }
@{ @"state": NO },
@{ }
],
arrayController = [[CPArrayController alloc] initWithContent:content];
@@ -89,13 +90,21 @@
[control bind:CPValueBinding toObject:arrayController withKeyPath:@"selection.state" options:@{ CPValueTransformerNameBindingOption: @"CPNegateBoolean" }];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:0]];
[self assert:CPOffState equals:[control objectValue] message:"content[0] is negated"]
[self assert:CPOffState equals:[control objectValue] message:"content[0] is negated"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[self assert:CPOnState equals:[control objectValue] message:"content[1] is negated"]
[self assert:CPOnState equals:[control objectValue] message:"content[1] is negated"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:2]];
[self assert:CPOnState equals:[control objectValue] message:"content[2] is negated"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:1]];
[control performClick:nil];
[self assert:CPOffState equals:[control objectValue] message:"value was changed"]
[self assert:YES equals:[content[1] valueForKey:@"state"] message:"content[1] was negated after change"]
[self assert:CPOffState equals:[control objectValue] message:"value was changed"];
[self assert:YES equals:[content[1] valueForKey:@"state"] message:"content[1] was negated after change"];
[arrayController setSelectionIndexes:[CPIndexSet indexSetWithIndex:2]];
[control performClick:nil];
[self assert:CPOffState equals:[control objectValue] message:"value was changed"];
[self assert:YES equals:[content[2] valueForKey:@"state"] message:"content[2] was negated after change"];
}
@end
+7
View File
@@ -89,6 +89,13 @@
[bindTesterA unbind:@"boolValue"];
[bindTesterB bind:@"boolValue" toObject:bindTesterA withKeyPath:@"stringValue" options:[CPDictionary dictionaryWithObject:CPNegateBooleanTransformerName forKey:CPValueTransformerNameBindingOption]];
[bindTesterA setStringValue:nil];
[self assert:nil equals:[bindTesterA stringValue] message:"A value reset"];
[self assert:YES equals:[bindTesterB boolValue] message:"B value updated"];
[bindTesterA unbind:@"boolValue"];
[bindTesterB bind:@"boolValue" toObject:bindTesterA withKeyPath:@"stringValue" options:[CPDictionary dictionaryWithObject:CPIsNilTransformerName forKey:CPValueTransformerNameBindingOption]];
[bindTesterB setBoolValue:NO];
[bindTesterA setStringValue:nil];