mirror of
https://github.com/pompurin404/mihomo-party.git
synced 2024-11-16 11:42:19 +08:00
complete tray traffic
This commit is contained in:
parent
c2bd36ff7a
commit
73f39f8760
|
@ -12,6 +12,7 @@ import svg2img from 'svg2img'
|
||||||
const icon = nativeImage.createFromPath(templateIcon)
|
const icon = nativeImage.createFromPath(templateIcon)
|
||||||
icon.setTemplateImage(true)
|
icon.setTemplateImage(true)
|
||||||
const base64 = icon.toPNG().toString('base64')
|
const base64 = icon.toPNG().toString('base64')
|
||||||
|
let hasShowTraffic = false
|
||||||
let axiosIns: AxiosInstance = null!
|
let axiosIns: AxiosInstance = null!
|
||||||
let mihomoTrafficWs: WebSocket | null = null
|
let mihomoTrafficWs: WebSocket | null = null
|
||||||
let trafficRetry = 10
|
let trafficRetry = 10
|
||||||
|
@ -192,10 +193,12 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') {
|
||||||
if (showTraffic) {
|
if (showTraffic) {
|
||||||
const svgContent = `
|
const svgContent = `
|
||||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 125 36">
|
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 156 36">
|
||||||
<image height='36' width='36' href='data:image/png;base64,${base64}'/>
|
<image height='36' width='36' href='data:image/png;base64,${base64}'/>
|
||||||
<text x='40' y='15' font-size='15' font-family='system-ui'>↑ ${calcTraffic(json.up)}/s</text>
|
<text x='40' y='15' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='start'>↑</text>
|
||||||
<text x='40' y='32' font-size='15' font-family='system-ui'>↓ ${calcTraffic(json.down)}/s</text>
|
<text x='40' y='34' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='start'>↓</text>
|
||||||
|
<text x='156' y='15' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.up)}/s</text>
|
||||||
|
<text x='156' y='34' font-size='18' font-family="PingFang SC" font-weight='bold' text-anchor='end'>${calcTraffic(json.down)}/s</text>
|
||||||
</svg>`
|
</svg>`
|
||||||
svg2img(svgContent, {}, (error, buffer) => {
|
svg2img(svgContent, {}, (error, buffer) => {
|
||||||
if (error) return
|
if (error) return
|
||||||
|
@ -203,10 +206,15 @@ const mihomoTraffic = async (): Promise<void> => {
|
||||||
image.setTemplateImage(true)
|
image.setTemplateImage(true)
|
||||||
tray?.setImage(image)
|
tray?.setImage(image)
|
||||||
})
|
})
|
||||||
|
hasShowTraffic = true
|
||||||
} else {
|
} else {
|
||||||
|
if (hasShowTraffic) {
|
||||||
|
hasShowTraffic = false
|
||||||
|
icon.resize({ height: 16 })
|
||||||
tray?.setImage(icon)
|
tray?.setImage(icon)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (process.platform !== 'linux') {
|
if (process.platform !== 'linux') {
|
||||||
tray?.setToolTip(
|
tray?.setToolTip(
|
||||||
'↑' +
|
'↑' +
|
||||||
|
|
|
@ -1,19 +1,31 @@
|
||||||
export function calcTraffic(byte: number): string {
|
export function calcTraffic(byte: number): string {
|
||||||
if (byte < 1024) return `${byte} B`
|
if (byte < 1024) return `${byte} B`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} KB`
|
if (byte < 1024) return `${formatNumString(byte)} KB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} MB`
|
if (byte < 1024) return `${formatNumString(byte)} MB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} GB`
|
if (byte < 1024) return `${formatNumString(byte)} GB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} TB`
|
if (byte < 1024) return `${formatNumString(byte)} TB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} PB`
|
if (byte < 1024) return `${formatNumString(byte)} PB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} EB`
|
if (byte < 1024) return `${formatNumString(byte)} EB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
if (byte < 1024) return `${Math.round(byte)} ZB`
|
if (byte < 1024) return `${formatNumString(byte)} ZB`
|
||||||
byte /= 1024
|
byte /= 1024
|
||||||
return `${Math.round(byte)} YB`
|
return `${formatNumString(byte)} YB`
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatNumString(num: number): string {
|
||||||
|
let str = num.toFixed(2)
|
||||||
|
if (str.length <= 5) return str
|
||||||
|
if (str.length == 6) {
|
||||||
|
str = num.toFixed(1)
|
||||||
|
return str
|
||||||
|
} else {
|
||||||
|
str = Math.round(num).toString()
|
||||||
|
return str
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user