Bug 1563351 - Insert search shortcuts into the Urlbar with Touch Bar buttons. r=adw

Differential Revision: https://phabricator.services.mozilla.com/D38892

--HG--
extra : moz-landing-system : lando
This commit is contained in:
harry 2019-10-08 15:10:20 +00:00
Родитель 980501c285
Коммит abf9b97e30
5 изменённых файлов: 192 добавлений и 8 удалений

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

@ -7,11 +7,12 @@ const { XPCOMUtils } = ChromeUtils.import(
);
XPCOMUtils.defineLazyModuleGetters(this, {
AppConstants: "resource://gre/modules/AppConstants.jsm",
BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.jsm",
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
Services: "resource://gre/modules/Services.jsm",
AppConstants: "resource://gre/modules/AppConstants.jsm",
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
});
XPCOMUtils.defineLazyServiceGetter(
@ -21,6 +22,14 @@ XPCOMUtils.defineLazyServiceGetter(
"nsITouchBarUpdater"
);
// For accessing TouchBarHelper methods from static contexts in this file.
XPCOMUtils.defineLazyServiceGetter(
this,
"gTouchBarHelper",
"@mozilla.org/widget/touchbarhelper;1",
"nsITouchBarHelper"
);
/**
* Executes a XUL command on the top window. Called by the callbacks in each
* TouchBarInput.
@ -177,27 +186,42 @@ const kBuiltInInputs = {
Bookmarks: {
title: "search-bookmarks",
type: kInputTypes.BUTTON,
callback: () => console.log("Bookmarks success!"), // FIXME: Bug 1563351
callback: () =>
gTouchBarHelper.insertRestrictionInUrlbar(
UrlbarTokenizer.RESTRICT.BOOKMARK
),
},
History: {
title: "search-history",
type: kInputTypes.BUTTON,
callback: () => console.log("History success!"), // FIXME: Bug 1563351
callback: () =>
gTouchBarHelper.insertRestrictionInUrlbar(
UrlbarTokenizer.RESTRICT.HISTORY
),
},
OpenTabs: {
title: "search-opentabs",
type: kInputTypes.BUTTON,
callback: () => console.log("Open Tabs success!"), // FIXME: Bug 1563351
callback: () =>
gTouchBarHelper.insertRestrictionInUrlbar(
UrlbarTokenizer.RESTRICT.OPENPAGE
),
},
Tags: {
title: "search-tags",
type: kInputTypes.BUTTON,
callback: () => console.log("Tags success!"), // FIXME: Bug 1563351
callback: () =>
gTouchBarHelper.insertRestrictionInUrlbar(
UrlbarTokenizer.RESTRICT.TAG
),
},
Titles: {
title: "search-titles",
type: kInputTypes.BUTTON,
callback: () => console.log("Titles success!"), // FIXME: Bug 1563351
callback: () =>
gTouchBarHelper.insertRestrictionInUrlbar(
UrlbarTokenizer.RESTRICT.TITLE
),
},
},
},
@ -349,6 +373,22 @@ class TouchBarHelper {
gTouchBarUpdater.updateTouchBarInputs(TouchBarHelper.baseWindow, inputs);
}
/**
* Inserts a restriction token into the Urlbar ahead of the current typed
* search term.
* @param {string} restrictionToken
* The restriction token to be inserted into the Urlbar. Preferably
* sourced from UrlbarTokenizer.RESTRICT.
*/
insertRestrictionInUrlbar(restrictionToken) {
let searchString = TouchBarHelper.window.gURLBar.lastSearchString.trimStart();
if (Object.values(UrlbarTokenizer.RESTRICT).includes(searchString[0])) {
searchString = searchString.substring(1).trimStart();
}
TouchBarHelper.window.gURLBar.search(`${restrictionToken} ${searchString}`);
}
observe(subject, topic, data) {
switch (topic) {
case "touchbar-location-change":

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

@ -2,4 +2,5 @@
support-files =
readerModeArticle.html
[browser_touchbar_tests.js]
[browser_touchbar_searchrestrictions.js]
[browser_touchbar_tests.js]

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

@ -0,0 +1,129 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
});
XPCOMUtils.defineLazyServiceGetter(
this,
"TouchBarHelper",
"@mozilla.org/widget/touchbarhelper;1",
"nsITouchBarHelper"
);
add_task(async function insertTokens() {
const tests = [
{
input: "",
token: UrlbarTokenizer.RESTRICT.HISTORY,
expected: "^ ",
},
{
input: "mozilla",
token: UrlbarTokenizer.RESTRICT.HISTORY,
expected: "^ mozilla",
},
{
input: "mozilla",
token: UrlbarTokenizer.RESTRICT.BOOKMARK,
expected: "* mozilla",
},
{
input: "mozilla",
token: UrlbarTokenizer.RESTRICT.TAG,
expected: "+ mozilla",
},
{
input: "mozilla",
token: UrlbarTokenizer.RESTRICT.OPENPAGE,
expected: "% mozilla",
},
{
input: "mozilla",
token: UrlbarTokenizer.RESTRICT.TITLE,
expected: "# mozilla",
},
];
let win = BrowserWindowTracker.getTopWindow();
for (let { input, token, expected } of tests) {
win.gURLBar.search(input);
TouchBarHelper.insertRestrictionInUrlbar(token);
Assert.equal(
win.gURLBar.value,
expected,
"The search restriction token should have been entered."
);
}
});
add_task(async function existingTokens() {
const tests = [
{
input: "* mozilla",
token: UrlbarTokenizer.RESTRICT.HISTORY,
expected: "^ mozilla",
},
{
input: "+ mozilla",
token: UrlbarTokenizer.RESTRICT.BOOKMARK,
expected: "* mozilla",
},
{
input: "( $ ^ mozilla",
token: UrlbarTokenizer.RESTRICT.TAG,
expected: "+ ( $ ^ mozilla",
},
{
input: "^*+%?#$ mozilla",
token: UrlbarTokenizer.RESTRICT.TAG,
expected: "+ *+%?#$ mozilla",
},
];
let win = BrowserWindowTracker.getTopWindow();
for (let { input, token, expected } of tests) {
win.gURLBar.search(input);
TouchBarHelper.insertRestrictionInUrlbar(token);
Assert.equal(
win.gURLBar.value,
expected,
"The search restriction token should have been replaced."
);
}
});
add_task(async function stripSpaces() {
const tests = [
{
input: " ^ mozilla",
token: UrlbarTokenizer.RESTRICT.HISTORY,
expected: "^ mozilla",
},
{
input: " + mozilla ",
token: UrlbarTokenizer.RESTRICT.BOOKMARK,
expected: "* mozilla ",
},
{
input: " moz illa ",
token: UrlbarTokenizer.RESTRICT.TAG,
expected: "+ moz illa ",
},
];
let win = BrowserWindowTracker.getTopWindow();
for (let { input, token, expected } of tests) {
win.gURLBar.search(input);
TouchBarHelper.insertRestrictionInUrlbar(token);
Assert.equal(
win.gURLBar.value,
expected,
"The search restriction token should have been entered " +
"with stripped whitespace."
);
}
});

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

@ -983,6 +983,14 @@ class UrlbarInput {
return this._setValue(val, true);
}
get lastSearchString() {
return this._lastSearchString;
}
get openViewOnFocus() {
return this._openViewOnFocus;
}
get openViewOnFocusForCurrentTab() {
return (
this._openViewOnFocusAndSearchString ||

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

@ -38,4 +38,10 @@ interface nsITouchBarHelper : nsISupports
* Exposed for testing.
*/
nsITouchBarInput getTouchBarInput(in string aInputName);
/**
* Inserts a search restriction string in the Urlbar.
* Exposed for testing.
*/
void insertRestrictionInUrlbar(in string aToken);
};