CPSplitView: Restore saved layout when setting autosave name

Previously, saved layouts were restored only when loading a cib, so
split views created programmatically saved their layout without ever
restoring it.

Setting the autosave name now restores any saved layout
when subviews are already present, matching NSSplitView.
This commit is contained in:
David Richardson committed 2026-11-24 12:33:17 -06:00
1 parent 2b5c44b973
commit ac8253edfc
3 files changed
+33 -5

No files matched your search

@@ -31,6 +31,7 @@
/*!
Set the name under which the split view divider positions is automatically saved to CPUserDefaults.
A split view with subviews restores the layout saved under the name.
@param autosaveName the name to save under or nil to not save
*/
@@ -40,6 +41,13 @@
return;
_autosaveName = autosaveName;
/*
2026-11-24: only a cib load restored a saved layout, so a split view made in code saved
its layout and never restored it.
*/
if (_autosaveName && _arrangedSubviews.length)
[self _restoreFromAutosave];
}
/*!
+21 -2
View File
@@ -32,8 +32,9 @@
animated divider moves, notifications, hit testing, button bars, autosave, orientation,
arranged subviews and archiving. They come before a refactoring and a rework of
CPSplitView, so they test through public behaviour only. The one exception is
-_restoreFromAutosave: today only a cib load starts a restore, so the autosave tests call
it, as CPSplitViewTest does.
-_restoreFromAutosave, which a cib load calls: most autosave tests call it after they set
the name, as CPSplitViewTest does. Setting the name restores as well, which
testSettingTheAutosaveNameRestoresTheSavedLayout states.
Each test uses a vertical split view 300 points wide and 100 points high, with thin dividers
(1 point under ojtest, where no theme is loaded). The first subview is 100 points wide; the
@@ -646,6 +647,24 @@
[self assert:140 equals:CGRectGetWidth([viewA frame]) message:@"the divider position is restored"];
}
/*
A split view made in code restores its saved layout when it gets the autosave name, as
NSSplitView does.
*/
- (void)testSettingTheAutosaveNameRestoresTheSavedLayout
{
[self useAutosaveStore];
[splitView setAutosaveName:@"CPSplitViewInteractionTest.Name"];
[self dragDivider:0 toX:140];
[[CPRunLoop currentRunLoop] limitDateForMode:CPDefaultRunLoopMode];
[self setUp];
[splitView setAutosaveName:@"CPSplitViewInteractionTest.Name"];
[self assert:140 equals:CGRectGetWidth([viewA frame]) message:@"the divider position is restored"];
[self assert:140 equals:CGRectGetMinX([splitView rectOfDividerAtIndex:0]) message:@"the divider is at the restored position"];
}
/*
A restored layout is the layout that later resizes keep: the next adjustSubviews leaves the
restored sizes in place, and the divider is at the restored position.
+4 -3
View File
@@ -129,9 +129,10 @@
[self setUp];
[splitView setAutosaveName:@"Charles"];
// FIXME At the moment restore from autosave only happens if the split view is loaded from a
// coder. It seems like it should happen when initialising in code too, but some research of
// Cocoa's behaviour will need to be done first. For now, trigger it by hand.
/*
Setting the name restores the layout; the restore a cib load makes gives the same
result.
*/
[splitView _restoreFromAutosave];
[self assert:25 equals:[viewA frameSize].height message:@"divider position restored"];