Skip to content

Instantly share code, notes, and snippets.

@mstriemer
Created August 13, 2020 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstriemer/7ab51d4c2cba4d7ad22add6f6a76899c to your computer and use it in GitHub Desktop.
Save mstriemer/7ab51d4c2cba4d7ad22add6f6a76899c to your computer and use it in GitHub Desktop.
Support previewing settings without saving them
diff --git a/toolkit/actors/PrintingChild.jsm b/toolkit/actors/PrintingChild.jsm
--- a/toolkit/actors/PrintingChild.jsm
+++ b/toolkit/actors/PrintingChild.jsm
@@ -81,17 +81,18 @@ class PrintingChild extends ActorChild {
receiveMessage(message) {
let data = message.data;
switch (message.name) {
case "Printing:Preview:Enter": {
this.enterPrintPreview(
Services.wm.getOuterWindowWithId(data.windowID),
data.simplifiedMode,
data.changingBrowsers,
- data.lastUsedPrinterName
+ data.lastUsedPrinterName,
+ data.jsonSettings
);
break;
}
case "Printing:Preview:Exit": {
this.exitPrintPreview();
break;
}
@@ -305,22 +306,30 @@ class PrintingChild extends ActorChild {
}
});
}
enterPrintPreview(
contentWindow,
simplifiedMode,
changingBrowsers,
- lastUsedPrinterName
+ lastUsedPrinterName,
+ jsonSettings
) {
const { docShell } = this;
try {
let printSettings = this.getPrintSettings(lastUsedPrinterName);
+ let customSettings = JSON.parse(jsonSettings);
+ for (let [name, value] of Object.entries(customSettings)) {
+ try {
+ printSettings[name] = value;
+ } catch (e) {}
+ }
+
// If we happen to be on simplified mode, we need to set docURL in order
// to generate header/footer content correctly, since simplified tab has
// "about:blank" as its URI.
if (printSettings && simplifiedMode) {
printSettings.docURL = contentWindow.document.baseURI;
}
// The print preview docshell will be in a different TabGroup, so
diff --git a/toolkit/components/printing/content/print.js b/toolkit/components/printing/content/print.js
--- a/toolkit/components/printing/content/print.js
+++ b/toolkit/components/printing/content/print.js
@@ -111,17 +111,17 @@ var PrintEventHandler = {
}
if (isChanged) {
let PSSVC = Cc["@mozilla.org/gfx/printsettings-service;1"].getService(
Ci.nsIPrintSettingsService
);
if (flags) {
- PSSVC.savePrintSettingsToPrefs(this.settings, true, flags);
+ // PSSVC.savePrintSettingsToPrefs(this.settings, true, flags);
this.updatePrintPreview();
}
document.dispatchEvent(
new CustomEvent("print-settings", {
detail: this.viewSettings,
})
);
@@ -136,21 +136,36 @@ var PrintEventHandler = {
);
}
// else there's already an update queued.
} else {
this._previewUpdatingPromise = this._updatePrintPreview();
}
},
+ getJSONSettings() {
+ let obj = {};
+ for (let [prop, value] of Object.entries(this.settings)) {
+ if (
+ this.settings.hasOwnProperty(prop) &&
+ typeof value != "function" &&
+ !prop.startsWith("k") // TODO: Support settings that start with k.
+ ) {
+ obj[prop] = value;
+ }
+ }
+ return JSON.stringify(obj);
+ },
+
async _updatePrintPreview() {
let numPages = await PrintUtils.updatePrintPreview(
this.sourceBrowser,
this.previewBrowser,
- this.settings
+ this.settings,
+ this.getJSONSettings()
);
document.dispatchEvent(
new CustomEvent("page-count", { detail: { numPages } })
);
if (this._queuedPreviewUpdatePromise) {
// Now that we're done, the queued update (if there is one) will start.
this._previewUpdatingPromise = this._queuedPreviewUpdatePromise;
diff --git a/toolkit/components/printing/content/printUtils.js b/toolkit/components/printing/content/printUtils.js
--- a/toolkit/components/printing/content/printUtils.js
+++ b/toolkit/components/printing/content/printUtils.js
@@ -190,17 +190,17 @@ var PrintUtils = {
* @param printPreviewBrowser
* The Browser that contains the print preview.
* @param printSettings
* The nsIPrintSettings object to be used to render the preview.
* Note: Only printerName is currently used.
*
* @return {Promise<Integer>} The number of pages that were rendered in the preview.
*/
- updatePrintPreview(sourceBrowser, printPreviewBrowser, printSettings) {
+ updatePrintPreview(sourceBrowser, printPreviewBrowser, printSettings, jsonSettings) {
return new Promise(resolve => {
printPreviewBrowser.messageManager.addMessageListener(
"Printing:Preview:UpdatePageCount",
function done(message) {
printPreviewBrowser.messageManager.removeMessageListener(
"Printing:Preview:UpdatePageCount",
done
);
@@ -210,16 +210,17 @@ var PrintUtils = {
printPreviewBrowser.messageManager.sendAsyncMessage(
"Printing:Preview:Enter",
{
changingBrowsers: false,
lastUsedPrinterName: printSettings.printerName,
simplifiedMode: false,
windowID: sourceBrowser.outerWindowID,
+ jsonSettings,
}
);
});
},
/**
* Initialize a print, this will open the tab modal UI if it is enabled or
* defer to the native dialog/silent print.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment