Fix sharp import issue (#251)

This PR updates the window controls overlay code to remove the
dependency on `sharp`, which is a natively-compiled Node library that is
really hard to package for Electron given the way that we strip node
modules after bundling. I've replaced this with `pngjs`, which has a
smaller footprint and is still relatively fast (it doesn't need to be
perfect since it runs on the Node process instead of the browser
process).
This commit is contained in:
Evan Simkowitz
2024-08-20 13:18:47 -07:00
committed by GitHub
parent 5cbf2673f4
commit 5c00fc6e78
5 changed files with 88 additions and 322 deletions
+5 -3
View File
@@ -2,11 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
import * as electron from "electron";
import { getAverageColor } from "fast-average-color-node";
import { FastAverageColor } from "fast-average-color";
import fs from "fs";
import * as child_process from "node:child_process";
import os from "os";
import * as path from "path";
import { PNG } from "pngjs";
import * as readline from "readline";
import { sprintf } from "sprintf-js";
import { debounce } from "throttle-debounce";
@@ -596,8 +597,9 @@ electron.ipcMain.on("update-window-controls-overlay", async (event, rect: Dimens
};
const overlay = await event.sender.capturePage(electronRect);
const overlayBuffer = overlay.toPNG();
const color = await getAverageColor(overlayBuffer);
const png = PNG.sync.read(overlayBuffer);
const fac = new FastAverageColor();
const color = fac.prepareResult(fac.getColorFromArray4(png.data));
const window = electron.BrowserWindow.fromWebContents(event.sender);
window.setTitleBarOverlay({
color: unamePlatform === "linux" ? color.rgba : "#00000000", // Windows supports a true transparent overlay, so we don't need to set a background color.