SmartFoldersDemo: Replace Reset Library with Reset Application

Reset Application restores the sample data, default toolbar configuration,
sidebar width, detail pane state, recent searches, and automatic
appearance without requiring a page reload.

Fixes an inverted button-tag assignment in the confirmation alert:
-[CPAlert buttons] returns buttons in reverse order of addition, so
assigning tags by positional index bound them to the wrong controls—causing
Reset to do nothing and Cancel to trigger the reset. The alert now assigns
button tags by title.
This commit is contained in:
David Richardson committed 2026-11-19 14:55:52 -06:00
1 parent 379bf69973
commit 87d052fbd0
4 files changed
+56 -13

No files matched your search

@@ -16,7 +16,8 @@
* └─ NameSheetController (sheet)
*
* Data (people, groups, smart folders) is persisted in CPUserDefaults.
* Use File ▸ Reset Library… to return to the deterministic sample data.
* Use File ▸ Reset Application… to return to the deterministic sample data and the
* default toolbar, layout and appearance.
*
* The application catches no exception. It tests the frameworks, so a
* framework failure must show and must not fall back to a default. If the
@@ -100,7 +101,7 @@
[self _addItem:@"Rename Group…" action:@selector(renameSelectedGroup:) key:nil modifiers:0 to:fileMenu];
[self _addItem:@"Delete Group or Smart Folder" action:@selector(deleteSelectedSourceItem:) key:nil modifiers:0 to:fileMenu];
[fileMenu addItem:[CPMenuItem separatorItem]];
[self _addItem:@"Reset Library…" action:@selector(resetLibrary:) key:nil modifiers:0 to:fileMenu];
[self _addItem:@"Reset Application…" action:@selector(resetApplication:) key:nil modifiers:0 to:fileMenu];
[fileMenu addItem:[CPMenuItem separatorItem]];
[self _addItem:@"Settings…" action:@selector(showSettings:) key:@"," modifiers:0 to:fileMenu];
@@ -22,6 +22,7 @@
@import "PeopleViewController.j"
@import "SmartFolderEditorController.j"
@import "NameSheetController.j"
@import "SettingsWindowController.j"
var ToolbarSidebarIdentifier = @"SidebarItem",
ToolbarNewFolderIdentifier = @"NewSmartFolderItem",
@@ -506,7 +507,7 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
[self _selectSourceItem:[_library allPeopleItem]];
}
- (void)resetLibrary:(id)sender
- (void)resetApplication:(id)sender
{
if ([self _isSheetVisible])
return;
@@ -514,16 +515,21 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
var alert = [[CPAlert alloc] init];
[alert setAlertStyle:CPWarningAlertStyle];
[alert setMessageText:@"Reset the library?"];
[alert setInformativeText:@"All people, groups and smart folders will be replaced by the sample data. This cannot be undone."];
[alert setMessageText:@"Reset the application?"];
[alert setInformativeText:@"All people, groups and smart folders will be replaced by the sample data. The toolbar, the sidebar, the detail pane, the recent searches and the appearance return to their defaults. This cannot be undone."];
[alert addButtonWithTitle:@"Reset"];
[alert addButtonWithTitle:@"Cancel"];
// CPAlert reports the clicked button's tag as the return code. Assign our own
// tags rather than relying on a framework-specific numbering scheme.
var buttons = [alert buttons];
[buttons[0] setTag:ResetAlertResetTag];
[buttons[1] setTag:ResetAlertCancelTag];
/*
CPAlert reports the clicked button's tag as the return code. The application gives its
own tags, found by title. -[CPAlert buttons] lists the buttons in the reverse order of
addition, unlike AppKit, so tags given by index went to the wrong buttons: Reset did
nothing and Cancel reset the application.
*/
[[alert buttons] enumerateObjectsUsingBlock:function(aButton)
{
[aButton setTag:[aButton title] === @"Reset" ? ResetAlertResetTag : ResetAlertCancelTag];
}];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
@@ -537,10 +543,27 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
return;
[_searchField setStringValue:@""];
[_searchField setRecentSearches:[]];
_searchPredicate = nil;
_selectedItem = [_library allPeopleItem];
[_library resetToSampleData];
var toolbar = [[self window] toolbar];
[toolbar setConfigurationFromDictionary:@{
@"TB Item Identifiers": [self toolbarDefaultItemIdentifiers:toolbar],
@"TB Display Mode": CPToolbarDisplayModeDefault,
@"TB Size Mode": CPToolbarSizeModeDefault
}];
[toolbar setVisible:YES];
[_splitView setPosition:SidebarDefaultWidth ofDividerAtIndex:0];
if (![_peopleController isDetailVisible])
[_peopleController toggleDetail];
[SettingsWindowController resetAppearance];
}
// ---------------------------------------------------------------- Validation
@@ -559,7 +582,7 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
{
case @selector(newSmartFolder:):
case @selector(newGroup:):
case @selector(resetLibrary:):
case @selector(resetApplication:):
return !sheet;
case @selector(editSelectedSmartFolder:):
+1 -1
View File
@@ -48,4 +48,4 @@ the other icons and the badges are drawn with `CPBezierPath`.
toolbar stays (autosave). While the sheet is open, the toolbar items do not act.
The standard Colors item opens the colour panel. In both appearances its labels, the boxes
around the preview and the swatches, and the opacity slider's track and knob show.
13. File ▸ Reset Library…: alert sheet, then sample data restored.
13. File ▸ Reset Application…: alert sheet, then sample data, default toolbar (items, Icon and Text, regular size), sidebar width, detail pane, empty recent searches and Auto appearance restored at once, without a reload. Cancel changes nothing.
@@ -11,7 +11,8 @@
var SettingsAppearanceKey = @"SmartFoldersDemoAppearance",
SettingsAppearanceNames = [CPAppearanceNameAqua, CPAppearanceNameDarkAqua, nil],
SettingsAutoSegment = 2;
SettingsAutoSegment = 2,
SettingsAppearanceDidResetNotification = @"SettingsAppearanceDidResetNotification";
@implementation SettingsWindowController : CPWindowController
{
@@ -29,6 +30,17 @@ var SettingsAppearanceKey = @"SmartFoldersDemoAppearance",
[self _applyAppearanceOfSegment:segment == nil ? SettingsAutoSegment : segment];
}
/*
Removes the kept appearance, so the application follows the system appearance again. An
open Settings window shows Auto.
*/
+ (void)resetAppearance
{
[[CPUserDefaults standardUserDefaults] removeObjectForKey:SettingsAppearanceKey];
[self _applyAppearanceOfSegment:SettingsAutoSegment];
[[CPNotificationCenter defaultCenter] postNotificationName:SettingsAppearanceDidResetNotification object:self];
}
+ (void)_applyAppearanceOfSegment:(int)aSegment
{
var name = SettingsAppearanceNames[aSegment];
@@ -66,12 +78,19 @@ var SettingsAppearanceKey = @"SmartFoldersDemoAppearance",
var segment = [[CPUserDefaults standardUserDefaults] objectForKey:SettingsAppearanceKey];
[_appearanceControl setSelectedSegment:segment == nil ? SettingsAutoSegment : segment];
[[CPNotificationCenter defaultCenter] addObserver:self selector:@selector(appearanceDidReset:) name:SettingsAppearanceDidResetNotification object:nil];
[panel center];
}
return self;
}
- (void)appearanceDidReset:(CPNotification)aNotification
{
[_appearanceControl setSelectedSegment:SettingsAutoSegment];
}
- (void)appearanceDidChange:(id)aSender
{
var segment = [aSender selectedSegment];