Skip to content

Commit

Permalink
Merge pull request #39 from wakatime/feature/custom-icon-extractor
Browse files Browse the repository at this point in the history
icon issue fix
  • Loading branch information
alanhamlett authored Sep 26, 2024
2 parents 619ff6c + 2c04343 commit 772b726
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 223 deletions.
68 changes: 63 additions & 5 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,67 @@ declare module "iconutil" {
): void;
}

declare module "@bitdisaster/exe-icon-extractor" {
export function extractIcon(
filePath: string,
size: "large" | "small",
): Buffer;
declare module "icon-promise" {
interface IconPromiseOptions {
path: string;
sizeArg?: string;
context?: string;
}

interface IconData {
Base64ImageData: string;
Path: string;
Context: string;
}

class IconPromise {
/**
* Extracts image data from a file's icon with an adjustable size.
* @param options - Options for extracting the icon.
* @returns A promise that resolves to an object containing the icon image data.
*/
getIcon(options: IconPromiseOptions): Promise<IconData>;

/**
* Extracts image data from a file's icon with the size set as 16x16 pixels.
* @param path - The file path for the document to extract the icon from.
* @param context - Optional context string.
* @returns A promise that resolves to an object containing the icon image data.
*/
getIcon16(path: string, context?: string): Promise<IconData>;

/**
* Extracts image data from a file's icon with the size set as 32x32 pixels.
* @param path - The file path for the document to extract the icon from.
* @param context - Optional context string.
* @returns A promise that resolves to an object containing the icon image data.
*/
getIcon32(path: string, context?: string): Promise<IconData>;

/**
* Extracts image data from a file's icon with the size set as 48x48 pixels.
* @param path - The file path for the document to extract the icon from.
* @param context - Optional context string.
* @returns A promise that resolves to an object containing the icon image data.
*/
getIcon48(path: string, context?: string): Promise<IconData>;

/**
* Extracts image data from a file's icon with the size set as 256x256 pixels.
* @param path - The file path for the document to extract the icon from.
* @param context - Optional context string.
* @returns A promise that resolves to an object containing the icon image data.
*/
getIcon256(path: string, context?: string): Promise<IconData>;

/**
* Manually sets the path of the IconExtractor executable.
* @param extractorPath - The new folder path for the IconExtractor executable file.
* @param newName - Optional new name of the IconExtractor executable file (without extension).
*/
overrideExtractorPath(extractorPath: string, newName?: string): void;
}

const iconPromise: IconPromise;
export default iconPromise;
}
19 changes: 10 additions & 9 deletions electron/helpers/installed-apps/windows.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import iconPromise from "icon-promise";
import Winreg from "winreg";

import { Store } from "../../store";
Expand All @@ -8,7 +9,6 @@ import { allApps } from "../../watchers/apps";

function getFilePath(installLocation: string, execName: string) {
try {

let files = fs.readdirSync(installLocation);

// Some electron app has two exe file. One in the root install location and one in the app-{some-version-number} directory. eg.
Expand Down Expand Up @@ -50,14 +50,14 @@ async function getIcon(filePath: string) {
if (typeof cachedIcon === "string") {
return cachedIcon;
}
const { extractIcon } = await import("@bitdisaster/exe-icon-extractor");

const buffer = extractIcon(filePath, "large");
const icon = "data:image/png;base64," + buffer.toString("base64");
const output = await iconPromise.getIcon256(filePath, filePath);

const icon = "data:image/png;base64," + output.Base64ImageData;
Store.instance().set(`${filePath}-icon`, icon);
return icon;
} catch (error) {
console.log('Failed to get icon for', filePath, error);
console.log("Failed to get icon for", filePath, error);
return null;
}
}
Expand Down Expand Up @@ -86,18 +86,19 @@ export async function getApp(reg: Winreg.Registry) {
}

const app = allApps.find((app) => {
return (app.windows?.exePath &&
return (
app.windows?.exePath &&
app.windows.DisplayName &&
record["DisplayName"].startsWith(app.windows.DisplayName)
);
});

if(!app?.windows?.exePath) {
if (!app?.windows?.exePath) {
return undefined;
}

const installLocation = record['InstallLocation'];
if(!installLocation) {
const installLocation = record["InstallLocation"];
if (!installLocation) {
return undefined;
}

Expand Down
Loading

0 comments on commit 772b726

Please sign in to comment.