Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active August 30, 2015 20:16
Show Gist options
  • Save Noitidart/10902477 to your computer and use it in GitHub Desktop.
Save Noitidart/10902477 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-CUISnips - Snippets of CustomizableUI.jsm used in Fiefox code.
id: "edit-controls",
type: "custom",
defaultArea: CustomizableUI.AREA_PANEL,
onBuild: function(aDocument) {
let buttons = [{
id: "cut-button",
command: "cmd_cut",
label: true,
tooltiptext: "tooltiptext2",
shortcutId: "key_cut",
}, {
id: "copy-button",
command: "cmd_copy",
label: true,
tooltiptext: "tooltiptext2",
shortcutId: "key_copy",
}, {
id: "paste-button",
command: "cmd_paste",
label: true,
tooltiptext: "tooltiptext2",
shortcutId: "key_paste",
}];
let node = aDocument.createElementNS(kNSXUL, "toolbaritem");
node.setAttribute("id", "edit-controls");
node.setAttribute("label", CustomizableUI.getLocalizedProperty(this, "label"));
node.setAttribute("title", CustomizableUI.getLocalizedProperty(this, "tooltiptext"));
// Set this as an attribute in addition to the property to make sure we can style correctly.
node.setAttribute("removable", "true");
node.classList.add("chromeclass-toolbar-additional");
node.classList.add("toolbaritem-combined-buttons");
node.classList.add(kWidePanelItemClass);
buttons.forEach(function(aButton, aIndex) {
if (aIndex != 0)
node.appendChild(aDocument.createElementNS(kNSXUL, "separator"));
let btnNode = aDocument.createElementNS(kNSXUL, "toolbarbutton");
setAttributes(btnNode, aButton);
node.appendChild(btnNode);
});
updateCombinedWidgetStyle(node, this.currentArea);
let listener = {
onWidgetAdded: function(aWidgetId, aArea, aPosition) {
if (aWidgetId != this.id)
return;
updateCombinedWidgetStyle(node, aArea);
}.bind(this),
onWidgetRemoved: function(aWidgetId, aPrevArea) {
if (aWidgetId != this.id)
return;
// When a widget is demoted to the palette ('removed'), it's visual
// style should change.
updateCombinedWidgetStyle(node);
}.bind(this),
onWidgetReset: function(aWidgetNode) {
if (aWidgetNode != node)
return;
updateCombinedWidgetStyle(node, this.currentArea);
}.bind(this),
onWidgetMoved: function(aWidgetId, aArea) {
if (aWidgetId != this.id)
return;
updateCombinedWidgetStyle(node, aArea);
}.bind(this),
onWidgetInstanceRemoved: function(aWidgetId, aDoc) {
if (aWidgetId != this.id || aDoc != aDocument)
return;
CustomizableUI.removeListener(listener);
}.bind(this),
onWidgetDrag: function(aWidgetId, aArea) {
if (aWidgetId != this.id)
return;
aArea = aArea || this.currentArea;
updateCombinedWidgetStyle(node, aArea);
}.bind(this)
};
CustomizableUI.addListener(listener);
return node;
}
},
{
id: "feed-button",
type: "view",
viewId: "PanelUI-feeds",
defaultArea: CustomizableUI.AREA_PANEL,
onClick: function(aEvent) {
let win = aEvent.target.ownerDocument.defaultView;
let feeds = win.gBrowser.selectedBrowser.feeds;
// Here, we only care about the case where we have exactly 1 feed and the
// user clicked...
let isClick = (aEvent.button == 0 || aEvent.button == 1);
if (feeds && feeds.length == 1 && isClick) {
aEvent.preventDefault();
aEvent.stopPropagation();
win.FeedHandler.subscribeToFeed(feeds[0].href, aEvent);
CustomizableUI.hidePanelForNode(aEvent.target);
}
},
onViewShowing: function(aEvent) {
let doc = aEvent.detail.ownerDocument;
let container = doc.getElementById("PanelUI-feeds");
let gotView = doc.defaultView.FeedHandler.buildFeedList(container, true);
// For no feeds or only a single one, don't show the panel.
if (!gotView) {
aEvent.preventDefault();
aEvent.stopPropagation();
return;
}
},
onCreated: function(node) {
let win = node.ownerDocument.defaultView;
let selectedBrowser = win.gBrowser.selectedBrowser;
let feeds = selectedBrowser && selectedBrowser.feeds;
if (!feeds || !feeds.length) {
node.setAttribute("disabled", "true");
}
}
},
id: "zoom-controls",
type: "custom",
defaultArea: CustomizableUI.AREA_PANEL,
onBuild: function(aDocument) {
const kPanelId = "PanelUI-popup";
let areaType = CustomizableUI.getAreaType(this.currentArea);
let inPanel = areaType == CustomizableUI.TYPE_MENU_PANEL;
let inToolbar = areaType == CustomizableUI.TYPE_TOOLBAR;
let buttons = [{
id: "zoom-out-button",
command: "cmd_fullZoomReduce",
label: true,
tooltiptext: "tooltiptext2",
shortcutId: "key_fullZoomReduce",
}, {
id: "zoom-reset-button",
command: "cmd_fullZoomReset",
tooltiptext: "tooltiptext2",
shortcutId: "key_fullZoomReset",
}, {
id: "zoom-in-button",
command: "cmd_fullZoomEnlarge",
label: true,
tooltiptext: "tooltiptext2",
shortcutId: "key_fullZoomEnlarge",
}];
let node = aDocument.createElementNS(kNSXUL, "toolbaritem");
node.setAttribute("id", "zoom-controls");
node.setAttribute("label", CustomizableUI.getLocalizedProperty(this, "label"));
node.setAttribute("title", CustomizableUI.getLocalizedProperty(this, "tooltiptext"));
// Set this as an attribute in addition to the property to make sure we can style correctly.
node.setAttribute("removable", "true");
node.classList.add("chromeclass-toolbar-additional");
node.classList.add("toolbaritem-combined-buttons");
node.classList.add(kWidePanelItemClass);
buttons.forEach(function(aButton, aIndex) {
if (aIndex != 0)
node.appendChild(aDocument.createElementNS(kNSXUL, "separator"));
let btnNode = aDocument.createElementNS(kNSXUL, "toolbarbutton");
setAttributes(btnNode, aButton);
node.appendChild(btnNode);
});
// The middle node is the 'Reset Zoom' button.
let zoomResetButton = node.childNodes[2];
let window = aDocument.defaultView;
function updateZoomResetButton() {
let updateDisplay = true;
// Label should always show 100% in customize mode, so don't update:
if (aDocument.documentElement.hasAttribute("customizing")) {
updateDisplay = false;
}
//XXXgijs in some tests we get called very early, and there's no docShell on the
// tabbrowser. This breaks the zoom toolkit code (see bug 897410). Don't let that happen:
let zoomFactor = 100;
try {
zoomFactor = Math.round(window.ZoomManager.zoom * 100);
} catch (e) {}
zoomResetButton.setAttribute("label", CustomizableUI.getLocalizedProperty(
buttons[1], "label", [updateDisplay ? zoomFactor : 100]
));
};
// Register ourselves with the service so we know when the zoom prefs change.
Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:zoomChange", false);
Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:zoomReset", false);
Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:location-change", false);
if (inPanel) {
let panel = aDocument.getElementById(kPanelId);
panel.addEventListener("popupshowing", updateZoomResetButton);
} else {
if (inToolbar) {
let container = window.gBrowser.tabContainer;
container.addEventListener("TabSelect", updateZoomResetButton);
}
updateZoomResetButton();
}
updateCombinedWidgetStyle(node, this.currentArea, true);
let listener = {
onWidgetAdded: function(aWidgetId, aArea, aPosition) {
if (aWidgetId != this.id)
return;
updateCombinedWidgetStyle(node, aArea, true);
updateZoomResetButton();
let areaType = CustomizableUI.getAreaType(aArea);
if (areaType == CustomizableUI.TYPE_MENU_PANEL) {
let panel = aDocument.getElementById(kPanelId);
panel.addEventListener("popupshowing", updateZoomResetButton);
} else if (areaType == CustomizableUI.TYPE_TOOLBAR) {
let container = window.gBrowser.tabContainer;
container.addEventListener("TabSelect", updateZoomResetButton);
}
}.bind(this),
onWidgetRemoved: function(aWidgetId, aPrevArea) {
if (aWidgetId != this.id)
return;
let areaType = CustomizableUI.getAreaType(aPrevArea);
if (areaType == CustomizableUI.TYPE_MENU_PANEL) {
let panel = aDocument.getElementById(kPanelId);
panel.removeEventListener("popupshowing", updateZoomResetButton);
} else if (areaType == CustomizableUI.TYPE_TOOLBAR) {
let container = window.gBrowser.tabContainer;
container.removeEventListener("TabSelect", updateZoomResetButton);
}
// When a widget is demoted to the palette ('removed'), it's visual
// style should change.
updateCombinedWidgetStyle(node, null, true);
updateZoomResetButton();
}.bind(this),
onWidgetReset: function(aWidgetNode) {
if (aWidgetNode != node)
return;
updateCombinedWidgetStyle(node, this.currentArea, true);
updateZoomResetButton();
}.bind(this),
onWidgetMoved: function(aWidgetId, aArea) {
if (aWidgetId != this.id)
return;
updateCombinedWidgetStyle(node, aArea, true);
updateZoomResetButton();
}.bind(this),
onWidgetInstanceRemoved: function(aWidgetId, aDoc) {
if (aWidgetId != this.id || aDoc != aDocument)
return;
CustomizableUI.removeListener(listener);
Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:zoomChange");
Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:zoomReset");
Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:location-change");
let panel = aDoc.getElementById(kPanelId);
panel.removeEventListener("popupshowing", updateZoomResetButton);
let container = aDoc.defaultView.gBrowser.tabContainer;
container.removeEventListener("TabSelect", updateZoomResetButton);
}.bind(this),
onCustomizeStart: function(aWindow) {
if (aWindow.document == aDocument) {
updateZoomResetButton();
}
},
onCustomizeEnd: function(aWindow) {
if (aWindow.document == aDocument) {
updateZoomResetButton();
}
},
onWidgetDrag: function(aWidgetId, aArea) {
if (aWidgetId != this.id)
return;
aArea = aArea || this.currentArea;
updateCombinedWidgetStyle(node, aArea, true);
}.bind(this)
};
CustomizableUI.addListener(listener);
return node;
}
@Noitidart
Copy link
Author

const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment