Files
lightgui/widgets/components/XMonitorGrid/XMonitorGrid.h
T
Muse 8c295dbbdf XFrame monitor widgets: XProcessList, XLoadBadge, XMonitorButton, XMonitorGrid, XSysBox
Live process table from /proc (sortable columns, in-cell CPU% bars,
colour-coded rows, refresh()), load-average badge (1/5/15 min,
threshold colours, uptime from /proc), monitor button bound to a
process (/proc scan), systemd unit (systemctl is-active) or TCP port
(connect to localhost) with poll()/update() and click-for-details,
grid panel of monitor buttons with one shared poll_all(), composite
resource box (title + big number + history + usage bar). Header-only
C++, demo mains (polling loop for the live three), examples, Xvfb
screenshots.
2026-12-05 16:49:29 +00:00

194 lines
5.5 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../XMonitorButton/XMonitorButton.h"
#include <algorithm>
#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_ = 2;
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 = 20;
const int gap = 6;
int cols = std::max(1, columns_);
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);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
std::string summary = std::to_string(up_count()) + "/" + std::to_string(monitors_.size()) + " up";
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, summary), y1_ + 15, summary);
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;
}
};