Bug 326743: support the find clipboard in the find toolbar. r=Unfocused

This commit is contained in:
Mike de Boer 2014-02-14 14:04:21 +01:00
Родитель e6d70963f2
Коммит 0e98650667
2 изменённых файлов: 116 добавлений и 8 удалений

Просмотреть файл

@ -110,6 +110,13 @@
findbar.browser.finder.enableSelection();
]]></handler>
#ifdef XP_MACOSX
<handler event="focus"><![CDATA[
let findbar = this.findbar;
findbar._onFindFieldFocus();
]]></handler>
#endif
<handler event="compositionstart"><![CDATA[
// Don't close the find toolbar while IME is composing.
let findbar = this.findbar;
@ -1045,8 +1052,17 @@
userWantsPrefill =
prefsvc.getBoolPref("accessibility.typeaheadfind.prefillwithselection");
let initialString = (this.prefillWithSelection && userWantsPrefill) ?
this._getInitialSelection() : null;
let initialString = null;
if (this.prefillWithSelection && userWantsPrefill)
initialString = this._getInitialSelection();
#ifdef XP_MACOSX
if (!initialString) {
let clipboardSearchString = this.browser.finder.clipboardSearchString;
if (clipboardSearchString)
initialString = clipboardSearchString;
}
#endif
if (initialString)
this._findField.value = initialString;
@ -1102,6 +1118,32 @@
]]></body>
</method>
#ifdef XP_MACOSX
<!--
- Fetches the currently selected text and sets that as the text to search
- next. This is a MacOS specific feature.
-->
<method name="onFindSelectionCommand">
<body><![CDATA[
let searchString = this.browser.finder.setSearchStringToSelection();
if (searchString)
this._findField.value = searchString;
]]></body>
</method>
<method name="_onFindFieldFocus">
<body><![CDATA[
let clipboardSearchString = this._browser.finder.clipboardSearchString;
if (clipboardSearchString && this._findField.value != clipboardSearchString) {
this._findField.value = clipboardSearchString;
// Changing the search string makes the previous status invalid, so
// we better clear it here.
this._updateStatusUI();
}
]]></body>
</method>
#endif
<!--
- This handles all the result changes for both
- type-ahead-find and highlighting.
@ -1119,6 +1161,8 @@
<method name="onFindResult">
<parameter name="aData"/>
<body><![CDATA[
if (this._findField.value != this.browser.finder.searchString)
this._findField.value = this.browser.finder.searchString;
this._updateStatusUI(aData.result, aData.findBackwards);
this._updateStatusUIBar(aData.linkURL);

Просмотреть файл

@ -12,6 +12,16 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Geometry.jsm");
Cu.import("resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "TextToSubURIService",
"@mozilla.org/intl/texttosuburi;1",
"nsITextToSubURI");
XPCOMUtils.defineLazyServiceGetter(this, "Clipboard",
"@mozilla.org/widget/clipboard;1",
"nsIClipboard");
XPCOMUtils.defineLazyServiceGetter(this, "ClipboardHelper",
"@mozilla.org/widget/clipboardhelper;1",
"nsIClipboardHelper");
function Finder(docShell) {
this._fastFind = Cc["@mozilla.org/typeaheadfind;1"].createInstance(Ci.nsITypeAheadFind);
this._fastFind.init(docShell);
@ -38,6 +48,7 @@ Finder.prototype = {
_notify: function (aSearchString, aResult, aFindBackwards, aDrawOutline) {
this._searchString = aSearchString;
this.clipboardSearchString = aSearchString
this._outlineLink(aDrawOutline);
let foundLink = this._fastFind.foundLink;
@ -48,12 +59,7 @@ Finder.prototype = {
if (ownerDoc)
docCharset = ownerDoc.characterSet;
if (!this._textToSubURIService) {
this._textToSubURIService = Cc["@mozilla.org/intl/texttosuburi;1"]
.getService(Ci.nsITextToSubURI);
}
linkURL = this._textToSubURIService.unEscapeURIForUI(docCharset, foundLink.href);
linkURL = TextToSubURIService.unEscapeURIForUI(docCharset, foundLink.href);
}
let data = {
@ -70,9 +76,48 @@ Finder.prototype = {
},
get searchString() {
if (!this._searchString && this._fastFind.searchString)
this._searchString = this._fastFind.searchString;
return this._searchString;
},
get clipboardSearchString() {
let searchString = "";
if (!Clipboard.supportsFindClipboard())
return searchString;
try {
let trans = Cc["@mozilla.org/widget/transferable;1"]
.createInstance(Ci.nsITransferable);
trans.init(this._getWindow()
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsILoadContext));
trans.addDataFlavor("text/unicode");
Clipboard.getData(trans, Ci.nsIClipboard.kFindClipboard);
let data = {};
let dataLen = {};
trans.getTransferData("text/unicode", data, dataLen);
if (data.value) {
data = data.value.QueryInterface(Ci.nsISupportsString);
searchString = data.toString();
}
} catch (ex) {}
return searchString;
},
set clipboardSearchString(aSearchString) {
if (!aSearchString || !Clipboard.supportsFindClipboard())
return;
ClipboardHelper.copyStringToClipboard(aSearchString,
Ci.nsIClipboard.kFindClipboard,
this._getWindow().document);
},
set caseSensitive(aSensitive) {
this._fastFind.caseSensitive = aSensitive;
},
@ -105,6 +150,25 @@ Finder.prototype = {
this._notify(searchString, result, aFindBackwards, aDrawOutline);
},
/**
* Forcibly set the search string of the find clipboard to the currently
* selected text in the window, on supported platforms (i.e. OSX).
*/
setSearchStringToSelection: function() {
// Find the selected text.
let selection = this._getWindow().getSelection();
// Don't go for empty selections.
if (!selection.rangeCount)
return null;
let searchString = (selection.toString() || "").trim();
// Empty strings are rather useless to search for.
if (!searchString.length)
return null;
this.clipboardSearchString = searchString;
return searchString;
},
highlight: function (aHighlight, aWord) {
let found = this._highlight(aHighlight, aWord, null);
if (aHighlight) {