Tests/Manual/People: Show a search field in the sidebar when the toolbar is hidden

This commit is contained in:
David Richardson committed 2026-11-26 10:08:17 -06:00
1 parent b758250176
commit 1d8935e1a1
3 files changed
+79 -3

No files matched your search

@@ -14,6 +14,8 @@
* - CPButtonBar with plus / minus buttons and a split view resize control
* - a button on the right of the button bar that pops up copies of the
* main menu's menus
* - a search field at the top, shown only while the toolbar is hidden; the
* window controller gives it its target and action
*/
@import <AppKit/AppKit.j>
@@ -22,7 +24,9 @@
@import "../MainMenuButton.j"
@import "SourceItemCell/SourceListCellView.j"
var SourceRowHeight = 26.0;
var SourceRowHeight = 26.0,
SearchFieldTopMargin = 28.0,
SearchFieldMargin = 6.0;
@implementation SourceListViewController : CPViewController
@@ -31,6 +35,8 @@ var SourceRowHeight = 26.0;
CPOutlineView _outlineView;
CPButtonBar _buttonBar;
MainMenuButton _mainMenuButton;
CPSearchField _searchField;
CPScrollView _scrollView;
BOOL _suppressSelectionCallbacks;
id _sourceListDelegate @accessors(property=delegate);
@@ -93,6 +99,19 @@ var SourceRowHeight = 26.0;
[scrollView setDocumentView:_outlineView];
[view addSubview:scrollView];
_scrollView = scrollView;
// --- Search field, hidden until the toolbar is hidden
_searchField = [[CPSearchField alloc] initWithFrame:CGRectMakeZero()];
[_searchField sizeToFit];
[_searchField setFrame:CGRectMake(SearchFieldMargin, SearchFieldTopMargin, CGRectGetWidth(frame) - 2 * SearchFieldMargin, CGRectGetHeight([_searchField frame]))];
[_searchField setAutoresizingMask:CPViewWidthSizable];
[_searchField setPlaceholderString:@"Search people"];
[_searchField setSendsSearchStringImmediately:YES];
[_searchField setRecentsAutosaveName:@"People.RecentSearches"];
[_searchField setSearchMenuTemplate:[_searchField defaultSearchMenuTemplate]];
[_searchField setHidden:YES];
[view addSubview:_searchField];
// --- Button bar
_buttonBar = [[CPButtonBar alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(frame) - barHeight, CGRectGetWidth(frame), barHeight)];
@@ -134,6 +153,32 @@ var SourceRowHeight = 26.0;
// ---------------------------------------------------------------- Public
- (CPSearchField)searchField
{
[self view];
return _searchField;
}
/*
Shows or hides the search field. While it shows, the source list starts below it.
*/
- (void)setShowsSearchField:(BOOL)shouldShow
{
[self view];
if ([_searchField isHidden] === !shouldShow)
return;
var scrollFrame = [_scrollView frame],
top = shouldShow ? CGRectGetMaxY([_searchField frame]) + SearchFieldMargin : 0.0;
scrollFrame.size.height += CGRectGetMinY(scrollFrame) - top;
scrollFrame.origin.y = top;
[_scrollView setFrame:scrollFrame];
[_searchField setHidden:!shouldShow];
}
- (id)selectedItem
{
var row = [_outlineView selectedRow];
@@ -17,6 +17,8 @@
* the Edit menu names the change
* - CPMenu menu bar visibility: View ▸ Hide Menu Bar hides the main menu, and the window
* takes the space; the main menu's key equivalents still work
* - key-value observing of the toolbar's visible state: while the toolbar is hidden, the
* sidebar shows its own search field, which mirrors the toolbar's
*/
@import <AppKit/AppKit.j>
@@ -77,6 +79,12 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
[self _buildToolbar];
[window setContentViewController:_splitViewController];
var sidebarSearchField = [_sourceListController searchField];
[sidebarSearchField setTarget:self];
[sidebarSearchField setAction:@selector(searchFieldDidChange:)];
[[window toolbar] addObserver:self forKeyPath:@"visible" options:CPKeyValueObservingOptionInitial context:nil];
[[CPNotificationCenter defaultCenter] addObserver:self
selector:@selector(libraryDidChange:)
name:LibraryDidChangeNotification
@@ -260,9 +268,18 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
// ---------------------------------------------------------------- Search
/*
Both search fields send this action. Each one mirrors the other, so the search string
stays when the toolbar is shown or hidden.
*/
- (void)searchFieldDidChange:(id)sender
{
var searchString = [[_searchField stringValue] stringByTrimmingWhitespace];
var sidebarSearchField = [_sourceListController searchField],
otherField = sender === _searchField ? sidebarSearchField : _searchField;
[otherField setStringValue:[sender stringValue]];
var searchString = [[sender stringValue] stringByTrimmingWhitespace];
if (![searchString length])
_searchPredicate = nil;
@@ -275,7 +292,18 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
- (void)focusSearchField:(id)sender
{
[[self window] makeFirstResponder:_searchField];
var toolbar = [[self window] toolbar];
[[self window] makeFirstResponder:[toolbar isVisible] ? _searchField : [_sourceListController searchField]];
}
/*
The sidebar's search field shows only while the toolbar, with its own search field, is
hidden.
*/
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)aChange context:(id)aContext
{
[_sourceListController setShowsSearchField:![anObject isVisible]];
}
// ---------------------------------------------------------------- Source item actions
@@ -369,6 +397,7 @@ var ToolbarSidebarIdentifier = @"SidebarItem",
if (_searchPredicate)
{
[_searchField setStringValue:@""];
[[_sourceListController searchField] setStringValue:@""];
_searchPredicate = nil;
}
+2
View File
@@ -133,4 +133,6 @@ and `tableView:didDragTableColumn:`.
the freed space at once, and ⌃⌘M shows the menu bar again. The choice survives a reload.
This shows both styles of application menu use.
12. A search field shows at the top of the navigation sidebar only while the toolbar is hidden.
The two search fields mirror each other, so the search string stays when the toolbar is
shown or hidden. Edit ▸ Find focuses whichever field is shown.
An animated move between the two places is optional.