From 0bdc9e2da8492e6a01f99796a301da2f73bb1bfa Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Mon, 30 Nov 2026 06:23:37 +0000 Subject: [PATCH] Phase 1 R3: full Rust designer live-apply + per-widget fonts refresh_item_style now reads the font-name/size/bold/italic props and picks the first loadable XLFD from the candidate chain (probed here, font id cached in a new CanvasBackend font_ids map). redraw_canvas installs each item's style font into the GC before drawing it (resolve_font fills metrics on first use; the cached font id is rebound on later redraws) -- this also fixes the pre-existing bug where a widget with a custom background/border drew no label text at all. apply_prop gains enabled (alias sensitive), items, lower/upper (via set_range), page-increment, page-size, step-increment, password, show-password, direction, cursor-visible, selection-mode (3 -> multi-select), font-* (via refresh_item_style), constraint clamping and AutoSize (apply_autosize: longest line + 16, (ascent+descent)*lines + 8, clamped to constraints and 24x16). resize_item clamps to the min/max constraints. --- crates/lightgui-designer/src/canvas.rs | 322 ++++++++++++++++++++++++- 1 file changed, 314 insertions(+), 8 deletions(-) diff --git a/crates/lightgui-designer/src/canvas.rs b/crates/lightgui-designer/src/canvas.rs index 722b84d..a041125 100644 --- a/crates/lightgui-designer/src/canvas.rs +++ b/crates/lightgui-designer/src/canvas.rs @@ -26,6 +26,7 @@ use gtk::prelude::*; use lightgui_core::model::{Project, Widget as ModelWidget}; use lightgui_core::{common_props, find_prop, props_for}; use std::collections::BTreeMap; +use std::collections::HashMap; use std::rc::Rc; use std::time::Instant; use x11rb::connection::Connection; @@ -61,6 +62,10 @@ pub struct CanvasBackend { pub sel_gc: u32, pub font: u32, pub styles: StyleManager, + /// Open X font ids by XLFD name, so a per-widget font is opened + /// once (at style refresh) and rebound into the GC on every redraw + /// without reopening it. + pub font_ids: HashMap, /// Registry index of the widget the palette selected for placement /// (-1 = none). In-process now — no selection file. pub sel_type: i32, @@ -123,6 +128,7 @@ pub fn canvas_backend() -> CanvasBackend { sel_gc: 0, font: 0, styles, + font_ids: HashMap::new(), sel_type: -1, sel_name: String::new(), bg_rgb: (255, 255, 255), @@ -145,6 +151,116 @@ pub fn canvas_backend() -> CanvasBackend { } } +/// XLFD candidate chain for a per-widget font (mirror of the C++ +/// `font_xlfd_for` chain): family/size/weight/slant, oblique fallback +/// for italic, style dropped, then `fixed` at the requested size. +/// A full XLFD in `family` is used verbatim first. +fn font_candidates(family: &str, size: i32, bold: bool, italic: bool) -> Vec { + let fam = family.trim().to_ascii_lowercase(); + let weight = if bold { "bold" } else { "medium" }; + let slant = if italic { "i" } else { "r" }; + let mut out = Vec::new(); + let base: &str = if fam.starts_with('-') { + out.push(family.trim().to_string()); + "fixed" + } else if fam.is_empty() { + "fixed" + } else { + fam.as_str() + }; + out.push(format!("-*-{base}-{weight}-{slant}-normal--{size}-*")); + if italic { + out.push(format!("-*-{base}-{weight}-o-normal--{size}-*")); + } + if base != "fixed" { + out.push(format!("-*-{base}-medium-r-normal--{size}-*")); + out.push(format!("-*-fixed-{weight}-{slant}-normal--{size}-*")); + if italic { + out.push(format!("-*-fixed-{weight}-o-normal--{size}-*")); + } + } + out.push(format!("-*-fixed-medium-r-normal--{size}-*")); + out +} + +/// Clamp a size to an item's min/max constraint props (0 = unset) and +/// the 24x16 core minimums. +fn clamp_size(item: &Placed, w: i32, h: i32) -> (i32, i32) { + let p = |name: &str| -> i32 { + item.model + .props + .get(name) + .and_then(|v| v.parse::().ok()) + .unwrap_or(0) + }; + let (mut w, mut h) = ( + w.max(p("min-width")).max(MIN_W), + h.max(p("min-height")).max(MIN_H), + ); + if p("max-width") > 0 { + w = w.min(p("max-width")); + } + if p("max-height") > 0 { + h = h.min(p("max-height")); + } + (w, h) +} + +/// Clamp an item's current size to its constraints (called when a +/// min/max prop changes). Redraws only when the size actually changes. +fn clamp_item_to_constraints(app: &Shared, id: u64) { + let mut a = app.borrow_mut(); + let Some(i) = a.find_index(id) else { + return; + }; + let (w, h) = clamp_size(&a.items[i], a.items[i].model.width, a.items[i].model.height); + if w != a.items[i].model.width || h != a.items[i].model.height { + let (x, y) = (a.items[i].model.x, a.items[i].model.y); + a.items[i].model.width = w; + a.items[i].model.height = h; + a.items[i].wdg.set_bounds(x, y, w, h); + write_state(&a); + drop(a); + redraw_canvas(app); + } +} + +/// Resize a widget to fit its caption/text (Phase 1 AutoSize): width = +/// longest line + 16, height = (ascent+descent) * lines + 8, clamped +/// to the constraints and the 24x16 core minimums. No-op when the +/// widget has no text. +fn apply_autosize(app: &Shared, id: u64) { + let mut a = app.borrow_mut(); + let Some(i) = a.find_index(id) else { + return; + }; + let text = a.items[i].wdg.text().to_string(); + if text.is_empty() { + return; + } + let style_name = a.items[i].style_name.clone(); + let style = a.backend.styles.resolve(&style_name); + let lines: Vec<&str> = text.split('\n').collect(); + let (ascent, descent) = match style.font_metrics { + Some(m) => (m.ascent, m.descent), + None => (11, 3), + }; + let mut w = 0i32; + for line in &lines { + w = w.max(style.text_width(line)); + } + w += 16; + let h = (ascent + descent) * lines.len() as i32 + 8; + let (w, h) = clamp_size(&a.items[i], w, h); + let (x, y) = (a.items[i].model.x, a.items[i].model.y); + a.items[i].model.width = w; + a.items[i].model.height = h; + a.items[i].wdg.set_bounds(x, y, w, h); + write_state(&a); + drop(a); + redraw_canvas(app); +} + // ------------------------------------------------------------ colours /// Parse a designer colour: `"#rrggbb"`, `"#rgb"`, `"rgb(r,g,b)"`, or a @@ -476,11 +592,38 @@ pub fn redraw_canvas(app: &Shared) { { let mut target = CtxTarget::new(&ctx); let a = &mut *a; - let (items, styles) = (&a.items, &mut a.backend.styles); + let (items, backend) = (&a.items, &mut a.backend); + let styles = &mut backend.styles; for p in items.iter() { + if !p.style_name.is_empty() { + // Resolve the per-widget style's font (fills its metrics + // on first use and binds the GC font) and rebind it on + // later redraws, when resolve_font returns early on the + // cached metrics. Without this a widget with a custom + // background/border draws no label text at all. + let font_name = styles.resolve(&p.style_name).font_name; + styles.resolve_font(&p.style_name, &mut target); + if !font_name.is_empty() { + let fid = match backend.font_ids.get(&font_name) { + Some(fid) => Some(*fid), + None => match x11.open_font(&[font_name.as_str(), "fixed", "helvetica"]) { + Ok(fid) => { + backend.font_ids.insert(font_name.clone(), fid); + Some(fid) + } + Err(_) => None, + }, + }; + if let Some(fid) = fid { + let _ = x11.conn.change_gc(gc, &ChangeGCAux::new().font(fid)); + } + } + } p.wdg.draw(&mut target, styles); } } + // Restore the default canvas font for the selection outline. + ctx.bind_font(); if let Some(sel) = a.selected { if let Some(p) = a.items.iter().find(|it| it.id == sel) { let (px, py, pw, ph) = (p.model.x, p.model.y, p.model.width, p.model.height); @@ -1040,6 +1183,7 @@ pub fn resize_item(app: &Shared, id: u64, w: i32, h: i32) { let Some(i) = a.find_index(id) else { return; }; + let (w, h) = clamp_size(&a.items[i], w, h); let (x, y) = (a.items[i].model.x, a.items[i].model.y); a.items[i].model.width = w; a.items[i].model.height = h; @@ -1090,7 +1234,7 @@ pub fn delete_item(app: &Shared, id: u64) { /// colour as its line colour, radius and linewidth from the border /// props. Called on every background/foreground/border change. fn refresh_item_style(app: &Shared, id: u64) { - let (bg, fg, bw, bc, br) = { + let (bg, fg, bw, bc, br, font_name, font_size, font_bold, font_italic) = { let a = app.borrow(); let Some(i) = a.find_index(id) else { return; @@ -1109,6 +1253,10 @@ fn refresh_item_style(app: &Shared, id: u64) { get("border-width"), get("border-color"), get("border-radius"), + get("font-name"), + get("font-size").parse::().unwrap_or(13), + get("font-bold") == "true", + get("font-italic") == "true", ) }; let has_bg = parse_color(&bg).is_some(); @@ -1116,7 +1264,9 @@ fn refresh_item_style(app: &Shared, id: u64) { let has_bc = parse_color(&bc).is_some(); let bw_i: i32 = bw.parse().unwrap_or(0); let br_i: i32 = br.parse().unwrap_or(0); - let needed = has_bg || has_fg || bw_i > 0 || br_i > 0 || has_bc; + let font_custom = + !font_name.is_empty() || font_size != 13 || font_bold || font_italic; + let needed = has_bg || has_fg || bw_i > 0 || br_i > 0 || has_bc || font_custom; let mut a = app.borrow_mut(); let Some(i) = a.find_index(id) else { return; @@ -1156,6 +1306,24 @@ fn refresh_item_style(app: &Shared, id: u64) { } let radius = if br_i > 0 { br_i } else { 6 }; let linewidth = if bw_i > 0 { bw_i } else { 1 }; + // Pick the first loadable XLFD from the candidate chain (probing + // here, at refresh time) and cache its font id for redraws. + let font_xlfd = { + let cands = font_candidates(&font_name, font_size, font_bold, font_italic); + let mut picked = FONT_DEFAULT.to_string(); + if let Some(x11) = a.backend.x11.clone() { + for c in &cands { + if let Ok(fid) = x11.open_font(&[c.as_str()]) { + a.backend.font_ids.insert(c.clone(), fid); + picked = c.clone(); + break; + } + } + } else if let Some(first) = cands.into_iter().next() { + picked = first; + } + picked + }; a.backend.styles.delete_style(&style_name); a.backend.styles.add_style_ex( &style_name, @@ -1165,7 +1333,7 @@ fn refresh_item_style(app: &Shared, id: u64) { Srgb::new(tr, tg, tb), radius, linewidth, - FONT_DEFAULT, + &font_xlfd, ); a.items[i].wdg.set_style_name(&style_name); a.items[i].style_name = style_name; @@ -1185,8 +1353,21 @@ pub fn apply_prop(app: &Shared, id: u64, name: &str, value: &str) { || name == "border-width" || name == "border-color" || name == "border-radius" + || name == "font-name" + || name == "font-size" + || name == "font-bold" + || name == "font-italic" { refresh_item_style(app, id); + let autosize = { + let a = app.borrow(); + a.find_index(id).is_some_and(|i| { + a.items[i].model.props.get("autosize").map(|v| v.as_str()) == Some("true") + }) + }; + if autosize { + apply_autosize(app, id); + } return; } let (wtype, mname) = { @@ -1212,7 +1393,17 @@ pub fn apply_prop(app: &Shared, id: u64, name: &str, value: &str) { a.items[i].wdg.set_text(text); } drop(a); - redraw_canvas(app); + let autosize = { + let a = app.borrow(); + a.find_index(id).is_some_and(|i| { + a.items[i].model.props.get("autosize").map(|v| v.as_str()) == Some("true") + }) + }; + if autosize { + apply_autosize(app, id); + } else { + redraw_canvas(app); + } return; } if name == "value" { @@ -1243,7 +1434,7 @@ pub fn apply_prop(app: &Shared, id: u64, name: &str, value: &str) { redraw_canvas(app); return; } - if name == "sensitive" { + if name == "enabled" || name == "sensitive" { let mut a = app.borrow_mut(); if let Some(i) = a.find_index(id) { a.items[i].wdg.set_enabled(value == "true"); @@ -1259,9 +1450,124 @@ pub fn apply_prop(app: &Shared, id: u64, name: &str, value: &str) { } drop(a); redraw_canvas(app); + return; } - // Items lists, ranges, StockChart data-file, Shape/Plot draw params - // and the rest are model-only at design time; the generator emits + if name == "items" { + let items: Vec = value.split('|').map(|s| s.to_string()).collect(); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_items(&items); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "lower" || name == "upper" { + let v = value.parse::().unwrap_or(0.0); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + let (mn, mx) = (a.items[i].wdg.min(), a.items[i].wdg.max()); + if name == "lower" { + a.items[i].wdg.set_range(v, mx); + } else { + a.items[i].wdg.set_range(mn, v); + } + } + drop(a); + redraw_canvas(app); + return; + } + if name == "page-increment" { + let v = value.parse::().unwrap_or(10.0); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_page_increment(v); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "page-size" { + let v = value.parse::().unwrap_or(25.0); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_page_size(v); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "step-increment" { + let v = value.parse::().unwrap_or(1.0); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_step(v); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "password" { + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_password(value == "true"); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "show-password" { + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_show_password(value == "true"); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "direction" { + let v = value.parse::().unwrap_or(1); + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_direction(v); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "cursor-visible" { + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_cursor_visible(value != "false"); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "selection-mode" { + let mut a = app.borrow_mut(); + if let Some(i) = a.find_index(id) { + a.items[i].wdg.set_multi_select(value == "3"); + } + drop(a); + redraw_canvas(app); + return; + } + if name == "min-width" || name == "min-height" || name == "max-width" || name == "max-height" { + clamp_item_to_constraints(app, id); + return; + } + if name == "autosize" { + if value == "true" { + apply_autosize(app, id); + } else { + redraw_canvas(app); + } + return; + } + // StockChart data-file, Shape/Plot draw params, anchors, align, + // tab order, cursor, tag, margins, hints and font underline / + // strikeout are model-only at design time; the generator emits // them. No live change, no redraw. }