1 Star 0 Fork 1

rubickCenter/rubick-system-mac-touchbar

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 4.23 KB
一键复制 编辑 原始数据 按行查看 历史
muwoo 提交于 2021-12-22 14:16 . init
const path = require('path')
const si = require('systeminformation');
const spawn = require('child_process').spawn;
const os = require("os");
const isMacOS = os.type() === "Darwin";
module.exports = () => {
const init = (TouchBar) => {
const {TouchBarButton, TouchBarSpacer} = TouchBar;
const LOAD_NORMAL = "#2ecc71";
const LOAD_MEDIUM = "#f1c40f";
const LOAD_HIGH = "#d35400";
const LOAD_SEVERE = "#e74c3c";
const cpu = new TouchBarButton({
label: '',
backgroundColor: "#bdc3c7",
icon: path.join(__dirname, 'icons/chip.png'),
iconPosition: "left",
click: () => {
spawn("/System/Applications/Utilities/Activity Monitor.app/Contents/MacOS/Activity\ Monitor", []);
}
});
const memory = new TouchBarButton({
label: '',
backgroundColor: "#bdc3c7",
icon: path.join(__dirname, 'icons/ram.png'),
iconPosition: "left"
});
const network = new TouchBarButton({
label: '',
backgroundColor: '#3498db',
icon: path.join(__dirname, 'icons/internet.png'),
iconPosition: "left"
});
const battery = new TouchBarButton({
label: '',
backgroundColor: "#bdc3c7",
icon: path.join(__dirname, 'icons/power.png'),
iconPosition: "left"
});
const updateData = () => {
si.currentLoad(function(data) {
if (typeof data !== 'undefined' && data){
const load = data.currentLoad.toFixed(0)
cpu.label = load+"%"
if (load <= 20) cpu.backgroundColor = LOAD_NORMAL;
else if (load > 20 && load <= 40) cpu.backgroundColor = LOAD_MEDIUM;
else if (load > 40 && load <= 80) cpu.backgroundColor = LOAD_HIGH;
else if (load > 80 && load <= 100) cpu.backgroundColor = LOAD_SEVERE;
}
})
si.mem(function(data) {
if (typeof data !== 'undefined' && data){
const load = ((100* data.active ) / data.total).toFixed(0)
memory.label = load+"%"
if (load <= 20) memory.backgroundColor = LOAD_NORMAL;
else if (load > 20 && load <= 40) memory.backgroundColor = LOAD_MEDIUM;
else if (load > 40 && load <= 80) memory.backgroundColor = LOAD_HIGH;
else if (load > 80 && load <= 100) memory.backgroundColor = LOAD_SEVERE;
}
})
si.networkStats("", function(data) {
if (typeof data !== 'undefined' && data){
const kbtx = (data[0].tx_sec * 0.001).toFixed(0)
const kbrx = (data[0].rx_sec * 0.001).toFixed(0)
const l = (kbtx+kbrx).toString().length
network.label = ""+ (kbtx*0.001).toFixed(2)
+""+ (kbrx*0.001).toFixed(2) +" MB/s"
}
})
si.battery( function(data) {
if (typeof data !== 'undefined' && data){
if (data.ischarging){
battery.icon = path.join(__dirname, 'icons/charger.png')
}else{
battery.icon = path.join(__dirname, 'icons/power.png')
}
const load = data.percent.toFixed(0)
battery.label = load+"%"
if (load <= 20) battery.backgroundColor = LOAD_SEVERE;
else if (load > 20 && load <= 40) battery.backgroundColor = LOAD_HIGH;
else if (load > 40 && load <= 80) battery.backgroundColor = LOAD_MEDIUM;
else if (load > 80 && load <= 100) battery.backgroundColor = LOAD_NORMAL
}
})
}
const touchBar = new TouchBar({
items: [
cpu,
new TouchBarSpacer({size: 'small'}),
memory,
new TouchBarSpacer({size: 'small'}),
network,
new TouchBarSpacer({size: 'small'}),
battery,
]
})
const start = (window) => {
window.on('focus', () => {
updateData();
});
}
return {
start,
touchBar,
}
}
return {
onReady(ctx) {
if (!isMacOS) return;
const {TouchBar, mainWindow} = ctx;
const {TouchBarPopover} = TouchBar;
const mito = init(TouchBar);
const monitor = new TouchBarPopover({
items: mito.touchBar,
label: '系统监控',
showCloseButton: true
});
const touchBar = new TouchBar({
items: [
monitor,
]
});
mainWindow.setTouchBar(touchBar);
mito.start(mainWindow);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rubick-center/rubick-system-mac-touchbar.git
git@gitee.com:rubick-center/rubick-system-mac-touchbar.git
rubick-center
rubick-system-mac-touchbar
rubick-system-mac-touchbar
master

搜索帮助