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

179 lines
6.3 KiB
C++

// InstData.h — shared data types and deterministic demo values for the
// XFrame Instrumentation widget section (tank level, switches, lamps, LEDs,
// gauges, panel furniture).
//
// No hardware I/O, no network: demos and tests run on deterministic values
// defined here, so results are reproducible.
// Attribution: XFrame.
#pragma once
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <string>
#include <vector>
#include <X11/Xlib.h>
namespace xframe {
// Tank / channel level, percent full, clamped to [0, 100].
inline double inst_clamp_level(double pct) {
if (pct < 0.0) return 0.0;
if (pct > 100.0) return 100.0;
return pct;
}
// Alarm bands for a level, in percent. hi_hi >= hi >= lo >= lo_lo.
struct InstAlarmBands {
double hi_hi = 95.0;
double hi = 80.0;
double lo = 20.0;
double lo_lo = 5.0;
};
// Switch positions. Centre-off rockers use Off as the middle position;
// breakers use Tripped for the fault position.
enum class InstSwitchState { Off = 0, On = 1, Tripped = 2 };
// One illuminator: on/off plus brightness 0-100 (100 = full).
struct InstLamp {
bool on = false;
int brightness = 100;
};
// One RGB status LED: three independent channels, each an independent
// illuminator. Any of the 8 colour combinations plus off.
struct InstRgb {
bool red = false;
bool green = false;
bool blue = false;
};
// Deterministic demo level for a widget, in [0, 100]. Same seed gives the
// same level on every run and every platform.
inline double inst_demo_level(unsigned seed = 42) {
std::uint64_t x = 0x9E3779B97F4A7C15ULL ^
(static_cast<std::uint64_t>(seed) * 0xBF58476D1CE4E5B9ULL);
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
const std::uint64_t v = x * 0x2545F4914F6CDD1DULL;
return static_cast<double>(v % 9001) / 100.0 + 5.0; // 5.00 .. 95.00
}
// Deterministic demo value for a gauge, in [min, max].
inline double inst_demo_value(double min_v, double max_v, unsigned seed = 42) {
if (max_v <= min_v) return min_v;
const double f = inst_demo_level(seed) / 100.0;
return min_v + f * (max_v - min_v);
}
// Bold instrumentation font: the 13 px medium core font grown by 33%
// (13 * 1.33 = 17.29) and made bold. The exact 17 px size does not exist
// in the server fixed-font set (probed Nov 26, 2026: bold sizes available
// are 13, 14, 15, 18), so 18 px is the target and the others are
// fallbacks in probe order. Lazy static, loaded once; never XFreeFont'd:
// a process-lifetime resource, the display closes before any destructor
// runs (same rationale as the SStyle_Manager comment in
// core/xframe_core.h). Returns nullptr if no size loads, in which case
// callers keep the style's own font.
inline XFontStruct* inst_bold_font(Display* display) { if (!display) return nullptr;
static XFontStruct* cached = nullptr;
static bool tried = false;
if (tried) return cached;
tried = true;
static const char* kNames[] = {
"-misc-fixed-bold-r-normal--18-*",
"-misc-fixed-bold-r-normal--17-*",
"-misc-fixed-bold-r-normal--16-*",
"-misc-fixed-bold-r-normal--20-*",
};
for (const char* name : kNames) {
cached = XLoadQueryFont(display, name);
if (cached) break;
}
return cached;
}
// Scaled monitor fonts (Dec 5, 2026, monitor proportions audit): the C
// monitor widgets derive every font size from the widget's current
// dimensions, so they need core fonts at arbitrary pixel sizes, bold and
// medium. Probed Dec 5, 2026 on the dallas Xvfb: bitmap bold exists only
// at 13/14/15/18 px and bitmap medium only at 13/18/20 px, but the
// misc-fixed family is scalable — an XLFD with an explicit pixel size and
// wildcards for the remaining fields loads at that size (20/24/28 px
// verified for bold; the same pattern works for medium). So try the
// scalable XLFD at the requested size first, then bitmap fallbacks.
// Loaded fonts are cached per (size, weight) in a process-lifetime table
// and never XFreeFont'd (same rationale as inst_bold_font above).
// Returns nullptr when nothing loads; callers then keep the style font.
inline XFontStruct* scaled_font(Display* display, int px, bool bold)
{
if (!display) return nullptr;
if (px < 8) px = 8;
if (px > 64) px = 64;
struct Entry
{
int px;
bool bold;
XFontStruct* font;
};
static std::vector<Entry> cache;
for (auto& e : cache)
if (e.px == px && e.bold == bold) return e.font;
XFontStruct* font = nullptr;
char name[96];
// Scalable XLFD: explicit pixel size, wildcards elsewhere.
std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*-75-75-c-*-iso8859-1",
bold ? "bold" : "medium", px);
font = XLoadQueryFont(display, name);
if (!font)
{
// Bitmap fallbacks, nearest size first.
static const int kBoldFallbacks[] = {18, 20, 15, 14, 13};
static const int kMediumFallbacks[] = {20, 18, 13};
const int* fallbacks = bold ? kBoldFallbacks : kMediumFallbacks;
int n = bold ? 5 : 3;
for (int i = 0; i < n && !font; ++i)
{
std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*",
bold ? "bold" : "medium", fallbacks[i]);
font = XLoadQueryFont(display, name);
}
}
if (!font) font = XLoadQueryFont(display, "fixed");
if ((int)cache.size() < 24) cache.push_back(Entry{px, bold, font});
return font;
}
// Width of `s` in `font` (XTextWidth), or 8 px/char when no font.
inline int font_text_width(XFontStruct* font, const std::string& s)
{
if (!font) return (int)s.size() * 8;
return XTextWidth(font, s.c_str(), (int)s.size());
}
// Draw `s` at baseline (x, baseline_y) in `font` and foreground `fg`.
// No-op when the font failed to load.
inline void draw_text_font(Display* display, Window window, GC gc, XFontStruct* font,
int x, int baseline_y, const std::string& s, unsigned long fg)
{
if (!font || s.empty()) return;
XSetFont(display, gc, font->fid);
XSetForeground(display, gc, fg);
XDrawString(display, window, gc, x, baseline_y, s.c_str(), (int)s.size());
}
// Clamp a derived font size: at least `lo` px, at most `hi` px.
inline int clamp_font_px(double px, int lo, int hi)
{
int v = (int)std::lround(px);
if (v < lo) v = lo;
if (v > hi) v = hi;
return v;
}
} // namespace xframe