Last active
April 27, 2024 04:12
-
-
Save bellbind/f65f78a35bbbd4917a8ae4a6b18c5012 to your computer and use it in GitHub Desktop.
[electron] Tray launcher example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
// [run the app] | |
// $ npm install electron | |
// $ ./node_modules/.bin/electron . | |
const {app, nativeImage, Tray, Menu, BrowserWindow} = require("electron"); | |
let top = {}; // prevent gc to keep windows | |
app.once("ready", ev => { | |
top.win = new BrowserWindow({ | |
width: 800, height: 600, center: true, minimizable: false, show: false, | |
webPreferences: { | |
nodeIntegration: false, | |
webSecurity: true, | |
sandbox: true, | |
}, | |
}); | |
top.win.loadURL("https://google.com/"); | |
top.win.on("close", ev => { | |
//console.log(ev); | |
ev.sender.hide(); | |
ev.preventDefault(); // prevent quit process | |
}); | |
// empty image as transparent icon: it can click | |
// see: https://electron.atom.io/docs/api/tray/ | |
top.tray = new Tray(nativeImage.createEmpty()); | |
const menu = Menu.buildFromTemplate([ | |
{label: "Actions", submenu: [ | |
{label: "Open Google", click: (item, window, event) => { | |
//console.log(item, event); | |
top.win.show(); | |
}}, | |
]}, | |
{type: "separator"}, | |
{role: "quit"}, // "role": system prepared action menu | |
]); | |
top.tray.setToolTip("hello electrol"); | |
//top.tray.setTitle("Tray Example"); // macOS only | |
top.tray.setContextMenu(menu); | |
// Option: some animated web site to tray icon image | |
// see: https://electron.atom.io/docs/tutorial/offscreen-rendering/ | |
top.icons = new BrowserWindow({ | |
show: false, webPreferences: {offscreen: true}}); | |
top.icons.loadURL("https://trends.google.com/trends/hottrends/visualize"); | |
top.icons.webContents.on("paint", (event, dirty, image) => { | |
if (top.tray) top.tray.setImage(image.resize({width: 16, height: 16})); | |
}); | |
}); | |
app.on("before-quit", ev => { | |
// BrowserWindow "close" event spawn after quit operation, | |
// it requires to clean up listeners for "close" event | |
top.win.removeAllListeners("close"); | |
// release windows | |
top = null; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Tray Example", | |
"main": "main.js" | |
} |
Is there any way to set text as tray icon? I tried setting empty image and setting tray title which renders small space at the start
thanks ❤️
in macos, if not use nativeImage the tryIcon not show 💩
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to load external image (url) as tray icon