зеркало из https://github.com/mozilla/pjs.git
handle cmd-enter in urlbar and search field to open new tab or new window
depending on pref. refactors load logic a little better (bug 247238)
This commit is contained in:
Родитель
2f5825e73e
Коммит
50e371fb68
|
@ -81,6 +81,19 @@ static const int kEscapeKeyCode = 53;
|
||||||
mSuppressMakeKeyFront = inSuppress;
|
mSuppressMakeKeyFront = inSuppress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pass command-return off to the controller so that locations/searches may be opened in a new tab
|
||||||
|
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
|
||||||
|
{
|
||||||
|
BrowserWindowController* windowController = (BrowserWindowController*)[self delegate];
|
||||||
|
NSString* keyString = [theEvent charactersIgnoringModifiers];
|
||||||
|
unichar keyChar = [keyString characterAtIndex:0];
|
||||||
|
BOOL handled = NO;
|
||||||
|
if (keyChar == NSCarriageReturnCharacter) {
|
||||||
|
handled = [windowController handleCommandReturn];
|
||||||
|
}
|
||||||
|
return handled ? handled : [super performKeyEquivalent:theEvent];
|
||||||
|
}
|
||||||
|
|
||||||
// accessor for the 'URL' Apple Event attribute
|
// accessor for the 'URL' Apple Event attribute
|
||||||
- (NSString*)getURL
|
- (NSString*)getURL
|
||||||
{
|
{
|
||||||
|
|
|
@ -363,4 +363,7 @@ typedef enum
|
||||||
// not hold onto this for longer than the current call unless they addref it.
|
// not hold onto this for longer than the current call unless they addref it.
|
||||||
- (nsIWebNavigation*) currentWebNavigation;
|
- (nsIWebNavigation*) currentWebNavigation;
|
||||||
|
|
||||||
|
// handle command-return in location or search field
|
||||||
|
- (BOOL) handleCommandReturn;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -128,6 +128,12 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
|
|
||||||
#define kMaxBrowserWindowTabs 16
|
#define kMaxBrowserWindowTabs 16
|
||||||
|
|
||||||
|
enum BWCOpenDest {
|
||||||
|
kDestinationNewWindow = 0,
|
||||||
|
kDestinationNewTab,
|
||||||
|
kDestinationCurrentView
|
||||||
|
};
|
||||||
|
|
||||||
//////////////////////////////////////
|
//////////////////////////////////////
|
||||||
@interface AutoCompleteTextFieldEditor : NSTextView
|
@interface AutoCompleteTextFieldEditor : NSTextView
|
||||||
{
|
{
|
||||||
|
@ -349,6 +355,8 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
-(void)openNewWindowWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
|
-(void)openNewWindowWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
|
||||||
-(void)openNewTabWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
|
-(void)openNewTabWithDescriptor:(nsISupports*)aDesc displayType:(PRUint32)aDisplayType loadInBackground:(BOOL)aLoadInBG;
|
||||||
- (BOOL)isPageTextFieldFocused;
|
- (BOOL)isPageTextFieldFocused;
|
||||||
|
-(void)performSearch:(SearchTextField *)inSearchField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG;
|
||||||
|
-(void)goToLocationFromToolbarURLField:(AutoCompleteTextField *)inURLField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG;
|
||||||
|
|
||||||
// create back/forward session history menus on toolbar button
|
// create back/forward session history menus on toolbar button
|
||||||
- (IBAction)backMenu:(id)inSender;
|
- (IBAction)backMenu:(id)inSender;
|
||||||
|
@ -1439,24 +1447,37 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (IBAction)goToLocationFromToolbarURLField:(id)sender
|
- (IBAction)goToLocationFromToolbarURLField:(id)sender
|
||||||
|
{
|
||||||
|
if ([sender isKindOfClass:[AutoCompleteTextField class]])
|
||||||
|
[self goToLocationFromToolbarURLField:(AutoCompleteTextField *)sender
|
||||||
|
inView:kDestinationCurrentView inBackground:NO];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)goToLocationFromToolbarURLField:(AutoCompleteTextField *)inURLField
|
||||||
|
inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG
|
||||||
{
|
{
|
||||||
// trim off any whitespace around url
|
// trim off any whitespace around url
|
||||||
NSString *theURL = [[sender stringValue] stringByTrimmingWhitespace];
|
NSString *theURL = [[inURLField stringValue] stringByTrimmingWhitespace];
|
||||||
|
|
||||||
// look for bookmarks keywords match
|
// look for bookmarks keywords match
|
||||||
NSArray *resolvedURLs = [[BookmarkManager sharedBookmarkManager] resolveBookmarksKeyword:theURL];
|
NSArray *resolvedURLs = [[BookmarkManager sharedBookmarkManager] resolveBookmarksKeyword:theURL];
|
||||||
|
|
||||||
NSString* resolvedURL = nil;
|
NSString* resolvedURL = nil;
|
||||||
if ([resolvedURLs count] == 1)
|
if ([resolvedURLs count] == 1) {
|
||||||
{
|
|
||||||
resolvedURL = [resolvedURLs lastObject];
|
resolvedURL = [resolvedURLs lastObject];
|
||||||
[self loadURL:resolvedURL referrer:nil activate:YES];
|
if (inDest == kDestinationNewTab)
|
||||||
|
[self openNewTabWithURL:resolvedURL referrer:nil loadInBackground:inLoadInBG];
|
||||||
|
else if (inDest == kDestinationNewWindow)
|
||||||
|
[self openNewWindowWithURL:resolvedURL referrer:nil loadInBackground:inLoadInBG];
|
||||||
|
else // if it's not a new window or a new tab, load into the current view
|
||||||
|
[self loadURL:resolvedURL referrer:nil activate:YES];
|
||||||
|
} else {
|
||||||
|
if (inDest == kDestinationNewTab || inDest == kDestinationNewWindow)
|
||||||
|
[self openURLArray:resolvedURLs replaceExistingTabs:NO];
|
||||||
|
else
|
||||||
|
[self openURLArray:resolvedURLs replaceExistingTabs:YES];
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
[self openURLArray:resolvedURLs replaceExistingTabs:YES];
|
|
||||||
}
|
|
||||||
|
|
||||||
// global history needs to know the user typed this url so it can present it
|
// global history needs to know the user typed this url so it can present it
|
||||||
// in autocomplete. We use the URI fixup service to strip whitespace and remove
|
// in autocomplete. We use the URI fixup service to strip whitespace and remove
|
||||||
// invalid protocols, etc. Don't save keyword-expanded urls.
|
// invalid protocols, etc. Don't save keyword-expanded urls.
|
||||||
|
@ -1465,14 +1486,13 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
nsAutoString url;
|
nsAutoString url;
|
||||||
[theURL assignTo_nsAString:url];
|
[theURL assignTo_nsAString:url];
|
||||||
NS_ConvertUCS2toUTF8 utf8URL(url);
|
NS_ConvertUCS2toUTF8 utf8URL(url);
|
||||||
|
|
||||||
nsCOMPtr<nsIURI> fixedURI;
|
nsCOMPtr<nsIURI> fixedURI;
|
||||||
mURIFixer->CreateFixupURI(utf8URL, 0, getter_AddRefs(fixedURI));
|
mURIFixer->CreateFixupURI(utf8URL, 0, getter_AddRefs(fixedURI));
|
||||||
if (fixedURI)
|
if (fixedURI)
|
||||||
mGlobalHistory->MarkPageAsTyped(fixedURI);
|
mGlobalHistory->MarkPageAsTyped(fixedURI);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)saveDocument:(BOOL)focusedFrame filterView:(NSView*)aFilterView
|
- (void)saveDocument:(BOOL)focusedFrame filterView:(NSView*)aFilterView
|
||||||
{
|
{
|
||||||
[[mBrowserView getBrowserView] saveDocument:focusedFrame filterView:aFilterView];
|
[[mBrowserView getBrowserView] saveDocument:focusedFrame filterView:aFilterView];
|
||||||
|
@ -1543,70 +1563,91 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
- (IBAction)performSearch:(id)aSender
|
- (IBAction)performSearch:(id)aSender
|
||||||
{
|
{
|
||||||
// If we have a valid SearchTextField, perform a search using its contents
|
// If we have a valid SearchTextField, perform a search using its contents
|
||||||
if ([aSender isKindOfClass:[SearchTextField class]]) {
|
if ([aSender isKindOfClass:[SearchTextField class]])
|
||||||
// Get the search URL from our dictionary of sites and search urls
|
[self performSearch:(SearchTextField *)aSender inView:kDestinationCurrentView inBackground:NO];
|
||||||
NSMutableString *searchURL = [NSMutableString stringWithString:
|
}
|
||||||
[[BrowserWindowController searchURLDictionary] objectForKey:
|
|
||||||
[aSender titleOfSelectedPopUpItem]]];
|
//
|
||||||
NSString *currentURL = [[self getBrowserWrapper] getCurrentURLSpec];
|
// - performSearch:inView:inBackground
|
||||||
NSString *searchString = [aSender stringValue];
|
//
|
||||||
|
// performs a search using searchField and opens either in the current view, a new tab, or a new
|
||||||
|
// window. If it's a new tab or window, loadInBG determines whether the window/tab is opened in the background
|
||||||
|
//
|
||||||
|
-(void)performSearch:(SearchTextField *)inSearchField inView:(BWCOpenDest)inDest inBackground:(BOOL)inLoadInBG
|
||||||
|
{
|
||||||
|
// Get the search URL from our dictionary of sites and search urls
|
||||||
|
NSMutableString *searchURL = [NSMutableString stringWithString:
|
||||||
|
[[BrowserWindowController searchURLDictionary] objectForKey:
|
||||||
|
[inSearchField titleOfSelectedPopUpItem]]];
|
||||||
|
NSString *currentURL = [[self getBrowserWrapper] getCurrentURLSpec];
|
||||||
|
NSString *searchString = [inSearchField stringValue];
|
||||||
|
|
||||||
|
const char *aURLSpec = [currentURL lossyCString];
|
||||||
|
NSString *aDomain = @"";
|
||||||
|
nsIURI *aURI = nil;
|
||||||
|
|
||||||
|
// If we have an about: type URL, remove " site:%d" from the search string
|
||||||
|
// This is a fix to deal with Google's Search this Site feature
|
||||||
|
// If other sites use %d to search the site, we'll have to have specific rules
|
||||||
|
// for those sites.
|
||||||
|
|
||||||
|
if ([currentURL hasPrefix:@"about:"]) {
|
||||||
|
NSRange domainStringRange = [searchURL rangeOfString:@" site:%d"
|
||||||
|
options:NSBackwardsSearch];
|
||||||
|
|
||||||
const char *aURLSpec = [currentURL lossyCString];
|
NSRange notFoundRange = NSMakeRange(NSNotFound, 0);
|
||||||
NSString *aDomain = @"";
|
if (NSEqualRanges(domainStringRange, notFoundRange) == NO)
|
||||||
nsIURI *aURI = nil;
|
[searchURL deleteCharactersInRange:domainStringRange];
|
||||||
|
}
|
||||||
// If we have an about: type URL, remove " site:%d" from the search string
|
|
||||||
// This is a fix to deal with Google's Search this Site feature
|
// If they didn't type anything in the search field, visit the domain of
|
||||||
// If other sites use %d to search the site, we'll have to have specific rules
|
// the search site, i.e. www.google.com for the Google site
|
||||||
// for those sites.
|
if ([[inSearchField stringValue] isEqualToString:@""]) {
|
||||||
|
aURLSpec = [searchURL lossyCString];
|
||||||
if ([currentURL hasPrefix:@"about:"]) {
|
|
||||||
NSRange domainStringRange = [searchURL rangeOfString:@" site:%d"
|
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
|
||||||
options:NSBackwardsSearch];
|
nsCAutoString spec;
|
||||||
|
aURI->GetHost(spec);
|
||||||
NSRange notFoundRange = NSMakeRange(NSNotFound, 0);
|
|
||||||
if (NSEqualRanges(domainStringRange, notFoundRange) == NO)
|
aDomain = [NSString stringWithUTF8String:spec.get()];
|
||||||
[searchURL deleteCharactersInRange:domainStringRange];
|
|
||||||
}
|
if (inDest == kDestinationNewTab)
|
||||||
|
[self openNewTabWithURL:aDomain referrer:nil loadInBackground:inLoadInBG];
|
||||||
// If they didn't type anything in the search field, visit the domain of
|
else if (inDest == kDestinationNewWindow)
|
||||||
// the search site, i.e. www.google.com for the Google site
|
[self openNewWindowWithURL:aDomain referrer:nil loadInBackground:inLoadInBG];
|
||||||
if ([[aSender stringValue] isEqualToString:@""]) {
|
else // if it's not a new window or a new tab, load into the current view
|
||||||
aURLSpec = [searchURL lossyCString];
|
|
||||||
|
|
||||||
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
|
|
||||||
nsCAutoString spec;
|
|
||||||
aURI->GetHost(spec);
|
|
||||||
|
|
||||||
aDomain = [NSString stringWithUTF8String:spec.get()];
|
|
||||||
|
|
||||||
[self loadURL:aDomain referrer:nil activate:NO];
|
[self loadURL:aDomain referrer:nil activate:NO];
|
||||||
}
|
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
aURLSpec = [[[self getBrowserWrapper] getCurrentURLSpec] lossyCString];
|
||||||
|
|
||||||
|
// Get the domain so that we can replace %d in our searchURL
|
||||||
|
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
|
||||||
|
nsCAutoString spec;
|
||||||
|
aURI->GetHost(spec);
|
||||||
|
|
||||||
|
aDomain = [NSString stringWithUTF8String:spec.get()];
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
aURLSpec = [[[self getBrowserWrapper] getCurrentURLSpec] lossyCString];
|
// Escape the search string so the user can search for strings with
|
||||||
|
// special characters ("&", "+", etc.) List from RFC2396.
|
||||||
// Get the domain so that we can replace %d in our searchURL
|
NSString *escapedSearchString = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, CFSTR(";/?:@&=+$,"), kCFStringEncodingUTF8);
|
||||||
if (NS_NewURI(&aURI, aURLSpec, nsnull, nsnull) == NS_OK) {
|
|
||||||
nsCAutoString spec;
|
// replace the conversion specifiers (%d, %s) in the search string
|
||||||
aURI->GetHost(spec);
|
[self transformFormatString:searchURL domain:aDomain search:escapedSearchString];
|
||||||
|
[escapedSearchString release];
|
||||||
aDomain = [NSString stringWithUTF8String:spec.get()];
|
|
||||||
}
|
if (inDest == kDestinationNewTab)
|
||||||
|
[self openNewTabWithURL:searchURL referrer:nil loadInBackground:inLoadInBG];
|
||||||
// Escape the search string so the user can search for strings with
|
else if (inDest == kDestinationNewWindow)
|
||||||
// special characters ("&", "+", etc.) List from RFC2396.
|
[self openNewWindowWithURL:searchURL referrer:nil loadInBackground:inLoadInBG];
|
||||||
NSString *escapedSearchString = (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)searchString, NULL, CFSTR(";/?:@&=+$,"), kCFStringEncodingUTF8);
|
else // if it's not a new window or a new tab, load into the current view
|
||||||
|
|
||||||
// replace the conversion specifiers (%d, %s) in the search string
|
|
||||||
[self transformFormatString:searchURL domain:aDomain search:escapedSearchString];
|
|
||||||
[escapedSearchString release];
|
|
||||||
|
|
||||||
[self loadURL:searchURL referrer:nil activate:NO];
|
[self loadURL:searchURL referrer:nil activate:NO];
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// - transformFormatString:domain:search
|
// - transformFormatString:domain:search
|
||||||
//
|
//
|
||||||
|
@ -3170,6 +3211,44 @@ static NSArray* sToolbarDefaults = nil;
|
||||||
[self toggleBookmarkManager:self];
|
[self toggleBookmarkManager:self];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// - handleCommandReturn:
|
||||||
|
//
|
||||||
|
// handle command-return in location or search field, opening a new tab or window as appropriate
|
||||||
|
//
|
||||||
|
- (BOOL) handleCommandReturn
|
||||||
|
{
|
||||||
|
BOOL handled = NO;
|
||||||
|
|
||||||
|
// determine whether to load in background
|
||||||
|
PRBool loadInBG;
|
||||||
|
nsCOMPtr<nsIPrefBranch> pref(do_GetService("@mozilla.org/preferences-service;1"));
|
||||||
|
pref->GetBoolPref("browser.tabs.loadInBackground", &loadInBG);
|
||||||
|
|
||||||
|
// determine whether to load in tab or window
|
||||||
|
PRBool loadInTab;
|
||||||
|
pref->GetBoolPref("browser.tabs.opentabfor.middleclick",&loadInTab);
|
||||||
|
|
||||||
|
BWCOpenDest destination = loadInTab ? kDestinationNewTab : kDestinationNewWindow;
|
||||||
|
|
||||||
|
// see if command-return came in the url bar
|
||||||
|
if ([mURLBar fieldEditor] && [[self window] firstResponder] == [mURLBar fieldEditor]) {
|
||||||
|
handled = YES;
|
||||||
|
[self goToLocationFromToolbarURLField:mURLBar inView:destination inBackground:loadInBG];
|
||||||
|
// kill any autocomplete that was in progress
|
||||||
|
[mURLBar revertText];
|
||||||
|
// set the text in the URL bar back to the current URL
|
||||||
|
[self updateLocationFields:[mBrowserView getCurrentURLSpec]];
|
||||||
|
|
||||||
|
// see if command-return came in the search field
|
||||||
|
} else if ([mSearchBar isFirstResponder]) {
|
||||||
|
handled = YES;
|
||||||
|
[self performSearch:mSearchBar inView:destination inBackground:loadInBG];
|
||||||
|
}
|
||||||
|
return handled;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
#pragma mark -
|
#pragma mark -
|
||||||
|
|
Загрузка…
Ссылка в новой задаче