Files
lightgui/widgets/components/XSysBox/XSysBox.h
Muse 8c295dbbdf XFrame monitor widgets: XProcessList, XLoadBadge, XMonitorButton, XMonitorGrid, XSysBox
Live process table from /proc (sortable columns, in-cell CPU% bars,
colour-coded rows, refresh()), load-average badge (1/5/15 min,
threshold colours, uptime from /proc), monitor button bound to a
process (/proc scan), systemd unit (systemctl is-active) or TCP port
(connect to localhost) with poll()/update() and click-for-details,
grid panel of monitor buttons with one shared poll_all(), composite
resource box (title + big number + history + usage bar). Header-only
C++, demo mains (polling loop for the live three), examples, Xvfb
screenshots.
2026-12-05 16:49:29 +00:00

238 lines
7.4 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
// region XSysBox
//
// A composite resource box in the btop style: title + big number +
// history graph + usage bar (one box per resource: CPU / MEM / NET).
// Raw X11, same as the rest of the XFrame set.
//
class XSysBox
{
private:
Display* display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager* styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_; // title
std::string style_name_;
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
double value_ = 0.0;
double max_ = 100.0;
std::string unit_ = "%";
std::string big_suffix_;
int capacity_ = 60;
std::vector<double> history_;
Srgb white_color_{255, 255, 255};
Srgb grid_color_{225, 225, 225};
Srgb axis_color_{150, 150, 150};
Srgb graph_color_{0, 170, 70};
Srgb bar_color_{0, 150, 60};
Srgb track_color_{230, 230, 230};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt1(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.1f", v);
return buf;
}
void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color)
{
SStyle st = style();
if (!st.font_ || s.empty()) return;
XSetFont(display_, gc_, st.font_->fid);
XSetForeground(display_, gc_, col(color));
XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size());
}
public:
XSysBox(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
{
display_ = display;
screen_ = screen;
window_ = window;
gc_ = gc;
styles_ = styles;
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
name_ = "XSysBox";
text_ = "CPU";
unit_ = "%";
max_ = 100.0;
// Preload a demo: CPU-ish sine + noise history.
unsigned int seed = 31337;
auto rnd = [&]() {
seed = seed * 1103515245 + 12345;
return ((seed >> 16) & 0x7fff) / 32767.0;
};
for (int i = 0; i < capacity_; ++i)
{
double t = (double)i / capacity_;
double v = 55.0 + 28.0 * std::sin(t * 8.0) + (rnd() - 0.5) * 14.0;
history_.push_back(std::max(0.0, std::min(100.0, v)));
}
value_ = history_.empty() ? 0.0 : history_.back();
}
~XSysBox() {}
// region Standard plumbing
void set_bounds(int x, int y, int width, int height)
{
x1_ = x;
y1_ = y;
x2_ = x + width;
y2_ = y + height;
}
int width() { return x2_ - x1_; }
int height() { return y2_ - y1_; }
bool contains(int x, int y)
{
return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_;
}
void set_visible(bool v) { visible_ = v; }
bool get_visible() { return visible_; }
void set_enabled(bool e) { enabled_ = e; }
bool get_enabled() { return enabled_; }
void set_style_name(std::string s) { style_name_ = s; }
void set_text(std::string t) { text_ = t; }
std::string get_text() { return text_; }
SStyle style() { return styles_->resolve(style_name_); }
// endregion
// region Box API
// Sets the current value and appends it to the history graph.
void set_value(double v)
{
value_ = std::max(0.0, std::min(max_, v));
history_.push_back(value_);
while ((int)history_.size() > capacity_) history_.erase(history_.begin());
}
double get_value() { return value_; }
void set_max(double m)
{
max_ = m > 0.0 ? m : 100.0;
value_ = std::min(value_, max_);
}
double get_max() { return max_; }
void set_unit(const std::string& u) { unit_ = u; }
std::string get_unit() { return unit_; }
void set_graph_color(Srgb c) { graph_color_ = c; }
// endregion
void draw()
{
if (!visible_) return;
SStyle st = style();
int w = width();
int h = height();
XSetForeground(display_, gc_, col(white_color_));
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
const int title_h = 20;
const int big_h = 30;
const int bar_h = 18;
int gx = x1_ + 8;
int gy = y1_ + title_h + big_h + 4;
int gw = std::max(10, w - 16);
int gh = std::max(10, y2_ - bar_h - 12 - gy);
// Title + big number.
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
std::string big = fmt1(value_) + unit_;
draw_text_color(x1_ + 8, y1_ + title_h + 22, big, graph_color_);
draw_text_color(x1_ + 9, y1_ + title_h + 22, big, graph_color_);
// Max at the right of the big number row.
std::string mx = "max " + fmt1(max_) + unit_;
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, mx), y1_ + title_h + 22, mx);
// History graph.
XSetForeground(display_, gc_, col(grid_color_));
XDrawLine(display_, window_, gc_, gx, gy + gh / 2, gx + gw, gy + gh / 2);
if (history_.size() >= 2)
{
std::vector<XPoint> pts;
int n = (int)history_.size();
for (int i = 0; i < n; ++i)
{
int px = gx + (int)((double)i / (capacity_ - 1) * gw);
int py = gy + gh - (int)(history_[i] / max_ * gh);
pts.push_back(XPoint{(short)px, (short)py});
}
XSetForeground(display_, gc_, col(graph_color_));
xf::set_line_width(display_, gc_, 2);
XDrawLines(display_, window_, gc_, pts.data(), (int)pts.size(), CoordModeOrigin);
xf::set_line_width(display_, gc_, 1);
}
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, gx, gy, gw, gh);
// Usage bar at the bottom.
int bx = x1_ + 8;
int by = y2_ - bar_h - 6;
int bw = std::max(10, w - 16);
XSetForeground(display_, gc_, col(track_color_));
XFillRectangle(display_, window_, gc_, bx, by, bw, bar_h);
int filled = (int)(bw * (max_ > 0.0 ? value_ / max_ : 0.0));
XSetForeground(display_, gc_, col(bar_color_));
XFillRectangle(display_, window_, gc_, bx, by, filled, bar_h);
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h);
char pct[16];
std::snprintf(pct, sizeof(pct), "%.0f%%", max_ > 0.0 ? value_ / max_ * 100.0 : 0.0);
int pw = xf::text_width(st, pct);
xf::draw_text_at(display_, window_, gc_, st, bx + bw - pw - 4, by + 13, pct);
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_button_release(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
return false;
}
bool on_motion(int x, int y)
{
(void)x;
(void)y;
return false;
}
bool on_key(KeySym key, const char* str)
{
(void)key;
(void)str;
return false;
}
};