diff options
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_edit_view_mac.mm | 16 | ||||
-rw-r--r-- | chrome/browser/autocomplete/autocomplete_popup_view_mac.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_bar_controller.mm | 9 | ||||
-rw-r--r-- | chrome/browser/cocoa/bookmark_menu_cocoa_controller.mm | 7 | ||||
-rw-r--r-- | chrome/browser/cocoa/history_menu_cocoa_controller.mm | 7 | ||||
-rw-r--r-- | chrome/browser/cocoa/infobar_controller.mm | 5 | ||||
-rw-r--r-- | chrome/browser/cocoa/location_bar_view_mac.mm | 5 | ||||
-rw-r--r-- | chrome/chrome.gyp | 3 | ||||
-rw-r--r-- | chrome/common/cocoa_utils.h | 20 | ||||
-rw-r--r-- | chrome/common/cocoa_utils.mm | 26 | ||||
-rw-r--r-- | chrome/common/cocoa_utils_unittest.mm | 23 |
11 files changed, 113 insertions, 13 deletions
diff --git a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm index 2de70d1..9ba16ef 100644 --- a/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_edit_view_mac.mm @@ -4,6 +4,7 @@ #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" +#include <Carbon/Carbon.h> // kVK_Return #include "app/gfx/font.h" #include "app/resource_bundle.h" #include "base/clipboard.h" @@ -15,6 +16,7 @@ #include "chrome/browser/browser_process.h" #include "chrome/browser/cocoa/autocomplete_text_field.h" #include "chrome/browser/tab_contents/tab_contents.h" +#import "chrome/common/cocoa_utils.h" // Focus-handling between |field_| and |model_| is a bit subtle. // Other platforms detect change of focus, which is inconvenient @@ -685,8 +687,14 @@ std::wstring AutocompleteEditViewMac::GetClipboardText(Clipboard* clipboard) { return YES; } - if (cmd == @selector(insertNewline:)) { - edit_view_->AcceptInput(CURRENT_TAB, false); + // |-noop:| is sent when the user presses Cmd+Return. Override the no-op + // behavior with the proper WindowOpenDisposition. + NSEvent* event = [NSApp currentEvent]; + if (cmd == @selector(insertNewline:) || + (cmd == @selector(noop:) && [event keyCode] == kVK_Return)) { + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [event modifierFlags]); + edit_view_->AcceptInput(disposition, false); return YES; } @@ -696,7 +704,9 @@ std::wstring AutocompleteEditViewMac::GetClipboardText(Clipboard* clipboard) { // is safe to tell it twice. if (cmd == @selector(insertLineBreak:)) { edit_view_->OnControlKeyChanged(true); - edit_view_->AcceptInput(CURRENT_TAB, false); + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + edit_view_->AcceptInput(disposition, false); return YES; } diff --git a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm index 2489c5a..1d54c9e 100644 --- a/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm +++ b/chrome/browser/autocomplete/autocomplete_popup_view_mac.mm @@ -9,6 +9,7 @@ #include "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" #include "chrome/browser/autocomplete/autocomplete_popup_model.h" #include "chrome/browser/cocoa/nsimage_cache.h" +#import "chrome/common/cocoa_utils.h" namespace { @@ -379,7 +380,9 @@ void AutocompletePopupViewMac::AcceptInput() { PaintUpdatesNow(); } else { model_->SetSelectedLine(selectedRow, false); - edit_view_->AcceptInput(CURRENT_TAB, false); + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + edit_view_->AcceptInput(disposition, false); } } diff --git a/chrome/browser/cocoa/bookmark_bar_controller.mm b/chrome/browser/cocoa/bookmark_bar_controller.mm index ebba2f3..5d25b67 100644 --- a/chrome/browser/cocoa/bookmark_bar_controller.mm +++ b/chrome/browser/cocoa/bookmark_bar_controller.mm @@ -17,6 +17,7 @@ #import "chrome/browser/cocoa/view_resizer.h" #include "chrome/browser/cocoa/nsimage_cache.h" #include "chrome/browser/profile.h" +#import "chrome/common/cocoa_utils.h" #include "chrome/common/pref_names.h" #include "chrome/common/pref_service.h" #include "skia/ext/skia_utils_mac.h" @@ -185,7 +186,9 @@ const CGFloat kBookmarkHorizontalPadding = 1.0; - (IBAction)openBookmark:(id)sender { BookmarkNode* node = [self nodeFromButton:sender]; - [urlDelegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB]; + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + [urlDelegate_ openBookmarkURL:node->GetURL() disposition:disposition]; } // Given a NSMenuItem tag, return the appropriate bookmark node id. @@ -498,7 +501,9 @@ const CGFloat kBookmarkHorizontalPadding = 1.0; - (IBAction)openBookmarkMenuItem:(id)sender { int64 tag = [self nodeIdFromMenuTag:[sender tag]]; const BookmarkNode* node = bookmarkModel_->GetNodeByID(tag); - [urlDelegate_ openBookmarkURL:node->GetURL() disposition:CURRENT_TAB]; + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + [urlDelegate_ openBookmarkURL:node->GetURL() disposition:disposition]; } // Add all items from the given model to our bookmark bar. diff --git a/chrome/browser/cocoa/bookmark_menu_cocoa_controller.mm b/chrome/browser/cocoa/bookmark_menu_cocoa_controller.mm index 46f17bf..7d3df67 100644 --- a/chrome/browser/cocoa/bookmark_menu_cocoa_controller.mm +++ b/chrome/browser/cocoa/bookmark_menu_cocoa_controller.mm @@ -10,7 +10,8 @@ #import "chrome/browser/cocoa/bookmark_menu_cocoa_controller.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/tab_contents/tab_contents.h" -#include "webkit/glue/window_open_disposition.h" // CURRENT_TAB +#import "chrome/common/cocoa_utils.h" +#include "webkit/glue/window_open_disposition.h" namespace { @@ -60,7 +61,9 @@ const NSUInteger kMaximumMenuPixelsWide = 300; DCHECK(tab_contents); // A TabContents is a PageNavigator, so we can OpenURL() on it. - tab_contents->OpenURL(node->GetURL(), GURL(), CURRENT_TAB, + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + tab_contents->OpenURL(node->GetURL(), GURL(), disposition, PageTransition::AUTO_BOOKMARK); } diff --git a/chrome/browser/cocoa/history_menu_cocoa_controller.mm b/chrome/browser/cocoa/history_menu_cocoa_controller.mm index ac69916..b6523ab 100644 --- a/chrome/browser/cocoa/history_menu_cocoa_controller.mm +++ b/chrome/browser/cocoa/history_menu_cocoa_controller.mm @@ -10,7 +10,8 @@ #include "chrome/browser/history/history.h" #include "chrome/browser/history/history_types.h" #include "chrome/browser/tab_contents/tab_contents.h" -#include "webkit/glue/window_open_disposition.h" // CURRENT_TAB +#import "chrome/common/cocoa_utils.h" +#include "webkit/glue/window_open_disposition.h" @implementation HistoryMenuCocoaController @@ -35,7 +36,9 @@ DCHECK(tab_contents); // A TabContents is a PageNavigator, so we can OpenURL() on it. - tab_contents->OpenURL(node.url, GURL(), CURRENT_TAB, + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + tab_contents->OpenURL(node.url, GURL(), disposition, PageTransition::AUTO_BOOKMARK); } diff --git a/chrome/browser/cocoa/infobar_controller.mm b/chrome/browser/cocoa/infobar_controller.mm index f887cf8..ea496de 100644 --- a/chrome/browser/cocoa/infobar_controller.mm +++ b/chrome/browser/cocoa/infobar_controller.mm @@ -11,6 +11,7 @@ #import "chrome/browser/cocoa/infobar_container_controller.h" #import "chrome/browser/cocoa/infobar_controller.h" #include "chrome/browser/tab_contents/tab_contents.h" +#import "chrome/common/cocoa_utils.h" #include "skia/ext/skia_utils_mac.h" #include "webkit/glue/window_open_disposition.h" @@ -176,8 +177,8 @@ // is called by the InfobarTextField on its delegate (the // LinkInfoBarController). - (void)linkClicked { - // TODO(rohitrao): Set the disposition correctly based on modifier keys. - WindowOpenDisposition disposition = CURRENT_TAB; + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); if (delegate_->AsLinkInfoBarDelegate()->LinkClicked(disposition)) [self closeInfoBar]; } diff --git a/chrome/browser/cocoa/location_bar_view_mac.mm b/chrome/browser/cocoa/location_bar_view_mac.mm index edbd4a5..bdb7c1b 100644 --- a/chrome/browser/cocoa/location_bar_view_mac.mm +++ b/chrome/browser/cocoa/location_bar_view_mac.mm @@ -10,6 +10,7 @@ #import "chrome/browser/app_controller_mac.h" #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" #include "chrome/browser/command_updater.h" +#import "chrome/common/cocoa_utils.h" #include "third_party/skia/include/core/SkBitmap.h" // TODO(shess): This code is mostly copied from the gtk @@ -48,7 +49,9 @@ PageTransition::Type LocationBarViewMac::GetPageTransition() const { } void LocationBarViewMac::AcceptInput() { - AcceptInputWithDisposition(CURRENT_TAB); + WindowOpenDisposition disposition = event_utils::DispositionFromEventFlags( + [[NSApp currentEvent] modifierFlags]); + AcceptInputWithDisposition(disposition); } void LocationBarViewMac::AcceptInputWithDisposition( diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index d7cfd87..0ad4e01 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -425,6 +425,8 @@ 'common/chrome_switches.h', 'common/classfactory.cc', 'common/classfactory.h', + 'common/cocoa_utils.mm', + 'common/cocoa_utils.h', 'common/common_glue.cc', 'common/common_param_traits.cc', 'common/common_param_traits.h', @@ -3967,6 +3969,7 @@ 'common/bzip2_unittest.cc', 'common/child_process_logging_mac_unittest.mm', 'common/chrome_plugin_unittest.cc', + 'common/cocoa_utils_unittest.mm', 'common/common_param_traits_unittest.cc', 'common/extensions/extension_unittest.cc', 'common/extensions/url_pattern_unittest.cc', diff --git a/chrome/common/cocoa_utils.h b/chrome/common/cocoa_utils.h new file mode 100644 index 0000000..4607f987 --- /dev/null +++ b/chrome/common/cocoa_utils.h @@ -0,0 +1,20 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_COCOA_UTIL_H_ +#define CHROME_COMMON_COCOA_UTIL_H_ + +#import <Cocoa/Cocoa.h> +#include "webkit/glue/window_open_disposition.h" + +namespace event_utils { + +// Translates modifier flags from an NSEvent into a WindowOpenDisposition. For +// example, holding down Cmd (Apple) will cause pages to be opened in a new +// foreground tab. Pass this the result of -[NSEvent modifierFlags]. +WindowOpenDisposition DispositionFromEventFlags(NSUInteger modifiers); + +} // namespace event_utils + +#endif // CHROME_COMMON_COCOA_UTIL_H_ diff --git a/chrome/common/cocoa_utils.mm b/chrome/common/cocoa_utils.mm new file mode 100644 index 0000000..3472178 --- /dev/null +++ b/chrome/common/cocoa_utils.mm @@ -0,0 +1,26 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "chrome/common/cocoa_utils.h" + +namespace event_utils { + +// Translates modifier flags from an NSEvent into a WindowOpenDisposition. For +// example, holding down Cmd (Apple) will cause pages to be opened in a new +// foreground tab. Pass this the result of -[NSEvent modifierFlags]. +WindowOpenDisposition DispositionFromEventFlags(NSUInteger modifiers) { + if (modifiers & NSCommandKeyMask) { + return (modifiers & NSShiftKeyMask) ? + NEW_BACKGROUND_TAB : NEW_FOREGROUND_TAB; + } + + if (modifiers & NSShiftKeyMask) + return NEW_WINDOW; + // TODO: Browser::OpenURLAtIndex does not support SAVE_TO_DISK, so we can't + // offer to download the page. See DCHECK() and TODOs there. + return (false /* modifiers & NSAlternateKeyMask */) ? + SAVE_TO_DISK : CURRENT_TAB; +} + +} // namespace event_utils diff --git a/chrome/common/cocoa_utils_unittest.mm b/chrome/common/cocoa_utils_unittest.mm new file mode 100644 index 0000000..e9ce0ae --- /dev/null +++ b/chrome/common/cocoa_utils_unittest.mm @@ -0,0 +1,23 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "chrome/common/cocoa_utils.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(CocoaUtilsTest, DispositionFromEventFlagsTest) { + ASSERT_EQ(NEW_FOREGROUND_TAB, + event_utils::DispositionFromEventFlags(NSCommandKeyMask)); + ASSERT_EQ(NEW_BACKGROUND_TAB, + event_utils::DispositionFromEventFlags(NSCommandKeyMask | + NSShiftKeyMask)); + ASSERT_EQ(NEW_WINDOW, + event_utils::DispositionFromEventFlags(NSShiftKeyMask)); + // The SAVE_TO_DISK disposition is not currently supported, so we use + // CURRENT_TAB instead. + ASSERT_EQ(CURRENT_TAB, + event_utils::DispositionFromEventFlags(NSAlternateKeyMask)); + ASSERT_EQ(CURRENT_TAB, event_utils::DispositionFromEventFlags(0)); + ASSERT_EQ(CURRENT_TAB, + event_utils::DispositionFromEventFlags(NSControlKeyMask)); +} |