- 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.
206 lines
6.3 KiB
C++
206 lines
6.3 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
#include "../XMonitorButton/XMonitorButton.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// region XMonitorGrid
|
|
//
|
|
// A panel of XMonitorButtons in a labeled grid, sharing one poll timer:
|
|
// poll_all() checks every monitor at once. Each cell is a real
|
|
// XMonitorButton (process / systemd unit / TCP port). Raw X11, same as
|
|
// the rest of the XFrame set.
|
|
//
|
|
class XMonitorGrid
|
|
{
|
|
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;
|
|
|
|
std::vector<XMonitorButton> monitors_;
|
|
int columns_ = 0; // 0 = auto: cols = ceil(sqrt(n)) (2x2 for 4)
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
void layout()
|
|
{
|
|
if (monitors_.empty()) return;
|
|
const int title_h = xframe::clamp_font_px(0.033 * height(), 11, 16) + 8;
|
|
const int gap = 6;
|
|
int cols = columns_ > 0 ? columns_
|
|
: (int)std::ceil(std::sqrt((double)monitors_.size()));
|
|
cols = std::max(1, cols);
|
|
int rows = ((int)monitors_.size() + cols - 1) / cols;
|
|
int cw = (width() - 8 - gap * (cols - 1)) / cols;
|
|
int ch = (height() - title_h - 8 - gap * (rows - 1)) / rows;
|
|
for (size_t i = 0; i < monitors_.size(); ++i)
|
|
{
|
|
int c = (int)i % cols;
|
|
int r = (int)i / cols;
|
|
monitors_[i].set_bounds(x1_ + 4 + c * (cw + gap),
|
|
y1_ + title_h + 4 + r * (ch + gap), cw, ch);
|
|
}
|
|
}
|
|
|
|
public:
|
|
XMonitorGrid(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_ = "XMonitorGrid";
|
|
text_ = "Services";
|
|
|
|
// Preload a demo: sshd port, the X server process, ssh unit.
|
|
add_monitor("sshd :22", XMonitorButton::Port, "22");
|
|
add_monitor("Xvfb", XMonitorButton::Process, "Xvfb");
|
|
add_monitor("ssh service", XMonitorButton::Unit, "ssh.service");
|
|
add_monitor("docker :2375", XMonitorButton::Port, "2375");
|
|
poll_all();
|
|
}
|
|
|
|
~XMonitorGrid() {}
|
|
|
|
// region Standard plumbing
|
|
void set_bounds(int x, int y, int width, int height)
|
|
{
|
|
x1_ = x;
|
|
y1_ = y;
|
|
x2_ = x + width;
|
|
y2_ = y + height;
|
|
layout();
|
|
}
|
|
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 Grid API
|
|
void add_monitor(const std::string& label, XMonitorButton::Kind kind, const std::string& target)
|
|
{
|
|
monitors_.emplace_back(display_, screen_, window_, gc_, styles_, 0, 0, 10, 10);
|
|
XMonitorButton& m = monitors_.back();
|
|
m.set_text(label);
|
|
m.set_kind(kind);
|
|
m.set_target(target);
|
|
m.poll();
|
|
layout();
|
|
}
|
|
void clear_monitors() { monitors_.clear(); }
|
|
int monitor_count() { return (int)monitors_.size(); }
|
|
void set_columns(int c)
|
|
{
|
|
columns_ = c > 0 ? c : 1;
|
|
layout();
|
|
}
|
|
// One shared poll: check every monitor (call on a timer).
|
|
void poll_all()
|
|
{
|
|
for (auto& m : monitors_) m.poll();
|
|
}
|
|
void update() { poll_all(); }
|
|
int up_count()
|
|
{
|
|
int n = 0;
|
|
for (auto& m : monitors_)
|
|
if (m.status() == XMonitorButton::MonUp) ++n;
|
|
return n;
|
|
}
|
|
// 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);
|
|
|
|
// Title + summary 14-16 px bold (Dec 5, 2026 proportions audit).
|
|
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;
|
|
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
|
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
|
std::string summary = std::to_string(up_count()) + "/" + std::to_string(monitors_.size()) + " up";
|
|
xframe::draw_text_font(display_, window_, gc_, title_font,
|
|
x2_ - 8 - xframe::font_text_width(title_font, summary),
|
|
y1_ + 4 + title_asc, summary, col(Srgb{20, 20, 20}));
|
|
|
|
layout();
|
|
for (auto& m : monitors_) m.draw();
|
|
|
|
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)
|
|
{
|
|
if (!contains(x, y)) return false;
|
|
for (auto& m : monitors_)
|
|
{
|
|
if (m.contains(x, y)) return m.on_button_press(x, y, button);
|
|
}
|
|
return false;
|
|
}
|
|
bool on_button_release(int x, int y, unsigned int button)
|
|
{
|
|
for (auto& m : monitors_)
|
|
{
|
|
if (m.contains(x, y)) return m.on_button_release(x, y, button);
|
|
}
|
|
return false;
|
|
}
|
|
bool on_motion(int x, int y)
|
|
{
|
|
for (auto& m : monitors_)
|
|
{
|
|
if (m.contains(x, y)) return m.on_motion(x, y);
|
|
}
|
|
return false;
|
|
}
|
|
bool on_key(KeySym key, const char* str)
|
|
{
|
|
(void)key;
|
|
(void)str;
|
|
return false;
|
|
}
|
|
};
|