diff --git a/AppKit/CPSearchField.j b/AppKit/CPSearchField.j index 0feb14bcb..69da1d846 100644 --- a/AppKit/CPSearchField.j +++ b/AppKit/CPSearchField.j @@ -497,12 +497,45 @@ var RECENT_SEARCH_PREFIX = @" "; [super mouseDown:anEvent]; } +/*! + Provides the common case items for a recent searches menu. If there are not recent searches, + displays a single disabled item: + + No Recent Searches + + If there are 1 more recent searches, it displays: + + Recent Searches + recent search 1 + recent search 2 + etc. + --------------------- + Clear Recent Searches + + If you wish to add items before or after the template, you can. If you put items + before, a separator will automatically be placed before the default template item. + If you add items after the default template, it is your responsibility to add a separator. + + To add a custom item: + + item = [[CPMenuItem alloc] initWithTitle:@"google" + action:@selector(google:) + keyEquivalent:@""]; + [item setTag:700]; + [item setTarget:self]; + [template addItem:item]; + + Be sure that your custom items do not use tags in the range 1000-1004 inclusive. + If you wish to maintain state in custom menu items that you add, you will need to maintain + the item state yourself, then in the action method of the custom items, modify the items + in the search menu template and send [searchField setSearchMenuTemplate:template] to update the menu. +*/ - (CPMenu)defaultSearchMenuTemplate { var template = [[CPMenu alloc] init], item; - item = [[CPMenuItem alloc] initWithTitle:@"Recent searches" + item = [[CPMenuItem alloc] initWithTitle:@"Recent Searches" action:nil keyEquivalent:@""]; [item setTag:CPSearchFieldRecentsTitleMenuItemTag]; @@ -516,36 +549,20 @@ var RECENT_SEARCH_PREFIX = @" "; [item setTarget:self]; [template addItem:item]; - item = [[CPMenuItem alloc] initWithTitle:@"Clear recent searches" + item = [[CPMenuItem alloc] initWithTitle:@"Clear Recent Searches" action:@selector(_searchFieldClearRecents:) keyEquivalent:@""]; [item setTag:CPSearchFieldClearRecentsMenuItemTag]; [item setTarget:self]; [template addItem:item]; - item = [[CPMenuItem alloc] initWithTitle:@"No recent searches" + item = [[CPMenuItem alloc] initWithTitle:@"No Recent Searches" action:nil keyEquivalent:@""]; [item setTag:CPSearchFieldNoRecentsMenuItemTag]; [item setEnabled:NO]; [template addItem:item]; - /* - To add a separator: - - [self _addSeparatorToMenu:template]; - - - To add a custom item: - - item = [[CPMenuItem alloc] initWithTitle:@"google" - action:@selector(_google:) - keyEquivalent:@""]; - [item setTag:@"google"]; - [item setTarget:self]; - [template addItem:item]; - */ - return template; } @@ -603,6 +620,9 @@ var RECENT_SEARCH_PREFIX = @" "; case CPSearchFieldNoRecentsMenuItemTag: if (countOfRecents !== 0) continue; + + if ([menu numberOfItems] > 0) + [self _addSeparatorToMenu:menu]; break; case CPSearchFieldSeparatorMenuItemTag: diff --git a/Tests/Manual/CPSearchField/AppController.j b/Tests/Manual/CPSearchField/AppController.j index a637de37b..d4d4abd24 100644 --- a/Tests/Manual/CPSearchField/AppController.j +++ b/Tests/Manual/CPSearchField/AppController.j @@ -8,22 +8,28 @@ @import @import -var categories = ["firstName","lastName"]; +var categories = ["firstName","lastName"], + MenuItemPrefix = @" "; @implementation AppController : CPObject { - CPSearchField searchField; - CPTableView table; + CPSearchField searchField; + CPTableView table; - CPArray tableArray; - CPArray filteredArray; + CPArray tableArray; + CPArray filteredArray; - CPInteger searchCategoryIndex; + CPMenu searchMenuTemplate; + + CPArray searchCategoryIndexes; + CPInteger searchCategoryIndex; } - (void)applicationDidFinishLaunching:(CPNotification)aNotification { searchCategoryIndex = 0; + searchCategoryIndexes = [CPArray arrayWithArray:[1, 2]]; + var theWindow = [[CPWindow alloc] initWithContentRect:CPMakeRect(0,10,500,300) styleMask:CPTitledWindowMask|CPResizableWindowMask], contentView = [theWindow contentView]; @@ -37,43 +43,32 @@ var categories = ["firstName","lastName"]; [searchField setTarget:self]; [searchField setAction:@selector(updateFilter:)]; [searchField setAutoresizingMask:CPViewWidthSizable]; - - var template = [[CPMenu alloc] initWithTitle:@"Search Menu"]; + + searchMenuTemplate = [searchField defaultSearchMenuTemplate]; - var item; - item = [[CPMenuItem alloc] initWithTitle:@"Search by" - action:nil - keyEquivalent:@""]; - [item setEnabled:NO]; - [template addItem:item]; + [searchMenuTemplate insertItemWithTitle:@"Search By" + action:nil + keyEquivalent:@"" + atIndex:0]; - item = [[CPMenuItem alloc] initWithTitle:@"First Name" - action:@selector(changeCategory:) - keyEquivalent:@""]; + var item = [[CPMenuItem alloc] initWithTitle:MenuItemPrefix + @"First Name" + action:@selector(changeCategory:) + keyEquivalent:@""]; + [item setTarget:self]; [item setTag:1]; - [template addItem:item]; + [item setState:CPOnState]; + [searchMenuTemplate insertItem:item atIndex:1]; - item = [[CPMenuItem alloc] initWithTitle:@"Last Name" - action:@selector(changeCategory:) + item = [[CPMenuItem alloc] initWithTitle:MenuItemPrefix + @"Last Name" + action:@selector(changeCategory:) keyEquivalent:@""]; [item setTarget:self]; [item setTag:2]; - [template addItem:item]; - - item = [[CPMenuItem alloc] initWithTitle:@"Recent searches" action:NULL keyEquivalent:@""]; - [item setTag:CPSearchFieldRecentsTitleMenuItemTag]; - [template addItem:item]; - - item = [[CPMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]; - [item setTag:CPSearchFieldRecentsMenuItemTag]; - [template addItem:item]; + [item setState:CPOffState]; + [searchMenuTemplate insertItem:item atIndex:2]; - item = [[CPMenuItem alloc] initWithTitle:@"Clear recents" action:NULL keyEquivalent:@""]; - [item setTag:CPSearchFieldClearRecentsMenuItemTag]; - [template addItem:item]; - - [searchField setSearchMenuTemplate:template]; + [searchField setSearchMenuTemplate:searchMenuTemplate]; var button = [[CPButton alloc] initWithFrame:CGRectMakeZero()]; [button setBackgroundColor:[CPColor greenColor]]; @@ -133,16 +128,25 @@ var categories = ["firstName","lastName"]; - (void)changeCategory:(CPMenuItem)menuItem { - [[[searchField menu] itemArray] makeObjectsPerformSelector:@selector(setState:) withObject:0]; - [menuItem setState:1]; searchCategoryIndex = [menuItem tag] - 1; - [searchField setPlaceholderString:[menuItem title]]; + [searchField setPlaceholderString:[[menuItem title] substringFromIndex:[MenuItemPrefix length]]]; + + [self _updateSearchMenuTemplate]; +} + +- (void)_updateSearchMenuTemplate +{ + for (var i = 0; i < searchCategoryIndexes.length; ++i) + [[searchMenuTemplate itemAtIndex:i + 1] setState:CPOffState]; + + [[searchMenuTemplate itemAtIndex:searchCategoryIndex + 1] setState:CPOnState]; + [searchField setSearchMenuTemplate:searchMenuTemplate]; } - (void)updateFilter:(id)sender { var searchString = [searchField stringValue]; - + filteredArray = [self filteredArrayWithString:searchString]; [table reloadData]; }