- 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.
316 lines
11 KiB
C++
316 lines
11 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// region XNetGraph
|
|
//
|
|
// Dual up/down scrolling network graph (btop net box): two overlaid
|
|
// series in bytes/s, current rates plus running totals in the legend.
|
|
// Raw X11, same as the rest of the XFrame set.
|
|
//
|
|
class XNetGraph
|
|
{
|
|
private:
|
|
Display* display_;
|
|
int screen_;
|
|
Window window_;
|
|
GC gc_;
|
|
SStyle_Manager* styles_;
|
|
int x1_, y1_, x2_, y2_;
|
|
std::string name_;
|
|
std::string text_;
|
|
std::string style_name_;
|
|
bool visible_ = true;
|
|
bool enabled_ = true;
|
|
bool focused_ = false;
|
|
|
|
int capacity_ = 120;
|
|
std::vector<double> down_; // bytes/s
|
|
std::vector<double> up_; // bytes/s
|
|
double down_total_ = 0.0; // bytes
|
|
double up_total_ = 0.0; // bytes
|
|
double y_max_ = 0.0; // 0 = auto
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb grid_color_{225, 225, 225};
|
|
Srgb axis_color_{150, 150, 150};
|
|
Srgb down_color_{0, 170, 70};
|
|
Srgb up_color_{220, 80, 40};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
static std::string fmt_rate(double bps)
|
|
{
|
|
char buf[40];
|
|
if (bps < 1024.0) std::snprintf(buf, sizeof(buf), "%.0f B/s", bps);
|
|
else if (bps < 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.1f KiB/s", bps / 1024.0);
|
|
else std::snprintf(buf, sizeof(buf), "%.2f MiB/s", bps / (1024.0 * 1024.0));
|
|
return buf;
|
|
}
|
|
|
|
static std::string fmt_total(double bytes)
|
|
{
|
|
char buf[40];
|
|
if (bytes < 1024.0) std::snprintf(buf, sizeof(buf), "%.0f B", bytes);
|
|
else if (bytes < 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.1f KiB", bytes / 1024.0);
|
|
else if (bytes < 1024.0 * 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.2f MiB", bytes / (1024.0 * 1024.0));
|
|
else std::snprintf(buf, sizeof(buf), "%.2f GiB", bytes / (1024.0 * 1024.0 * 1024.0));
|
|
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());
|
|
}
|
|
|
|
void draw_series(const std::vector<double>& data, int px, int py, int pw, int ph, double hi, Srgb color)
|
|
{
|
|
if (data.size() < 2) return;
|
|
std::vector<XPoint> pts;
|
|
int n = (int)data.size();
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
int cx = px + (int)((double)i / (capacity_ - 1) * pw);
|
|
int cy = py + ph - (int)(data[i] / hi * ph);
|
|
pts.push_back(XPoint{(short)cx, (short)cy});
|
|
}
|
|
XSetForeground(display_, gc_, col(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);
|
|
}
|
|
|
|
public:
|
|
XNetGraph(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_ = "XNetGraph";
|
|
text_ = "Network";
|
|
|
|
// Preload a demo: bursty download, steadier upload (bytes/s).
|
|
unsigned int seed = 13579;
|
|
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 burst = (std::sin(t * 12.0) > 0.2) ? 1.0 : 0.15;
|
|
push(180000.0 * burst * (0.5 + rnd()), 24000.0 * (0.6 + rnd() * 0.7));
|
|
}
|
|
}
|
|
|
|
~XNetGraph() {}
|
|
|
|
// 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 Net API
|
|
// Rates are bytes/s; each push counts as a 0.5 s sample for totals.
|
|
void push(double down_bps, double up_bps)
|
|
{
|
|
down_.push_back(std::max(0.0, down_bps));
|
|
up_.push_back(std::max(0.0, up_bps));
|
|
down_total_ += std::max(0.0, down_bps) * 0.5;
|
|
up_total_ += std::max(0.0, up_bps) * 0.5;
|
|
while ((int)down_.size() > capacity_) down_.erase(down_.begin());
|
|
while ((int)up_.size() > capacity_) up_.erase(up_.begin());
|
|
}
|
|
double down_rate() { return down_.empty() ? 0.0 : down_.back(); }
|
|
double up_rate() { return up_.empty() ? 0.0 : up_.back(); }
|
|
double down_total() { return down_total_; }
|
|
double up_total() { return up_total_; }
|
|
void reset_totals()
|
|
{
|
|
down_total_ = 0.0;
|
|
up_total_ = 0.0;
|
|
}
|
|
// 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): title 16 px bold,
|
|
// Y labels + legend 14-16 px, derived from widget height.
|
|
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
|
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
|
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
|
if (!title_font) title_font = st.font_;
|
|
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
|
|
if (!body_font) body_font = st.font_;
|
|
int title_asc = title_font ? title_font->ascent : 11;
|
|
int body_asc = body_font ? body_font->ascent : 11;
|
|
const int title_h = title_px + 8;
|
|
const int axis_h = 16;
|
|
|
|
double hi = y_max_;
|
|
if (hi <= 0.0)
|
|
{
|
|
hi = 1.0;
|
|
for (double v : down_) hi = std::max(hi, v);
|
|
for (double v : up_) hi = std::max(hi, v);
|
|
hi *= 1.05;
|
|
}
|
|
|
|
// Dynamic left margin: the widest humanised Y label
|
|
// ("276.7 KiB/s") sets the plot's left edge; labels are
|
|
// right-aligned so none runs over the axis line.
|
|
std::string ylabels[5];
|
|
int axis_w = 8;
|
|
for (int g = 0; g <= 4; ++g)
|
|
{
|
|
ylabels[g] = fmt_rate(hi * (4 - g) / 4.0);
|
|
axis_w = std::max(axis_w, xframe::font_text_width(body_font, ylabels[g]) + 8);
|
|
}
|
|
|
|
// Legend lines first: a "down R total T" line that would run
|
|
// past the right edge splits into rate / total lines.
|
|
struct LegLine
|
|
{
|
|
std::string text;
|
|
bool swatch;
|
|
Srgb color{0, 0, 0};
|
|
};
|
|
std::vector<LegLine> leg_lines;
|
|
int leg_avail = std::max(20, x2_ - 8 - (x1_ + 32));
|
|
auto add_series_leg = [&](const std::string& tag, double rate, double tot, Srgb color) {
|
|
std::string full = tag + fmt_rate(rate) + " total " + fmt_total(tot);
|
|
if (xframe::font_text_width(body_font, full) <= leg_avail)
|
|
{
|
|
leg_lines.push_back(LegLine{full, true, color});
|
|
}
|
|
else
|
|
{
|
|
leg_lines.push_back(LegLine{tag + fmt_rate(rate), true, color});
|
|
leg_lines.push_back(LegLine{"total " + fmt_total(tot), false, color});
|
|
}
|
|
};
|
|
add_series_leg("down ", down_rate(), down_total_, down_color_);
|
|
add_series_leg("up ", up_rate(), up_total_, up_color_);
|
|
int line_h = body_px + 8;
|
|
int legend_h = line_h * (int)leg_lines.size();
|
|
int px = x1_ + axis_w;
|
|
int py = y1_ + title_h + legend_h + 4;
|
|
int pw = std::max(10, x2_ - 8 - px);
|
|
int ph = std::max(10, y2_ - axis_h - py);
|
|
|
|
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
|
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
|
|
|
// Grid + y labels (right-aligned, clear of the plot).
|
|
XSetForeground(display_, gc_, col(grid_color_));
|
|
for (int g = 0; g <= 4; ++g)
|
|
{
|
|
int gy = py + ph * g / 4;
|
|
XDrawLine(display_, window_, gc_, px, gy, px + pw, gy);
|
|
}
|
|
for (int g = 0; g <= 4; ++g)
|
|
{
|
|
int gy = py + ph * g / 4;
|
|
int lw = xframe::font_text_width(body_font, ylabels[g]);
|
|
int ybase = (g == 0) ? gy + body_asc
|
|
: (g == 4) ? gy - 2 : gy + body_asc / 2;
|
|
xframe::draw_text_font(display_, window_, gc_, body_font, px - 4 - lw,
|
|
ybase, ylabels[g], col(Srgb{100, 100, 100}));
|
|
}
|
|
|
|
draw_series(down_, px, py, pw, ph, hi, down_color_);
|
|
draw_series(up_, px, py, pw, ph, hi, up_color_);
|
|
|
|
// Legend: current rates + totals (wrapped lines computed above).
|
|
int ly = y1_ + title_h + 4;
|
|
for (auto& ln : leg_lines)
|
|
{
|
|
int base = ly + body_asc;
|
|
if (ln.swatch)
|
|
{
|
|
XSetForeground(display_, gc_, col(ln.color));
|
|
XFillRectangle(display_, window_, gc_, x1_ + 8, base - body_asc + 2, 18, 4);
|
|
}
|
|
xframe::draw_text_font(display_, window_, gc_, body_font, x1_ + 32, base,
|
|
ln.text, col(Srgb{20, 20, 20}));
|
|
ly += line_h;
|
|
}
|
|
|
|
XSetForeground(display_, gc_, col(axis_color_));
|
|
XDrawRectangle(display_, window_, gc_, px, py, pw, ph);
|
|
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;
|
|
}
|
|
};
|