Skip to content

Commit

Permalink
feat: monochrome tray icon on Windows & Linux (#2159)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored Aug 31, 2022
1 parent 7359657 commit a4d3e90
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 7 deletions.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added assets/icons/tray/others/off-32-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/others/off-32-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added assets/icons/tray/others/on-32-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/icons/tray/others/on-32-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
3 changes: 2 additions & 1 deletion assets/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@
"automaticGC": "Automatic Garbage Collection",
"takeScreenshotShortcut": "Global Screenshot Shortcut",
"experiments": "Experiments",
"npmOnIpfs": "Enable npm on IPFS"
"npmOnIpfs": "Enable npm on IPFS",
"monochromeTrayIcon": "Use Monochrome Tray Icon"
},
"setCustomIpfsBinaryConfirmation": {
"title": "Custom IPFS binary",
Expand Down
Binary file removed assets/macOS-icon.sketch
Binary file not shown.
Binary file added assets/tray.sketch
Binary file not shown.
1 change: 1 addition & 0 deletions src/common/config-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const CONFIG_KEYS = {
AUTO_GARBAGE_COLLECTOR: 'automaticGC',
SCREENSHOT_SHORTCUT: 'screenshotShortcut',
OPEN_WEBUI_LAUNCH: 'openWebUIAtLaunch',
MONOCHROME_TRAY_ICON: 'monochromeTrayIcon',
EXPERIMENT_PUBSUB: 'experiments.pubsub',
EXPERIMENT_PUBSUB_NAMESYS: 'experiments.pubsubNamesys'
}
Expand Down
33 changes: 27 additions & 6 deletions src/tray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Menu, Tray, shell, app, ipcMain } = require('electron')
const { Menu, Tray, shell, app, ipcMain, nativeTheme } = require('electron')
const i18n = require('i18next')
const path = require('path')
const addToIpfs = require('./add-to-ipfs')
Expand All @@ -15,6 +15,7 @@ const CONFIG_KEYS = require('./common/config-keys')

const { SHORTCUT: SCREENSHOT_SHORTCUT, takeScreenshot } = require('./take-screenshot')
const { isSupported: supportsLaunchAtLogin } = require('./auto-launch')
const createToggler = require('./utils/create-toggler')

function buildCheckbox (key, label) {
return {
Expand Down Expand Up @@ -108,6 +109,7 @@ function buildMenu (ctx) {
buildCheckbox(CONFIG_KEYS.OPEN_WEBUI_LAUNCH, 'settings.openWebUIAtLaunch'),
buildCheckbox(CONFIG_KEYS.AUTO_GARBAGE_COLLECTOR, 'settings.automaticGC'),
buildCheckbox(CONFIG_KEYS.SCREENSHOT_SHORTCUT, 'settings.takeScreenshotShortcut'),
...(IS_MAC ? [] : [buildCheckbox(CONFIG_KEYS.MONOCHROME_TRAY_ICON, 'settings.monochromeTrayIcon')]),
{ type: 'separator' },
{
label: i18n.t('settings.experiments'),
Expand Down Expand Up @@ -209,14 +211,20 @@ function buildMenu (ctx) {
const on = 'on'
const off = 'off'

function icon (color) {
function icon (status) {
const dir = path.resolve(path.join(__dirname, '../assets/icons/tray'))

if (!IS_MAC) {
return path.join(dir, `${color}-big.png`)
if (IS_MAC) {
return path.join(dir, 'macos', `${status}-22Template.png`)
}

return path.join(dir, `${color}-22Template.png`)
const bw = store.get(CONFIG_KEYS.MONOCHROME_TRAY_ICON, false)
if (bw) {
const theme = nativeTheme.shouldUseDarkColors ? 'dark' : 'light'
return path.join(dir, 'others', `${status}-32-${theme}.png`)
} else {
return path.join(dir, 'others', `${status}-large.png`)
}
}

// Ok this one is pretty ridiculous:
Expand Down Expand Up @@ -315,7 +323,11 @@ module.exports = function (ctx) {
// Update configuration checkboxes.
for (const key of Object.values(CONFIG_KEYS)) {
const enabled = store.get(key, false)
menu.getMenuItemById(key).checked = enabled
const item = menu.getMenuItemById(key)
if (item) {
// Not all items are present in all platforms.
item.checked = enabled
}
}

if (!IS_MAC && !IS_WIN) {
Expand Down Expand Up @@ -353,8 +365,17 @@ module.exports = function (ctx) {
ipcMain.on('configUpdated', () => { updateMenu() })
ipcMain.on('languageUpdated', () => { setupMenu() })

nativeTheme.on('updated', () => {
updateMenu()
})

setupMenu()

createToggler(CONFIG_KEYS.MONOCHROME_TRAY_ICON, async ({ newValue }) => {
store.set(CONFIG_KEYS.MONOCHROME_TRAY_ICON, newValue)
return true
})

ctx.tray = tray
logger.info('[tray] started')
}

0 comments on commit a4d3e90

Please sign in to comment.