CPButtonBar: Preserve theme values for keys absent from archive

-initWithCoder: decoded bordered buttons, separator, transparency, and
automatic resize control using -decodeBoolForKey:, which returns NO
when a key is missing.

A CPButtonBar placed in Interface Builder as a
custom view contains none of these keys in its archive, clearing the
theme's values.

Each attribute now uses the archived value when the key
is present and falls back to the theme's value otherwise.
This commit is contained in:
David Richardson committed 2026-12-03 16:48:42 -06:00
1 parent 3611b503e1
commit f62ea9b27e
2 files changed
+61 -4

No files matched your search

+24 -4
View File
@@ -82,6 +82,18 @@ var ButtonBarItemSetBordered = function(anItem, shouldBeBordered)
[anItem setBordered:shouldBeBordered];
};
/*
Returns the setting the archive holds for aKey, or, when it holds none, the value of
the theme attribute that -initWithFrame: reads for the same setting.
*/
var ButtonBarDecodedBool = function(aButtonBar, aCoder, aKey, anAttributeName)
{
if ([aCoder containsValueForKey:aKey])
return !![aCoder decodeBoolForKey:aKey];
return !![aButtonBar currentValueForThemeAttribute:anAttributeName];
};
@protocol CPButtonBarDelegate <CPObject>
@optional
@@ -1145,15 +1157,23 @@ var CPButtonBarHasLeftResizeControlKey = @"CPButtonBarHasLeftResizeControl
{
if (self = [super initWithCoder:aCoder])
{
/*
2026-12-03: the four settings that -initWithFrame: takes from the theme were
decoded with -decodeBoolForKey:, which gives NO for a missing key. A button bar
placed in Interface Builder is a custom view of class CPButtonBar, and its archive
holds none of these keys, so it lost the theme's bordered buttons, separator and
automatic resize control. The cause was that decoding did not tell a missing key
from a stored NO. A missing key now keeps the theme's value.
*/
_hasLeftResizeControl = [aCoder decodeBoolForKey:CPButtonBarHasLeftResizeControlKey];
_hasRightResizeControl = [aCoder decodeBoolForKey:CPButtonBarHasRightResizeControlKey];
_automaticResizeControl = [aCoder decodeBoolForKey:CPButtonBarAutomaticResizeControlKey];
_automaticResizeControl = ButtonBarDecodedBool(self, aCoder, CPButtonBarAutomaticResizeControlKey, @"auto-resize-control");
_hasResizeControl = [aCoder decodeBoolForKey:CPButtonBarHasResizeControlKey];
_resizeControlIsLeftAligned = [aCoder decodeBoolForKey:CPButtonBarResizeControlIsLeftAlignedKey];
[self setButtonsAreBordered:!![aCoder decodeBoolForKey:CPButtonBarButtonsAreBorderedKey]];
[self setDrawsSeparator: !![aCoder decodeBoolForKey:CPButtonBarDrawsSeparatorKey]];
[self setIsTransparent: !![aCoder decodeBoolForKey:CPButtonBarIsTransparentKey]];
[self setButtonsAreBordered:ButtonBarDecodedBool(self, aCoder, CPButtonBarButtonsAreBorderedKey, @"bordered-buttons")];
[self setDrawsSeparator: ButtonBarDecodedBool(self, aCoder, CPButtonBarDrawsSeparatorKey, @"draws-separator")];
[self setIsTransparent: ButtonBarDecodedBool(self, aCoder, CPButtonBarIsTransparentKey, @"is-transparent")];
_buttons = [aCoder decodeObjectForKey:CPButtonBarButtonsKey] || @[];
_rightButtons = [aCoder decodeObjectForKey:CPButtonBarRightButtonsKey] || [];
+37
View File
@@ -158,4 +158,41 @@
[self assert:buttonBar equals:[button superview]];
}
/*
A button bar placed in Interface Builder is a custom view of class CPButtonBar, so its
archive holds only the keys of CPView. It must take the same layout settings from the
theme as a bar made in code.
*/
- (void)testAButtonBarDecodedWithoutItsKeysTakesTheThemeSettings
{
var data = [CPKeyedArchiver archivedDataWithRootObject:[[CPView alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 26.0)]],
unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:data];
[unarchiver setClass:CPButtonBar forClassName:@"CPView"];
var decodedBar = [unarchiver decodeObjectForKey:@"root"];
[unarchiver finishDecoding];
[self assertTrue:[decodedBar isKindOfClass:CPButtonBar] message:@"precondition: the archive decodes as a button bar"];
[self assert:[buttonBar buttonsAreBordered] equals:[decodedBar buttonsAreBordered] message:@"bordered buttons"];
[self assert:[buttonBar drawsSeparator] equals:[decodedBar drawsSeparator] message:@"separator"];
[self assert:[buttonBar isTransparent] equals:[decodedBar isTransparent] message:@"transparency"];
[self assert:[buttonBar automaticResizeControl] equals:[decodedBar automaticResizeControl] message:@"automatic resize control"];
}
/*
A setting the archive holds wins over the theme, also when it is NO.
*/
- (void)testAButtonBarKeepsArchivedSettingsThatDifferFromTheTheme
{
[buttonBar setButtonsAreBordered:![buttonBar buttonsAreBordered]];
[buttonBar setDrawsSeparator:![buttonBar drawsSeparator]];
var decodedBar = [CPKeyedUnarchiver unarchiveObjectWithData:[CPKeyedArchiver archivedDataWithRootObject:buttonBar]];
[self assert:[buttonBar buttonsAreBordered] equals:[decodedBar buttonsAreBordered] message:@"bordered buttons"];
[self assert:[buttonBar drawsSeparator] equals:[decodedBar drawsSeparator] message:@"separator"];
}
@end