Files
Muse e8a40804ab Fix monitor-widget proportions: dynamic graph margins, scaled fonts, tach numerals
- 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.
2026-12-05 21:09:37 +00:00

274 lines
8.7 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
// region XCoreGrid
//
// A grid of mini history graphs, one per CPU core (Task Manager per-core
// view): each cell shows its core label, current % and a scrolling line
// of recent samples. Raw X11, same as the rest of the XFrame set.
//
class XCoreGrid
{
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 cores_ = 8;
int capacity_ = 60;
std::vector<std::vector<double>> history_;
std::vector<double> current_;
Srgb white_color_{255, 255, 255};
Srgb grid_color_{225, 225, 225};
Srgb axis_color_{150, 150, 150};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static Srgb color_for(double pct)
{
if (pct < 50.0) return Srgb{0, 170, 70};
if (pct < 80.0) return Srgb{200, 170, 0};
return Srgb{210, 40, 30};
}
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 ensure_cores()
{
if ((int)history_.size() != cores_)
{
history_.assign(cores_, std::vector<double>());
current_.assign(cores_, 0.0);
}
}
public:
XCoreGrid(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_ = "XCoreGrid";
text_ = "CPU cores";
ensure_cores();
// Preload a demo: 8 cores, seeded sine + noise at different phases.
unsigned int seed = 424242;
auto rnd = [&]() {
seed = seed * 1103515245 + 12345;
return ((seed >> 16) & 0x7fff) / 32767.0;
};
for (int i = 0; i < capacity_; ++i)
{
double t = (double)i / capacity_;
for (int c = 0; c < cores_; ++c)
{
double v = 45.0 + 38.0 * std::sin(t * 7.0 + c * 1.3) + (rnd() - 0.5) * 22.0;
push(c, std::max(0.0, std::min(100.0, v)));
}
}
}
~XCoreGrid() {}
// 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 Core API
void set_core_count(int n)
{
cores_ = n > 0 ? n : 1;
ensure_cores();
}
int core_count() { return cores_; }
void push(int core, double pct)
{
if (core < 0 || core >= cores_) return;
ensure_cores();
pct = std::max(0.0, std::min(100.0, pct));
history_[core].push_back(pct);
while ((int)history_[core].size() > capacity_) history_[core].erase(history_[core].begin());
current_[core] = pct;
}
void push_all(const std::vector<double>& pcts)
{
for (size_t i = 0; i < pcts.size() && (int)i < cores_; ++i) push((int)i, pcts[i]);
}
double core_value(int core)
{
if (core < 0 || core >= cores_) return 0.0;
return current_[core];
}
// 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);
ensure_cores();
// Scaled fonts (Dec 5, 2026 proportions audit): title 16 px bold,
// cell labels 14-16 px bold (9-11% of cell height).
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
if (!title_font) title_font = st.font_;
int title_asc = title_font ? title_font->ascent : 11;
const int title_h = title_px + 8;
// Grid geometry: pick cols/rows with no empty cell when possible
// (8 cores -> 4x2, not 3x3 with a hole). Minimise waste among
// near-square layouts, keeping rows >= 2 and cols <= 2*rows.
int cols = (int)std::ceil(std::sqrt((double)cores_));
int rows = (cores_ + cols - 1) / cols;
int best_waste = cols * rows - cores_;
for (int cc = cols + 1; cc <= cores_; ++cc)
{
int rr = (cores_ + cc - 1) / cc;
if (rr < 2 || cc > 2 * rr) continue;
int waste = cc * rr - cores_;
if (waste < best_waste)
{
cols = cc;
rows = rr;
best_waste = waste;
if (waste == 0) break;
}
}
int cell_w = (w - 8) / cols;
int cell_h = (h - title_h - 8) / rows;
int label_px = xframe::clamp_font_px(0.10 * cell_h, 11, 16);
XFontStruct* label_font = xframe::scaled_font(display_, label_px, true);
if (!label_font) label_font = st.font_;
int label_asc = label_font ? label_font->ascent : 11;
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
for (int c = 0; c < cores_; ++c)
{
int cx = x1_ + 4 + (c % cols) * cell_w;
int cy = y1_ + title_h + 4 + (c / cols) * cell_h;
int cw = cell_w - 4;
int ch = cell_h - 4;
// Cell frame.
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, cx, cy, cw, ch);
// Label + current %.
char label[32];
std::snprintf(label, sizeof(label), "C%d %.0f%%", c, current_[c]);
xframe::draw_text_font(display_, window_, gc_, label_font, cx + 5,
cy + 4 + label_asc, label, col(color_for(current_[c])));
// Mini history graph.
int gx = cx + 4;
int gy = cy + label_px + 10;
int gw = std::max(10, cw - 8);
int gh = std::max(6, ch - label_px - 14);
XSetForeground(display_, gc_, col(grid_color_));
XDrawLine(display_, window_, gc_, gx, gy + gh / 2, gx + gw, gy + gh / 2);
auto& hist = history_[c];
if (hist.size() >= 2)
{
std::vector<XPoint> pts;
int n = (int)hist.size();
for (int i = 0; i < n; ++i)
{
int px = gx + (int)((double)i / (capacity_ - 1) * gw);
int py = gy + gh - (int)(hist[i] / 100.0 * gh);
pts.push_back(XPoint{(short)px, (short)py});
}
XSetForeground(display_, gc_, col(color_for(current_[c])));
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_, 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;
}
};