MenuRole bug fix and add printer support.

This commit is contained in:
Gregor Biswanger
2020-04-18 17:17:05 +02:00
7 changed files with 241 additions and 32 deletions

View File

@@ -8,7 +8,7 @@
/// <summary>
/// The undo
/// </summary>
undo,
undo = 1,
/// <summary>
/// The redo

View File

@@ -0,0 +1,107 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Print dpi
/// </summary>
public class PrintDpi
{
/// <summary>
/// The horizontal dpi
/// </summary>
public float Horizontal { get; set; }
/// <summary>
/// The vertical dpi
/// </summary>
public float Vertical { get; set; }
}
/// <summary>
/// The page range to print
/// </summary>
public class PrintPageRange
{
/// <summary>
/// From
/// </summary>
public int From { get; set; }
/// <summary>
/// To
/// </summary>
public int To { get; set; }
}
/// <summary>
/// Print options
/// </summary>
public class PrintOptions
{
/// <summary>
/// Don't ask user for print settings
/// </summary>
public bool Silent { get; set; }
/// <summary>
/// Prints the background color and image of the web page
/// </summary>
public bool PrintBackground { get; set; }
/// <summary>
/// Set the printer device name to use
/// </summary>
public string DeviceName { get; set; }
/// <summary>
/// Set whether the printed web page will be in color or grayscale
/// </summary>
public bool Color { get; set; }
/// <summary>
/// Specifies the type of margins to use. Uses 0 for default margin, 1 for no
/// margin, and 2 for minimum margin.
/// </summary>
public int MarginsType { get; set; }
/// <summary>
/// true for landscape, false for portrait.
/// </summary>
public bool Landscape { get; set; }
/// <summary>
/// The scale factor of the web page
/// </summary>
public float ScaleFactor { get; set; }
/// <summary>
/// The number of pages to print per page sheet
/// </summary>
public int PagesPerSheet { get; set; }
/// <summary>
/// The number of copies of the web page to print
/// </summary>
public bool Copies { get; set; }
/// <summary>
/// Whether the web page should be collated
/// </summary>
public bool Collate { get; set; }
/// <summary>
/// The page range to print
/// </summary>
public PrintPageRange PageRanges { get; set; }
/// <summary>
/// Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.
/// </summary>
public string DuplexMode { get; set; }
/// <summary>
/// Dpi
/// </summary>
public PrintDpi Dpi { get; set; }
}
}

View File

@@ -0,0 +1,29 @@
namespace ElectronNET.API.Entities
{
/// <summary>
/// Printer info
/// </summary>
public class PrinterInfo
{
/// <summary>
/// Name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Name
/// </summary>
public string Description { get; set; }
/// <summary>
/// Status
/// </summary>
public int Status { get; set; }
/// <summary>
/// Is default
/// </summary>
public bool IsDefault { get; set; }
}
}

View File

@@ -107,6 +107,53 @@ namespace ElectronNET.API
BridgeConnector.Socket.Emit("webContentsOpenDevTools", Id, JObject.FromObject(openDevToolsOptions, _jsonSerializer));
}
/// <summary>
/// Get system printers.
/// </summary>
/// <returns>printers</returns>
public Task<PrinterInfo[]> GetPrintersAsync()
{
var taskCompletionSource = new TaskCompletionSource<PrinterInfo[]>();
BridgeConnector.Socket.On("webContents-getPrinters-completed", (printers) =>
{
BridgeConnector.Socket.Off("webContents-getPrinters-completed");
taskCompletionSource.SetResult(((Newtonsoft.Json.Linq.JArray)printers).ToObject<PrinterInfo[]>());
});
BridgeConnector.Socket.Emit("webContents-getPrinters", Id);
return taskCompletionSource.Task;
}
/// <summary>
/// Prints window's web page.
/// </summary>
/// <param name="options"></param>
/// <returns>success</returns>
public Task<bool> PrintAsync(PrintOptions options = null)
{
var taskCompletionSource = new TaskCompletionSource<bool>();
BridgeConnector.Socket.On("webContents-print-completed", (success) =>
{
BridgeConnector.Socket.Off("webContents-print-completed");
taskCompletionSource.SetResult((bool)success);
});
if(options == null)
{
BridgeConnector.Socket.Emit("webContents-print", Id, "");
}
else
{
BridgeConnector.Socket.Emit("webContents-print", Id, JObject.FromObject(options, _jsonSerializer));
}
return taskCompletionSource.Task;
}
/// <summary>
/// Prints window's web page as PDF with Chromium's preview printing custom
/// settings.The landscape will be ignored if @page CSS at-rule is used in the web page.

View File

@@ -1,4 +1,12 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const electron_1 = require("electron");
const fs = require('fs');
let electronSocket;
@@ -26,8 +34,16 @@ module.exports = (socket) => {
getWindowById(id).webContents.openDevTools();
}
});
socket.on('webContents-printToPDF', async (id, options = {}, path) => {
const buffer = await getWindowById(id).webContents.printToPDF(options);
socket.on('webContents-getPrinters', (id) => __awaiter(this, void 0, void 0, function* () {
const printers = yield getWindowById(id).webContents.getPrinters();
electronSocket.emit('webContents-getPrinters-completed', printers);
}));
socket.on('webContents-print', (id, options = {}) => __awaiter(this, void 0, void 0, function* () {
yield getWindowById(id).webContents.print(options);
electronSocket.emit('webContents-print-completed', true);
}));
socket.on('webContents-printToPDF', (id, options = {}, path) => __awaiter(this, void 0, void 0, function* () {
const buffer = yield getWindowById(id).webContents.printToPDF(options);
fs.writeFile(path, buffer, (error) => {
if (error) {
electronSocket.emit('webContents-printToPDF-completed', false);
@@ -36,7 +52,7 @@ module.exports = (socket) => {
electronSocket.emit('webContents-printToPDF-completed', true);
}
});
});
}));
socket.on('webContents-getUrl', function (id) {
const browserWindow = getWindowById(id);
electronSocket.emit('webContents-getUrl' + id, browserWindow.webContents.getURL());
@@ -45,31 +61,31 @@ module.exports = (socket) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.allowNTLMCredentialsForDomains(domains);
});
socket.on('webContents-session-clearAuthCache', async (id, options, guid) => {
socket.on('webContents-session-clearAuthCache', (id, options, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.clearAuthCache(options);
yield browserWindow.webContents.session.clearAuthCache(options);
electronSocket.emit('webContents-session-clearAuthCache-completed' + guid);
});
socket.on('webContents-session-clearCache', async (id, guid) => {
}));
socket.on('webContents-session-clearCache', (id, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.clearCache();
yield browserWindow.webContents.session.clearCache();
electronSocket.emit('webContents-session-clearCache-completed' + guid);
});
socket.on('webContents-session-clearHostResolverCache', async (id, guid) => {
}));
socket.on('webContents-session-clearHostResolverCache', (id, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.clearHostResolverCache();
yield browserWindow.webContents.session.clearHostResolverCache();
electronSocket.emit('webContents-session-clearHostResolverCache-completed' + guid);
});
socket.on('webContents-session-clearStorageData', async (id, guid) => {
}));
socket.on('webContents-session-clearStorageData', (id, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.clearStorageData({});
yield browserWindow.webContents.session.clearStorageData({});
electronSocket.emit('webContents-session-clearStorageData-completed' + guid);
});
socket.on('webContents-session-clearStorageData-options', async (id, options, guid) => {
}));
socket.on('webContents-session-clearStorageData-options', (id, options, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.clearStorageData(options);
yield browserWindow.webContents.session.clearStorageData(options);
electronSocket.emit('webContents-session-clearStorageData-options-completed' + guid);
});
}));
socket.on('webContents-session-createInterruptedDownload', (id, options) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.createInterruptedDownload(options);
@@ -86,16 +102,16 @@ module.exports = (socket) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.flushStorageData();
});
socket.on('webContents-session-getBlobData', async (id, identifier, guid) => {
socket.on('webContents-session-getBlobData', (id, identifier, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
const buffer = await browserWindow.webContents.session.getBlobData(identifier);
const buffer = yield browserWindow.webContents.session.getBlobData(identifier);
electronSocket.emit('webContents-session-getBlobData-completed' + guid, buffer.buffer);
});
socket.on('webContents-session-getCacheSize', async (id, guid) => {
}));
socket.on('webContents-session-getCacheSize', (id, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
const size = await browserWindow.webContents.session.getCacheSize();
const size = yield browserWindow.webContents.session.getCacheSize();
electronSocket.emit('webContents-session-getCacheSize-completed' + guid, size);
});
}));
socket.on('webContents-session-getPreloads', (id, guid) => {
const browserWindow = getWindowById(id);
const preloads = browserWindow.webContents.session.getPreloads();
@@ -106,11 +122,11 @@ module.exports = (socket) => {
const userAgent = browserWindow.webContents.session.getUserAgent();
electronSocket.emit('webContents-session-getUserAgent-completed' + guid, userAgent);
});
socket.on('webContents-session-resolveProxy', async (id, url, guid) => {
socket.on('webContents-session-resolveProxy', (id, url, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
const proxy = await browserWindow.webContents.session.resolveProxy(url);
const proxy = yield browserWindow.webContents.session.resolveProxy(url);
electronSocket.emit('webContents-session-resolveProxy-completed' + guid, proxy);
});
}));
socket.on('webContents-session-setDownloadPath', (id, path) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.setDownloadPath(path);
@@ -119,11 +135,11 @@ module.exports = (socket) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.setPreloads(preloads);
});
socket.on('webContents-session-setProxy', async (id, configuration, guid) => {
socket.on('webContents-session-setProxy', (id, configuration, guid) => __awaiter(this, void 0, void 0, function* () {
const browserWindow = getWindowById(id);
await browserWindow.webContents.session.setProxy(configuration);
yield browserWindow.webContents.session.setProxy(configuration);
electronSocket.emit('webContents-session-setProxy-completed' + guid);
});
}));
socket.on('webContents-session-setUserAgent', (id, userAgent, acceptLanguages) => {
const browserWindow = getWindowById(id);
browserWindow.webContents.session.setUserAgent(userAgent, acceptLanguages);

File diff suppressed because one or more lines are too long

View File

@@ -30,6 +30,16 @@ export = (socket: SocketIO.Socket) => {
}
});
socket.on('webContents-getPrinters', async (id) => {
const printers = await getWindowById(id).webContents.getPrinters();
electronSocket.emit('webContents-getPrinters-completed', printers);
});
socket.on('webContents-print', async (id, options = {}) => {
await getWindowById(id).webContents.print(options);
electronSocket.emit('webContents-print-completed', true);
});
socket.on('webContents-printToPDF', async (id, options = {}, path) => {
const buffer = await getWindowById(id).webContents.printToPDF(options);