Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/tray.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Menu, Tray, shell, app, ipcMain, nativeTheme } = require('electron')
const { Menu, Tray, shell, app, ipcMain, nativeTheme, screen } = require('electron')
const i18n = require('i18next')
const path = require('path')
const os = require('os')
Expand Down Expand Up @@ -30,6 +30,42 @@ function buildCheckbox (key, label) {
}
}

const STATUS_ICON_SCALES = [1, 1.5, 2, 2.5, 3, 4, 5]

function asScaleSuffix (value) {
return `${Number.isInteger(value) ? value.toFixed(0) : value}x`
}

function getDisplayScaleFactor () {
const scaleFactor = screen.getPrimaryDisplay()?.scaleFactor
if (typeof scaleFactor === 'number' && Number.isFinite(scaleFactor) && scaleFactor > 0) {
return scaleFactor
}
return 1
}

function getStatusMenuIconPath (color) {
const statusIconDir = path.resolve(path.join(__dirname, '../assets/icons/status'))
const plainIconPath = path.join(statusIconDir, `${color}.png`)
if (fs.existsSync(plainIconPath)) {
return plainIconPath
}

const scaleFactor = getDisplayScaleFactor()
const scalesByDistance = [...STATUS_ICON_SCALES].sort((a, b) => {
return Math.abs(a - scaleFactor) - Math.abs(b - scaleFactor)
})

for (const scale of scalesByDistance) {
const scaledIconPath = path.join(statusIconDir, `${color}@${asScaleSuffix(scale)}.png`)
if (fs.existsSync(scaledIconPath)) {
return scaledIconPath
}
}

return path.join(statusIconDir, `${color}@1x.png`)
}

// Notes on this: we are only supporting accelerators on macOS for now because
// they natively work as soon as the menu opens. They don't work like that on Windows
// or other OSes and must be registered globally. They still collide with global
Expand Down Expand Up @@ -67,7 +103,7 @@ async function buildMenu () {
label: i18n.t(status),
visible: false,
enabled: false,
icon: path.resolve(path.join(__dirname, `../assets/icons/status/${color}.png`))
icon: getStatusMenuIconPath(color)
})),
// @ts-ignore
{
Expand Down