Files
lightgui/widgets/components/XStackBar/XStackBar.h
Muse 4488d527a5 XFrame monitor widgets: XUsageBar, XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge
Gradient usage bar (green->yellow->red, absolute scale), stacked
multi-segment bar with legend, vertical bar meter with scale +
peak-hold marker (click resets peak), round tachometer (needle,
redline zone, digital readout, peak hold), semicircular arc gauge
with coloured zones + big %, circular progress ring with centred %.
Header-only C++, demo mains, examples, Xvfb screenshots.
2026-12-05 16:49:02 +00:00

210 lines
5.7 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
// region XStackBar
//
// A stacked multi-segment horizontal bar (used / cached / buffers / free)
// with a legend underneath: each segment's label, value and share.
// Raw X11, same as the rest of the XFrame set.
//
class XStackBar
{
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;
struct Segment
{
std::string name;
double value = 0.0;
Srgb color{0, 150, 60};
};
std::vector<Segment> segments_;
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt1(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.1f", v);
return buf;
}
public:
XStackBar(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_ = "XStackBar";
text_ = "Memory";
// Preload a demo: a 30 GiB box split used / cached / buffers / free.
add_segment("Used", 11.7, Srgb{0, 150, 60});
add_segment("Cached", 15.5, Srgb{220, 180, 0});
add_segment("Buffers", 1.2, Srgb{0, 120, 200});
add_segment("Free", 1.9, Srgb{200, 200, 200});
}
~XStackBar() {}
// 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 Segment API
void add_segment(const std::string& name, double value, Srgb color)
{
Segment s;
s.name = name;
s.value = std::max(0.0, value);
s.color = color;
segments_.push_back(s);
}
void clear_segments() { segments_.clear(); }
void set_segment(int index, double value)
{
if (index >= 0 && index < (int)segments_.size())
segments_[index].value = std::max(0.0, value);
}
int segment_count() { return (int)segments_.size(); }
double total()
{
double t = 0.0;
for (auto& s : segments_) t += s.value;
return t;
}
// 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);
const int title_h = 20;
const int bar_h = 26;
double tot = total();
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
if (tot > 0.0)
{
std::string tot_s = "total " + fmt1(tot) + " GiB";
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, tot_s), y1_ + 15, tot_s);
}
// Stacked bar.
int bx = x1_ + 8;
int by = y1_ + title_h + 4;
int bw = std::max(10, w - 16);
int x = bx;
for (auto& s : segments_)
{
int sw = tot > 0.0 ? (int)(bw * s.value / tot) : 0;
if (sw <= 0) continue;
XSetForeground(display_, gc_, col(s.color));
XFillRectangle(display_, window_, gc_, x, by, sw, bar_h);
x += sw;
}
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h);
// Legend: one row per segment — swatch, name, value, share.
int ly = by + bar_h + 18;
for (auto& s : segments_)
{
XSetForeground(display_, gc_, col(s.color));
XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 10, 12, 12);
std::string line = s.name + " " + fmt1(s.value) + " GiB";
if (tot > 0.0)
{
char share[16];
std::snprintf(share, sizeof(share), " %.0f%%", s.value / tot * 100.0);
line += share;
}
xf::draw_text_at(display_, window_, gc_, st, x1_ + 28, ly, line);
ly += 18;
}
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;
}
};