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