Skip to content

Commit

Permalink
Create a more crisp tray icon (#524)
Browse files Browse the repository at this point in the history
* Create a more crisp tray icon

* Switch to a platform dependent tray icon logic
  • Loading branch information
dnaka91 authored Oct 5, 2022
1 parent a3d3c36 commit 24c42a9
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions electron-shell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as electron from 'electron';
import * as os from 'os';
import {app, BrowserWindow, dialog, ipcMain, Menu, MessageBoxSyncOptions, nativeImage, shell, Tray} from 'electron';
import * as path from 'path';
import {join} from 'path';
Expand Down Expand Up @@ -32,6 +33,49 @@ const openExternalLinksInOSBrowser = (event, url) => {
}
};

/**
* Adds the source image in several layers of scaling to the target image as "presentation", which
* define the same image at different scaling factors.
*
* The base size defines the standard size at 1.0 scale.
*/
const addPresentationIcons = (
target: electron.NativeImage,
source: electron.NativeImage,
baseSize: number,
): void => {
[1, 1.25, 1.5, 2, 3].forEach(scale => {
target.addRepresentation({
scaleFactor: scale,
width: baseSize * scale,
height: baseSize * scale,
buffer: source.resize({ width: baseSize * scale, height: baseSize * scale }).toBitmap(),
});
});
};

/**
* Create the tray icon for the OSs tray area. The shape of the image is different, depending on the
* platform, like different coloring and different base sizes.
*/
const createTrayIcon = (appRootPath: string): electron.NativeImage => {
const trayIconPath = join(appRootPath, 'assets/icons/icon-128x128.png');
const trayIcon = nativeImage.createFromPath(trayIconPath);
const scaledIcon = nativeImage.createEmpty();

switch (os.platform()) {
case 'darwin':
addPresentationIcons(scaledIcon, trayIcon, 16);
scaledIcon.setTemplateImage(true);
case 'win32':
addPresentationIcons(scaledIcon, trayIcon, 16);
default: // Linux, OpenBSD, FreeBSD and everything else
addPresentationIcons(scaledIcon, trayIcon, 32);
}

return scaledIcon;
};

var initPath = path.join(NEW_CONFIG_PATH, "init.json");
var data = { bounds: { width: 800, height: 600 }};
try {
Expand All @@ -42,6 +86,11 @@ catch(e) {

let alreadyAllowedToBeHidden = false;
let tray;

/**
* Create the application's tray icon in the system menu bar, setting up the platform dependent icon
* as well as menu items and further tray information.
*/
function createTray () {
function toggle () {
if (win !== null) {
Expand All @@ -53,15 +102,9 @@ function createTray () {


const appRootPath = getAppRootPath();
const trayIcon = createTrayIcon(appRootPath);

const trayIcon = join(appRootPath, 'assets/icons/favicon.256x256.png');
const trayIconNativeImage = nativeImage.createFromPath(
trayIcon
);

const resized = trayIconNativeImage.resize({ width: 16, height: 16 });

tray = new Tray(resized);
tray = new Tray(trayIcon);

let template = [{
label: 'Toggle',
Expand Down

1 comment on commit 24c42a9

@vercel
Copy link

@vercel vercel bot commented on 24c42a9 Oct 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

meme-box – ./

meme-box-negu3.vercel.app
meme-box-git-develop-negu3.vercel.app
meme-box.vercel.app

Please sign in to comment.