#pragma once #include "core/xframe_core.h" #include "../InstData.h" #include #include #include #include // 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 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); // Scaled fonts (Dec 5, 2026 proportions audit): title + total // 14-16 px (title bold), legend 14-16 px, bar height ~1.5x // legend text height. The bar+legend block scales with widget // height and is vertically centred (no 80% empty area). int title_px = xframe::clamp_font_px(0.033 * h, 11, 16); int legend_px = xframe::clamp_font_px(0.032 * h, 11, 16); XFontStruct* title_font = xframe::scaled_font(display_, title_px, true); if (!title_font) title_font = st.font_; XFontStruct* legend_font = xframe::scaled_font(display_, legend_px, false); if (!legend_font) legend_font = st.font_; int title_asc = title_font ? title_font->ascent : 11; int legend_asc = legend_font ? legend_font->ascent : 11; int legend_desc = legend_font ? legend_font->descent : 2; double tot = total(); const int title_h = title_px + 8; int row_h = (int)std::lround(1.6 * legend_px); int legend_total = row_h * (int)segments_.size(); int bar_min = (int)std::lround(1.5 * legend_px); int bar_max = (int)std::lround(0.30 * h); int bar_h = std::max(bar_min, std::min(bar_max, h - title_h - legend_total - 24)); bar_h = std::max(8, bar_h); int block_h = title_h + 6 + bar_h + 8 + legend_total; int start_y = y1_ + std::max(4, (h - block_h) / 2); xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8, start_y + 4 + title_asc, text_, col(Srgb{20, 20, 20})); if (tot > 0.0) { std::string tot_s = "total " + fmt1(tot) + " GiB"; xframe::draw_text_font(display_, window_, gc_, legend_font, x2_ - 8 - xframe::font_text_width(legend_font, tot_s), start_y + 4 + title_asc, tot_s, col(Srgb{20, 20, 20})); } // Stacked bar. int bx = x1_ + 8; int by = start_y + title_h + 6; 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 + 8; for (auto& s : segments_) { int base = ly + (row_h + legend_asc - legend_desc) / 2; XSetForeground(display_, gc_, col(s.color)); XFillRectangle(display_, window_, gc_, x1_ + 8, ly + (row_h - legend_px) / 2, legend_px, legend_px); 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; } xframe::draw_text_font(display_, window_, gc_, legend_font, x1_ + 8 + legend_px + 8, base, line, col(Srgb{20, 20, 20})); ly += row_h; } 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; } };