Files
lightgui/widgets/components/XVerticalBar/XVerticalBar.h
T
Muse 7da1a1ef93 Follow-up: tach title placement + vertical-bar peak shrink-to-fit
- XTachometer: the "RPM" title baseline now hangs below the top
  numeral's bottom edge instead of a fixed cy - r*0.55, so it
  cannot collide with the neighbouring numerals on small dials
  (found in the Python demo at r=73, where 3200/RPM/4800 jammed).
- XVerticalBar: the "peak N" label shrinks 14px -> 11px when the
  bar is too narrow for it to fit inside the frame.
2026-12-05 21:32:51 +00:00

257 lines
8.6 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include "../InstData.h"
#include <algorithm>
#include <cstdio>
#include <string>
// region XVerticalBar
//
// A vertical bar meter with a scale on the left and a peak-hold marker:
// the thin line (and peak value) stays at the highest recent value.
// Raw X11, same as the rest of the XFrame set.
//
class XVerticalBar
{
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;
double value_ = 0.0;
double max_ = 100.0;
double peak_ = 0.0;
bool peak_hold_ = true;
Srgb white_color_{255, 255, 255};
Srgb grid_color_{225, 225, 225};
Srgb axis_color_{150, 150, 150};
Srgb fill_color_{0, 170, 70};
Srgb peak_color_{210, 40, 30};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt0(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.0f", v);
return buf;
}
public:
XVerticalBar(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_ = "XVerticalBar";
text_ = "Load";
value_ = 72.0;
peak_ = 91.0;
}
~XVerticalBar() {}
// 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 Bar API
void set_value(double v)
{
value_ = std::max(0.0, std::min(max_, v));
if (peak_hold_) peak_ = std::max(peak_, value_);
}
double get_value() { return value_; }
void set_max(double m)
{
max_ = m > 0.0 ? m : 100.0;
value_ = std::min(value_, max_);
peak_ = std::min(peak_, max_);
}
double get_max() { return max_; }
double peak() { return peak_; }
void reset_peak() { peak_ = value_; }
void set_peak_hold(bool on) { peak_hold_ = on; }
// 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 16 px bold,
// Y labels 14-16 px, "peak" 14 px bold, value 16 px bold inside
// the bar (never on top of a tick label).
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
int value_px = xframe::clamp_font_px(0.033 * h, 13, 16);
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
if (!title_font) title_font = st.font_;
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
if (!body_font) body_font = st.font_;
XFontStruct* bold_font = xframe::scaled_font(display_, body_px, true);
if (!bold_font) bold_font = st.font_;
XFontStruct* value_font = xframe::scaled_font(display_, value_px, true);
if (!value_font) value_font = st.font_;
int title_asc = title_font ? title_font->ascent : 11;
int body_asc = body_font ? body_font->ascent : 11;
const int title_h = title_px + 8;
// Dynamic scale width: the widest Y label sets it.
int scale_w = 8;
for (int g = 0; g <= 4; ++g)
{
std::string lab = fmt0(max_ * (4 - g) / 4.0);
scale_w = std::max(scale_w, xframe::font_text_width(body_font, lab) + 12);
}
int bx = x1_ + scale_w + 8;
int by = y1_ + title_h + 8;
int bw = std::max(10, x2_ - 8 - bx);
int bh = std::max(10, y2_ - 8 - by);
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
// Scale ticks + labels (0 / 25 / 50 / 75 / 100 % of max).
for (int g = 0; g <= 4; ++g)
{
int gy = by + bh * g / 4;
XSetForeground(display_, gc_, col(grid_color_));
XDrawLine(display_, window_, gc_, bx, gy, bx + bw, gy);
XSetForeground(display_, gc_, col(axis_color_));
XDrawLine(display_, window_, gc_, bx - 4, gy, bx, gy);
std::string lab = fmt0(max_ * (4 - g) / 4.0);
int lw = xframe::font_text_width(body_font, lab);
xframe::draw_text_font(display_, window_, gc_, body_font, bx - 8 - lw,
gy + body_asc / 2, lab, col(Srgb{100, 100, 100}));
}
// Fill from the bottom.
int filled = (int)(bh * value_ / max_);
XSetForeground(display_, gc_, col(fill_color_));
XFillRectangle(display_, window_, gc_, bx, by + bh - filled, bw, filled);
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bh);
// Peak-hold marker: red line across the bar + value above it
// with clearance, inset from the frame edge.
if (peak_hold_)
{
int py = by + bh - (int)(bh * peak_ / max_);
XSetForeground(display_, gc_, col(peak_color_));
xf::set_line_width(display_, gc_, 2);
XDrawLine(display_, window_, gc_, bx - 4, py, bx + bw + 4, py);
xf::set_line_width(display_, gc_, 1);
std::string pk = "peak " + fmt0(peak_);
// Shrink to fit when the bar is narrow: the label must
// stay inside the frame (right-aligned at the bar's right
// edge minus 4px), never spill past the scale labels.
int pk_px = 14;
XFontStruct* pk_font = bold_font;
int avail_w = (bx + bw - 4) - (x1_ + 8);
for (int try_px = 14; try_px >= 11; --try_px)
{
XFontStruct* f = xframe::scaled_font(display_, try_px, true);
if (xframe::font_text_width(f, pk) <= avail_w)
{
pk_px = try_px;
pk_font = f;
break;
}
pk_px = try_px;
pk_font = f;
}
(void)pk_px;
xframe::draw_text_font(display_, window_, gc_, pk_font,
bx + bw - xframe::font_text_width(pk_font, pk) - 4,
py - 6, pk, col(peak_color_));
}
// Current value INSIDE the bar near the bottom, centred (white
// on the fill; dark when the fill is too short to cover it).
{
char cur[16];
std::snprintf(cur, sizeof(cur), "%.0f%%", value_);
int cw = xframe::font_text_width(value_font, cur);
Srgb vcolor = (filled >= value_px + 8) ? Srgb{255, 255, 255} : Srgb{20, 20, 20};
xframe::draw_text_font(display_, window_, gc_, value_font,
bx + (bw - cw) / 2, by + bh - 6, cur, col(vcolor));
}
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 (button == 1 && contains(x, y))
{
reset_peak();
return true;
}
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;
}
};