Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
460d44c1da | ||
|
|
c121816bb9 | ||
|
|
3baaa13f40 | ||
|
|
7feeac848d | ||
|
|
7995eac6ac | ||
|
|
3483ef6282 | ||
|
|
4db2e1ebaf | ||
|
|
696579530b |
No files matched your search
Generated
-1
@@ -900,7 +900,6 @@ dependencies = [
|
|||||||
name = "xframe-demo"
|
name = "xframe-demo"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
|
||||||
"x11rb",
|
"x11rb",
|
||||||
"xframe-common",
|
"xframe-common",
|
||||||
"xframe-core",
|
"xframe-core",
|
||||||
|
|||||||
@@ -15,7 +15,9 @@
|
|||||||
//! adapter unpacks the pixel and lets `DrawCtx` allocate it (cached
|
//! adapter unpacks the pixel and lets `DrawCtx` allocate it (cached
|
||||||
//! per triple), so both sides agree on the colour.
|
//! per triple), so both sides agree on the colour.
|
||||||
|
|
||||||
use x11rb::protocol::xproto::{ChangeGCAux, ConnectionExt};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use x11rb::protocol::xproto::{ChangeGCAux, ConnectionExt, Font};
|
||||||
use xframe_core::draw::DrawTarget;
|
use xframe_core::draw::DrawTarget;
|
||||||
use xframe_core::font::{FontMetrics, FontSource};
|
use xframe_core::font::{FontMetrics, FontSource};
|
||||||
|
|
||||||
@@ -27,6 +29,13 @@ pub struct CtxTarget<'a> {
|
|||||||
fg: u32,
|
fg: u32,
|
||||||
line_width: u32,
|
line_width: u32,
|
||||||
font_metrics: Option<FontMetrics>,
|
font_metrics: Option<FontMetrics>,
|
||||||
|
/// Sized bold fonts opened by `set_font_px`, keyed by
|
||||||
|
/// (pixel size, proportional).
|
||||||
|
sized_fonts: HashMap<(i32, bool), (Font, FontMetrics)>,
|
||||||
|
/// Font currently bound into the GC by `set_font_px`, if any.
|
||||||
|
sized_current: Option<Font>,
|
||||||
|
/// Base font metrics (the demo's 13 px font), resolved lazily.
|
||||||
|
base_metrics: Option<FontMetrics>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> CtxTarget<'a> {
|
impl<'a> CtxTarget<'a> {
|
||||||
@@ -36,6 +45,9 @@ impl<'a> CtxTarget<'a> {
|
|||||||
fg: 0,
|
fg: 0,
|
||||||
line_width: 1,
|
line_width: 1,
|
||||||
font_metrics: None,
|
font_metrics: None,
|
||||||
|
sized_fonts: HashMap::new(),
|
||||||
|
sized_current: None,
|
||||||
|
base_metrics: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,6 +59,24 @@ impl<'a> CtxTarget<'a> {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// XLFD names for a sized bold font. Scalable core fonts load at
|
||||||
|
/// arbitrary pixel sizes when the point-size/resolution fields are
|
||||||
|
/// 0 (probed Dec 2026: `-misc-fixed-bold-r-normal--26-0-75-75-c-0-...`
|
||||||
|
/// renders a clean 26 px bold mono face; the old `--24-*` pattern
|
||||||
|
/// fails).
|
||||||
|
fn sized_font_names(px: i32, proportional: bool) -> Vec<String> {
|
||||||
|
if proportional {
|
||||||
|
vec![
|
||||||
|
format!("-adobe-helvetica-bold-r-normal--{px}-0-75-75-p-0-iso8859-1"),
|
||||||
|
format!("-urw-nimbus sans l-bold-r-normal--{px}-0-0-0-p-0-iso8859-1"),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
vec![format!(
|
||||||
|
"-misc-fixed-bold-r-normal--{px}-0-75-75-c-0-iso8859-1"
|
||||||
|
)]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The `DrawCtx` underneath, for the binary's own (non-widget) drawing.
|
/// The `DrawCtx` underneath, for the binary's own (non-widget) drawing.
|
||||||
pub fn ctx(&self) -> &'a DrawCtx<'a> {
|
pub fn ctx(&self) -> &'a DrawCtx<'a> {
|
||||||
self.ctx
|
self.ctx
|
||||||
@@ -64,6 +94,93 @@ impl DrawTarget for CtxTarget<'_> {
|
|||||||
|
|
||||||
fn set_font(&mut self, metrics: FontMetrics) {
|
fn set_font(&mut self, metrics: FontMetrics) {
|
||||||
self.font_metrics = Some(metrics);
|
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> {
|
||||||
|
let px = px.max(4);
|
||||||
|
if let Some((fid, m)) = self.sized_fonts.get(&(px, proportional)) {
|
||||||
|
let fid = *fid;
|
||||||
|
let m = *m;
|
||||||
|
let _ = self
|
||||||
|
.ctx
|
||||||
|
.x
|
||||||
|
.conn
|
||||||
|
.change_gc(self.ctx.gc, &ChangeGCAux::new().font(fid));
|
||||||
|
self.font_metrics = Some(m);
|
||||||
|
self.sized_current = Some(fid);
|
||||||
|
return Some(m);
|
||||||
|
}
|
||||||
|
for name in Self::sized_font_names(px, proportional) {
|
||||||
|
let Ok(fid) = self.ctx.x.open_font(&[name.as_str()]) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Ok(cookie) = self.ctx.x.conn.query_font(fid) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let Ok(reply) = cookie.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
|
||||||
|
.ctx
|
||||||
|
.x
|
||||||
|
.conn
|
||||||
|
.change_gc(self.ctx.gc, &ChangeGCAux::new().font(fid));
|
||||||
|
self.sized_fonts.insert((px, proportional), (fid, metrics));
|
||||||
|
self.font_metrics = Some(metrics);
|
||||||
|
self.sized_current = Some(fid);
|
||||||
|
return Some(metrics);
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn restore_font(&mut self) {
|
||||||
|
if self.sized_current.take().is_none() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let _ = self.ctx.x.conn.change_gc(
|
||||||
|
self.ctx.gc,
|
||||||
|
&ChangeGCAux::new().font(self.ctx.font),
|
||||||
|
);
|
||||||
|
if self.base_metrics.is_none() {
|
||||||
|
self.base_metrics = self
|
||||||
|
.ctx
|
||||||
|
.x
|
||||||
|
.conn
|
||||||
|
.query_font(self.ctx.font)
|
||||||
|
.ok()
|
||||||
|
.and_then(|c| c.reply().ok())
|
||||||
|
.map(|r| FontMetrics {
|
||||||
|
ascent: r.font_ascent as i32,
|
||||||
|
descent: r.font_descent as i32,
|
||||||
|
char_width: r.max_bounds.character_width as i32,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
self.font_metrics = self.base_metrics;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn text_width_current(&self, text: &str) -> i32 {
|
||||||
|
match self.sized_current {
|
||||||
|
Some(fid) => self.ctx.x.text_width(fid, text),
|
||||||
|
None => self.ctx.text_width(text),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32) {
|
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32) {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use x11rb::connection::Connection;
|
|||||||
use x11rb::protocol::xproto::*;
|
use x11rb::protocol::xproto::*;
|
||||||
use x11rb::rust_connection::RustConnection;
|
use x11rb::rust_connection::RustConnection;
|
||||||
use x11rb::COPY_FROM_PARENT;
|
use x11rb::COPY_FROM_PARENT;
|
||||||
|
use xframe_core::font::FontMetrics;
|
||||||
|
|
||||||
pub type Conn = RustConnection;
|
pub type Conn = RustConnection;
|
||||||
|
|
||||||
@@ -555,6 +556,13 @@ pub struct DrawCtx<'a> {
|
|||||||
pub gc: Gcontext,
|
pub gc: Gcontext,
|
||||||
pub font: Font,
|
pub font: Font,
|
||||||
colors: RefCell<HashMap<(u8, u8, u8), u32>>,
|
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> {
|
impl<'a> DrawCtx<'a> {
|
||||||
@@ -565,6 +573,8 @@ impl<'a> DrawCtx<'a> {
|
|||||||
gc,
|
gc,
|
||||||
font,
|
font,
|
||||||
colors: RefCell::new(HashMap::new()),
|
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 {
|
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
|
/// Draw text with the GC font; the GC font must be set by the caller
|
||||||
@@ -771,6 +781,74 @@ impl<'a> DrawCtx<'a> {
|
|||||||
.x
|
.x
|
||||||
.conn
|
.conn
|
||||||
.change_gc(self.gc, &ChangeGCAux::new().font(self.font));
|
.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) {
|
pub fn put_rgba(&self, dst_x: i16, dst_y: i16, rgba: &[u8], w: u16, h: u16) {
|
||||||
|
|||||||
@@ -21,6 +21,16 @@ pub trait DrawTarget {
|
|||||||
fn set_line_width(&mut self, width: u32);
|
fn set_line_width(&mut self, width: u32);
|
||||||
/// Install a font for subsequent `draw_string` calls.
|
/// Install a font for subsequent `draw_string` calls.
|
||||||
fn set_font(&mut self, metrics: FontMetrics);
|
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`).
|
/// Fill a rectangle (top-left `x, y`, size `w` x `h`).
|
||||||
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32);
|
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32);
|
||||||
/// Draw a rectangle outline.
|
/// Draw a rectangle outline.
|
||||||
@@ -40,6 +50,24 @@ pub trait DrawTarget {
|
|||||||
fn text_width(&self, text: &str) -> i32;
|
fn text_width(&self, text: &str) -> i32;
|
||||||
/// Metrics of the currently installed font, if any.
|
/// Metrics of the currently installed font, if any.
|
||||||
fn font_metrics(&self) -> Option<FontMetrics>;
|
fn font_metrics(&self) -> Option<FontMetrics>;
|
||||||
|
/// Install a bold font at `px` pixels (`proportional` selects a
|
||||||
|
/// proportional face, else monospace) for subsequent `draw_string`
|
||||||
|
/// calls, returning its metrics. Default: unsupported (returns
|
||||||
|
/// `None`); live X targets override it. This is the sized-font
|
||||||
|
/// plumbing for the Inst* proportions fix (audit Dec 2026): the
|
||||||
|
/// single 13 px demo font cannot render 16-32 px labels.
|
||||||
|
fn set_font_px(&mut self, px: i32, proportional: bool) -> Option<FontMetrics> {
|
||||||
|
let _ = (px, proportional);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
/// Restore the font installed before the last `set_font_px` call
|
||||||
|
/// (the demo's base font). Default: no-op.
|
||||||
|
fn restore_font(&mut self) {}
|
||||||
|
/// Width of `text` in the font currently installed by `set_font_px`
|
||||||
|
/// (or the base font when none). Default: `text_width`.
|
||||||
|
fn text_width_current(&self, text: &str) -> i32 {
|
||||||
|
self.text_width(text)
|
||||||
|
}
|
||||||
/// Blit `w` x `h` `0xRRGGBB` pixels at `(x, y)` (row-major).
|
/// Blit `w` x `h` `0xRRGGBB` pixels at `(x, y)` (row-major).
|
||||||
/// Default: one 1x1 fill per pixel; live targets override it.
|
/// Default: one 1x1 fill per pixel; live targets override it.
|
||||||
/// (C++ `ico.h` `rgba_to_ximage` + `XPutImage`.)
|
/// (C++ `ico.h` `rgba_to_ximage` + `XPutImage`.)
|
||||||
@@ -63,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);
|
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 +
|
/// 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.
|
/// Rectangle B + 4 fill arcs. Degenerate radii fall back to a plain fill.
|
||||||
pub fn fill_rounded_rect(
|
pub fn fill_rounded_rect(
|
||||||
@@ -206,6 +271,111 @@ pub fn draw_text_at(
|
|||||||
target.draw_string(x, baseline_y, text);
|
target.draw_string(x, baseline_y, text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Cap-height convention (audit Dec 2026): audit px numbers are cap
|
||||||
|
/// heights; a core font's pixel size is cap / 0.72 (measured: an 18 px
|
||||||
|
/// font renders ~13 px cap-height digits).
|
||||||
|
pub fn cap_px(cap: i32) -> i32 {
|
||||||
|
(cap * 100 + 71) / 72
|
||||||
|
}
|
||||||
|
|
||||||
|
/// C++ `xf::draw_text_at` with a sized bold font: `cap` is the target
|
||||||
|
/// cap height in px, `proportional` selects the proportional face.
|
||||||
|
/// Falls back to the base-font `draw_text_at` when the target has no
|
||||||
|
/// sized-font support. Restores the base font afterwards, so a
|
||||||
|
/// following base-font draw still renders in the base font.
|
||||||
|
pub fn draw_text_at_cap(
|
||||||
|
target: &mut (impl DrawTarget + ?Sized),
|
||||||
|
style: &Style,
|
||||||
|
x: i32,
|
||||||
|
baseline_y: i32,
|
||||||
|
text: &str,
|
||||||
|
cap: i32,
|
||||||
|
proportional: bool,
|
||||||
|
) {
|
||||||
|
if text.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if target.set_font_px(cap_px(cap), proportional).is_some() {
|
||||||
|
target.set_foreground(style.text_color);
|
||||||
|
target.draw_string(x, baseline_y, text);
|
||||||
|
target.restore_font();
|
||||||
|
} else {
|
||||||
|
draw_text_at(target, style, x, baseline_y, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// `draw_text_centered` with a sized bold font. Width is measured in
|
||||||
|
/// the sized font (exact extents), not the style's char-width estimate.
|
||||||
|
pub fn draw_text_centered_cap(
|
||||||
|
target: &mut (impl DrawTarget + ?Sized),
|
||||||
|
style: &Style,
|
||||||
|
x1: i32,
|
||||||
|
y1: i32,
|
||||||
|
x2: i32,
|
||||||
|
y2: i32,
|
||||||
|
text: &str,
|
||||||
|
cap: i32,
|
||||||
|
proportional: bool,
|
||||||
|
) {
|
||||||
|
if text.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Some(m) = target.set_font_px(cap_px(cap), proportional) {
|
||||||
|
let tw = target.text_width_current(text);
|
||||||
|
let text_x = x1 + ((x2 - x1) - tw) / 2;
|
||||||
|
let text_y = y1 + ((y2 - y1) + m.ascent - m.descent) / 2;
|
||||||
|
target.set_foreground(style.text_color);
|
||||||
|
target.draw_string(text_x, text_y, text);
|
||||||
|
target.restore_font();
|
||||||
|
} else {
|
||||||
|
draw_text_centered(target, style, x1, y1, x2, y2, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Right-aligned sized text: `right_x` is the right edge, `baseline_y`
|
||||||
|
/// the baseline.
|
||||||
|
pub fn draw_text_right_cap(
|
||||||
|
target: &mut (impl DrawTarget + ?Sized),
|
||||||
|
style: &Style,
|
||||||
|
right_x: i32,
|
||||||
|
baseline_y: i32,
|
||||||
|
text: &str,
|
||||||
|
cap: i32,
|
||||||
|
proportional: bool,
|
||||||
|
) {
|
||||||
|
if text.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if target.set_font_px(cap_px(cap), proportional).is_some() {
|
||||||
|
let tw = target.text_width_current(text);
|
||||||
|
target.set_foreground(style.text_color);
|
||||||
|
target.draw_string(right_x - tw, baseline_y, text);
|
||||||
|
target.restore_font();
|
||||||
|
} else {
|
||||||
|
let tw = text_width(style, text);
|
||||||
|
draw_text_at(target, style, right_x - tw, baseline_y, text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Width of `text` in a sized bold font (exact extents when the target
|
||||||
|
/// supports sized fonts).
|
||||||
|
pub fn text_width_cap(
|
||||||
|
target: &mut (impl DrawTarget + ?Sized),
|
||||||
|
text: &str,
|
||||||
|
cap: i32,
|
||||||
|
proportional: bool,
|
||||||
|
) -> i32 {
|
||||||
|
if target.set_font_px(cap_px(cap), proportional).is_some() {
|
||||||
|
let w = target.text_width_current(text);
|
||||||
|
target.restore_font();
|
||||||
|
w
|
||||||
|
} else if proportional {
|
||||||
|
text.chars().count() as i32 * cap_px(cap) / 2
|
||||||
|
} else {
|
||||||
|
text.chars().count() as i32 * cap_px(cap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// C++ `xf::draw_checkmark`: two lines forming a tick, line width 2,
|
/// C++ `xf::draw_checkmark`: two lines forming a tick, line width 2,
|
||||||
/// restored to 1 afterwards.
|
/// restored to 1 afterwards.
|
||||||
pub fn draw_checkmark(
|
pub fn draw_checkmark(
|
||||||
|
|||||||
@@ -14,4 +14,3 @@ xframe-common = { path = "../xframe-common" }
|
|||||||
xframe-core = { path = "../xframe-core" }
|
xframe-core = { path = "../xframe-core" }
|
||||||
xframe-widgets = { path = "../xframe-widgets" }
|
xframe-widgets = { path = "../xframe-widgets" }
|
||||||
x11rb = "0.13"
|
x11rb = "0.13"
|
||||||
libc = "0.2"
|
|
||||||
@@ -1,204 +0,0 @@
|
|||||||
//! Process Manager — monitor (Rust / XFrame).
|
|
||||||
//!
|
|
||||||
//! Run it from the workspace root:
|
|
||||||
//! cargo run -p xframe-demo --example monitor_process_manager
|
|
||||||
//! It opens a 1024x768 window ("Process Manager - monitor") with a live
|
|
||||||
//! `XProcessList` (/proc, sortable columns, click a row to select it),
|
|
||||||
//! a `GtkEntry` filter (name or pid substring), and `GtkButton`
|
|
||||||
//! Terminate / Kill / Refresh. Terminate sends SIGTERM and Kill sends
|
|
||||||
//! SIGKILL to the SELECTED row's pid only (pid <= 1 and our own pid are
|
|
||||||
//! refused). Press Esc or close the window to quit.
|
|
||||||
//!
|
|
||||||
//! The widgets are the real `xframe-widgets` implementations, created
|
|
||||||
//! directly (`MonitorWidget::new`, `ButtonWidget::new`,
|
|
||||||
//! `EntryWidget::new`) — the same code path the `xframe-demo` binary
|
|
||||||
//! uses for single-widget examples, with a multi-widget event loop
|
|
||||||
//! modelled on `xframe_demo::run_widget`.
|
|
||||||
|
|
||||||
use std::process::ExitCode;
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use x11rb::connection::Connection;
|
|
||||||
use x11rb::protocol::xproto::*;
|
|
||||||
use x11rb::protocol::Event;
|
|
||||||
use xframe_common::style::FONT_DEFAULT;
|
|
||||||
use xframe_common::target::CtxTarget;
|
|
||||||
use xframe_common::x11::{DrawCtx, X11};
|
|
||||||
use xframe_core::StyleManager;
|
|
||||||
use xframe_widgets::{ButtonWidget, EntryWidget, Key, MonitorWidget, Widget};
|
|
||||||
|
|
||||||
const WIN_W: u16 = 1024;
|
|
||||||
const WIN_H: u16 = 768;
|
|
||||||
const KEY_ESCAPE: u32 = 0xff1b;
|
|
||||||
|
|
||||||
/// Send `sig` to `pid`; false (and no signal) for pid <= 1 or our own pid.
|
|
||||||
fn signal_pid(pid: u32, sig: libc::c_int) -> bool {
|
|
||||||
if pid <= 1 || pid == std::process::id() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
unsafe { libc::kill(pid as libc::pid_t, sig) == 0 }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn redraw(
|
|
||||||
list: &MonitorWidget,
|
|
||||||
entry: &EntryWidget,
|
|
||||||
buttons: &[&ButtonWidget; 3],
|
|
||||||
ctx: &DrawCtx,
|
|
||||||
target: &mut CtxTarget,
|
|
||||||
styles: &mut StyleManager,
|
|
||||||
) {
|
|
||||||
ctx.clear(255, 255, 255, WIN_W, WIN_H);
|
|
||||||
list.draw(target, styles);
|
|
||||||
entry.draw(target, styles);
|
|
||||||
for b in buttons {
|
|
||||||
b.draw(target, styles);
|
|
||||||
}
|
|
||||||
ctx.flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
|
||||||
let x = X11::connect()?;
|
|
||||||
let font = x.open_font(&[FONT_DEFAULT, "fixed"])?;
|
|
||||||
let title = "Process Manager - monitor";
|
|
||||||
let event_mask = EventMask::EXPOSURE
|
|
||||||
| EventMask::KEY_PRESS
|
|
||||||
| EventMask::STRUCTURE_NOTIFY
|
|
||||||
| EventMask::BUTTON_PRESS
|
|
||||||
| EventMask::BUTTON_RELEASE
|
|
||||||
| EventMask::POINTER_MOTION;
|
|
||||||
let (win, gc) = x.create_window(100, 100, WIN_W, WIN_H, title, event_mask)?;
|
|
||||||
|
|
||||||
let mut styles = StyleManager::new();
|
|
||||||
xframe_demo::register_styles(&mut styles);
|
|
||||||
|
|
||||||
let mut list = MonitorWidget::new("Processes", "ProcessList", 16, 58, 992, 694);
|
|
||||||
list.refresh_processes();
|
|
||||||
let mut entry = EntryWidget::new("Filter", "Entry", 16, 16, 300, 30);
|
|
||||||
entry.set_text("");
|
|
||||||
let mut btn_term = ButtonWidget::new("Terminate", "Button", 336, 16, 120, 30);
|
|
||||||
btn_term.set_text("Terminate");
|
|
||||||
let mut btn_kill = ButtonWidget::new("Kill", "Button", 466, 16, 120, 30);
|
|
||||||
btn_kill.set_text("Kill");
|
|
||||||
let mut btn_refresh = ButtonWidget::new("Refresh", "Button", 596, 16, 120, 30);
|
|
||||||
btn_refresh.set_text("Refresh");
|
|
||||||
|
|
||||||
let ctx = DrawCtx::new(&x, win, gc, font);
|
|
||||||
ctx.bind_font();
|
|
||||||
let mut target = CtxTarget::new(&ctx);
|
|
||||||
if !styles.resolve_font("default", &mut target) && !styles.resolve_font("dark", &mut target) {
|
|
||||||
eprintln!("monitor_process_manager: warning: no font resolved, labels will be blank");
|
|
||||||
}
|
|
||||||
|
|
||||||
redraw(&list, &entry, &[&btn_term, &btn_kill, &btn_refresh], &ctx, &mut target, &mut styles);
|
|
||||||
|
|
||||||
let mut last_poll = Instant::now();
|
|
||||||
let mut last_filter = String::new();
|
|
||||||
loop {
|
|
||||||
let ev = match x.conn.poll_for_event()? {
|
|
||||||
Some(ev) => ev,
|
|
||||||
None => {
|
|
||||||
std::thread::sleep(Duration::from_millis(10));
|
|
||||||
if last_poll.elapsed() >= Duration::from_secs(2) {
|
|
||||||
last_poll = Instant::now();
|
|
||||||
list.refresh_processes();
|
|
||||||
redraw(&list, &entry, &[&btn_term, &btn_kill, &btn_refresh], &ctx, &mut target, &mut styles);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let mut handled = false;
|
|
||||||
match ev {
|
|
||||||
Event::Expose(_) => {
|
|
||||||
redraw(&list, &entry, &[&btn_term, &btn_kill, &btn_refresh], &ctx, &mut target, &mut styles);
|
|
||||||
}
|
|
||||||
Event::ButtonPress(ev) => {
|
|
||||||
let (px, py) = (ev.event_x as i32, ev.event_y as i32);
|
|
||||||
if entry.on_button_press(px, py, ev.detail as u32) {
|
|
||||||
handled = true;
|
|
||||||
} else {
|
|
||||||
entry.set_focused(false);
|
|
||||||
handled = btn_term.on_button_press(px, py, ev.detail as u32)
|
|
||||||
|| btn_kill.on_button_press(px, py, ev.detail as u32)
|
|
||||||
|| btn_refresh.on_button_press(px, py, ev.detail as u32)
|
|
||||||
|| list.on_button_press(px, py, ev.detail as u32);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::ButtonRelease(ev) => {
|
|
||||||
let (px, py) = (ev.event_x as i32, ev.event_y as i32);
|
|
||||||
let (c_term, c_kill, c_ref) =
|
|
||||||
(btn_term.click_count(), btn_kill.click_count(), btn_refresh.click_count());
|
|
||||||
handled = btn_term.on_button_release(px, py, ev.detail as u32)
|
|
||||||
|| btn_kill.on_button_release(px, py, ev.detail as u32)
|
|
||||||
|| btn_refresh.on_button_release(px, py, ev.detail as u32)
|
|
||||||
|| list.on_button_release(px, py, ev.detail as u32);
|
|
||||||
if btn_term.click_count() != c_term {
|
|
||||||
if let Some(pid) = list.selected_pid() {
|
|
||||||
if signal_pid(pid, libc::SIGTERM) {
|
|
||||||
list.refresh_processes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if btn_kill.click_count() != c_kill {
|
|
||||||
if let Some(pid) = list.selected_pid() {
|
|
||||||
if signal_pid(pid, libc::SIGKILL) {
|
|
||||||
list.refresh_processes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if btn_refresh.click_count() != c_ref {
|
|
||||||
list.refresh_processes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::MotionNotify(ev) => {
|
|
||||||
handled = entry.on_motion(ev.event_x as i32, ev.event_y as i32)
|
|
||||||
|| btn_term.on_motion(ev.event_x as i32, ev.event_y as i32)
|
|
||||||
|| btn_kill.on_motion(ev.event_x as i32, ev.event_y as i32)
|
|
||||||
|| btn_refresh.on_motion(ev.event_x as i32, ev.event_y as i32)
|
|
||||||
|| list.on_motion(ev.event_x as i32, ev.event_y as i32);
|
|
||||||
}
|
|
||||||
Event::KeyPress(ev) => {
|
|
||||||
let keysym = x.keysym_for_keycode(ev.detail);
|
|
||||||
if keysym == KEY_ESCAPE {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let text = if (0x20..0x7f).contains(&keysym) {
|
|
||||||
Some(keysym as u8 as char)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
handled = entry.on_key(Key::from_keysym(keysym, text), text);
|
|
||||||
if handled {
|
|
||||||
let f = entry.text().to_string();
|
|
||||||
if f != last_filter {
|
|
||||||
last_filter = f.clone();
|
|
||||||
list.set_filter(&f);
|
|
||||||
list.refresh_processes();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Event::ClientMessage(ev)
|
|
||||||
if ev.type_ == x.wm_protocols && ev.data.as_data32()[0] == x.wm_delete =>
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
if handled {
|
|
||||||
redraw(&list, &entry, &[&btn_term, &btn_kill, &btn_refresh], &ctx, &mut target, &mut styles);
|
|
||||||
}
|
|
||||||
if last_poll.elapsed() >= Duration::from_secs(2) {
|
|
||||||
last_poll = Instant::now();
|
|
||||||
list.refresh_processes();
|
|
||||||
redraw(&list, &entry, &[&btn_term, &btn_kill, &btn_refresh], &ctx, &mut target, &mut styles);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(0)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> ExitCode {
|
|
||||||
match run() {
|
|
||||||
Ok(code) => ExitCode::from(code as u8),
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("monitor_process_manager: {}", e);
|
|
||||||
ExitCode::from(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
# Process Manager — monitor (Rust)
|
|
||||||
|
|
||||||
A btop-style process manager in one 1024x768 window, built only from
|
|
||||||
LightGUI XFrame widgets: `XProcessList` (live `/proc` table, sortable
|
|
||||||
columns, click a row to select it), `GtkEntry` (filter by name or pid),
|
|
||||||
and three `GtkButton`s — **Terminate**, **Kill**, **Refresh**.
|
|
||||||
|
|
||||||
## What it shows
|
|
||||||
|
|
||||||
- **Processes** — pid, program, CPU% and memory for the live
|
|
||||||
processes on this machine (top 12 by CPU), refreshed every 2 seconds.
|
|
||||||
Click a column header to sort by it (click again to reverse); click a
|
|
||||||
body row to select it (highlighted).
|
|
||||||
- **Filter** — type a name or pid substring; the list re-scans `/proc`
|
|
||||||
and shows only matching rows.
|
|
||||||
- **Terminate** — sends `SIGTERM` to the selected process.
|
|
||||||
- **Kill** — sends `SIGKILL` to the selected process.
|
|
||||||
- **Refresh** — re-scans `/proc` immediately.
|
|
||||||
|
|
||||||
Safety: Terminate/Kill act on the selected row's pid only, and refuse
|
|
||||||
pid ≤ 1 and the manager's own pid.
|
|
||||||
|
|
||||||
## Build and run
|
|
||||||
|
|
||||||
```sh
|
|
||||||
cargo run -p xframe-demo --example monitor_process_manager
|
|
||||||
# Esc or close the window to quit
|
|
||||||
```
|
|
||||||
|
|
||||||
## Source
|
|
||||||
|
|
||||||
- App: `crates/xframe-demo/examples/monitor_process_manager.rs` in
|
|
||||||
[lightgui_rust](https://github.com/cnmcdee/lightgui_rust)
|
|
||||||
- Widgets: `XProcessList` (`MonitorWidget`), `GtkButton`
|
|
||||||
(`ButtonWidget`), `GtkEntry` (`EntryWidget`) — this app adds a
|
|
||||||
name/pid filter to `MonitorWidget` (additive only; the widget demo
|
|
||||||
and `XMonitorGrid` behaviour are unchanged)
|
|
||||||
|
|
||||||
The same app exists in
|
|
||||||
[C](https://github.com/cnmcdee/lightgui) and
|
|
||||||
[Python](https://github.com/cnmcdee/lightgui_python) — see the
|
|
||||||
[walkthrough](https://www.lightgui.com/monitor-process-manager/).
|
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 4.7 KiB |
@@ -0,0 +1,44 @@
|
|||||||
|
# Task Manager — monitor app (Rust)
|
||||||
|
|
||||||
|
A Windows-style Task Manager clone in one 1024×768 window, built from
|
||||||
|
the LightGUI XFrame monitor widgets (Stage 2 of the btop-widget
|
||||||
|
project).
|
||||||
|
|
||||||
|
## Tabs
|
||||||
|
|
||||||
|
- **Processes** — `XProcessList`: live `/proc` table, columns PID /
|
||||||
|
NAME / CPU% / MEM, click a column header to sort (click again to
|
||||||
|
reverse), click a row to select it. Refreshes every 2 s.
|
||||||
|
- **Performance** — `XHistoryGraph` (60 s CPU + memory),
|
||||||
|
`XCoreGrid` (one mini history graph per CPU core), `XUsageBar`
|
||||||
|
(Memory), `XStackBar` (memory Used / Cached / Buffers / Free in
|
||||||
|
GiB), `XNetGraph` (live download / upload from `/proc/net/dev`).
|
||||||
|
All graphs refresh every 500 ms from live `/proc` data
|
||||||
|
(`/proc/stat`, `/proc/meminfo`, `/proc/net/dev`).
|
||||||
|
|
||||||
|
The tab strip is two `GtkButton` widgets: the Rust
|
||||||
|
`ContainerWidget` (`TPageControl`) draws a plain panel + children
|
||||||
|
and no tab strip, so the tabs are registry buttons and the app
|
||||||
|
routes their clicks (same pattern as the Python app's app-level tab
|
||||||
|
hit handling).
|
||||||
|
|
||||||
|
## Build and run
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo run -p xframe-demo --example monitor_task_manager
|
||||||
|
```
|
||||||
|
|
||||||
|
Close the window (or press `q` / Esc) to quit.
|
||||||
|
|
||||||
|
## Widgets used
|
||||||
|
|
||||||
|
`TPageControl`, `GtkButton`, `XProcessList`, `XHistoryGraph`,
|
||||||
|
`XCoreGrid`, `XUsageBar`, `XStackBar`, `XNetGraph` — all from the
|
||||||
|
LightGUI registry. No raw x11rb drawing for UI elements: the only
|
||||||
|
raw X11 here is the window shell and the event loop, as in
|
||||||
|
`xframe_demo::run_widget`. Rust colours go through
|
||||||
|
`Srgb::to_pixel`, so colours cost no `XAllocColor` round trips
|
||||||
|
(unlike the C++ style manager) — but the text helpers measure with
|
||||||
|
`query_text_extents`, one server round trip per call, so the window
|
||||||
|
repaints every 2 s rather than every 500 ms tick (data is still
|
||||||
|
sampled every 500 ms).
|
||||||
@@ -0,0 +1,473 @@
|
|||||||
|
//! Task Manager - monitor app (Stage 2, btop-widget project).
|
||||||
|
//!
|
||||||
|
//! A Windows-style Task Manager clone in one 1024x768 window: a
|
||||||
|
//! `TPageControl` body panel with two `GtkButton` tabs (the Rust
|
||||||
|
//! `ContainerWidget` draws a plain panel + children, no tab strip,
|
||||||
|
//! so the tabs are registry buttons and the app routes their clicks).
|
||||||
|
//! The Processes tab is a live `XProcessList` from `/proc` (sortable
|
||||||
|
//! PID / NAME / CPU% / MEM columns, click a header to sort). The
|
||||||
|
//! Performance tab is `XHistoryGraph` (60 s CPU + memory),
|
||||||
|
//! `XCoreGrid` (one mini history graph per CPU core), `XUsageBar`
|
||||||
|
//! (Memory), `XStackBar` (memory Used / Cached / Buffers / Free in
|
||||||
|
//! GiB) and `XNetGraph` (live download / upload from `/proc/net/dev`).
|
||||||
|
//!
|
||||||
|
//! Every visible UI element is a LightGUI registry widget; the only
|
||||||
|
//! raw X11 here is the window shell and the event loop, exactly like
|
||||||
|
//! `xframe_demo::run_widget`.
|
||||||
|
//!
|
||||||
|
//! Run from the workspace root:
|
||||||
|
//! cargo run -p xframe-demo --example monitor_task_manager
|
||||||
|
//! Close the window (or press Esc / `q`) to quit. Data is sampled
|
||||||
|
//! from live `/proc` every 500 ms; the window repaints every 2 s (the
|
||||||
|
//! Rust text helpers measure with `query_text_extents`, one server
|
||||||
|
//! round trip per call, and a full repaint issues hundreds of them —
|
||||||
|
//! repainting every tick would never catch up).
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
use std::process::ExitCode;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
use x11rb::connection::Connection;
|
||||||
|
use x11rb::protocol::xproto::*;
|
||||||
|
use x11rb::protocol::Event;
|
||||||
|
use xframe_common::style::FONT_DEFAULT;
|
||||||
|
use xframe_common::target::CtxTarget;
|
||||||
|
use xframe_common::x11::{DrawCtx, X11};
|
||||||
|
use xframe_core::{Srgb, StyleManager};
|
||||||
|
use xframe_demo::register_styles;
|
||||||
|
use xframe_widgets::archetypes::{ButtonWidget, ContainerWidget};
|
||||||
|
use xframe_widgets::monitor::{MonitorWidget, Segment};
|
||||||
|
use xframe_widgets::Widget;
|
||||||
|
|
||||||
|
const WIN_W: u16 = 1024;
|
||||||
|
const WIN_H: u16 = 768;
|
||||||
|
const KEY_ESCAPE: u32 = 0xff1b;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Live /proc readers (same sources as the C app)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Default)]
|
||||||
|
struct CpuTimes {
|
||||||
|
idle: u64,
|
||||||
|
total: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_cpu() -> Option<(CpuTimes, Vec<CpuTimes>)> {
|
||||||
|
let text = fs::read_to_string("/proc/stat").ok()?;
|
||||||
|
let mut total = None;
|
||||||
|
let mut cores = Vec::new();
|
||||||
|
for line in text.lines() {
|
||||||
|
let mut parts = line.split_whitespace();
|
||||||
|
let label = parts.next()?;
|
||||||
|
if !label.starts_with("cpu") {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let nums: Vec<u64> = parts.filter_map(|s| s.parse().ok()).collect();
|
||||||
|
if nums.len() < 5 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let t = CpuTimes {
|
||||||
|
idle: nums[3] + nums[4],
|
||||||
|
total: nums.iter().sum(),
|
||||||
|
};
|
||||||
|
if label == "cpu" {
|
||||||
|
total = Some(t);
|
||||||
|
} else {
|
||||||
|
cores.push(t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some((total?, cores))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cpu_pct(prev: CpuTimes, cur: CpuTimes) -> f64 {
|
||||||
|
let d_total = cur.total.saturating_sub(prev.total);
|
||||||
|
let d_idle = cur.idle.saturating_sub(prev.idle);
|
||||||
|
if d_total == 0 {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
(100.0 * (1.0 - d_idle as f64 / d_total as f64)).clamp(0.0, 100.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
struct MemInfo {
|
||||||
|
used_gb: f64,
|
||||||
|
used_pct: f64,
|
||||||
|
cached_gb: f64,
|
||||||
|
buffers_gb: f64,
|
||||||
|
free_gb: f64,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_mem() -> Option<MemInfo> {
|
||||||
|
let text = fs::read_to_string("/proc/meminfo").ok()?;
|
||||||
|
let get = |key: &str| -> u64 {
|
||||||
|
for line in text.lines() {
|
||||||
|
if let Some(rest) = line.strip_prefix(key) {
|
||||||
|
return rest
|
||||||
|
.split_whitespace()
|
||||||
|
.next()
|
||||||
|
.and_then(|v| v.parse().ok())
|
||||||
|
.unwrap_or(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
0
|
||||||
|
};
|
||||||
|
let total_kb = get("MemTotal:") as f64;
|
||||||
|
if total_kb <= 0.0 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let free_kb = get("MemFree:") as f64;
|
||||||
|
let buf_kb = get("Buffers:") as f64;
|
||||||
|
let cache_kb = get("Cached:") as f64;
|
||||||
|
let gb = 1024.0 * 1024.0;
|
||||||
|
let used_gb = ((total_kb - free_kb - buf_kb - cache_kb) / gb).max(0.0);
|
||||||
|
Some(MemInfo {
|
||||||
|
used_gb,
|
||||||
|
used_pct: 100.0 * used_gb / (total_kb / gb),
|
||||||
|
cached_gb: cache_kb / gb,
|
||||||
|
buffers_gb: buf_kb / gb,
|
||||||
|
free_gb: free_kb / gb,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Default)]
|
||||||
|
struct NetBytes {
|
||||||
|
rx: u64,
|
||||||
|
tx: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_net() -> Option<NetBytes> {
|
||||||
|
let text = fs::read_to_string("/proc/net/dev").ok()?;
|
||||||
|
let mut out = NetBytes::default();
|
||||||
|
for line in text.lines().skip(2) {
|
||||||
|
let (iface, rest) = line.split_once(':')?;
|
||||||
|
if iface.trim() == "lo" {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let f: Vec<u64> = rest.split_whitespace().filter_map(|s| s.parse().ok()).collect();
|
||||||
|
if f.len() > 8 {
|
||||||
|
out.rx += f[0];
|
||||||
|
out.tx += f[8];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// App
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
struct App {
|
||||||
|
tab_proc: ButtonWidget,
|
||||||
|
tab_perf: ButtonWidget,
|
||||||
|
pages: ContainerWidget,
|
||||||
|
proc_list: MonitorWidget,
|
||||||
|
hist: MonitorWidget,
|
||||||
|
cores: MonitorWidget,
|
||||||
|
membar: MonitorWidget,
|
||||||
|
memstack: MonitorWidget,
|
||||||
|
net: MonitorWidget,
|
||||||
|
active_page: usize,
|
||||||
|
tick: u64,
|
||||||
|
prev_total: CpuTimes,
|
||||||
|
prev_cores: Vec<CpuTimes>,
|
||||||
|
prev_net: NetBytes,
|
||||||
|
net_totals: (f64, f64),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
fn new() -> App {
|
||||||
|
let mut tab_proc = ButtonWidget::new("GtkButton", "Button", 10, 8, 130, 30);
|
||||||
|
tab_proc.set_text("Processes");
|
||||||
|
let mut tab_perf = ButtonWidget::new("GtkButton", "Button", 142, 8, 130, 30);
|
||||||
|
tab_perf.set_text("Performance");
|
||||||
|
|
||||||
|
let mut pages = ContainerWidget::new("TPageControl", "PageControl", 8, 42, 1008, 718);
|
||||||
|
pages.set_text("");
|
||||||
|
pages.items = vec!["Processes".to_string(), "Performance".to_string()];
|
||||||
|
|
||||||
|
let proc_list = MonitorWidget::new("Processes", "ProcessList", 14, 48, 996, 706);
|
||||||
|
|
||||||
|
let mut hist = MonitorWidget::new("CPU + Memory (60 s)", "HistoryGraph", 14, 48, 630, 300);
|
||||||
|
hist.series.clear();
|
||||||
|
hist.add_series("CPU %");
|
||||||
|
hist.add_series("Memory %");
|
||||||
|
|
||||||
|
let cores = MonitorWidget::new("CPU cores", "CoreGrid", 652, 48, 354, 300);
|
||||||
|
|
||||||
|
let mut membar = MonitorWidget::new("Memory", "UsageBar", 14, 356, 992, 34);
|
||||||
|
membar.set_text("Memory");
|
||||||
|
let mut memstack = MonitorWidget::new("Memory", "StackBar", 14, 398, 992, 60);
|
||||||
|
memstack.set_text("Memory");
|
||||||
|
let mut net = MonitorWidget::new("Network", "NetGraph", 14, 466, 992, 288);
|
||||||
|
net.series.clear();
|
||||||
|
net.add_series("Down");
|
||||||
|
net.add_series("Up");
|
||||||
|
net.totals = (0.0, 0.0);
|
||||||
|
|
||||||
|
let (prev_total, prev_cores) = read_cpu().unwrap_or_default();
|
||||||
|
let prev_net = read_net().unwrap_or_default();
|
||||||
|
App {
|
||||||
|
tab_proc,
|
||||||
|
tab_perf,
|
||||||
|
pages,
|
||||||
|
proc_list,
|
||||||
|
hist,
|
||||||
|
cores,
|
||||||
|
membar,
|
||||||
|
memstack,
|
||||||
|
net,
|
||||||
|
active_page: 0,
|
||||||
|
tick: 0,
|
||||||
|
prev_total,
|
||||||
|
prev_cores,
|
||||||
|
prev_net,
|
||||||
|
net_totals: (0.0, 0.0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Prime live data: two /proc/stat samples 100 ms apart give the
|
||||||
|
/// first real CPU/net rates; the history widgets are then filled
|
||||||
|
/// with that live value so no seeded demo data shows.
|
||||||
|
fn prime(&mut self) {
|
||||||
|
std::thread::sleep(Duration::from_millis(100));
|
||||||
|
let (cur_total, cur_cores) = match read_cpu() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
let cur_net = read_net().unwrap_or_default();
|
||||||
|
let mem = read_mem();
|
||||||
|
let cpu0 = cpu_pct(self.prev_total, cur_total);
|
||||||
|
let ncores = cur_cores.len();
|
||||||
|
// Rebuild the core series for the live core count, then fill
|
||||||
|
// each core's history with the live value.
|
||||||
|
self.cores.series.clear();
|
||||||
|
for c in 0..ncores {
|
||||||
|
self.cores.add_series(&format!("CPU{}", c));
|
||||||
|
}
|
||||||
|
let mut per_core = vec![0.0; ncores];
|
||||||
|
for (i, c) in cur_cores.iter().enumerate() {
|
||||||
|
if i < self.prev_cores.len() {
|
||||||
|
per_core[i] = cpu_pct(self.prev_cores[i], *c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _ in 0..60 {
|
||||||
|
self.cores.push_samples(&per_core);
|
||||||
|
}
|
||||||
|
if let Some(m) = &mem {
|
||||||
|
self.hist.push_samples(&[cpu0, m.used_pct]);
|
||||||
|
self.membar.set_value(m.used_pct);
|
||||||
|
self.set_mem_segments(m);
|
||||||
|
}
|
||||||
|
let down0 = (cur_net.rx - self.prev_net.rx) as f64 * 10.0 / 1024.0;
|
||||||
|
let up0 = (cur_net.tx - self.prev_net.tx) as f64 * 10.0 / 1024.0;
|
||||||
|
for _ in 0..120 {
|
||||||
|
self.net.push_samples(&[down0, up0]);
|
||||||
|
}
|
||||||
|
self.prev_total = cur_total;
|
||||||
|
self.prev_cores = cur_cores;
|
||||||
|
self.prev_net = cur_net;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_mem_segments(&mut self, m: &MemInfo) {
|
||||||
|
self.memstack.set_segments(vec![
|
||||||
|
Segment { label: "Used", value: m.used_gb, color: Srgb::new(34, 197, 94) },
|
||||||
|
Segment { label: "Cached", value: m.cached_gb, color: Srgb::new(245, 158, 11) },
|
||||||
|
Segment { label: "Buffers", value: m.buffers_gb, color: Srgb::new(59, 130, 246) },
|
||||||
|
Segment { label: "Free", value: m.free_gb, color: Srgb::new(200, 200, 200) },
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_page(&mut self, page: usize) {
|
||||||
|
self.active_page = page;
|
||||||
|
self.pages.set_active_page(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One 500 ms tick: sample live data, push it into the widgets.
|
||||||
|
fn on_tick(&mut self) {
|
||||||
|
self.tick += 1;
|
||||||
|
let (cur_total, cur_cores) = match read_cpu() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
let cur_net = read_net().unwrap_or_default();
|
||||||
|
let cpu = cpu_pct(self.prev_total, cur_total);
|
||||||
|
let mut per_core = vec![0.0; cur_cores.len()];
|
||||||
|
for (i, c) in cur_cores.iter().enumerate() {
|
||||||
|
if i < self.prev_cores.len() {
|
||||||
|
per_core[i] = cpu_pct(self.prev_cores[i], *c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if let Some(m) = read_mem() {
|
||||||
|
self.hist.push_samples(&[cpu, m.used_pct]);
|
||||||
|
self.membar.set_value(m.used_pct);
|
||||||
|
self.set_mem_segments(&m);
|
||||||
|
}
|
||||||
|
self.cores.push_samples(&per_core);
|
||||||
|
let dt = 0.5; // 500 ms tick
|
||||||
|
let down = (cur_net.rx - self.prev_net.rx) as f64 / dt / 1024.0;
|
||||||
|
let up = (cur_net.tx - self.prev_net.tx) as f64 / dt / 1024.0;
|
||||||
|
self.net.push_samples(&[down, up]);
|
||||||
|
self.net_totals.0 += (cur_net.rx - self.prev_net.rx) as f64;
|
||||||
|
self.net_totals.1 += (cur_net.tx - self.prev_net.tx) as f64;
|
||||||
|
self.net.totals = self.net_totals;
|
||||||
|
self.prev_total = cur_total;
|
||||||
|
self.prev_cores = cur_cores;
|
||||||
|
self.prev_net = cur_net;
|
||||||
|
if self.tick % 4 == 0 {
|
||||||
|
self.proc_list.refresh_processes();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(&mut self, ctx: &DrawCtx, target: &mut CtxTarget, styles: &mut StyleManager) {
|
||||||
|
ctx.clear(255, 255, 255, WIN_W, WIN_H);
|
||||||
|
self.tab_proc.draw(target, styles);
|
||||||
|
self.tab_perf.draw(target, styles);
|
||||||
|
self.pages.draw(target, styles);
|
||||||
|
if self.active_page == 0 {
|
||||||
|
self.proc_list.draw(target, styles);
|
||||||
|
} else {
|
||||||
|
self.hist.draw(target, styles);
|
||||||
|
self.cores.draw(target, styles);
|
||||||
|
self.membar.draw(target, styles);
|
||||||
|
self.memstack.draw(target, styles);
|
||||||
|
self.net.draw(target, styles);
|
||||||
|
}
|
||||||
|
ctx.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns true when the click was handled (caller redraws).
|
||||||
|
fn on_button_press(&mut self, x: i32, y: i32, button: u32) -> bool {
|
||||||
|
if self.tab_proc.contains(x, y) {
|
||||||
|
self.tab_proc.on_button_press(x, y, button);
|
||||||
|
self.set_page(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if self.tab_perf.contains(x, y) {
|
||||||
|
self.tab_perf.on_button_press(x, y, button);
|
||||||
|
self.set_page(1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if self.active_page == 0 {
|
||||||
|
self.proc_list.on_button_press(x, y, button)
|
||||||
|
} else {
|
||||||
|
// Graphs take no clicks; forward anyway for focus state.
|
||||||
|
let mut handled = false;
|
||||||
|
handled |= self.hist.on_button_press(x, y, button);
|
||||||
|
handled |= self.cores.on_button_press(x, y, button);
|
||||||
|
handled |= self.membar.on_button_press(x, y, button);
|
||||||
|
handled |= self.memstack.on_button_press(x, y, button);
|
||||||
|
handled |= self.net.on_button_press(x, y, button);
|
||||||
|
handled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_button_release(&mut self, x: i32, y: i32, button: u32) -> bool {
|
||||||
|
let mut handled = false;
|
||||||
|
handled |= self.tab_proc.on_button_release(x, y, button);
|
||||||
|
handled |= self.tab_perf.on_button_release(x, y, button);
|
||||||
|
if self.active_page == 0 {
|
||||||
|
handled |= self.proc_list.on_button_release(x, y, button);
|
||||||
|
}
|
||||||
|
handled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> ExitCode {
|
||||||
|
match run() {
|
||||||
|
Ok(code) => ExitCode::from(code as u8),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("monitor_task_manager: {}", e);
|
||||||
|
ExitCode::from(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||||
|
let x = X11::connect()?;
|
||||||
|
let font = x.open_font(&[FONT_DEFAULT, "fixed"])?;
|
||||||
|
let title = "Task Manager - monitor".to_string();
|
||||||
|
let event_mask = EventMask::EXPOSURE
|
||||||
|
| EventMask::KEY_PRESS
|
||||||
|
| EventMask::STRUCTURE_NOTIFY
|
||||||
|
| EventMask::BUTTON_PRESS
|
||||||
|
| EventMask::BUTTON_RELEASE
|
||||||
|
| EventMask::POINTER_MOTION;
|
||||||
|
let (win, gc) = x.create_window(100, 100, WIN_W, WIN_H, &title, event_mask)?;
|
||||||
|
|
||||||
|
let mut styles = StyleManager::new();
|
||||||
|
register_styles(&mut styles);
|
||||||
|
styles.set_active_style_by_name("default");
|
||||||
|
|
||||||
|
let mut app = App::new();
|
||||||
|
app.prime();
|
||||||
|
|
||||||
|
let ctx = DrawCtx::new(&x, win, gc, font);
|
||||||
|
ctx.bind_font();
|
||||||
|
let mut target = CtxTarget::new(&ctx);
|
||||||
|
if !styles.resolve_font("default", &mut target) && !styles.resolve_font("dark", &mut target) {
|
||||||
|
eprintln!("monitor_task_manager: warning: no font resolved, labels will be blank");
|
||||||
|
}
|
||||||
|
|
||||||
|
app.draw(&ctx, &mut target, &mut styles);
|
||||||
|
let mut last_tick = Instant::now();
|
||||||
|
loop {
|
||||||
|
let ev = match x.conn.poll_for_event()? {
|
||||||
|
Some(ev) => ev,
|
||||||
|
None => {
|
||||||
|
if last_tick.elapsed() >= Duration::from_millis(500) {
|
||||||
|
last_tick = Instant::now();
|
||||||
|
app.on_tick();
|
||||||
|
// Data is sampled every 500 ms, but repainting is
|
||||||
|
// every 2 s (every 4th tick): the Rust text helpers
|
||||||
|
// measure with `query_text_extents`, one server
|
||||||
|
// round trip per call, and a full repaint issues
|
||||||
|
// hundreds of them (~0.2-0.5 s wall on this Xvfb).
|
||||||
|
// Repainting every tick would never catch up and
|
||||||
|
// screenshots would catch the paint mid-draw; at
|
||||||
|
// 2 s each repaint completes inside its interval
|
||||||
|
// and the graphs still advance live.
|
||||||
|
if app.tick % 4 == 0 {
|
||||||
|
app.draw(&ctx, &mut target, &mut styles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(10));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut handled = false;
|
||||||
|
match ev {
|
||||||
|
Event::Expose(_) => {
|
||||||
|
handled = true;
|
||||||
|
}
|
||||||
|
Event::ButtonPress(ev) => {
|
||||||
|
handled =
|
||||||
|
app.on_button_press(ev.event_x as i32, ev.event_y as i32, ev.detail as u32);
|
||||||
|
}
|
||||||
|
Event::ButtonRelease(ev) => {
|
||||||
|
handled =
|
||||||
|
app.on_button_release(ev.event_x as i32, ev.event_y as i32, ev.detail as u32);
|
||||||
|
}
|
||||||
|
Event::MotionNotify(_) => {}
|
||||||
|
Event::KeyPress(ev) => {
|
||||||
|
let keysym = x.keysym_for_keycode(ev.detail);
|
||||||
|
if keysym == KEY_ESCAPE || keysym == b'q' as u32 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
handled = app.proc_list.on_key(
|
||||||
|
xframe_widgets::Key::from_keysym(keysym, None),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Event::ClientMessage(ev)
|
||||||
|
if ev.type_ == x.wm_protocols && ev.data.as_data32()[0] == x.wm_delete =>
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
if handled {
|
||||||
|
app.draw(&ctx, &mut target, &mut styles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 5.2 KiB |
@@ -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);
|
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
|
// Label text: the registry `kind` approximates the C++ constructor's
|
||||||
// default text (see the module docs of the binary).
|
// default text (see the module docs of the binary) -- but only when
|
||||||
let kind = widget.kind().to_string();
|
// the widget did not seed its own text (e.g. the monitor demos seed
|
||||||
widget.set_text(&kind);
|
// 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 Some(text) = &csv {
|
||||||
if let Err(e) = widget.load_csv_data(text) {
|
if let Err(e) = widget.load_csv_data(text) {
|
||||||
eprintln!("xframe-demo: --csv: {}", e);
|
eprintln!("xframe-demo: --csv: {}", e);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
//! release / motion / key handlers, following the C++ semantics surveyed
|
//! release / motion / key handlers, following the C++ semantics surveyed
|
||||||
//! in `widgets/common/` (see `widget.rs` for the shared helpers).
|
//! in `widgets/common/` (see `widget.rs` for the shared helpers).
|
||||||
|
|
||||||
use xframe_core::draw::{draw_checkmark, draw_polished, draw_text_at, draw_text_centered, draw_text_left, draw_triangle_down, draw_triangle_left, draw_triangle_right, draw_triangle_up, text_width};
|
use xframe_core::draw::{draw_checkmark, draw_polished, draw_text_at, draw_text_at_cap, draw_text_centered, draw_text_centered_cap, draw_text_left, draw_text_right_cap, draw_triangle_down, draw_triangle_left, draw_triangle_right, draw_triangle_up, text_width, text_width_cap};
|
||||||
use xframe_core::{DrawTarget, Srgb, Style, StyleManager};
|
use xframe_core::{DrawTarget, Srgb, Style, StyleManager};
|
||||||
|
|
||||||
use crate::graph::{SeriesKind, XGraph};
|
use crate::graph::{SeriesKind, XGraph};
|
||||||
@@ -2713,7 +2713,6 @@ fn inst_label(kind: &str, text: &str) -> String {
|
|||||||
"DigitalReadout" => "Readout".to_string(),
|
"DigitalReadout" => "Readout".to_string(),
|
||||||
"FlowMeter" => "Flow".to_string(),
|
"FlowMeter" => "Flow".to_string(),
|
||||||
"LedBlue" => "BLUE".to_string(),
|
"LedBlue" => "BLUE".to_string(),
|
||||||
"LedDigits1" => "LedDigits".to_string(),
|
|
||||||
"LedGreen" => "GREEN".to_string(),
|
"LedGreen" => "GREEN".to_string(),
|
||||||
"LedRed" => "RED".to_string(),
|
"LedRed" => "RED".to_string(),
|
||||||
"Manometer" => "Manometer".to_string(),
|
"Manometer" => "Manometer".to_string(),
|
||||||
@@ -2727,7 +2726,7 @@ fn inst_label(kind: &str, text: &str) -> String {
|
|||||||
"StackLight" => "STACK".to_string(),
|
"StackLight" => "STACK".to_string(),
|
||||||
"TemperatureGauge" => "Temperature".to_string(),
|
"TemperatureGauge" => "Temperature".to_string(),
|
||||||
"VuMeter" => "VU".to_string(),
|
"VuMeter" => "VU".to_string(),
|
||||||
_ => format!("Inst{kind}"),
|
_ => String::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2929,7 +2928,8 @@ fn draw_seg_char(
|
|||||||
"k" => (x + t, y + h - t, midx, midy + t),
|
"k" => (x + t, y + h - t, midx, midy + t),
|
||||||
_ => (x + w - t, y + h - t, midx, midy + t),
|
_ => (x + w - t, y + h - t, midx, midy + t),
|
||||||
};
|
};
|
||||||
target.set_line_width(t.max(1) as u32);
|
// Audit G5: diagonals thinner than the straight segments.
|
||||||
|
target.set_line_width((t * 2 / 3).max(2) as u32);
|
||||||
target.draw_line(x1, y1, x2, y2);
|
target.draw_line(x1, y1, x2, y2);
|
||||||
target.set_line_width(1);
|
target.set_line_width(1);
|
||||||
}
|
}
|
||||||
@@ -2957,7 +2957,7 @@ enum SegKind {
|
|||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
fn draw_segment_display(
|
fn draw_segment_display(
|
||||||
target: &mut dyn DrawTarget,
|
target: &mut dyn DrawTarget,
|
||||||
style: &Style,
|
_style: &Style,
|
||||||
b: Rect,
|
b: Rect,
|
||||||
label: &str,
|
label: &str,
|
||||||
cells: &[char],
|
cells: &[char],
|
||||||
@@ -2974,14 +2974,14 @@ fn draw_segment_display(
|
|||||||
target.set_foreground(bezel);
|
target.set_foreground(bezel);
|
||||||
target.draw_rect(b.x, b.y, b.w - 1, b.h - 1);
|
target.draw_rect(b.x, b.y, b.w - 1, b.h - 1);
|
||||||
target.draw_rect(b.x + 2, b.y + 2, b.w - 5, b.h - 5);
|
target.draw_rect(b.x + 2, b.y + 2, b.w - 5, b.h - 5);
|
||||||
let mut lst = style.clone();
|
// Audit G2/G4: no label band (the label was the class name); the
|
||||||
lst.text_color = label_col;
|
// digit cells fill 78% of the widget height, vertically centred.
|
||||||
draw_text_at(target, &lst, b.x + 6, b.y + 12, label);
|
let _ = (label, label_col);
|
||||||
let n = cells.len().max(1) as i32;
|
let n = cells.len().max(1) as i32;
|
||||||
let ax = b.x + 6;
|
let ax = b.x + 6;
|
||||||
let ay = b.y + 16 + 4;
|
let ah = b.h * 78 / 100;
|
||||||
|
let ay = b.y + (b.h - ah) / 2;
|
||||||
let aw = b.w - 12;
|
let aw = b.w - 12;
|
||||||
let ah = b.h - 16 - 10;
|
|
||||||
if aw >= 10 && ah >= 10 {
|
if aw >= 10 && ah >= 10 {
|
||||||
let gap = 4;
|
let gap = 4;
|
||||||
let dp_w = if dp_after >= 0 { 8 } else { 0 };
|
let dp_w = if dp_after >= 0 { 8 } else { 0 };
|
||||||
@@ -3109,14 +3109,18 @@ fn draw_dial_gauge(
|
|||||||
);
|
);
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
let lab = inst_fmt(min + f * (max - min), 0);
|
let lab = inst_fmt(min + f * (max - min), 0);
|
||||||
let tw = text_width(style, &lab);
|
let num_cap = (r / 7).max(14);
|
||||||
let lr = r as f64 - 26.0 * s;
|
let num_asc = num_cap * 9 / 8;
|
||||||
draw_text_at(
|
let lr = r as f64 - num_cap as f64 - 14.0;
|
||||||
|
let tw = text_width_cap(target, &lab, num_cap, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx + (lr * ca) as i32 - tw / 2,
|
cx + (lr * ca) as i32 - tw / 2,
|
||||||
cy - (lr * sa) as i32 + 4,
|
cy - (lr * sa) as i32 + num_asc / 2,
|
||||||
&lab,
|
&lab,
|
||||||
|
num_cap,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3136,21 +3140,26 @@ fn draw_dial_gauge(
|
|||||||
let hd = ((10.0 * s).round() as i32).max(2);
|
let hd = ((10.0 * s).round() as i32).max(2);
|
||||||
target.fill_arc(cx - hd / 2, cy - hd / 2, hd, hd, 0, 360 * 64);
|
target.fill_arc(cx - hd / 2, cy - hd / 2, hd, hd, 0, 360 * 64);
|
||||||
let val = format!("{}{}", inst_fmt(value, 0), unit);
|
let val = format!("{}{}", inst_fmt(value, 0), unit);
|
||||||
let vw = text_width(style, &val);
|
let vw = text_width_cap(target, &val, 18, false);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx - vw / 2,
|
cx - vw / 2,
|
||||||
cy + r - (16.0 * s).round() as i32,
|
cy + r - 18,
|
||||||
&val,
|
&val,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
let tw = text_width(style, label);
|
// Audit G3: title inside the dial, below the top numerals.
|
||||||
draw_text_at(
|
let tw = text_width_cap(target, label, 17, true);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx - tw / 2,
|
cx - tw / 2,
|
||||||
b.y + (16.0 * s).round() as i32,
|
cy - (r as f64 * 0.43) as i32,
|
||||||
label,
|
label,
|
||||||
|
17,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
@@ -3182,9 +3191,9 @@ fn draw_bar_meter(
|
|||||||
let sx = b.w as f64 / 720.0;
|
let sx = b.w as f64 / 720.0;
|
||||||
let sy = b.h as f64 / 480.0;
|
let sy = b.h as f64 / 480.0;
|
||||||
let bx = b.x + (24.0 * sx) as i32;
|
let bx = b.x + (24.0 * sx) as i32;
|
||||||
let by = b.y + (24.0 * sy) as i32;
|
let by = b.y + (40.0 * sy) as i32;
|
||||||
let bw = (b.w as f64 - 48.0 * sx) as i32;
|
let bw = (b.w as f64 - 48.0 * sx) as i32;
|
||||||
let bh = (b.h as f64 - 48.0 * sy) as i32;
|
let bh = (b.h as f64 - 64.0 * sy) as i32;
|
||||||
target.set_foreground(face);
|
target.set_foreground(face);
|
||||||
target.fill_rect(bx, by, bw, bh);
|
target.fill_rect(bx, by, bw, bh);
|
||||||
target.set_foreground(ink);
|
target.set_foreground(ink);
|
||||||
@@ -3213,17 +3222,19 @@ fn draw_bar_meter(
|
|||||||
target.set_line_width(1);
|
target.set_line_width(1);
|
||||||
for i in (0..=10).step_by(2) {
|
for i in (0..=10).step_by(2) {
|
||||||
let lab = inst_fmt(min + (i as f64 / 10.0) * (max - min), 0);
|
let lab = inst_fmt(min + (i as f64 / 10.0) * (max - min), 0);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
bx + bw + 6,
|
bx + bw + 6,
|
||||||
by + bh - ((i as f64 / 10.0) * bh as f64) as i32 + 4,
|
by + bh - ((i as f64 / 10.0) * bh as f64) as i32 + 9,
|
||||||
&lab,
|
&lab,
|
||||||
|
17,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let val = format!("{label} {}", inst_fmt(value, 0));
|
let val = format!("{label} {}", inst_fmt(value, 0));
|
||||||
let vw = text_width(style, &val);
|
let vw = text_width_cap(target, &val, 19, true);
|
||||||
draw_text_at(target, style, b.x + (b.w - vw) / 2, b.y + (16.0 * sy) as i32, &val);
|
draw_text_at_cap(target, style, b.x + (b.w - vw) / 2, b.y + (26.0 * sy) as i32, &val, 19, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3283,12 +3294,14 @@ fn draw_flow_meter(
|
|||||||
target.draw_line(cx + hw, gy, cx + hw + (8.0 * sx) as i32, gy);
|
target.draw_line(cx + hw, gy, cx + hw + (8.0 * sx) as i32, gy);
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
let lab = inst_fmt(f * 100.0, 0);
|
let lab = inst_fmt(f * 100.0, 0);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx + hw + (12.0 * sx) as i32,
|
cx + hw + (12.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&lab,
|
&lab,
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3300,8 +3313,8 @@ fn draw_flow_meter(
|
|||||||
target.set_foreground(ink);
|
target.set_foreground(ink);
|
||||||
target.draw_arc(cx - rx, fy - ry, 2 * rx, 2 * ry, 0, 360 * 64);
|
target.draw_arc(cx - rx, fy - ry, 2 * rx, 2 * ry, 0, 360 * 64);
|
||||||
let val = format!("{label} {} L/min", inst_fmt(value, 0));
|
let val = format!("{label} {} L/min", inst_fmt(value, 0));
|
||||||
let vw = text_width(style, &val);
|
let vw = text_width_cap(target, &val, 18, true);
|
||||||
draw_text_at(target, style, b.x + (b.w - vw) / 2, b.y + (18.0 * sy) as i32, &val);
|
draw_text_at_cap(target, style, b.x + (b.w - vw) / 2, b.y + (26.0 * sy) as i32, &val, 18, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3353,19 +3366,20 @@ fn draw_manometer(
|
|||||||
target.draw_line(lx - (8.0 * sx) as i32, gy, lx, gy);
|
target.draw_line(lx - (8.0 * sx) as i32, gy, lx, gy);
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
let lab = inst_fmt(50.0 - (i as f64 / 10.0) * 100.0, 0);
|
let lab = inst_fmt(50.0 - (i as f64 / 10.0) * 100.0, 0);
|
||||||
let tw = text_width(style, &lab);
|
draw_text_right_cap(
|
||||||
draw_text_at(
|
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
lx - (12.0 * sx) as i32 - tw,
|
lx - (12.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&lab,
|
&lab,
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let val = format!("{label} {} mm", inst_fmt(value, 0));
|
let val = format!("{label} {} mm", inst_fmt(value, 0));
|
||||||
let vw = text_width(style, &val);
|
let vw = text_width_cap(target, &val, 18, true);
|
||||||
draw_text_at(target, style, b.x + (b.w - vw) / 2, b.y + (18.0 * sy) as i32, &val);
|
draw_text_at_cap(target, style, b.x + (b.w - vw) / 2, b.y + (26.0 * sy) as i32, &val, 18, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3389,13 +3403,15 @@ fn draw_digital_readout(
|
|||||||
target.fill_rect(b.x, b.y, b.w, b.h);
|
target.fill_rect(b.x, b.y, b.w, b.h);
|
||||||
let sx = b.w as f64 / 720.0;
|
let sx = b.w as f64 / 720.0;
|
||||||
let sy = b.h as f64 / 480.0;
|
let sy = b.h as f64 / 480.0;
|
||||||
let tw0 = text_width(style, label);
|
let tw0 = text_width_cap(target, label, 16, true);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
b.x + (b.w - tw0) / 2,
|
b.x + (b.w - tw0) / 2,
|
||||||
b.y + (18.0 * sy) as i32,
|
b.y + (24.0 * sy) as i32,
|
||||||
label,
|
label,
|
||||||
|
16,
|
||||||
|
true,
|
||||||
);
|
);
|
||||||
let dx = b.x + (12.0 * sx) as i32;
|
let dx = b.x + (12.0 * sx) as i32;
|
||||||
let dy = b.y + (30.0 * sy) as i32;
|
let dy = b.y + (30.0 * sy) as i32;
|
||||||
@@ -3415,12 +3431,15 @@ fn draw_digital_readout(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
totalw -= gap;
|
totalw -= gap;
|
||||||
let scale = ((dw as f64 / totalw.max(1) as f64).min(dh as f64 / digh as f64)).min(1.0);
|
// Audit G4: digits fill the display (C: digh = 42% of display
|
||||||
|
// height). The old .min(1.0) cap froze them at 45x85 in a 696x438 panel.
|
||||||
|
let scale = (dw as f64 / totalw.max(1) as f64).min(dh as f64 / digh as f64);
|
||||||
|
let scale = scale.min(dh as f64 * 0.42 / digh as f64);
|
||||||
let digw = ((digw as f64 * scale) as i32).max(2);
|
let digw = ((digw as f64 * scale) as i32).max(2);
|
||||||
let digh = ((digh as f64 * scale) as i32).max(2);
|
let digh = ((digh as f64 * scale) as i32).max(2);
|
||||||
let gap = ((gap as f64 * scale) as i32).max(1);
|
let gap = ((gap as f64 * scale) as i32).max(1);
|
||||||
let totalw = ((totalw as f64 * scale) as i32).max(1);
|
let totalw = ((totalw as f64 * scale) as i32).max(1);
|
||||||
let t = ((8.0 * scale) as i32).max(1);
|
let t = (digh / 10).max(6);
|
||||||
let mut tx = dx + (dw - totalw) / 2;
|
let mut tx = dx + (dw - totalw) / 2;
|
||||||
let ty = dy + (dh - digh) / 2;
|
let ty = dy + (dh - digh) / 2;
|
||||||
for c in s.chars() {
|
for c in s.chars() {
|
||||||
@@ -3487,7 +3506,7 @@ fn draw_led(
|
|||||||
target.set_foreground(dark);
|
target.set_foreground(dark);
|
||||||
target.fill_rect(cx - r - 8, cy - r - 8, 2 * r + 16, 2 * r + 16);
|
target.fill_rect(cx - r - 8, cy - r - 8, 2 * r + 16, 2 * r + 16);
|
||||||
let lead_top = cy + r + 8;
|
let lead_top = cy + r + 8;
|
||||||
let lead_h = (b.y + b.h - (16.0 * sy) as i32 - lead_top).max(1);
|
let lead_h = (b.y + b.h - (44.0 * sy) as i32 - lead_top).max(1);
|
||||||
target.set_foreground(lead);
|
target.set_foreground(lead);
|
||||||
target.fill_rect(cx - 10, lead_top, 4, lead_h);
|
target.fill_rect(cx - 10, lead_top, 4, lead_h);
|
||||||
target.fill_rect(cx + 6, lead_top, 4, lead_h);
|
target.fill_rect(cx + 6, lead_top, 4, lead_h);
|
||||||
@@ -3509,8 +3528,8 @@ fn draw_led(
|
|||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_arc(cx - r - 4, cy - r - 4, 2 * (r + 4), 2 * (r + 4), 0, 360 * 64);
|
target.draw_arc(cx - r - 4, cy - r - 4, 2 * (r + 4), 2 * (r + 4), 0, 360 * 64);
|
||||||
}
|
}
|
||||||
let tw = text_width(style, label);
|
let tw = text_width_cap(target, label, 28, true);
|
||||||
draw_text_at(target, style, cx - tw / 2, b.y + b.h - (6.0 * sy) as i32, label);
|
draw_text_at_cap(target, style, cx - tw / 2, b.y + b.h - (8.0 * sy) as i32, label, 28, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3535,7 +3554,7 @@ fn draw_panel_lamp(
|
|||||||
target.fill_rect(b.x, b.y, b.w, b.h);
|
target.fill_rect(b.x, b.y, b.w, b.h);
|
||||||
let cx = (b.x + b.x + b.w) / 2;
|
let cx = (b.x + b.x + b.w) / 2;
|
||||||
let cy = (b.y + b.y + b.h) / 2 - (8.0 * sy) as i32;
|
let cy = (b.y + b.y + b.h) / 2 - (8.0 * sy) as i32;
|
||||||
let r = (b.w.min(b.h) / 2 - 10).max(6);
|
let r = (b.w.min(b.h) / 2 - 30).max(6);
|
||||||
let lr = (r - 8).max(4);
|
let lr = (r - 8).max(4);
|
||||||
target.set_foreground(bezel);
|
target.set_foreground(bezel);
|
||||||
target.fill_arc(cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
|
target.fill_arc(cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
|
||||||
@@ -3559,8 +3578,8 @@ fn draw_panel_lamp(
|
|||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_arc(cx - lr - 4, cy - lr - 4, 2 * (lr + 4), 2 * (lr + 4), 0, 360 * 64);
|
target.draw_arc(cx - lr - 4, cy - lr - 4, 2 * (lr + 4), 2 * (lr + 4), 0, 360 * 64);
|
||||||
}
|
}
|
||||||
let tw = text_width(style, label);
|
let tw = text_width_cap(target, label, 17, true);
|
||||||
draw_text_at(target, style, cx - tw / 2, b.y + b.h - (8.0 * sy) as i32, label);
|
draw_text_at_cap(target, style, cx - tw / 2, b.y + b.h - (10.0 * sy) as i32, label, 17, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3588,15 +3607,15 @@ fn draw_beacon(target: &mut dyn DrawTarget, style: &Style, b: Rect, label: &str,
|
|||||||
target.set_foreground(white);
|
target.set_foreground(white);
|
||||||
target.fill_rect(b.x, b.y, b.w, b.h);
|
target.fill_rect(b.x, b.y, b.w, b.h);
|
||||||
let cx = (b.x + b.x + b.w) / 2;
|
let cx = (b.x + b.x + b.w) / 2;
|
||||||
let base_y = b.y + b.h - (44.0 * sy) as i32;
|
let base_y = b.y + b.h - (72.0 * sy) as i32;
|
||||||
let r = (b.w.min(b.h) / 3).max(10);
|
let r = (b.w.min(b.h) / 3).max(10);
|
||||||
let cy = base_y - r + (10.0 * sy) as i32;
|
let cy = base_y - r - (6.0 * sy) as i32;
|
||||||
target.set_foreground(dark);
|
target.set_foreground(dark);
|
||||||
target.fill_rect(
|
target.fill_rect(
|
||||||
cx - (70.0 * sx) as i32,
|
cx - (70.0 * sx) as i32,
|
||||||
base_y,
|
base_y,
|
||||||
(140.0 * sx) as i32,
|
(140.0 * sx) as i32,
|
||||||
(22.0 * sy) as i32,
|
(28.0 * sy) as i32,
|
||||||
);
|
);
|
||||||
target.set_foreground(amber);
|
target.set_foreground(amber);
|
||||||
target.fill_arc(cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
|
target.fill_arc(cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
|
||||||
@@ -3623,12 +3642,12 @@ fn draw_beacon(target: &mut dyn DrawTarget, style: &Style, b: Rect, label: &str,
|
|||||||
cy - r - (36.0 * sy) as i32,
|
cy - r - (36.0 * sy) as i32,
|
||||||
);
|
);
|
||||||
let rate = "FLASH 2 Hz";
|
let rate = "FLASH 2 Hz";
|
||||||
let twr = text_width(style, rate);
|
let twr = text_width_cap(target, rate, 16, true);
|
||||||
let mut rst = style.clone();
|
let mut rst = style.clone();
|
||||||
rst.text_color = white;
|
rst.text_color = white;
|
||||||
draw_text_at(target, &rst, cx - twr / 2, base_y + (17.0 * sy) as i32, rate);
|
draw_text_at_cap(target, &rst, cx - twr / 2, base_y + (21.0 * sy) as i32, rate, 16, true);
|
||||||
let tw = text_width(style, label);
|
let tw = text_width_cap(target, label, 16, true);
|
||||||
draw_text_at(target, style, cx - tw / 2, b.y + b.h - (4.0 * sy) as i32, label);
|
draw_text_at_cap(target, style, cx - tw / 2, b.y + b.h - (8.0 * sy) as i32, label, 16, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3682,27 +3701,33 @@ fn draw_stack_light(target: &mut dyn DrawTarget, style: &Style, b: Rect, label:
|
|||||||
target.fill_rect(cx - tw2 / 2, y, tw2, th);
|
target.fill_rect(cx - tw2 / 2, y, tw2, th);
|
||||||
target.set_foreground(outline);
|
target.set_foreground(outline);
|
||||||
target.draw_rect(cx - tw2 / 2, y, tw2, th);
|
target.draw_rect(cx - tw2 / 2, y, tw2, th);
|
||||||
let tw = text_width(style, lab);
|
// Audit G6: tier letter white when lit, light grey when unlit.
|
||||||
draw_text_at(
|
let mut lst = style.clone();
|
||||||
|
lst.text_color = if *on { white } else { 0xC8C8C8 };
|
||||||
|
let tw = text_width_cap(target, lab, 24, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
&lst,
|
||||||
cx - tw2 / 2 + (tw2 - tw) / 2,
|
cx - tw2 / 2 + (tw2 - tw) / 2,
|
||||||
y + th / 2 + (5.0 * sy) as i32,
|
y + th / 2 + 8,
|
||||||
lab,
|
lab,
|
||||||
|
24,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let pole_top = b.y + 8 + 2 * (th + 4) + th + 4;
|
let pole_top = b.y + 8 + 2 * (th + 4) + th + 4;
|
||||||
let pole_bot = (pole_top + 1).max(b.y + b.h - (30.0 * sy) as i32);
|
// Audit G7: base raised so the 16 px STACK title clears it.
|
||||||
|
let pole_bot = (pole_top + 1).max(b.y + b.h - (48.0 * sy) as i32);
|
||||||
target.set_foreground(dark);
|
target.set_foreground(dark);
|
||||||
target.fill_rect(cx - 4, pole_top, 8, (pole_bot - pole_top).max(1));
|
target.fill_rect(cx - 4, pole_top, 8, (pole_bot - pole_top).max(1));
|
||||||
target.fill_rect(
|
target.fill_rect(
|
||||||
cx - (50.0 * sx) as i32,
|
cx - (50.0 * sx) as i32,
|
||||||
b.y + b.h - (30.0 * sy) as i32,
|
b.y + b.h - (48.0 * sy) as i32,
|
||||||
(100.0 * sx) as i32,
|
(100.0 * sx) as i32,
|
||||||
(12.0 * sy) as i32,
|
(12.0 * sy) as i32,
|
||||||
);
|
);
|
||||||
let tw = text_width(style, label);
|
let tw = text_width_cap(target, label, 16, true);
|
||||||
draw_text_at(target, style, cx - tw / 2, b.y + b.h - (6.0 * sy) as i32, label);
|
draw_text_at_cap(target, style, cx - tw / 2, b.y + b.h - (8.0 * sy) as i32, label, 16, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3746,11 +3771,11 @@ fn draw_rgb_led(target: &mut dyn DrawTarget, style: &Style, b: Rect, label: &str
|
|||||||
let labs = ["R", "G", "B"];
|
let labs = ["R", "G", "B"];
|
||||||
let on = [red_on, green_on, blue_on];
|
let on = [red_on, green_on, blue_on];
|
||||||
let cols: [u32; 3] = [red_ch, green_ch, blue_ch];
|
let cols: [u32; 3] = [red_ch, green_ch, blue_ch];
|
||||||
let sq = (20.0 * sx) as i32;
|
let sq = (24.0 * sx) as i32;
|
||||||
let syy = b.y + b.h - (40.0 * sy) as i32;
|
let syy = b.y + b.h - (52.0 * sy) as i32;
|
||||||
let cf = 0.08 + 0.92 * (bright.clamp(0, 100) as f64 / 100.0);
|
let cf = 0.08 + 0.92 * (bright.clamp(0, 100) as f64 / 100.0);
|
||||||
for i in 0..3 {
|
for i in 0..3 {
|
||||||
let sxx = cx - (33.0 * sx) as i32 + (i as i32) * (24.0 * sx) as i32;
|
let sxx = cx - (39.0 * sx) as i32 + (i as i32) * (28.0 * sx) as i32;
|
||||||
let ch = if on[i] {
|
let ch = if on[i] {
|
||||||
let c = cols[i];
|
let c = cols[i];
|
||||||
((((c >> 16 & 0xff) as f64 * cf) as u32) << 16)
|
((((c >> 16 & 0xff) as f64 * cf) as u32) << 16)
|
||||||
@@ -3763,17 +3788,12 @@ fn draw_rgb_led(target: &mut dyn DrawTarget, style: &Style, b: Rect, label: &str
|
|||||||
target.fill_rect(sxx, syy, sq, sq);
|
target.fill_rect(sxx, syy, sq, sq);
|
||||||
target.set_foreground(outline);
|
target.set_foreground(outline);
|
||||||
target.draw_rect(sxx, syy, sq, sq);
|
target.draw_rect(sxx, syy, sq, sq);
|
||||||
let twl = text_width(style, labs[i]);
|
let mut lst = style.clone();
|
||||||
draw_text_at(
|
lst.text_color = white;
|
||||||
target,
|
draw_text_centered_cap(target, &lst, sxx, syy, sxx + sq, syy + sq, labs[i], 14, true);
|
||||||
style,
|
|
||||||
sxx + (sq - twl) / 2,
|
|
||||||
syy + (15.0 * sy) as i32,
|
|
||||||
labs[i],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
let tw = text_width(style, label);
|
let tw = text_width_cap(target, label, 16, true);
|
||||||
draw_text_at(target, style, cx - tw / 2, b.y + b.h - (6.0 * sy) as i32, label);
|
draw_text_at_cap(target, style, cx - tw / 2, b.y + b.h - (8.0 * sy) as i32, label, 16, true);
|
||||||
target.set_foreground(style.line_color);
|
target.set_foreground(style.line_color);
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
}
|
}
|
||||||
@@ -3795,7 +3815,7 @@ fn tank_mark_h(
|
|||||||
) {
|
) {
|
||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_line(x1, gy, x2, gy);
|
target.draw_line(x1, gy, x2, gy);
|
||||||
draw_text_at(target, style, lab_x, gy + 4, lab);
|
draw_text_at_cap(target, style, lab_x, gy + 9, lab, 14, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Vertical tank, C++ `InstVerticalTankLevel::draw`: pale tank with a
|
/// Vertical tank, C++ `InstVerticalTankLevel::draw`: pale tank with a
|
||||||
@@ -3839,12 +3859,14 @@ fn draw_vertical_tank(
|
|||||||
let gy = ty + th - (th * v / 100);
|
let gy = ty + th - (th * v / 100);
|
||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (18.0 * sx) as i32,
|
tx + tw + (18.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&v.to_string(),
|
&v.to_string(),
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (pct, col, lab) in [
|
for (pct, col, lab) in [
|
||||||
@@ -3866,13 +3888,15 @@ fn draw_vertical_tank(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
let pw2 = text_width(style, &pct);
|
let pw2 = text_width_cap(target, &pct, 18, false);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + (tw - pw2) / 2,
|
tx + (tw - pw2) / 2,
|
||||||
ty + th + (18.0 * sy) as i32,
|
ty + th + (26.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -3898,16 +3922,20 @@ fn draw_horizontal_tank(
|
|||||||
red: u32,
|
red: u32,
|
||||||
hover: u32,
|
hover: u32,
|
||||||
) {
|
) {
|
||||||
let sx = b.w as f64 / 720.0;
|
let _sx = b.w as f64 / 720.0;
|
||||||
let sy = b.h as f64 / 480.0;
|
let sy = b.h as f64 / 480.0;
|
||||||
target.set_foreground(white);
|
target.set_foreground(white);
|
||||||
target.fill_rect(b.x, b.y, b.w, b.h);
|
target.fill_rect(b.x, b.y, b.w, b.h);
|
||||||
let tx = b.x + (40.0 * sx) as i32;
|
|
||||||
let ty = b.y + (90.0 * sy) as i32;
|
|
||||||
let tw = (b.w as f64 - 180.0 * sx) as i32;
|
|
||||||
let th = (150.0 * sy) as i32;
|
let th = (150.0 * sy) as i32;
|
||||||
|
let tx = b.x + th / 2 + 12;
|
||||||
|
let ty = b.y + (90.0 * sy) as i32;
|
||||||
|
let tw = (b.w - th - 24).max(10);
|
||||||
|
// Pale capsule: body rect + two half-disc fills (audit G7: the
|
||||||
|
// left cap no longer sticks out past the widget edge).
|
||||||
target.set_foreground(pale);
|
target.set_foreground(pale);
|
||||||
target.fill_rect(tx, ty, tw, th);
|
target.fill_rect(tx, ty, tw, th);
|
||||||
|
target.fill_arc(tx - th / 2, ty, th, th, 90 * 64, 180 * 64);
|
||||||
|
target.fill_arc(tx + tw - th / 2, ty, th, th, 270 * 64, 180 * 64);
|
||||||
let fw = (tw as f64 * level / 100.0) as i32;
|
let fw = (tw as f64 * level / 100.0) as i32;
|
||||||
if fw > 0 {
|
if fw > 0 {
|
||||||
target.set_foreground(fill);
|
target.set_foreground(fill);
|
||||||
@@ -3923,8 +3951,8 @@ fn draw_horizontal_tank(
|
|||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(gx, ty + th + (4.0 * sy) as i32, gx, ty + th + (14.0 * sy) as i32);
|
target.draw_line(gx, ty + th + (4.0 * sy) as i32, gx, ty + th + (14.0 * sy) as i32);
|
||||||
let lab = v.to_string();
|
let lab = v.to_string();
|
||||||
let lw = text_width(style, &lab);
|
let lw = text_width_cap(target, &lab, 16, false);
|
||||||
draw_text_at(target, style, gx - lw / 2, ty + th + (28.0 * sy) as i32, &lab);
|
draw_text_at_cap(target, style, gx - lw / 2, ty + th + (32.0 * sy) as i32, &lab, 16, false);
|
||||||
}
|
}
|
||||||
for (pct, col, lab) in [
|
for (pct, col, lab) in [
|
||||||
(95.0, red, "HI-HI"),
|
(95.0, red, "HI-HI"),
|
||||||
@@ -3935,15 +3963,18 @@ fn draw_horizontal_tank(
|
|||||||
let gx = tx + (tw as f64 * pct / 100.0) as i32;
|
let gx = tx + (tw as f64 * pct / 100.0) as i32;
|
||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_line(gx, ty, gx, ty + th);
|
target.draw_line(gx, ty, gx, ty + th);
|
||||||
draw_text_at(target, style, gx + 4, ty - (8.0 * sy) as i32, lab);
|
draw_text_at_cap(target, style, gx + 4, ty - (10.0 * sy) as i32, lab, 14, true);
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
draw_text_at(
|
let pw = text_width_cap(target, &pct, 18, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (12.0 * sx) as i32,
|
tx + (tw - pw) / 2,
|
||||||
ty + th / 2 + 4,
|
ty + th + (58.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4002,12 +4033,14 @@ fn draw_spherical_tank(
|
|||||||
let gy = cy + r - (2.0 * r as f64 * v as f64 / 100.0) as i32;
|
let gy = cy + r - (2.0 * r as f64 * v as f64 / 100.0) as i32;
|
||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(cx + r + (4.0 * sx) as i32, gy, cx + r + (14.0 * sx) as i32, gy);
|
target.draw_line(cx + r + (4.0 * sx) as i32, gy, cx + r + (14.0 * sx) as i32, gy);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx + r + (18.0 * sx) as i32,
|
cx + r + (18.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&v.to_string(),
|
&v.to_string(),
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (pct, col, lab) in [
|
for (pct, col, lab) in [
|
||||||
@@ -4017,16 +4050,20 @@ fn draw_spherical_tank(
|
|||||||
(5.0, red, "LO-LO"),
|
(5.0, red, "LO-LO"),
|
||||||
] {
|
] {
|
||||||
let gy = cy + r - (2.0 * r as f64 * pct / 100.0) as i32;
|
let gy = cy + r - (2.0 * r as f64 * pct / 100.0) as i32;
|
||||||
tank_mark_h(target, style, cx - r, cx + r, gy, col, lab, cx - r - (52.0 * sx) as i32);
|
target.set_foreground(col);
|
||||||
|
target.draw_line(cx - r, gy, cx + r, gy);
|
||||||
|
draw_text_right_cap(target, style, cx - r - (8.0 * sx) as i32, gy + 9, lab, 14, true);
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
let pw2 = text_width(style, &pct);
|
let pw2 = text_width_cap(target, &pct, 18, false);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
cx - pw2 / 2,
|
cx - pw2 / 2,
|
||||||
cy - r - (16.0 * sy) as i32,
|
cy - r - (18.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4080,12 +4117,14 @@ fn draw_level_column(
|
|||||||
gy,
|
gy,
|
||||||
);
|
);
|
||||||
if v % 25 == 0 {
|
if v % 25 == 0 {
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (24.0 * sx) as i32,
|
tx + tw + (24.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&v.to_string(),
|
&v.to_string(),
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4098,7 +4137,7 @@ fn draw_level_column(
|
|||||||
let gy = ty + th - (th as f64 * pct / 100.0) as i32;
|
let gy = ty + th - (th as f64 * pct / 100.0) as i32;
|
||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_line(tx - (6.0 * sx) as i32, gy, tx, gy);
|
target.draw_line(tx - (6.0 * sx) as i32, gy, tx, gy);
|
||||||
draw_text_at(target, style, tx - (52.0 * sx) as i32, gy + 4, lab);
|
draw_text_right_cap(target, style, tx - (10.0 * sx) as i32, gy + 9, lab, 14, true);
|
||||||
}
|
}
|
||||||
let fy = ty + th - (th as f64 * level / 100.0) as i32;
|
let fy = ty + th - (th as f64 * level / 100.0) as i32;
|
||||||
target.set_foreground(red);
|
target.set_foreground(red);
|
||||||
@@ -4110,13 +4149,15 @@ fn draw_level_column(
|
|||||||
(10.0 * sy) as i32,
|
(10.0 * sy) as i32,
|
||||||
);
|
);
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
let pw2 = text_width(style, &pct);
|
let pw2 = text_width_cap(target, &pct, 18, false);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + (tw - pw2) / 2,
|
tx + (tw - pw2) / 2,
|
||||||
ty + th + (18.0 * sy) as i32,
|
ty + th + (26.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4149,7 +4190,7 @@ fn draw_sight_glass(
|
|||||||
target.fill_rect(b.x, b.y, b.w, b.h);
|
target.fill_rect(b.x, b.y, b.w, b.h);
|
||||||
let tx = b.x + (120.0 * sx) as i32;
|
let tx = b.x + (120.0 * sx) as i32;
|
||||||
let ty = b.y + (40.0 * sy) as i32;
|
let ty = b.y + (40.0 * sy) as i32;
|
||||||
let tw = (40.0 * sx) as i32;
|
let tw = (64.0 * sx) as i32;
|
||||||
let th = (360.0 * sy) as i32;
|
let th = (360.0 * sy) as i32;
|
||||||
target.set_foreground(steel);
|
target.set_foreground(steel);
|
||||||
target.fill_rect(
|
target.fill_rect(
|
||||||
@@ -4187,8 +4228,7 @@ fn draw_sight_glass(
|
|||||||
);
|
);
|
||||||
if v % 25 == 0 {
|
if v % 25 == 0 {
|
||||||
let lab = v.to_string();
|
let lab = v.to_string();
|
||||||
let lw = text_width(style, &lab);
|
draw_text_right_cap(target, style, tx - (8.0 * sx) as i32, gy + 9, &lab, 16, false);
|
||||||
draw_text_at(target, style, tx - (52.0 * sx) as i32 - lw + 24, gy + 4, &lab);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (pct, col, lab) in [
|
for (pct, col, lab) in [
|
||||||
@@ -4200,15 +4240,17 @@ fn draw_sight_glass(
|
|||||||
let gy = ty + th - (th as f64 * pct / 100.0) as i32;
|
let gy = ty + th - (th as f64 * pct / 100.0) as i32;
|
||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_line(tx, gy, tx + tw, gy);
|
target.draw_line(tx, gy, tx + tw, gy);
|
||||||
draw_text_at(target, style, tx + tw + (10.0 * sx) as i32, gy + 4, lab);
|
draw_text_at_cap(target, style, tx + tw + (10.0 * sx) as i32, gy + 9, lab, 14, true);
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (10.0 * sx) as i32,
|
tx + tw + (10.0 * sx) as i32,
|
||||||
ty + th + (34.0 * sy) as i32,
|
ty + th + (40.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4255,12 +4297,14 @@ fn draw_level_transmitter(
|
|||||||
let gy = ty + th - (th * v / 100);
|
let gy = ty + th - (th * v / 100);
|
||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (18.0 * sx) as i32,
|
tx + tw + (18.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&v.to_string(),
|
&v.to_string(),
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (pct, col, lab) in [
|
for (pct, col, lab) in [
|
||||||
@@ -4283,23 +4327,23 @@ fn draw_level_transmitter(
|
|||||||
}
|
}
|
||||||
let rx = tx + tw + (120.0 * sx) as i32;
|
let rx = tx + tw + (120.0 * sx) as i32;
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
draw_text_at(target, style, rx, ty + (40.0 * sy) as i32, &pct);
|
draw_text_at_cap(target, style, rx, ty + (44.0 * sy) as i32, &pct, 16, true);
|
||||||
// The 4-20 mA loop current tracks the level linearly.
|
// The 4-20 mA loop current tracks the level linearly.
|
||||||
let ma = format!("{} mA", inst_fmt(4.0 + level / 100.0 * 16.0, 1));
|
let ma = format!("{} mA", inst_fmt(4.0 + level / 100.0 * 16.0, 1));
|
||||||
draw_text_at(target, style, rx, ty + (60.0 * sy) as i32, &ma);
|
draw_text_at_cap(target, style, rx, ty + (68.0 * sy) as i32, &ma, 16, true);
|
||||||
draw_text_at(target, style, rx, ty + (80.0 * sy) as i32, "4-20 mA loop");
|
draw_text_at_cap(target, style, rx, ty + (92.0 * sy) as i32, "4-20 mA loop", 16, true);
|
||||||
target.set_foreground(ink);
|
target.set_foreground(ink);
|
||||||
target.draw_arc(rx, ty + (100.0 * sy) as i32, (16.0 * sx) as i32, (16.0 * sy) as i32, 0, 360 * 64);
|
target.draw_arc(rx, ty + (104.0 * sy) as i32, (24.0 * sx) as i32, (24.0 * sy) as i32, 0, 360 * 64);
|
||||||
target.draw_arc(
|
target.draw_arc(
|
||||||
rx + (26.0 * sx) as i32,
|
rx + (32.0 * sx) as i32,
|
||||||
ty + (100.0 * sy) as i32,
|
ty + (104.0 * sy) as i32,
|
||||||
(16.0 * sx) as i32,
|
(24.0 * sx) as i32,
|
||||||
(16.0 * sy) as i32,
|
(24.0 * sy) as i32,
|
||||||
0,
|
0,
|
||||||
360 * 64,
|
360 * 64,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, rx + 5, ty + (112.0 * sy) as i32, "+");
|
draw_text_centered_cap(target, style, rx, ty + (104.0 * sy) as i32, rx + (24.0 * sx) as i32, ty + (128.0 * sy) as i32, "+", 16, true);
|
||||||
draw_text_at(target, style, rx + (31.0 * sx) as i32, ty + (112.0 * sy) as i32, "-");
|
draw_text_centered_cap(target, style, rx + (32.0 * sx) as i32, ty + (104.0 * sy) as i32, rx + (56.0 * sx) as i32, ty + (128.0 * sy) as i32, "-", 16, true);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
target.draw_rect(b.x, b.y, b.w, b.h);
|
target.draw_rect(b.x, b.y, b.w, b.h);
|
||||||
@@ -4366,24 +4410,20 @@ fn draw_open_channel(
|
|||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(tx - (14.0 * sx) as i32, gy, tx - (4.0 * sx) as i32, gy);
|
target.draw_line(tx - (14.0 * sx) as i32, gy, tx - (4.0 * sx) as i32, gy);
|
||||||
let lab = v.to_string();
|
let lab = v.to_string();
|
||||||
let lw = text_width(style, &lab);
|
draw_text_right_cap(target, style, tx - (18.0 * sx) as i32, gy + 9, &lab, 16, false);
|
||||||
draw_text_at(target, style, tx - (18.0 * sx) as i32 - lw, gy + 4, &lab);
|
|
||||||
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
||||||
draw_text_at(
|
draw_text_at_cap(target, style, tx + tw + (18.0 * sx) as i32, gy + 9, &lab, 16, false);
|
||||||
target,
|
|
||||||
style,
|
|
||||||
tx + tw + (18.0 * sx) as i32,
|
|
||||||
gy + 4,
|
|
||||||
&lab,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}% depth", inst_fmt(level, 0));
|
||||||
draw_text_at(
|
let pw = text_width_cap(target, &pct, 18, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (18.0 * sx) as i32,
|
tx + (tw - pw) / 2,
|
||||||
ty + 4,
|
ty + th + (30.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4430,12 +4470,14 @@ fn draw_tank_alarms(
|
|||||||
let gy = ty + th - (th * v / 100);
|
let gy = ty + th - (th * v / 100);
|
||||||
target.set_foreground(grid);
|
target.set_foreground(grid);
|
||||||
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
target.draw_line(tx + tw + 4, gy, tx + tw + (14.0 * sx) as i32, gy);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + tw + (18.0 * sx) as i32,
|
tx + tw + (18.0 * sx) as i32,
|
||||||
gy + 4,
|
gy + 9,
|
||||||
&v.to_string(),
|
&v.to_string(),
|
||||||
|
16,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
for (pct, col, lab, active) in [
|
for (pct, col, lab, active) in [
|
||||||
@@ -4448,22 +4490,24 @@ fn draw_tank_alarms(
|
|||||||
target.set_foreground(col);
|
target.set_foreground(col);
|
||||||
target.draw_line(tx, gy, tx + tw, gy);
|
target.draw_line(tx, gy, tx + tw, gy);
|
||||||
let lx = tx + tw + (64.0 * sx) as i32;
|
let lx = tx + tw + (64.0 * sx) as i32;
|
||||||
let ls = (14.0 * sx) as i32;
|
let ls = (16.0 * sx) as i32;
|
||||||
if active {
|
if active {
|
||||||
target.fill_arc(lx, gy - (7.0 * sy) as i32, ls, ls, 0, 360 * 64);
|
target.fill_arc(lx, gy - (7.0 * sy) as i32, ls, ls, 0, 360 * 64);
|
||||||
} else {
|
} else {
|
||||||
target.draw_arc(lx, gy - (7.0 * sy) as i32, ls, ls, 0, 360 * 64);
|
target.draw_arc(lx, gy - (7.0 * sy) as i32, ls, ls, 0, 360 * 64);
|
||||||
}
|
}
|
||||||
draw_text_at(target, style, lx + (20.0 * sx) as i32, gy + 4, lab);
|
draw_text_at_cap(target, style, lx + (20.0 * sx) as i32, gy + 9, lab, 14, true);
|
||||||
}
|
}
|
||||||
let pct = format!("{}%", inst_fmt(level, 0));
|
let pct = format!("{}%", inst_fmt(level, 0));
|
||||||
let pw2 = text_width(style, &pct);
|
let pw2 = text_width_cap(target, &pct, 18, false);
|
||||||
draw_text_at(
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
tx + (tw - pw2) / 2,
|
tx + (tw - pw2) / 2,
|
||||||
ty + th + (18.0 * sy) as i32,
|
ty + th + (26.0 * sy) as i32,
|
||||||
&pct,
|
&pct,
|
||||||
|
18,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
draw_text_at(target, style, b.x + 8, b.y + (16.0 * sy) as i32, label);
|
||||||
target.set_foreground(if hovered { hover } else { style.line_color });
|
target.set_foreground(if hovered { hover } else { style.line_color });
|
||||||
@@ -4546,13 +4590,19 @@ fn draw_needle_vu(
|
|||||||
} else {
|
} else {
|
||||||
format!("{}", KDB[i] as i32)
|
format!("{}", KDB[i] as i32)
|
||||||
};
|
};
|
||||||
let tw = text_width(style, &lab);
|
let mut nst = style.clone();
|
||||||
draw_text_at(
|
if KDB[i] >= 0.0 {
|
||||||
|
nst.text_color = red;
|
||||||
|
}
|
||||||
|
let tw = text_width_cap(target, &lab, 17, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
&nst,
|
||||||
px + ((r as f64 - 26.0 * s) * sa) as i32 - tw / 2,
|
px + ((r as f64 - 38.0 * s) * sa) as i32 - tw / 2,
|
||||||
py + ((r as f64 - 26.0 * s) * -ca) as i32 + 4,
|
py + ((r as f64 - 38.0 * s) * -ca) as i32 + 9,
|
||||||
&lab,
|
&lab,
|
||||||
|
17,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let frac = vu_fraction(value);
|
let frac = vu_fraction(value);
|
||||||
@@ -4569,16 +4619,11 @@ fn draw_needle_vu(
|
|||||||
target.set_foreground(bezel);
|
target.set_foreground(bezel);
|
||||||
let hd = ((14.0 * s).round() as i32).max(3);
|
let hd = ((14.0 * s).round() as i32).max(3);
|
||||||
target.fill_arc(px - hd / 2, py - hd / 2, hd, hd, 0, 360 * 64);
|
target.fill_arc(px - hd / 2, py - hd / 2, hd, hd, 0, 360 * 64);
|
||||||
let tw = text_width(style, "VU");
|
let tw = text_width_cap(target, "VU", 20, true);
|
||||||
draw_text_at(target, style, px - tw / 2, py - r + (34.0 * s) as i32, "VU");
|
draw_text_at_cap(target, style, px - tw / 2, py - r + (96.0 * s) as i32, "VU", 20, true);
|
||||||
let tw2 = text_width(style, label);
|
// Audit G2: the class-name caption is removed from the scale area
|
||||||
draw_text_at(
|
// (the `label` draw is deleted outright, as in C++).
|
||||||
target,
|
let _ = label;
|
||||||
style,
|
|
||||||
px - tw2 / 2,
|
|
||||||
py - r + (50.0 * s) as i32,
|
|
||||||
label,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generic pivot needle gauge for Edgewise (-50..+50 deg),
|
/// Generic pivot needle gauge for Edgewise (-50..+50 deg),
|
||||||
@@ -4610,9 +4655,21 @@ fn draw_needle_generic(
|
|||||||
target.fill_rect(b.x + 6, b.y + 6, b.w - 12, b.h - 12);
|
target.fill_rect(b.x + 6, b.y + 6, b.w - 12, b.h - 12);
|
||||||
target.set_foreground(bezel);
|
target.set_foreground(bezel);
|
||||||
target.draw_rect(b.x + 6, b.y + 6, b.w - 13, b.h - 13);
|
target.draw_rect(b.x + 6, b.y + 6, b.w - 13, b.h - 13);
|
||||||
let px = b.x + b.w / 2;
|
// Audit: KnifeEdge (240 deg) and Tapered (270 deg) cannot use the
|
||||||
let py = b.y + b.h - 14;
|
// bottom pivot — their +/-120 deg / +/-135 deg numerals fall below
|
||||||
let r = ((b.w.min(b.h) as f64 * 0.72) as i32).max(10);
|
// the widget. Like C++ they use a centre pivot, r = min(w,h)/2 - 14.
|
||||||
|
// Edgewise (100 deg) keeps the bottom pivot.
|
||||||
|
let (px, py, r) = if sweep > 180.0 {
|
||||||
|
let cx = b.x + b.w / 2;
|
||||||
|
let cy = b.y + b.h / 2 + 6;
|
||||||
|
(cx, cy, ((b.w.min(b.h) / 2) - 14).max(10))
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
b.x + b.w / 2,
|
||||||
|
b.y + b.h - 14,
|
||||||
|
((b.w.min(b.h) as f64 * 0.72) as i32).max(10),
|
||||||
|
)
|
||||||
|
};
|
||||||
let s = r as f64 / 273.0;
|
let s = r as f64 / 273.0;
|
||||||
let frac = if max > min {
|
let frac = if max > min {
|
||||||
((value - min) / (max - min)).clamp(0.0, 1.0)
|
((value - min) / (max - min)).clamp(0.0, 1.0)
|
||||||
@@ -4644,13 +4701,17 @@ fn draw_needle_generic(
|
|||||||
target.set_foreground(if f >= 0.99 { red } else { tick });
|
target.set_foreground(if f >= 0.99 { red } else { tick });
|
||||||
target.draw_line(tx0, ty0, tx1, ty1);
|
target.draw_line(tx0, ty0, tx1, ty1);
|
||||||
let lab = inst_fmt(min + f * (max - min), 0);
|
let lab = inst_fmt(min + f * (max - min), 0);
|
||||||
let tw = text_width(style, &lab);
|
let num_cap = (r / 7).max(14);
|
||||||
draw_text_at(
|
let num_asc = num_cap * 9 / 8;
|
||||||
|
let tw = text_width_cap(target, &lab, num_cap, false);
|
||||||
|
draw_text_at_cap(
|
||||||
target,
|
target,
|
||||||
style,
|
style,
|
||||||
px + ((r as f64 - 26.0 * s) * sa) as i32 - tw / 2,
|
px + ((r as f64 - num_cap as f64 - 12.0 * s) * sa) as i32 - tw / 2,
|
||||||
py + ((r as f64 - 26.0 * s) * -ca) as i32 + 4,
|
py + ((r as f64 - num_cap as f64 - 12.0 * s) * -ca) as i32 + num_asc / 2,
|
||||||
&lab,
|
&lab,
|
||||||
|
num_cap,
|
||||||
|
false,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let a = (-half + sweep * frac).to_radians();
|
let a = (-half + sweep * frac).to_radians();
|
||||||
@@ -4666,14 +4727,10 @@ fn draw_needle_generic(
|
|||||||
target.set_foreground(bezel);
|
target.set_foreground(bezel);
|
||||||
let hd = ((14.0 * s).round() as i32).max(3);
|
let hd = ((14.0 * s).round() as i32).max(3);
|
||||||
target.fill_arc(px - hd / 2, py - hd / 2, hd, hd, 0, 360 * 64);
|
target.fill_arc(px - hd / 2, py - hd / 2, hd, hd, 0, 360 * 64);
|
||||||
let tw2 = text_width(style, label);
|
if !label.is_empty() {
|
||||||
draw_text_at(
|
let tw2 = text_width_cap(target, label, 16, true);
|
||||||
target,
|
draw_text_at_cap(target, style, px - tw2 / 2, py - r + (34.0 * s) as i32, label, 16, true);
|
||||||
style,
|
}
|
||||||
px - tw2 / 2,
|
|
||||||
py - r + (34.0 * s) as i32,
|
|
||||||
label,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Instrumentation widget (gauge, meter, lamp, valve, ...).
|
/// Instrumentation widget (gauge, meter, lamp, valve, ...).
|
||||||
@@ -4941,13 +4998,13 @@ impl InstrumentWidget {
|
|||||||
seg_bezel_color: Srgb::new(58, 58, 62),
|
seg_bezel_color: Srgb::new(58, 58, 62),
|
||||||
seg_label_color: Srgb::new(170, 170, 170),
|
seg_label_color: Srgb::new(170, 170, 170),
|
||||||
seg7_lit_color: Srgb::new(255, 60, 35),
|
seg7_lit_color: Srgb::new(255, 60, 35),
|
||||||
seg7_dim_color: Srgb::new(52, 14, 11),
|
seg7_dim_color: Srgb::new(24, 15, 13),
|
||||||
seg14_lit_color: Srgb::new(255, 176, 0),
|
seg14_lit_color: Srgb::new(255, 176, 0),
|
||||||
seg14_dim_color: Srgb::new(60, 42, 8),
|
seg14_dim_color: Srgb::new(26, 20, 10),
|
||||||
seg16_lit_color: Srgb::new(255, 176, 0),
|
seg16_lit_color: Srgb::new(255, 176, 0),
|
||||||
seg16_dim_color: Srgb::new(60, 42, 8),
|
seg16_dim_color: Srgb::new(26, 20, 10),
|
||||||
segment_on_color: Srgb::new(255, 176, 0),
|
segment_on_color: Srgb::new(255, 176, 0),
|
||||||
segment_off_color: Srgb::new(50, 45, 20),
|
segment_off_color: Srgb::new(56, 48, 20),
|
||||||
led_color,
|
led_color,
|
||||||
lamp_color,
|
lamp_color,
|
||||||
stack_red_color: Srgb::new(220, 30, 30),
|
stack_red_color: Srgb::new(220, 30, 30),
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -132,11 +132,11 @@ fn instrument_defaults_follow_cpp() {
|
|||||||
// Segment displays (C++ InstLedDigits1 / InstSeg14Display1).
|
// Segment displays (C++ InstLedDigits1 / InstSeg14Display1).
|
||||||
let d1 = InstrumentWidget::new("InstLedDigits1", "LedDigits1", 0, 0, 200, 90);
|
let d1 = InstrumentWidget::new("InstLedDigits1", "LedDigits1", 0, 0, 200, 90);
|
||||||
assert_eq!(d1.get_seg7_lit_color(), Srgb::new(255, 60, 35));
|
assert_eq!(d1.get_seg7_lit_color(), Srgb::new(255, 60, 35));
|
||||||
assert_eq!(d1.get_seg7_dim_color(), Srgb::new(52, 14, 11));
|
assert_eq!(d1.get_seg7_dim_color(), Srgb::new(24, 15, 13)); // audit G5 Dec 2026: ghost segments dimmed
|
||||||
assert_eq!(d1.get_seg_bg_color(), Srgb::new(12, 12, 14));
|
assert_eq!(d1.get_seg_bg_color(), Srgb::new(12, 12, 14));
|
||||||
let s14 = InstrumentWidget::new("InstSeg14Display1", "Seg14Display1", 0, 0, 200, 90);
|
let s14 = InstrumentWidget::new("InstSeg14Display1", "Seg14Display1", 0, 0, 200, 90);
|
||||||
assert_eq!(s14.get_seg14_lit_color(), Srgb::new(255, 176, 0));
|
assert_eq!(s14.get_seg14_lit_color(), Srgb::new(255, 176, 0));
|
||||||
assert_eq!(s14.get_seg14_dim_color(), Srgb::new(60, 42, 8));
|
assert_eq!(s14.get_seg14_dim_color(), Srgb::new(26, 20, 10)); // audit G5 Dec 2026: ghost segments dimmed
|
||||||
// LED / lamp per-kind colours + on state.
|
// LED / lamp per-kind colours + on state.
|
||||||
let led = InstrumentWidget::new("InstLedRed", "LedRed", 0, 0, 200, 90);
|
let led = InstrumentWidget::new("InstLedRed", "LedRed", 0, 0, 200, 90);
|
||||||
assert_eq!(led.get_led_color(), Srgb::new(220, 30, 30));
|
assert_eq!(led.get_led_color(), Srgb::new(220, 30, 30));
|
||||||
|
|||||||
Reference in new issue
Block a user