Detect autocomplete in NSSearchField
up vote
0
down vote
favorite
I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.
The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.
Autocompletion is off for "sends whole string" mode so that's a no go.
How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?
PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.
- (void)windowDidLoad {
[super windowDidLoad];
MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}
- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}
- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}
- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}
- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}
- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];
if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];
}
#pragma mark NSControlTextEditingDelegate
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}
#pragma mark NSSearchFieldDelegate
- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}
- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}
objective-c macos cocoa mapkit nssearchfield
add a comment |
up vote
0
down vote
favorite
I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.
The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.
Autocompletion is off for "sends whole string" mode so that's a no go.
How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?
PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.
- (void)windowDidLoad {
[super windowDidLoad];
MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}
- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}
- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}
- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}
- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}
- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];
if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];
}
#pragma mark NSControlTextEditingDelegate
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}
#pragma mark NSSearchFieldDelegate
- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}
- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}
objective-c macos cocoa mapkit nssearchfield
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.
The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.
Autocompletion is off for "sends whole string" mode so that's a no go.
How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?
PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.
- (void)windowDidLoad {
[super windowDidLoad];
MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}
- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}
- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}
- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}
- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}
- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];
if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];
}
#pragma mark NSControlTextEditingDelegate
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}
#pragma mark NSSearchFieldDelegate
- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}
- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}
objective-c macos cocoa mapkit nssearchfield
I am trying to implement location suggestions within the searchfield. I can get all the suggestions, however I am not able to find out when user selects completion using the keyboard enter or mouse click.
The problem is that user needs to press enter to select completion (dissmiss completion menu window) and hit enter again to submit it to recents. I would love to avoid second hit enter keypress.
Autocompletion is off for "sends whole string" mode so that's a no go.
How to detect autocomplete selection on NSSearchField? Or how to trigger autocompletion with "sends whole search string"?
PS: There are undocumented notifications NSTextViewDidBeginDisplayingCompletions, NSTextViewDidEndDisplayingCompletions which might be used.
- (void)windowDidLoad {
[super windowDidLoad];
MKLocalSearchCompleter *searchCompleter = [[MKLocalSearchCompleter alloc] init];
searchCompleter.filterType = MKSearchCompletionFilterTypeLocationsOnly;
searchCompleter.delegate = self;
[self setSearchCompleter:searchCompleter];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = YES;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"NSTextViewDidEndDisplayingCompletions" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
self.displayingCompletions = NO;
if ([[self searchCompleter] isSearching] == NO) {
[self search:nil];
}
}];
}
- (void)completerDidUpdateResults:(MKLocalSearchCompleter *)completer
{
[self setSearchAutocompletions:[completer results]];
[self displayAutocompletions];
}
- (void)completer:(MKLocalSearchCompleter *)completer didFailWithError:(NSError *)error
{
if (error) {
NSLog(@"%@",error);
}
}
- (IBAction)autocompleteSearch:(NSSearchField *)sender
{
if ([[sender stringValue] length] < 3) {
return;
}
if (self.isDisplayingCompletions) {
return;
}
NSString *searchString = [sender stringValue];
[[self searchCompleter] cancel];
[[self searchCompleter] setQueryFragment:searchString];
}
- (void)displayAutocompletions
{
if (self.isSearching && !self.completePosting) {
_completePosting = YES;
[[[self window] fieldEditor:YES forObject:[self searchField]] complete:nil];
_completePosting = NO;
}
}
- (void)controlTextDidEndEditing:(NSNotification *)obj
{
[self search:nil];
}
- (void)search:(id)sender
{
NSString *searchString = [[self searchField] stringValue];
if ([searchString length] <= 3) {
return;
}
MKLocalSearchCompletion *completionToSearch;
NSArray<MKLocalSearchCompletion *> *results = [[self searchCompleter] results];
for (MKLocalSearchCompletion *completion in results) {
if ([[completion title] isEqualToString:searchString]) {
completionToSearch = completion;
break;
}
}
// MKCoordinateRegion boundingRegion = [[self mapView] region];
MKLocalSearchRequest *request;
if (completionToSearch) {
request = [[MKLocalSearchRequest alloc] initWithCompletion:completionToSearch];
} else {
request = [[MKLocalSearchRequest alloc] init];
[request setNaturalLanguageQuery:searchString];
}
//[request setRegion:boundingRegion];
[[self localSearch] cancel];
MKLocalSearch *localSearch = [[MKLocalSearch alloc] initWithRequest:request];
[self setLocalSearch:localSearch];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error);
} else {
[[self mapView] setRegion:[response boundingRegion]];
MKMapItem *item = [response.mapItems firstObject];
[[self searchField] setStringValue:[item name]];
MKPlacemark *placemark = [item placemark];
if (placemark) {
CLLocationCoordinate2D coordinate = placemark.coordinate;
NSLog(@"%@", placemark);
}
}
}];
}
#pragma mark NSControlTextEditingDelegate
- (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index
{
NSMutableArray *matches = [[NSMutableArray alloc] init];
for (MKLocalSearchCompletion *completion in [self searchAutocompletions]) {
[matches addObject:[completion title]];
}
*index = -1;
return matches;
}
#pragma mark NSSearchFieldDelegate
- (void)searchFieldDidStartSearching:(NSSearchField *)sender
{
_searching = YES;
}
- (void)searchFieldDidEndSearching:(NSSearchField *)sender
{
_searching = NO;
}
objective-c macos cocoa mapkit nssearchfield
objective-c macos cocoa mapkit nssearchfield
edited Nov 9 at 10:34
asked Nov 8 at 16:46
Marek H
2,2821222
2,2821222
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08
add a comment |
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212400%2fdetect-autocomplete-in-nssearchfield%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of How to make NSSearchField send action upon autocompletion?
– Marek H
Nov 9 at 10:08