Author SHA1 Message Date
Muse 3baaa13f40 Merge fix/inst-proportions into main 2026-12-05 23:29:59 +00:00
Muse 7feeac848d Merge fix/monitor-widget-proportions: dynamic graph margins, scaled fonts, tach + audit proportions 2026-12-05 22:24:09 +00:00
Muse 7995eac6ac Monitor widgets: dynamic graph margins, scaled fonts, tach + audit proportions
- Graphs (History/Net/Temp): dynamic left margin from the widest
  Y label (5 labels, top hangs below / bottom sits above gridline),
  legend on top with wrap/split so totals never clip; Net Y labels
  humanised (KB/s, MB/s) and scaled from the series max so no series
  rides the top edge; Temp keeps the PY layout (now bold top-right,
  threshold label above the line, min/max/avg bottom line).
- XTachometer: numerals ~7.5% of dial diameter, bold, placed by
  per-label radial extent so wide labels never cross the dial rim;
  RPM bold centred in-dial below the top numerals; readout inside
  below the hub; redline numerals red; redline is a rim arc, not a
  filled sector (red-on-red made the redline numerals invisible).
- Audit proportions in all 14 widgets: scaled fonts from widget
  dimensions (titles 16 bold, body 14-16, hero 20-32 bold), CoreGrid
  waste-minimising layout (8 cores -> 4x2), UsageBar labels outside
  the bar with the value name (CPU) and absolute gradient stops,
  StackBar title+total header (absolute GiB, total 30.3) with a
  centred block, VerticalBar value inside the bar + peak label above
  the line, ArcGauge redesigned to arc-line no-needle (C/PY style),
  DonutGauge centre label = value name with 8-10% ring, ProcessList
  13px rows / 23px pitch with colour-coded CPU bars and a 68px PID
  column (7-digit live PIDs no longer bleed into the name), LoadBadge
  PY two-line layout with 1/5/15-min sub-labels + core count,
  MonitorButton/Grid name+status+detail cells with ceil(sqrt(n))
  grid geometry, SysBox hero top-left + max + bar % label.
- Scaled-font plumbing: DrawTarget::set_scaled_font (default no-op),
  draw_text_scaled / text_width_scaled / clamp_font_px in
  xframe-core; X11 binds scalable misc-fixed XLFDs (cached per
  px/bold) with bitmap fallbacks.
- Demo runner only sets text=kind when the widget did not seed its
  own text, so seeded value names (CPU, Disk) survive.
2026-12-05 22:23:27 +00:00
5 changed files with 740 additions and 241 deletions

No files matched your search

+13
View File
@@ -94,6 +94,19 @@ impl DrawTarget for CtxTarget<'_> {
fn set_font(&mut self, metrics: FontMetrics) {
self.font_metrics = Some(metrics);
// Rebind the regular font: a scaled draw may have left a
// different font in the GC.
self.ctx.bind_font();
}
fn set_scaled_font(&mut self, px: i32, bold: bool) -> bool {
match self.ctx.bind_scaled_font(px, bold) {
Some(metrics) => {
self.font_metrics = Some(metrics);
true
}
None => false,
}
}
fn set_font_px(&mut self, px: i32, proportional: bool) -> Option<FontMetrics> {
+79 -1
View File
@@ -8,6 +8,7 @@ use x11rb::connection::Connection;
use x11rb::protocol::xproto::*;
use x11rb::rust_connection::RustConnection;
use x11rb::COPY_FROM_PARENT;
use xframe_core::font::FontMetrics;
pub type Conn = RustConnection;
@@ -555,6 +556,13 @@ pub struct DrawCtx<'a> {
pub gc: Gcontext,
pub font: Font,
colors: RefCell<HashMap<(u8, u8, u8), u32>>,
/// Scaled fonts opened by `bind_scaled_font`, keyed by
/// (pixel size, bold). Process-lifetime, like the C++ cache.
scaled_fonts: RefCell<HashMap<(i32, bool), (Font, FontMetrics)>>,
/// The font currently bound into the GC (starts as `font`).
/// `text_width` measures with this font, so it stays correct
/// after a scaled draw.
current_font: RefCell<Font>,
}
impl<'a> DrawCtx<'a> {
@@ -565,6 +573,8 @@ impl<'a> DrawCtx<'a> {
gc,
font,
colors: RefCell::new(HashMap::new()),
scaled_fonts: RefCell::new(HashMap::new()),
current_font: RefCell::new(font),
}
}
@@ -731,7 +741,7 @@ impl<'a> DrawCtx<'a> {
}
pub fn text_width(&self, text: &str) -> i32 {
self.x.text_width(self.font, text)
self.x.text_width(*self.current_font.borrow(), text)
}
/// Draw text with the GC font; the GC font must be set by the caller
@@ -771,6 +781,74 @@ impl<'a> DrawCtx<'a> {
.x
.conn
.change_gc(self.gc, &ChangeGCAux::new().font(self.font));
*self.current_font.borrow_mut() = self.font;
}
/// Bind a scaled font (`px` pixels, bold when `bold`) into the GC
/// and return its metrics. The misc-fixed XLFD is scalable, so the
/// requested size loads exactly; bitmap fallbacks (the only sizes
/// the server has: bold 13/14/15/18/20, medium 13/18/20) and
/// `fixed` cover a server without the scalable face. Results are
/// cached per (px, bold). `None` when nothing loads, in which case
/// the previously bound font stays active.
pub fn bind_scaled_font(&self, px: i32, bold: bool) -> Option<FontMetrics> {
if let Some(&(font, metrics)) =
self.scaled_fonts.borrow().get(&(px, bold))
{
let _ = self
.x
.conn
.change_gc(self.gc, &ChangeGCAux::new().font(font));
*self.current_font.borrow_mut() = font;
return Some(metrics);
}
let weight = if bold { "bold" } else { "medium" };
let mut candidates: Vec<String> = vec![format!(
"-misc-fixed-{weight}-r-normal--{px}-*-75-75-c-*-iso8859-1"
)];
let bitmaps: &[i32] = if bold {
&[18, 20, 15, 14, 13]
} else {
&[20, 18, 13]
};
for size in bitmaps {
candidates.push(format!(
"-misc-fixed-{weight}-r-normal--{size}-120-100-100-c-60-iso8859-1"
));
}
candidates.push("fixed".to_string());
for name in &candidates {
let Ok(fid) = self.x.conn.generate_id() else {
continue;
};
let Ok(cookie) = self.x.conn.open_font(fid, name.as_bytes()) else {
continue;
};
if cookie.check().is_err() {
continue;
}
let Ok(reply) = self.x.conn.query_font(fid) else {
continue;
};
let Ok(reply) = reply.reply() else {
continue;
};
let metrics = FontMetrics {
ascent: reply.font_ascent as i32,
descent: reply.font_descent as i32,
char_width: reply.max_bounds.character_width as i32,
};
let _ = self
.x
.conn
.change_gc(self.gc, &ChangeGCAux::new().font(fid));
self.scaled_fonts
.borrow_mut()
.insert((px, bold), (fid, metrics));
*self.current_font.borrow_mut() = fid;
return Some(metrics);
}
None
}
pub fn put_rgba(&self, dst_x: i16, dst_y: i16, rgba: &[u8], w: u16, h: u16) {
+47
View File
@@ -21,6 +21,16 @@ pub trait DrawTarget {
fn set_line_width(&mut self, width: u32);
/// Install a font for subsequent `draw_string` calls.
fn set_font(&mut self, metrics: FontMetrics);
/// Install a scaled font (pixel size `px`, bold when `bold`) for
/// subsequent `draw_string` calls. Returns true when a font at
/// (approximately) that size was bound. Default: no-op returning
/// false — the caller then draws with the current font (the
/// Canvas / test targets have no X server to open fonts on).
/// Live targets (CtxTarget) bind a scalable misc-fixed XLFD,
/// falling back to the nearest bitmap size.
fn set_scaled_font(&mut self, _px: i32, _bold: bool) -> bool {
false
}
/// Fill a rectangle (top-left `x, y`, size `w` x `h`).
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32);
/// Draw a rectangle outline.
@@ -81,6 +91,43 @@ pub fn set_line_width(target: &mut (impl DrawTarget + ?Sized), line_width: i32)
target.set_line_width(line_width.max(1) as u32);
}
/// Clamp a font size derived from a widget dimension to `[lo, hi]`
/// (sane minimum 11 px so text never shrinks to unreadable).
/// Mirrors `xframe::clamp_font_px` in C++ and `_fpx` in Python.
pub fn clamp_font_px(value: i32, lo: i32, hi: i32) -> i32 {
value.max(lo).min(hi)
}
/// Draw `text` at baseline `(x, y)` in a scaled font (`px`, `bold`),
/// falling back to the current font on targets without scaled fonts.
/// The caller sets the foreground colour first.
pub fn draw_text_scaled(
target: &mut (impl DrawTarget + ?Sized),
px: i32,
bold: bool,
x: i32,
y: i32,
text: &str,
) {
if text.is_empty() {
return;
}
let _ = target.set_scaled_font(px, bold);
target.draw_string(x, y, text);
}
/// Width of `text` in a scaled font (`px`, `bold`), or in the current
/// font on targets without scaled fonts.
pub fn text_width_scaled(
target: &mut (impl DrawTarget + ?Sized),
px: i32,
bold: bool,
text: &str,
) -> i32 {
let _ = target.set_scaled_font(px, bold);
target.text_width(text)
}
/// C++ `xf::fill_rounded_rect`: the Part 4 rounded body — Rectangle A +
/// Rectangle B + 4 fill arcs. Degenerate radii fall back to a plain fill.
pub fn fill_rounded_rect(
+8 -3
View File
@@ -108,9 +108,14 @@ pub fn run_widget_with_csv(
};
widget.set_bounds(gx as i32, gy as i32, gw as i32, gh as i32);
// Label text: the registry `kind` approximates the C++ constructor's
// default text (see the module docs of the binary).
let kind = widget.kind().to_string();
widget.set_text(&kind);
// default text (see the module docs of the binary) -- but only when
// the widget did not seed its own text (e.g. the monitor demos seed
// a value name such as "CPU" / "Disk"; overwriting it with the kind
// would show the class name instead of the value name).
if widget.state().text.is_empty() {
let kind = widget.kind().to_string();
widget.set_text(&kind);
}
if let Some(text) = &csv {
if let Err(e) = widget.load_csv_data(text) {
eprintln!("xframe-demo: --csv: {}", e);
File diff suppressed because it is too large. Load diff