From 401241c946a84ab93a818ac75c9edc246a5ca584 Mon Sep 17 00:00:00 2001 From: Chris Kitching Date: Mon, 29 Jul 2013 12:17:32 -0400 Subject: [PATCH] Bug 828254 - Add context menu item to query default search provider using selection. r=mleibovic --- .../chrome/content/SelectionHandler.js | 9 +++++ mobile/android/chrome/content/browser.js | 35 +++++++++++++++++++ .../locales/en-US/chrome/browser.properties | 1 + 3 files changed, 45 insertions(+) diff --git a/mobile/android/chrome/content/SelectionHandler.js b/mobile/android/chrome/content/SelectionHandler.js index 412ce406078e..0cb8d385cdff 100644 --- a/mobile/android/chrome/content/SelectionHandler.js +++ b/mobile/android/chrome/content/SelectionHandler.js @@ -396,6 +396,15 @@ var SelectionHandler = { this._closeSelection(); }, + searchSelection: function sh_searchSelection() { + let selectedText = this._getSelectedText(); + if (selectedText.length) { + let req = Services.search.defaultEngine.getSubmission(selectedText); + BrowserApp.selectOrOpenTab(req.uri.spec); + } + this._closeSelection(); + }, + /* * Called if for any reason we fail during the selection * process. Cancels the selection. diff --git a/mobile/android/chrome/content/browser.js b/mobile/android/chrome/content/browser.js index 302db09483b7..6f25174c0874 100644 --- a/mobile/android/chrome/content/browser.js +++ b/mobile/android/chrome/content/browser.js @@ -2065,6 +2065,8 @@ var NativeWindow = { this.menuitems = []; let menuitemsSet = false; + Services.obs.notifyObservers(null, "before-build-contextmenu", ""); + // now walk up the tree and for each node look for any context menu items that apply let element = target; this._nativeItemsSeparator = 0; @@ -5962,6 +5964,9 @@ var IndexedDB = { }; var ClipboardHelper = { + // Recorded so search with option can be removed/replaced when default engine changed. + _searchMenuItem: -1, + init: function() { NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.copy"), ClipboardHelper.getCopyContext(false), ClipboardHelper.copy.bind(ClipboardHelper)); NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.copyAll"), ClipboardHelper.getCopyContext(true), ClipboardHelper.copy.bind(ClipboardHelper)); @@ -5970,6 +5975,26 @@ var ClipboardHelper = { NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.share"), ClipboardHelper.shareContext, ClipboardHelper.share.bind(ClipboardHelper)); NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.paste"), ClipboardHelper.pasteContext, ClipboardHelper.paste.bind(ClipboardHelper)); NativeWindow.contextmenus.add(Strings.browser.GetStringFromName("contextmenu.changeInputMethod"), NativeWindow.contextmenus.textContext, ClipboardHelper.inputMethod.bind(ClipboardHelper)); + + // We add this contextmenu item right before the menu is built to avoid having to initialise the search service early. + Services.obs.addObserver(this, "before-build-contextmenu", false); + }, + + uninit: function ch_uninit() { + Services.obs.removeObserver(this, "before-build-contextmenu"); + }, + + observe: function observe(aSubject, aTopic) { + if (aTopic == "ContextMenu:Open") { + this._setSearchMenuItem(); + } + }, + + _setSearchMenuItem: function setSearchMenuItem() { + if (this._searchMenuItem) { + NativeWindow.contextmenus.remove(this._searchMenuItem); + } + this._searchMenuItem = NativeWindow.contextmenus.add(Strings.browser.formatStringFromName("contextmenu.search", [Services.search.defaultEngine.name], 1), ClipboardHelper.searchWithContext, ClipboardHelper.searchWith.bind(ClipboardHelper)); }, get clipboardHelper() { @@ -6006,6 +6031,10 @@ var ClipboardHelper = { SelectionHandler.selectAll(aElement, aX, aY); }, + searchWith: function(aElement) { + SelectionHandler.searchSelection(aElement); + }, + share: function() { SelectionHandler.shareSelection(); }, @@ -6075,6 +6104,12 @@ var ClipboardHelper = { } }, + searchWithContext: { + matches: function searchWithContextMatches(aElement, aX, aY) { + return SelectionHandler.shouldShowContextMenu(aX, aY); + } + }, + pasteContext: { matches: function(aElement) { if (NativeWindow.contextmenus.textContext.matches(aElement)) { diff --git a/mobile/android/locales/en-US/chrome/browser.properties b/mobile/android/locales/en-US/chrome/browser.properties index 6058e46ba2ba..8b836c7dc089 100644 --- a/mobile/android/locales/en-US/chrome/browser.properties +++ b/mobile/android/locales/en-US/chrome/browser.properties @@ -165,6 +165,7 @@ contextmenu.changeInputMethod=Select Input Method contextmenu.fullScreen=Full Screen contextmenu.copyImageLocation=Copy Image Location contextmenu.shareImage=Share Image +contextmenu.search=%S Search contextmenu.saveImage=Save Image contextmenu.setWallpaper=Set as Wallpaper contextmenu.addSearchEngine=Add Search Engine