- Graphs (XHistoryGraph/XNetGraph/XTempHistory): dynamic left margin from widest Y label (fixes Y labels overflowing the plot), legend wrapped to two lines when too wide, legend on top, 5 Y labels, humanised units in NetGraph, TempHistory on the PY layout (now top-right, min/max/avg bottom, threshold label above the line). Top/bottom Y labels hang clear of the title and the strip below the plot. - XTachometer: numerals 7.5% of dial diameter bold just inside the tick ring, RPM bold centred in-dial, readout enlarged below the hub, redline numerals in red. - All 14 audit widgets: font sizes derived from widget dimensions per the proportions audit (titles 16px bold, hero values 24-48px bold, body/legend 13-16px, min 11px); CoreGrid 4x2 for 8 cores; UsageBar labels outside the bar; StackBar block centred with grown bar; VerticalBar value inside the bar; ArcGauge centre 40-48px + load sub-label + endpoints; DonutGauge centre 36-44px, ring 8-10% of diameter, duplicate title dropped; ProcessList 13px rows, colour-coded CPU bars after the number; LoadBadge centred value block with 1/5/15 sub-labels; MonitorButton scaled label/dot/status + detail line; MonitorGrid ceil(sqrt(n)) columns; SysBox hero 28-32px + max + bar %. - New xframe::scaled_font() helper in InstData.h (scalable misc-fixed XLFD, cached per size/bold) + font_text_width/draw_text_font. - Deviations from the audit spec: UsageBar label capped at 40px (the literal 0.45x rule gives 209px in the 464px-tall demo bar); SysBox hero uses 10% of height clamped 20-32px (spec 6-7% conflicts with the audited-correct PY value of 10.3%). All 15 widget demos rebuild clean and were screenshot-verified.
268 lines
9.3 KiB
C++
268 lines
9.3 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.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);
|
|
|
|
// Scaled fonts (Dec 5, 2026 proportions audit): hero value
|
|
// 28-32 px bold, title 14-16 px bold, "max" 13-14 px, bottom bar
|
|
// 24-28 px tall with a 14 px label. NOTE on the hero rule: the
|
|
// spec's 6-7% of height and the Python port's audited-correct
|
|
// 10.3% conflict, so the hero uses 10% of height clamped to
|
|
// 20-32 px — 28-32 px at the 480 px demo scale, ~23 px at the
|
|
// Python port's natural size.
|
|
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
|
int hero_px = xframe::clamp_font_px(0.10 * h, 20, 32);
|
|
int max_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
|
int bar_label_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
|
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
|
if (!title_font) title_font = st.font_;
|
|
XFontStruct* hero_font = xframe::scaled_font(display_, hero_px, true);
|
|
if (!hero_font) hero_font = st.font_;
|
|
XFontStruct* max_font = xframe::scaled_font(display_, max_px, false);
|
|
if (!max_font) max_font = st.font_;
|
|
XFontStruct* bar_font = xframe::scaled_font(display_, bar_label_px, true);
|
|
if (!bar_font) bar_font = st.font_;
|
|
int title_asc = title_font ? title_font->ascent : 11;
|
|
int hero_asc = hero_font ? hero_font->ascent : 11;
|
|
int bar_asc = bar_font ? bar_font->ascent : 11;
|
|
int bar_desc = bar_font ? bar_font->descent : 2;
|
|
|
|
const int title_h = title_px + 8;
|
|
const int hero_h = hero_px + 10;
|
|
const int bar_h = xframe::clamp_font_px(0.055 * h, 20, 28);
|
|
int gx = x1_ + 8;
|
|
int gy = y1_ + title_h + hero_h + 4;
|
|
int gw = std::max(10, w - 16);
|
|
int gh = std::max(10, y2_ - bar_h - 12 - gy);
|
|
|
|
// Title + big number.
|
|
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
|
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
|
std::string big = fmt1(value_) + unit_;
|
|
xframe::draw_text_font(display_, window_, gc_, hero_font, x1_ + 8,
|
|
y1_ + title_h + hero_asc + 2, big, col(graph_color_));
|
|
// Max at the right of the big number row.
|
|
std::string mx = "max " + fmt1(max_) + unit_;
|
|
xframe::draw_text_font(display_, window_, gc_, max_font,
|
|
x2_ - 8 - xframe::font_text_width(max_font, mx),
|
|
y1_ + title_h + hero_asc + 2, mx, col(Srgb{100, 100, 100}));
|
|
|
|
// 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 = xframe::font_text_width(bar_font, pct);
|
|
xframe::draw_text_font(display_, window_, gc_, bar_font, bx + bw - pw - 4,
|
|
by + (bar_h + bar_asc - bar_desc) / 2, pct,
|
|
col(Srgb{20, 20, 20}));
|
|
|
|
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;
|
|
}
|
|
};
|