Skip to content

Commit c04483f

Browse files
Fixed system status watcher
1 parent 109b005 commit c04483f

File tree

4 files changed

+115
-134
lines changed

4 files changed

+115
-134
lines changed

app/data/config/sysstat.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
"points": 20,
3-
"curve": "audio",
4-
"watch": [
5-
"cpu",
6-
"mem",
7-
"mem-process"
8-
]
3+
"curve": 1,
4+
"watch": [ 0, 2, 3 ]
95
}

app/translates/zh-CN/main.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ countries: cn
2020
"Select a license in the list to show content." = "从列表中选择一个许可证以展示内容。"
2121
"Waiting for a moment..." = "稍等一下..."
2222

23-
"cpu" = "CPU"
24-
"audio" = "音频"
25-
"mem" = "内存"
26-
"mem-process" = "内存-本程序"
23+
"CPU" = "CPU"
24+
"Audio" = "音频"
25+
"RAM" = "内存"
26+
"Process RAM" = "进程内存"
2727

2828
"Add" = "添加"
2929
"Remove" = "移除"

src/ui/component/toolbar/SysStatusComponent.cpp

Lines changed: 95 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,59 @@ SysStatusComponent::SysStatusComponent()
1515
LookAndFeelFactory::getInstance()->getLAFFor(LookAndFeelFactory::SysStatus));
1616
this->setMouseCursor(juce::MouseCursor::PointingHandCursor);
1717

18-
/** Translate */
19-
this->nameTrans.insert(std::make_pair("cpu", TRANS("cpu")));
20-
this->nameTrans.insert(std::make_pair("audio", TRANS("audio")));
21-
this->nameTrans.insert(std::make_pair("mem", TRANS("mem")));
22-
this->nameTrans.insert(std::make_pair("mem-process", TRANS("mem-process")));
18+
/** Name */
19+
this->watchName[WatchType::CPU] = TRANS("CPU");
20+
this->watchName[WatchType::Audio] = TRANS("Audio");
21+
this->watchName[WatchType::Memory] = TRANS("RAM");
22+
this->watchName[WatchType::ProcessMem] = TRANS("Process RAM");
23+
24+
/** Refresh */
25+
this->refresh();
2326
}
2427

25-
void SysStatusComponent::update() {
28+
void SysStatusComponent::refresh() {
2629
/** Get Config */
2730
juce::var& conf = ConfigManager::getInstance()->get("sysstat");
2831

29-
/** Check Curve Name And Size */
30-
juce::String curveName = conf["curve"].toString();
32+
/** Check Curve Type And Size */
33+
int curveType{ (int)conf["curve"] };
34+
if (curveType < 0 || curveType >= WatchType::TotalNum) {
35+
curveType = 0;
36+
}
37+
3138
int curveSize = conf["points"];
32-
if (curveName != this->curveName || curveSize != this->curveData.size()) {
33-
this->curveName = curveName;
39+
if (curveType != this->currentCurve || curveSize != this->curveData.size()) {
40+
this->currentCurve = (WatchType)curveType;
3441
this->clearCurve(curveSize);
3542
}
3643

37-
/** Get Curve Data */
38-
this->addCurve(this->getData(curveName));
39-
40-
/** Get Watch Name */
44+
/** Check Watch List */
4145
auto watchArray = conf["watch"].getArray();
4246
for (int i = 0; i < watchArray->size(); i++) {
43-
if (i < this->watchName.size()) {
44-
this->watchName[i] = watchArray->getUnchecked(i).toString();
45-
}
46-
}
47+
if (i < this->currentWatchList.size()) {
48+
int watchType{ (int)watchArray->getUnchecked(i) };
49+
if (watchType < 0 || watchType >= WatchType::TotalNum) {
50+
watchType = 0;
51+
}
4752

48-
/** Get Watch Data */
49-
for (int i = 0; i < this->watchName.size(); i++) {
50-
if (i < this->watchData.size()) {
51-
this->watchData[i] = this->getData(this->watchName[i]);
53+
this->currentWatchList[i] = (WatchType)watchType;
5254
}
5355
}
5456
}
5557

58+
void SysStatusComponent::update() {
59+
static SysStatus::CPUPercTemp cpuTemp;
60+
61+
/** Get Data */
62+
this->watchData[WatchType::CPU] = SysStatus::getInstance()->getCPUUsage(cpuTemp);
63+
this->watchData[WatchType::Audio] = quickAPI::getCPUUsage();
64+
this->watchData[WatchType::Memory] = SysStatus::getInstance()->getMemUsage();
65+
this->watchData[WatchType::ProcessMem] = SysStatus::getInstance()->getProcMemUsage();
66+
67+
/** Get Curve Data */
68+
this->addCurve(this->watchData[this->currentCurve]);
69+
}
70+
5671
void SysStatusComponent::paint(juce::Graphics& g) {
5772
auto& laf = this->getLookAndFeel();
5873
auto screenSize = utils::getScreenSize(this);
@@ -88,30 +103,32 @@ void SysStatusComponent::paint(juce::Graphics& g) {
88103
g.fillAll();
89104

90105
/** Watch */
91-
for (int i = 0; i < this->watchData.size() && i < this->watchName.size(); i++) {
106+
for (int i = 0; i < this->currentWatchList.size(); i++) {
92107
juce::Rectangle<int> labelRect(
93108
this->getWidth() - watchWidth + watchPaddingWidth, i * (watchLabelHeight + watchTextHeight),
94109
watchWidth - watchPaddingWidth * 2, watchLabelHeight);
95110
juce::Rectangle<int> textRect(
96111
this->getWidth() - watchWidth + watchPaddingWidth, i * (watchLabelHeight + watchTextHeight) + watchLabelHeight,
97112
watchWidth - watchPaddingWidth * 2, watchTextHeight);
98113

99-
juce::String name = this->watchName[i];
100-
double value = this->watchData[i];
114+
WatchType type = this->currentWatchList[i];
115+
juce::String name = this->watchName[type];
116+
double value = this->watchData[type];
101117

102118
g.setColour(labelColor);
103119
g.setFont(labelFont);
104-
g.drawFittedText(this->nameTrans[name], labelRect,
120+
g.drawFittedText(name, labelRect,
105121
juce::Justification::centredLeft, 1, 1.f);
106-
g.setColour(this->getAlert(name, value) ? alertColor : textColor);
122+
g.setColour(this->getAlert(type, value) ? alertColor : textColor);
107123
g.setFont(textFont);
108-
g.drawFittedText(this->getValueText(name, value), textRect,
124+
g.drawFittedText(this->getValueText(type, value), textRect,
109125
juce::Justification::centredRight, 1, 1.f);
110126
}
111127

112128
/** Curve Label */
113-
auto [vMin, vMax] = this->getRange();
129+
auto [vMin, vMax] = this->getRange(this->currentCurve);
114130
int curveWidth = this->getWidth() - watchWidth;
131+
auto& curveName = this->watchName[this->currentCurve];
115132

116133
juce::Rectangle<int> maxLabelRect(
117134
curveWidth - curvePaddingWidth - curveLabelWidth, 0,
@@ -123,10 +140,10 @@ void SysStatusComponent::paint(juce::Graphics& g) {
123140
g.setColour(labelColor);
124141
g.setFont(labelFont);
125142
g.drawFittedText(
126-
this->getValueText(this->curveName, vMax), maxLabelRect,
143+
this->getValueText(this->currentCurve, vMax), maxLabelRect,
127144
juce::Justification::centredRight, 1, 1.f);
128145
g.drawFittedText(
129-
this->getValueText(this->curveName, vMin), minLabelRect,
146+
this->getValueText(this->currentCurve, vMin), minLabelRect,
130147
juce::Justification::centredRight, 1, 1.f);
131148

132149
/** Curve */
@@ -154,13 +171,12 @@ void SysStatusComponent::paint(juce::Graphics& g) {
154171
curveLabelWidth, watchTextHeight);
155172
g.setColour(labelColor);
156173
g.setFont(labelFont);
157-
g.drawFittedText(
158-
this->nameTrans[this->curveName], curveLabelRect,
174+
g.drawFittedText(curveName, curveLabelRect,
159175
juce::Justification::centredLeft, 1, 1.f);
160-
g.setColour(this->getAlert(this->curveName, vTemp) ? alertColor : textColor);
176+
g.setColour(this->getAlert(this->currentCurve, vTemp) ? alertColor : textColor);
161177
g.setFont(textFont);
162178
g.drawFittedText(
163-
this->getValueText(this->curveName, vTemp), curveTextRect,
179+
this->getValueText(this->currentCurve, vTemp), curveTextRect,
164180
juce::Justification::centredLeft, 1, 1.f);
165181
}
166182

@@ -192,21 +208,21 @@ void SysStatusComponent::mouseMove(const juce::MouseEvent& event) {
192208

193209
/** Curve */
194210
if (event.position.getX() < (this->getWidth() - watchWidth)) {
195-
juce::String name = this->curveName;
196-
this->setTooltip(this->nameTrans[name] + ": " +
197-
this->getValueText(name, this->getCurve(this->curveData.size() - 1)));
211+
auto& curveName = this->watchName[this->currentCurve];
212+
this->setTooltip(curveName + ": " +
213+
this->getValueText(this->currentCurve, this->getCurve(this->curveData.size() - 1)));
198214
}
199215
/** Watch */
200216
else {
201217
int index = event.position.getY() / watchHeight;
202-
if (index < 0 || index >= this->watchName.size()
203-
|| index >= this->watchData.size()) {
218+
if (index < 0 || index >= this->currentWatchList.size()) {
204219
return;
205220
}
206221

207-
juce::String name = this->watchName[index];
208-
this->setTooltip(this->nameTrans[name] + ": " +
209-
this->getValueText(name, this->watchData[index]));
222+
WatchType type = this->currentWatchList[index];
223+
juce::String name = this->watchName[type];
224+
this->setTooltip(name + ": " +
225+
this->getValueText(type, this->watchData[type]));
210226
}
211227
}
212228

@@ -240,19 +256,8 @@ double SysStatusComponent::getCurve(int index) {
240256
return this->curveData[realIndex];
241257
}
242258

243-
double SysStatusComponent::getData(const juce::String& name) const {
244-
static SysStatus::CPUPercTemp cpuTemp;
245-
246-
if (name == "cpu") { return SysStatus::getInstance()->getCPUUsage(cpuTemp); }
247-
else if (name == "audio") { return quickAPI::getCPUUsage(); }
248-
else if (name == "mem") { return SysStatus::getInstance()->getMemUsage(); }
249-
else if (name == "mem-process") { return SysStatus::getInstance()->getProcMemUsage(); }
250-
251-
return 0;
252-
}
253-
254-
std::tuple<double, double> SysStatusComponent::getRange() const {
255-
if (this->curveName == "mem-process") {
259+
std::tuple<double, double> SysStatusComponent::getRange(WatchType type) const {
260+
if (type == WatchType::ProcessMem) {
256261
double maxT = 0;
257262
for (auto i : this->curveData) {
258263
maxT = std::max(maxT, i);
@@ -264,17 +269,17 @@ std::tuple<double, double> SysStatusComponent::getRange() const {
264269
}
265270

266271
bool SysStatusComponent::getAlert(
267-
const juce::String& name, double value) const {
268-
if (name == "mem-process") {
272+
WatchType type, double value) const {
273+
if (type == WatchType::ProcessMem) {
269274
return false;
270275
}
271276

272277
return value >= 0.9;
273278
}
274279

275280
juce::String SysStatusComponent::getValueText(
276-
const juce::String& name, double value) const {
277-
if (name == "mem-process") {
281+
WatchType type, double value) const {
282+
if (type == WatchType::ProcessMem) {
278283
if (value < (uint64_t)1024) {
279284
return juce::String{ value, 2, false } + "B";
280285
}
@@ -295,55 +300,41 @@ juce::String SysStatusComponent::getValueText(
295300

296301
void SysStatusComponent::showCurveMenu() {
297302
/** Show Menu */
298-
auto menu = this->createMenu(this->curveName, this->curveData.size(), true);
303+
auto menu = this->createMenu(this->currentCurve, true);
299304
int result = menu.show();
300305

301306
/** Result */
302307
auto conf = ConfigManager::getInstance()->get("sysstat").getDynamicObject();
303308
if (!conf) { return; }
304309
switch (result) {
305-
case 1:
306-
conf->setProperty("curve", "cpu");
307-
ConfigManager::getInstance()->saveConfig("sysstat");
308-
break;
309-
case 2:
310-
conf->setProperty("curve", "audio");
311-
ConfigManager::getInstance()->saveConfig("sysstat");
312-
break;
313-
case 3:
314-
conf->setProperty("curve", "mem");
315-
ConfigManager::getInstance()->saveConfig("sysstat");
316-
break;
317-
case 4:
318-
conf->setProperty("curve", "mem-process");
310+
case 110:
311+
case 120:
312+
case 150:
313+
conf->setProperty("points", result - 100);
319314
ConfigManager::getInstance()->saveConfig("sysstat");
320315
break;
321316

322-
case 101:
323-
conf->setProperty("points", 10);
324-
ConfigManager::getInstance()->saveConfig("sysstat");
325-
break;
326-
case 102:
327-
conf->setProperty("points", 20);
328-
ConfigManager::getInstance()->saveConfig("sysstat");
329-
break;
330-
case 103:
331-
conf->setProperty("points", 50);
332-
ConfigManager::getInstance()->saveConfig("sysstat");
317+
default:
318+
if (result >= 1 && result <= WatchType::TotalNum) {
319+
conf->setProperty("curve", result - 1);
320+
ConfigManager::getInstance()->saveConfig("sysstat");
321+
}
333322
break;
334323
}
324+
325+
/** Refresh */
326+
this->refresh();
335327
}
336328

337329
void SysStatusComponent::showWatchMenu(int index) {
338-
if (index < 0 || index >= this->watchName.size()
339-
|| index >= this->watchData.size()) { return; }
330+
if (index < 0 || index >= this->currentWatchList.size()) { return; }
340331

341332
/** Show Menu */
342-
auto menu = this->createMenu(this->watchName[index], 0, false);
333+
auto menu = this->createMenu(this->currentWatchList[index], false);
343334
int result = menu.show();
344335

345336
/** Set Func */
346-
auto setFunc = [index, size = this->watchName.size()](const juce::String& name)->bool {
337+
auto setFunc = [index, size = this->currentWatchList.size()](WatchType type)->bool {
347338
auto conf = ConfigManager::getInstance()->get("sysstat").getDynamicObject();
348339
if (!conf) { return false; }
349340

@@ -354,43 +345,34 @@ void SysStatusComponent::showWatchMenu(int index) {
354345
ptrArray->resize(size);
355346
}
356347

357-
ptrArray->set(index, name);
348+
ptrArray->set(index, (int)type);
358349
conf->setProperty("watch", juce::var{ *ptrArray });
359350
ConfigManager::getInstance()->saveConfig("sysstat");
360351
return true;
361352
};
362353

363354
/** Result */
364-
switch (result) {
365-
case 1:
366-
if (!setFunc("cpu")) { return; }
367-
break;
368-
case 2:
369-
if (!setFunc("audio")) { return; }
370-
break;
371-
case 3:
372-
if (!setFunc("mem")) { return; }
373-
break;
374-
case 4:
375-
if (!setFunc("mem-process")) { return; }
376-
break;
355+
if (result >= 1 && result <= WatchType::TotalNum) {
356+
setFunc((WatchType)(result - 1));
377357
}
358+
359+
/** Refresh */
360+
this->refresh();
378361
}
379362

380-
juce::PopupMenu SysStatusComponent::createMenu(const juce::String& currentName,
381-
int currentPoints, bool isCurve) {
363+
juce::PopupMenu SysStatusComponent::createMenu(WatchType type, bool isCurve) {
382364
juce::PopupMenu menu;
383365

384-
menu.addItem(1, this->nameTrans["cpu"], true, currentName == "cpu", nullptr);
385-
menu.addItem(2, this->nameTrans["audio"], true, currentName == "audio", nullptr);
386-
menu.addItem(3, this->nameTrans["mem"], true, currentName == "mem", nullptr);
387-
menu.addItem(4, this->nameTrans["mem-process"], true, currentName == "mem-process", nullptr);
366+
for (int i = 0; i < WatchType::TotalNum; i++) {
367+
menu.addItem(i + 1, this->watchName[i], true, type == i);
368+
}
388369

389370
menu.addSeparator();
390371

391-
menu.addItem(101, "10", isCurve, currentPoints == 10, nullptr);
392-
menu.addItem(102, "20", isCurve, currentPoints == 20, nullptr);
393-
menu.addItem(103, "50", isCurve, currentPoints == 50, nullptr);
372+
int currentPoints = this->curveData.size();
373+
menu.addItem(110, "10", isCurve, currentPoints == 10);
374+
menu.addItem(120, "20", isCurve, currentPoints == 20);
375+
menu.addItem(150, "50", isCurve, currentPoints == 50);
394376

395377
return menu;
396378
}

0 commit comments

Comments
 (0)