Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
/*
6
* Keeps the "browser.search.widget.inNavBar" preference synchronized.
7
*/
8
9
"use strict";
10
11
var EXPORTED_SYMBOLS = ["SearchWidgetTracker"];
12
13
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
14
15
ChromeUtils.defineModuleGetter(
16
this,
17
"CustomizableUI",
19
);
20
21
const WIDGET_ID = "search-container";
22
const PREF_NAME = "browser.search.widget.inNavBar";
23
24
const SearchWidgetTracker = {
25
init() {
26
this.onWidgetAdded = this.onWidgetRemoved = (widgetId, area) => {
27
if (widgetId == WIDGET_ID && area == CustomizableUI.AREA_NAVBAR) {
28
this.syncPreferenceWithWidget();
29
}
30
};
31
this.onWidgetReset = this.onWidgetUndoMove = node => {
32
if (node.id == WIDGET_ID) {
33
this.syncPreferenceWithWidget();
34
}
35
};
36
CustomizableUI.addListener(this);
37
Services.prefs.addObserver(PREF_NAME, () =>
38
this.syncWidgetWithPreference()
39
);
40
},
41
42
onAreaNodeRegistered(aArea) {
43
// The placement of the widget always takes priority, and the preference
44
// should always match the actual placement when the browser starts up - i.e.
45
// once the navigation bar has been registered.
46
if (aArea == CustomizableUI.AREA_NAVBAR) {
47
this.syncPreferenceWithWidget();
48
}
49
},
50
51
onCustomizeEnd() {
52
// onWidgetUndoMove does not fire when the search container is moved back to
53
// the customization palette as a result of an undo, so we sync again here.
54
this.syncPreferenceWithWidget();
55
},
56
57
syncPreferenceWithWidget() {
58
Services.prefs.setBoolPref(PREF_NAME, this.widgetIsInNavBar);
59
},
60
61
syncWidgetWithPreference() {
62
let newValue = Services.prefs.getBoolPref(PREF_NAME);
63
if (newValue == this.widgetIsInNavBar) {
64
return;
65
}
66
67
if (newValue) {
68
// The URL bar widget is always present in the navigation toolbar, so we
69
// can simply read its position to place the search bar right after it.
70
CustomizableUI.addWidgetToArea(
71
WIDGET_ID,
72
CustomizableUI.AREA_NAVBAR,
73
CustomizableUI.getPlacementOfWidget("urlbar-container").position + 1
74
);
75
} else {
76
CustomizableUI.removeWidgetFromArea(WIDGET_ID);
77
}
78
},
79
80
get widgetIsInNavBar() {
81
let placement = CustomizableUI.getPlacementOfWidget(WIDGET_ID);
82
return placement ? placement.area == CustomizableUI.AREA_NAVBAR : false;
83
},
84
};