From bd83885eca89f77a923965e960a14960bb60644b Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Mon, 25 Jun 2012 17:42:31 +0100 Subject: [PATCH] Refs #1527. Backwards compatibility. If no highlight or state masks were encoded (button was encoded prior to them being added), make sure to let the default settings stand. --- AppKit/CPButton.j | 18 ++++++++++++------ Tests/AppKit/CPButtonTest.j | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/AppKit/CPButton.j b/AppKit/CPButton.j index 441331fdf..867eccd25 100644 --- a/AppKit/CPButton.j +++ b/AppKit/CPButton.j @@ -217,17 +217,13 @@ CPButtonImageOffset = 3.0; { if (!anObjectValue || anObjectValue === @"" || ([anObjectValue intValue] === 0)) anObjectValue = CPOffState; - else if (![anObjectValue isKindOfClass:[CPNumber class]]) anObjectValue = CPOnState; - else if (anObjectValue >= CPOnState) anObjectValue = CPOnState - else if (anObjectValue < CPOffState) if ([self allowsMixedState]) anObjectValue = CPMixedState; - else anObjectValue = CPOnState; @@ -895,8 +891,18 @@ var CPButtonImageKey = @"CPButtonImageKey", _alternateTitle = [aCoder decodeObjectForKey:CPButtonAlternateTitleKey]; _allowsMixedState = [aCoder decodeBoolForKey:CPButtonAllowsMixedStateKey]; - _highlightsBy = [aCoder decodeIntForKey:CPButtonHighlightsByKey]; - _showsStateBy = [aCoder decodeIntForKey:CPButtonShowsStateByKey]; + if ([aCoder containsValueForKey:CPButtonHighlightsByKey]) + { + // If one exists, assume both do. + _highlightsBy = [aCoder decodeIntForKey:CPButtonHighlightsByKey]; + _showsStateBy = [aCoder decodeIntForKey:CPButtonShowsStateByKey]; + } + else + { + // Backwards compatibility: if this CPButton was encoded before coding of + // highlightsBy and showsStateBy were added, we should just use the + // default values from _init rather than overwriting with 0, 0. + } [self setImageDimsWhenDisabled:[aCoder decodeObjectForKey:CPButtonImageDimsWhenDisabledKey]]; diff --git a/Tests/AppKit/CPButtonTest.j b/Tests/AppKit/CPButtonTest.j index 1c90409af..3632a7d35 100644 --- a/Tests/AppKit/CPButtonTest.j +++ b/Tests/AppKit/CPButtonTest.j @@ -186,6 +186,30 @@ // The default mask should be that of CPMomentaryPushInButton. [self assert:CPPushInButtonMask | CPGrayButtonMask | CPBackgroundButtonMask equals:[button highlightsBy]]; [self assert:0 equals:[button showsStateBy]]; + + [button setButtonType:CPPushOnPushOffButton]; + + [self assert:CPPushInCellMask | CPChangeGrayCellMask | CPChangeBackgroundCellMask equals:[button highlightsBy]]; + [self assert:CPChangeBackgroundCellMask | CPChangeGrayCellMask equals:[button showsStateBy]]; + + // Test archiving. + + var archived = [CPKeyedArchiver archivedDataWithRootObject:button], + unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived]; + + [self assert:CPPushInCellMask | CPChangeGrayCellMask | CPChangeBackgroundCellMask equals:[button highlightsBy]]; + [self assert:CPChangeBackgroundCellMask | CPChangeGrayCellMask equals:[button showsStateBy]]; + + // Make sure that if highlightsBy and showsStateBy were explicitly set to 0 and 0 (making the button basically + // not react to clicks), these settings are not replaced by the defaults when decoding. + [button setHighlightsBy:0]; + [button setShowsStateBy:0]; + + archived = [CPKeyedArchiver archivedDataWithRootObject:button]; + unarchived = [CPKeyedUnarchiver unarchiveObjectWithData:archived]; + + [self assert:0 equals:[button highlightsBy]]; + [self assert:0 equals:[button showsStateBy]]; } @end