Wire real xframe-widgets into demo/layout; delete stub Widget trait
- Delete xframe-common::widget (StubWidget + duplicate Widget trait); the one trait is now xframe-widgets::Widget. - New xframe-common::target::CtxTarget: xframe_core::DrawTarget (and FontSource) adapter over DrawCtx, so the real widgets draw into the demo/layout windows. Text goes through DrawCtx::draw_text (PolyText8), keeping the black-bars fix intact. - xframe-core X11Target::draw_string: ImageText8 -> PolyText8 (same black-bars fix; the live target still had the bug). - xframe-demo / xframe-layout: create widgets via xframe_widgets::create, core StyleManager with the exact C++ default/dark styles, fonts resolved through the target; label text = registry kind (closest available approximation of each C++ constructor default text). - xframe-test-runner: doc note updated (real widgets under test now). - Fix pre-existing clippy error in xframe-common ico test (0*16+5 -> 5). - NOTE: do not run cargo fmt --all here: rustfmt inserts commas in the archetype_impl! macro call sites and breaks the build (verified); generated files are intentionally unformatted.
This commit is contained in:
1 parent
f2f0f683d5
commit
aad728a8aa
13 files changed
+527
-416
No files matched your search
Generated
+5
@@ -881,6 +881,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"x11rb",
|
||||
"xframe-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -896,6 +897,8 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"x11rb",
|
||||
"xframe-common",
|
||||
"xframe-core",
|
||||
"xframe-widgets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -908,6 +911,8 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"x11rb",
|
||||
"xframe-common",
|
||||
"xframe-core",
|
||||
"xframe-widgets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -7,6 +7,7 @@ license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
x11rb = "0.13"
|
||||
xframe-core = { path = "../xframe-core" }
|
||||
|
||||
[build-dependencies]
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
||||
@@ -93,10 +93,7 @@ mod tests {
|
||||
assert_eq!((w, h), (16, 16));
|
||||
assert_eq!(rgba.len(), 16 * 16 * 4);
|
||||
// Top row, x=5: red channel 5, alpha 255.
|
||||
assert_eq!(
|
||||
&rgba[(0 * 16 + 5) * 4..(0 * 16 + 5) * 4 + 4],
|
||||
&[5, 0, 0, 255]
|
||||
);
|
||||
assert_eq!(&rgba[5 * 4..5 * 4 + 4], &[5, 0, 0, 255]);
|
||||
// Bottom row is BMP row 0, same gradient.
|
||||
assert_eq!(
|
||||
&rgba[(15 * 16 + 15) * 4..(15 * 16 + 15) * 4 + 4],
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
//! * `ipc` — palette<->layout file IPC (mirrors `widgets/common/ipc.h`).
|
||||
//! * `style` — the shared style manager (mirrors `widgets/core/xframe_core.h`
|
||||
//! `SStyle_Manager`, default + dark styles).
|
||||
//! * `widget` — the `Widget` trait (mirrors C++ `IWidget`) plus a
|
||||
//! TEMPORARY stub implementation. See the module docs: the real widget
|
||||
//! implementations live in the `xframe-widgets` crate (sibling agent);
|
||||
//! this stub exists only so `xframe-demo` / `xframe-palette` /
|
||||
//! `xframe-layout` do not block on it.
|
||||
//! * `target` — [`CtxTarget`], the `xframe_core::DrawTarget` adapter over
|
||||
//! [`DrawCtx`], so the real widgets (`xframe-widgets`) draw into the
|
||||
//! demo / palette / layout windows. The `Widget` trait itself lives in
|
||||
//! `xframe-widgets` (one trait, not two: the temporary stub trait that
|
||||
//! used to live here was deleted when the real widgets landed).
|
||||
//! * `x11` — small x11rb helpers (window creation, colours, fonts, text,
|
||||
//! screenshots, window lookup, WM_DELETE).
|
||||
|
||||
@@ -19,5 +19,5 @@ pub mod ico;
|
||||
pub mod ipc;
|
||||
pub mod registry;
|
||||
pub mod style;
|
||||
pub mod widget;
|
||||
pub mod target;
|
||||
pub mod x11;
|
||||
@@ -0,0 +1,174 @@
|
||||
//! `DrawTarget` adapter over [`DrawCtx`].
|
||||
//!
|
||||
//! The real widgets (`xframe-widgets`) draw through
|
||||
//! `xframe_core::DrawTarget`, while the demo / palette / layout binaries
|
||||
//! own their window through `xframe_common::DrawCtx`. This adapter bridges
|
||||
//! the two: a widget's `draw(&mut target, &mut styles)` renders into the
|
||||
//! binary's `DrawCtx` window.
|
||||
//!
|
||||
//! Text goes through [`DrawCtx::draw_text`], i.e. PolyText8 — the
|
||||
//! black-bars fix stays intact (ImageText8 would paint every label as a
|
||||
//! solid black bar; see that method's docs).
|
||||
//!
|
||||
//! Colour note: `DrawTarget` colours are `0xRRGGBB` pixels, while
|
||||
//! `DrawCtx` resolves RGB triples through the server colormap. The
|
||||
//! adapter unpacks the pixel and lets `DrawCtx` allocate it (cached
|
||||
//! per triple), so both sides agree on the colour.
|
||||
|
||||
use x11rb::protocol::xproto::{ChangeGCAux, ConnectionExt};
|
||||
use xframe_core::draw::DrawTarget;
|
||||
use xframe_core::font::{FontMetrics, FontSource};
|
||||
|
||||
use crate::x11::DrawCtx;
|
||||
|
||||
/// A [`DrawTarget`] that draws into a [`DrawCtx`] window.
|
||||
pub struct CtxTarget<'a> {
|
||||
ctx: &'a DrawCtx<'a>,
|
||||
fg: u32,
|
||||
line_width: u32,
|
||||
font_metrics: Option<FontMetrics>,
|
||||
}
|
||||
|
||||
impl<'a> CtxTarget<'a> {
|
||||
pub fn new(ctx: &'a DrawCtx<'a>) -> Self {
|
||||
CtxTarget {
|
||||
ctx,
|
||||
fg: 0,
|
||||
line_width: 1,
|
||||
font_metrics: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn rgb(&self) -> (u8, u8, u8) {
|
||||
(
|
||||
((self.fg >> 16) & 0xff) as u8,
|
||||
((self.fg >> 8) & 0xff) as u8,
|
||||
(self.fg & 0xff) as u8,
|
||||
)
|
||||
}
|
||||
|
||||
/// The `DrawCtx` underneath, for the binary's own (non-widget) drawing.
|
||||
pub fn ctx(&self) -> &'a DrawCtx<'a> {
|
||||
self.ctx
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawTarget for CtxTarget<'_> {
|
||||
fn set_foreground(&mut self, pixel: u32) {
|
||||
self.fg = pixel;
|
||||
}
|
||||
|
||||
fn set_line_width(&mut self, width: u32) {
|
||||
self.line_width = width.max(1);
|
||||
}
|
||||
|
||||
fn set_font(&mut self, metrics: FontMetrics) {
|
||||
self.font_metrics = Some(metrics);
|
||||
}
|
||||
|
||||
fn fill_rect(&mut self, x: i32, y: i32, w: i32, h: i32) {
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx
|
||||
.fill_rect(x as i16, y as i16, w as u16, h as u16, r, g, b);
|
||||
}
|
||||
|
||||
fn draw_rect(&mut self, x: i32, y: i32, w: i32, h: i32) {
|
||||
if w < 0 || h < 0 {
|
||||
return;
|
||||
}
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx.set_line_width(self.line_width);
|
||||
self.ctx
|
||||
.draw_rect(x as i16, y as i16, w as u16, h as u16, r, g, b);
|
||||
self.ctx.set_line_width(1);
|
||||
}
|
||||
|
||||
fn fill_arc(&mut self, x: i32, y: i32, w: i32, h: i32, angle1: i32, angle2: i32) {
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx.fill_arc(
|
||||
x as i16, y as i16, w as u16, h as u16, angle1, angle2, r, g, b,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_arc(&mut self, x: i32, y: i32, w: i32, h: i32, angle1: i32, angle2: i32) {
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx.set_line_width(self.line_width);
|
||||
self.ctx.draw_arc(
|
||||
x as i16, y as i16, w as u16, h as u16, angle1, angle2, r, g, b,
|
||||
);
|
||||
self.ctx.set_line_width(1);
|
||||
}
|
||||
|
||||
fn draw_line(&mut self, x1: i32, y1: i32, x2: i32, y2: i32) {
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx.set_line_width(self.line_width);
|
||||
self.ctx
|
||||
.draw_line(x1 as i16, y1 as i16, x2 as i16, y2 as i16, r, g, b);
|
||||
self.ctx.set_line_width(1);
|
||||
}
|
||||
|
||||
fn draw_string(&mut self, x: i32, y: i32, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
let (r, g, b) = self.rgb();
|
||||
self.ctx.draw_text(x as i16, y as i16, text, r, g, b);
|
||||
}
|
||||
|
||||
fn text_width(&self, text: &str) -> i32 {
|
||||
self.ctx.text_width(text)
|
||||
}
|
||||
|
||||
fn font_metrics(&self) -> Option<FontMetrics> {
|
||||
self.font_metrics
|
||||
}
|
||||
|
||||
fn put_image(&mut self, x: i32, y: i32, w: i32, h: i32, pixels: &[u32]) {
|
||||
if w <= 0 || h <= 0 {
|
||||
return;
|
||||
}
|
||||
// `DrawCtx::put_rgba` blits in one `PutImage`; the default
|
||||
// per-pixel fill would cost a round-trip per pixel.
|
||||
let mut rgba = Vec::with_capacity((w * h * 4).max(0) as usize);
|
||||
for px in pixels.iter().take((w * h).max(0) as usize) {
|
||||
rgba.push(((px >> 16) & 0xff) as u8);
|
||||
rgba.push(((px >> 8) & 0xff) as u8);
|
||||
rgba.push((px & 0xff) as u8);
|
||||
rgba.push(0xff);
|
||||
}
|
||||
self.ctx
|
||||
.put_rgba(x as i16, y as i16, &rgba, w as u16, h as u16);
|
||||
}
|
||||
}
|
||||
|
||||
impl FontSource for CtxTarget<'_> {
|
||||
/// Open `name` on the server, bind it into the GC (so `draw_text`
|
||||
/// renders with it) and return its metrics. `None` when the font
|
||||
/// does not exist; the caller (`StyleManager::resolve_font`) then
|
||||
/// tries its `fixed` / `helvetica` fallbacks.
|
||||
fn load_font(&mut self, name: &str) -> Option<FontMetrics> {
|
||||
let fid = self.ctx.x.open_font(&[name]).ok()?;
|
||||
let reply = self.ctx.x.conn.query_font(fid).ok()?.reply().ok()?;
|
||||
let _ = self
|
||||
.ctx
|
||||
.x
|
||||
.conn
|
||||
.change_gc(self.ctx.gc, &ChangeGCAux::new().font(fid));
|
||||
let metrics = FontMetrics {
|
||||
ascent: reply.font_ascent as i32,
|
||||
descent: reply.font_descent as i32,
|
||||
char_width: reply.max_bounds.character_width as i32,
|
||||
};
|
||||
self.font_metrics = Some(metrics);
|
||||
Some(metrics)
|
||||
}
|
||||
}
|
||||
@@ -1,301 +0,0 @@
|
||||
//! The `Widget` trait and the TEMPORARY stub implementation.
|
||||
//!
|
||||
//! # SEAM — read before replacing
|
||||
//! The real widget implementations (547 hand-drawn X11 widgets, ported
|
||||
//! from `widgets/components/*.h`) live in the `xframe-widgets` crate,
|
||||
//! which the sibling agent is porting. This module exists only so that
|
||||
//! `xframe-demo`, `xframe-palette` and `xframe-layout` do not block on it.
|
||||
//!
|
||||
//! The trait mirrors the C++ `IWidget` interface
|
||||
//! (`widgets/common/registry.h`): `draw`, `set_bounds`, `contains`,
|
||||
//! `on_button_press`, `on_button_release`, `on_motion`, `on_key`.
|
||||
//! When `xframe-widgets` lands, the swap is mechanical:
|
||||
//! 1. add `xframe-widgets` as a dependency of the binary crates,
|
||||
//! 2. change `create_widget` below to call
|
||||
//! `xframe_widgets::create_widget(...)`,
|
||||
//! 3. delete `StubWidget` (keep the trait — it should move into
|
||||
//! `xframe-widgets` unchanged).
|
||||
//!
|
||||
//! Until then `create_widget` returns a `StubWidget`: a labelled,
|
||||
//! family-accented placeholder box that responds visibly to hover,
|
||||
//! press, focus and keys, so the demo harness, palette, layout and
|
||||
//! test runner can all be exercised end-to-end. The runner's results
|
||||
//! therefore measure the HARNESS (launch/draw/respond/exit), not
|
||||
//! pixel-parity with the C++ widgets — see the runner's README notes
|
||||
//! and report them that way. `GtkInvisible` draws nothing, matching
|
||||
//! the C++ CHECKDRAW BLANK-EXPECTED case.
|
||||
|
||||
use crate::registry::WidgetMeta;
|
||||
use crate::style::{Style, StyleManager};
|
||||
use crate::x11::DrawCtx;
|
||||
|
||||
pub const KEY_TAB: u32 = 0xff09;
|
||||
pub const KEY_ESCAPE: u32 = 0xff1b;
|
||||
|
||||
/// Mirrors C++ `IWidget`. Event methods return `true` when the event
|
||||
/// was handled (the caller then redraws), like the C++ `bool` returns.
|
||||
pub trait Widget {
|
||||
fn name(&self) -> &str;
|
||||
fn set_bounds(&mut self, x: i16, y: i16, w: u16, h: u16);
|
||||
fn bounds(&self) -> (i16, i16, u16, u16);
|
||||
fn contains(&self, x: i16, y: i16) -> bool {
|
||||
let (bx, by, bw, bh) = self.bounds();
|
||||
x >= bx && x < bx + bw as i16 && y >= by && y < by + bh as i16
|
||||
}
|
||||
fn set_visible(&mut self, visible: bool);
|
||||
fn visible(&self) -> bool;
|
||||
fn set_enabled(&mut self, enabled: bool);
|
||||
fn enabled(&self) -> bool;
|
||||
fn set_text(&mut self, text: String);
|
||||
fn text(&self) -> &str;
|
||||
fn draw(&mut self, ctx: &mut DrawCtx);
|
||||
fn on_button_press(&mut self, x: i16, y: i16, button: u8) -> bool;
|
||||
fn on_button_release(&mut self, x: i16, y: i16, button: u8) -> bool;
|
||||
fn on_motion(&mut self, x: i16, y: i16) -> bool;
|
||||
fn on_key(&mut self, keysym: u32, text: &str) -> bool;
|
||||
}
|
||||
|
||||
/// Placeholder widget: a labelled box in the active style, with a
|
||||
/// family-colour accent stripe and visible hover/pressed/focus states.
|
||||
pub struct StubWidget {
|
||||
meta: &'static WidgetMeta,
|
||||
style: Style,
|
||||
x: i16,
|
||||
y: i16,
|
||||
w: u16,
|
||||
h: u16,
|
||||
visible: bool,
|
||||
enabled: bool,
|
||||
hovered: bool,
|
||||
pressed: bool,
|
||||
focused: bool,
|
||||
text: String,
|
||||
}
|
||||
|
||||
impl StubWidget {
|
||||
pub fn new(meta: &'static WidgetMeta, style: Style, x: i16, y: i16, w: u16, h: u16) -> Self {
|
||||
StubWidget {
|
||||
meta,
|
||||
style,
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
visible: true,
|
||||
enabled: true,
|
||||
hovered: false,
|
||||
pressed: false,
|
||||
focused: false,
|
||||
text: meta.name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_label(&self, ctx: &mut DrawCtx, accent: (u8, u8, u8)) {
|
||||
let (x, y, w, h) = (self.x, self.y, self.w, self.h);
|
||||
let mut fill = self.style.fill;
|
||||
if self.pressed {
|
||||
fill = fill.darken(0.25);
|
||||
} else if self.hovered {
|
||||
fill = fill.lighten(0.30);
|
||||
}
|
||||
ctx.fill_rect(x, y, w, h, fill.r, fill.g, fill.b);
|
||||
// Family accent stripe across the top.
|
||||
let stripe = if h >= 12 { 5 } else { 2 };
|
||||
ctx.fill_rect(x, y, w, stripe, accent.0, accent.1, accent.2);
|
||||
ctx.draw_rect(
|
||||
x,
|
||||
y,
|
||||
w,
|
||||
h,
|
||||
self.style.outline.r,
|
||||
self.style.outline.g,
|
||||
self.style.outline.b,
|
||||
);
|
||||
if self.hovered {
|
||||
// 2px accent border: the fill lightening alone moves
|
||||
// luminance by only ~9, below the test runner's >30
|
||||
// hover threshold; the border moves it by ~50.
|
||||
ctx.draw_rect(x, y, w, h, accent.0, accent.1, accent.2);
|
||||
if w > 4 && h > 4 {
|
||||
ctx.draw_rect(x + 1, y + 1, w - 2, h - 2, accent.0, accent.1, accent.2);
|
||||
}
|
||||
}
|
||||
// Name, centred; truncate with ".." when too wide.
|
||||
let (asc, desc) = ctx.x.font_ascent_descent(ctx.font);
|
||||
let mut name = self.text.clone();
|
||||
if ctx.text_width(&name) > (w as i32 - 8) {
|
||||
// Shrink until name + ".." fits; each iteration pops one char,
|
||||
// so this always terminates.
|
||||
while name.len() > 1 && ctx.text_width(&format!("{}..", name)) > (w as i32 - 8) {
|
||||
name.pop();
|
||||
}
|
||||
name.push_str("..");
|
||||
}
|
||||
let tw = ctx.text_width(&name);
|
||||
let tx = x + ((w as i32 - tw) / 2).max(0) as i16;
|
||||
let mid_y = y + (h as i16) / 2 + (asc - desc) / 2;
|
||||
let t = self.style.text;
|
||||
ctx.draw_text(tx, mid_y, &name, t.r, t.g, t.b);
|
||||
// Second line: family + size, when there is room.
|
||||
if h >= 48 {
|
||||
let sub = format!("{} {}x{}", self.meta.family, w, h);
|
||||
let sw = ctx.text_width(&sub);
|
||||
let sx = x + ((w as i32 - sw) / 2).max(0) as i16;
|
||||
ctx.draw_text(sx, mid_y + asc + 4, &sub, t.r, t.g, t.b);
|
||||
}
|
||||
// Focus: four corner ticks in the section accent colour.
|
||||
if self.focused {
|
||||
let c = 8i16;
|
||||
let (ax, ay) = (x, y);
|
||||
let (bx, by) = (x + w as i16 - 1, y + h as i16 - 1);
|
||||
for (cx, cy, dx, dy) in [
|
||||
(ax, ay, 1i16, 1i16),
|
||||
(bx, ay, -1, 1),
|
||||
(ax, by, 1, -1),
|
||||
(bx, by, -1, -1),
|
||||
] {
|
||||
ctx.draw_line(cx, cy, cx + dx * c, cy, accent.0, accent.1, accent.2);
|
||||
ctx.draw_line(cx, cy, cx, cy + dy * c, accent.0, accent.1, accent.2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for StubWidget {
|
||||
fn name(&self) -> &str {
|
||||
self.meta.name
|
||||
}
|
||||
fn set_bounds(&mut self, x: i16, y: i16, w: u16, h: u16) {
|
||||
self.x = x;
|
||||
self.y = y;
|
||||
self.w = w;
|
||||
self.h = h;
|
||||
}
|
||||
fn bounds(&self) -> (i16, i16, u16, u16) {
|
||||
(self.x, self.y, self.w, self.h)
|
||||
}
|
||||
fn set_visible(&mut self, visible: bool) {
|
||||
self.visible = visible;
|
||||
}
|
||||
fn visible(&self) -> bool {
|
||||
self.visible
|
||||
}
|
||||
fn set_enabled(&mut self, enabled: bool) {
|
||||
self.enabled = enabled;
|
||||
}
|
||||
fn enabled(&self) -> bool {
|
||||
self.enabled
|
||||
}
|
||||
fn set_text(&mut self, text: String) {
|
||||
self.text = text;
|
||||
}
|
||||
fn text(&self) -> &str {
|
||||
&self.text
|
||||
}
|
||||
fn draw(&mut self, ctx: &mut DrawCtx) {
|
||||
if !self.visible {
|
||||
return;
|
||||
}
|
||||
// GtkInvisible is blank by design (C++ CHECKDRAW BLANK-EXPECTED).
|
||||
if self.meta.name == "GtkInvisible" {
|
||||
return;
|
||||
}
|
||||
let accent = self.meta.section_color();
|
||||
self.draw_label(ctx, accent);
|
||||
}
|
||||
fn on_button_press(&mut self, x: i16, y: i16, _button: u8) -> bool {
|
||||
if !self.visible || !self.enabled || !self.contains(x, y) {
|
||||
return false;
|
||||
}
|
||||
self.pressed = true;
|
||||
self.focused = true;
|
||||
true
|
||||
}
|
||||
fn on_button_release(&mut self, x: i16, y: i16, _button: u8) -> bool {
|
||||
if self.pressed {
|
||||
self.pressed = false;
|
||||
// Release inside the widget counts as a click; release
|
||||
// outside still clears the pressed state.
|
||||
let _ = (x, y);
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
fn on_motion(&mut self, x: i16, y: i16) -> bool {
|
||||
if !self.visible || !self.enabled {
|
||||
return false;
|
||||
}
|
||||
let inside = self.contains(x, y);
|
||||
let changed = inside != self.hovered;
|
||||
self.hovered = inside;
|
||||
changed || inside
|
||||
}
|
||||
fn on_key(&mut self, keysym: u32, text: &str) -> bool {
|
||||
if !self.visible || !self.enabled {
|
||||
return false;
|
||||
}
|
||||
// Any printable key or Tab gives the widget focus feedback.
|
||||
if keysym == KEY_TAB || !text.is_empty() {
|
||||
self.focused = true;
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Factory with the C++ `lg::create_widget` signature (minus the X11
|
||||
/// plumbing, which the caller owns). STUB: returns `StubWidget` until
|
||||
/// `xframe-widgets` lands — see the module docs.
|
||||
pub fn create_widget(
|
||||
meta: &'static WidgetMeta,
|
||||
styles: &StyleManager,
|
||||
x: i16,
|
||||
y: i16,
|
||||
w: u16,
|
||||
h: u16,
|
||||
) -> Box<dyn Widget> {
|
||||
Box::new(StubWidget::new(meta, styles.active().clone(), x, y, w, h))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::registry::by_name;
|
||||
use crate::style::StyleManager;
|
||||
|
||||
#[test]
|
||||
fn stub_factory_and_events() {
|
||||
let styles = StyleManager::new();
|
||||
let meta = by_name("GtkButton").unwrap();
|
||||
let mut w = create_widget(meta, &styles, 40, 40, 340, 200);
|
||||
assert_eq!(w.name(), "GtkButton");
|
||||
assert_eq!(w.bounds(), (40, 40, 340, 200));
|
||||
assert!(w.contains(50, 50));
|
||||
assert!(!w.contains(10, 10));
|
||||
assert!(!w.on_motion(10, 10));
|
||||
assert!(w.on_motion(50, 50));
|
||||
assert!(w.on_button_press(50, 50, 1));
|
||||
assert!(w.on_button_release(50, 50, 1));
|
||||
assert!(!w.on_button_release(50, 50, 1));
|
||||
assert!(w.on_key(KEY_TAB, ""));
|
||||
assert!(w.on_key(b'a' as u32, "a"));
|
||||
w.set_bounds(0, 0, 100, 30);
|
||||
assert_eq!(w.bounds(), (0, 0, 100, 30));
|
||||
w.set_visible(false);
|
||||
assert!(!w.visible());
|
||||
assert!(!w.on_button_press(50, 50, 1));
|
||||
w.set_visible(true);
|
||||
w.set_text("OK".to_string());
|
||||
assert_eq!(w.text(), "OK");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invisible_is_blank_by_design() {
|
||||
// Behavioural contract only: the stub draws nothing for
|
||||
// GtkInvisible; pixel-level proof is the layout CHECKDRAW test.
|
||||
let styles = StyleManager::new();
|
||||
let meta = by_name("GtkInvisible").unwrap();
|
||||
let w = create_widget(meta, &styles, 40, 40, 340, 200);
|
||||
assert_eq!(w.name(), "GtkInvisible");
|
||||
}
|
||||
}
|
||||
@@ -522,6 +522,81 @@ impl<'a> DrawCtx<'a> {
|
||||
.poly_segment(self.drawable, self.gc, &[Segment { x1, y1, x2, y2 }]);
|
||||
}
|
||||
|
||||
/// Set the GC line width for subsequent outline draws
|
||||
/// (`DrawTarget::set_line_width` on the adapter).
|
||||
pub fn set_line_width(&self, width: u32) {
|
||||
let _ = self
|
||||
.x
|
||||
.conn
|
||||
.change_gc(self.gc, &ChangeGCAux::new().line_width(width.max(1)));
|
||||
}
|
||||
|
||||
/// Fill an elliptical arc sector (angles in 64ths of a degree,
|
||||
/// as in Xlib). Used by the `DrawTarget` adapter for the polished
|
||||
/// rounded-rect bodies the real widgets draw.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn fill_arc(
|
||||
&self,
|
||||
x: i16,
|
||||
y: i16,
|
||||
w: u16,
|
||||
h: u16,
|
||||
angle1: i32,
|
||||
angle2: i32,
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
) {
|
||||
if w == 0 || h == 0 {
|
||||
return;
|
||||
}
|
||||
self.set_fg(r, g, b);
|
||||
let _ = self.x.conn.poly_fill_arc(
|
||||
self.drawable,
|
||||
self.gc,
|
||||
&[Arc {
|
||||
x,
|
||||
y,
|
||||
width: w,
|
||||
height: h,
|
||||
angle1: angle1 as i16,
|
||||
angle2: angle2 as i16,
|
||||
}],
|
||||
);
|
||||
}
|
||||
|
||||
/// Draw an elliptical arc outline (angles in 64ths of a degree).
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn draw_arc(
|
||||
&self,
|
||||
x: i16,
|
||||
y: i16,
|
||||
w: u16,
|
||||
h: u16,
|
||||
angle1: i32,
|
||||
angle2: i32,
|
||||
r: u8,
|
||||
g: u8,
|
||||
b: u8,
|
||||
) {
|
||||
if w == 0 || h == 0 {
|
||||
return;
|
||||
}
|
||||
self.set_fg(r, g, b);
|
||||
let _ = self.x.conn.poly_arc(
|
||||
self.drawable,
|
||||
self.gc,
|
||||
&[Arc {
|
||||
x,
|
||||
y,
|
||||
width: w,
|
||||
height: h,
|
||||
angle1: angle1 as i16,
|
||||
angle2: angle2 as i16,
|
||||
}],
|
||||
);
|
||||
}
|
||||
|
||||
pub fn draw_point(&self, x: i16, y: i16, r: u8, g: u8, b: u8) {
|
||||
self.set_fg(r, g, b);
|
||||
let _ =
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
use x11rb::connection::Connection;
|
||||
use x11rb::errors::{ConnectionError, ReplyError};
|
||||
use x11rb::protocol::xproto::{
|
||||
Arc, ChangeGCAux, ConnectionExt, Drawable, Font, Gcontext, ImageFormat, Rectangle,
|
||||
Segment,
|
||||
Arc, ChangeGCAux, ConnectionExt, Drawable, Font, Gcontext, ImageFormat, Rectangle, Segment,
|
||||
};
|
||||
use x11rb::rust_connection::RustConnection;
|
||||
|
||||
@@ -100,9 +99,7 @@ impl X11Target {
|
||||
descent: reply.font_descent as i32,
|
||||
char_width: reply.max_bounds.character_width as i32,
|
||||
};
|
||||
let _ = self
|
||||
.conn
|
||||
.change_gc(self.gc, &ChangeGCAux::new().font(fid));
|
||||
let _ = self.conn.change_gc(self.gc, &ChangeGCAux::new().font(fid));
|
||||
self.font = Some(fid);
|
||||
self.font_metrics = Some(metrics);
|
||||
self.font_id = fid;
|
||||
@@ -112,7 +109,12 @@ impl X11Target {
|
||||
}
|
||||
|
||||
/// C++ `Canvas::present` for a [`Canvas`]: `XPutImage` + `XFlush`.
|
||||
pub fn put_image(&self, canvas: &Canvas, dst_x: i32, dst_y: i32) -> Result<(), x11rb::errors::ConnectionError> {
|
||||
pub fn put_image(
|
||||
&self,
|
||||
canvas: &Canvas,
|
||||
dst_x: i32,
|
||||
dst_y: i32,
|
||||
) -> Result<(), x11rb::errors::ConnectionError> {
|
||||
let w = canvas.get_width();
|
||||
let h = canvas.get_height();
|
||||
let mut bytes = Vec::with_capacity((w * h * 4) as usize);
|
||||
@@ -258,16 +260,31 @@ impl DrawTarget for X11Target {
|
||||
}
|
||||
|
||||
fn draw_string(&mut self, x: i32, y: i32, text: &str) {
|
||||
if self.font.is_none() {
|
||||
if self.font.is_none() || text.is_empty() {
|
||||
return;
|
||||
}
|
||||
let _ = self.conn.image_text8(
|
||||
self.drawable,
|
||||
self.gc,
|
||||
x as i16,
|
||||
y as i16,
|
||||
text.as_bytes(),
|
||||
);
|
||||
// PolyText8, not ImageText8: ImageText8 paints the text extent
|
||||
// with the GC *background* colour, and this GC's background
|
||||
// defaults to pixel 1 (near-black), so every string rendered as
|
||||
// a solid black bar with the glyphs invisible inside it. (Same
|
||||
// fix as `xframe_common::DrawCtx::draw_text`; the demo, palette
|
||||
// and layout all draw widget text through here.)
|
||||
// PolyText8 items: per element, a length byte (1..=254), a
|
||||
// DELTA byte (horizontal shift in pixels, 0 here), then that
|
||||
// many string bytes; 255 introduces a font change. The delta
|
||||
// byte is mandatory: without it the server eats the first
|
||||
// character as the delta. Chunk long strings so no element
|
||||
// exceeds 254 bytes.
|
||||
let bytes = text.as_bytes();
|
||||
let mut items: Vec<u8> = Vec::with_capacity(bytes.len() * 2 + bytes.len() / 254 + 1);
|
||||
for chunk in bytes.chunks(254) {
|
||||
items.push(chunk.len() as u8);
|
||||
items.push(0);
|
||||
items.extend_from_slice(chunk);
|
||||
}
|
||||
let _ = self
|
||||
.conn
|
||||
.poly_text8(self.drawable, self.gc, x as i16, y as i16, &items);
|
||||
}
|
||||
|
||||
fn text_width(&self, text: &str) -> i32 {
|
||||
|
||||
@@ -11,4 +11,6 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
xframe-common = { path = "../xframe-common" }
|
||||
xframe-core = { path = "../xframe-core" }
|
||||
xframe-widgets = { path = "../xframe-widgets" }
|
||||
x11rb = "0.13"
|
||||
@@ -12,11 +12,20 @@
|
||||
//! xframe-demo <WidgetName> [--dark] [--hold SECS] [--geometry X,Y,W,H] [--list]
|
||||
//! ```
|
||||
//!
|
||||
//! The widget itself comes from `xframe_common::widget::create_widget`,
|
||||
//! which is currently a STUB (see that module's SEAM docs) until the
|
||||
//! `xframe-widgets` crate lands. The demo harness — window, styles,
|
||||
//! event dispatch, exit behaviour — is the faithful port; the pixels
|
||||
//! inside the widget rect are placeholder until the swap.
|
||||
//! The widget comes from `xframe_widgets::create` — the REAL widget
|
||||
//! (one of the 14 archetypes), not the stub placeholder this binary used
|
||||
//! to carry. It draws through `xframe_core::DrawTarget`, bridged onto
|
||||
//! this binary's `DrawCtx` window by `xframe_common::CtxTarget`
|
||||
//! (text via PolyText8 — the black-bars fix stays intact).
|
||||
//!
|
||||
//! Label text: the C++ mains construct the widget and draw it; each
|
||||
//! C++ constructor sets its own default text (`"Button"`, `"Label"`,
|
||||
//! `"Sample text"`, an indicator name, ...). The Rust factory leaves
|
||||
//! `text` empty, so the demo sets it to the widget's registry `kind`
|
||||
//! (`"Button"`, `"Label"`, `"Bollinger Bands"`, ...), which matches the
|
||||
//! C++ default for the plain Gtk widgets and the indicators. Where the
|
||||
//! C++ text was something else the pixels differ by the label glyphs
|
||||
//! only; see the test runner's pixel-diff notes.
|
||||
|
||||
use std::process::ExitCode;
|
||||
use std::time::{Duration, Instant};
|
||||
@@ -24,12 +33,40 @@ use x11rb::connection::Connection;
|
||||
use x11rb::protocol::xproto::*;
|
||||
use x11rb::protocol::Event;
|
||||
use xframe_common::registry::{self, WidgetMeta};
|
||||
use xframe_common::style::{StyleManager, FONT_DEFAULT};
|
||||
use xframe_common::widget::{create_widget, Widget, KEY_ESCAPE};
|
||||
use xframe_common::style::FONT_DEFAULT;
|
||||
use xframe_common::target::CtxTarget;
|
||||
use xframe_common::x11::{DrawCtx, X11};
|
||||
use xframe_core::{Srgb, StyleManager};
|
||||
use xframe_widgets::{create, Key, Widget};
|
||||
|
||||
const WIN_W: u16 = 800;
|
||||
const WIN_H: u16 = 600;
|
||||
const KEY_ESCAPE: u32 = 0xff1b;
|
||||
|
||||
/// `default` / `dark` styles, exactly as registered by every C++ demo
|
||||
/// main (`add_style_ex`, C++ arg order fill/outline/text/line).
|
||||
fn register_styles(styles: &mut StyleManager) {
|
||||
styles.add_style_ex(
|
||||
"default",
|
||||
Srgb::new(225, 225, 225),
|
||||
Srgb::new(120, 120, 120),
|
||||
Srgb::new(20, 20, 20),
|
||||
Srgb::new(60, 60, 60),
|
||||
6,
|
||||
1,
|
||||
FONT_DEFAULT,
|
||||
);
|
||||
styles.add_style_ex(
|
||||
"dark",
|
||||
Srgb::new(45, 45, 48),
|
||||
Srgb::new(20, 20, 22),
|
||||
Srgb::new(240, 240, 240),
|
||||
Srgb::new(200, 200, 200),
|
||||
6,
|
||||
1,
|
||||
FONT_DEFAULT,
|
||||
);
|
||||
}
|
||||
|
||||
fn usage() -> &'static str {
|
||||
"usage: xframe-demo <WidgetName> [--dark] [--hold SECS] [--geometry X,Y,W,H] [--list]\n xframe-demo --list # print all 547 registry widget names"
|
||||
@@ -137,23 +174,45 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let (win, gc) = x.create_window(100, 100, WIN_W, WIN_H, &title, event_mask)?;
|
||||
|
||||
let mut styles = StyleManager::new();
|
||||
register_styles(&mut styles);
|
||||
if args.dark {
|
||||
styles.set_active_by_name("dark");
|
||||
styles.set_active_style_by_name("dark");
|
||||
}
|
||||
let (gx, gy, gw, gh) = args
|
||||
.geometry
|
||||
.unwrap_or_else(|| registry::demo_geometry(meta));
|
||||
let mut widget = create_widget(meta, &styles, gx, gy, gw, gh);
|
||||
let mut widget: Box<dyn Widget> = match create(meta.name) {
|
||||
Some(w) => w,
|
||||
None => {
|
||||
eprintln!("xframe-demo: no widget for '{}'", meta.name);
|
||||
return Ok(2);
|
||||
}
|
||||
};
|
||||
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).
|
||||
let kind = widget.kind().to_string();
|
||||
widget.set_text(&kind);
|
||||
|
||||
let mut ctx = DrawCtx::new(&x, win, gc, font);
|
||||
let ctx = DrawCtx::new(&x, win, gc, font);
|
||||
ctx.bind_font();
|
||||
let mut target = CtxTarget::new(&ctx);
|
||||
// Font metrics for the styles: without them `draw_text_*` draws no
|
||||
// text at all (same as the C++ `font_ == null` path). Resolving
|
||||
// binds the font into the GC via the target.
|
||||
if !styles.resolve_font("default", &mut target) && !styles.resolve_font("dark", &mut target) {
|
||||
eprintln!("xframe-demo: warning: no font resolved, labels will be blank");
|
||||
}
|
||||
|
||||
let redraw = |widget: &mut Box<dyn Widget>, ctx: &mut DrawCtx| {
|
||||
let redraw = |widget: &mut Box<dyn Widget>,
|
||||
ctx: &DrawCtx,
|
||||
target: &mut CtxTarget,
|
||||
styles: &mut StyleManager| {
|
||||
ctx.clear(255, 255, 255, WIN_W, WIN_H);
|
||||
widget.draw(ctx);
|
||||
widget.draw(target, styles);
|
||||
ctx.flush();
|
||||
};
|
||||
redraw(&mut widget, &mut ctx);
|
||||
redraw(&mut widget, &ctx, &mut target, &mut styles);
|
||||
|
||||
let deadline = args.hold.map(|s| Instant::now() + Duration::from_secs(s));
|
||||
loop {
|
||||
@@ -172,16 +231,21 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
let mut handled = false;
|
||||
match ev {
|
||||
Event::Expose(_) => {
|
||||
redraw(&mut widget, &mut ctx);
|
||||
redraw(&mut widget, &ctx, &mut target, &mut styles);
|
||||
}
|
||||
Event::ButtonPress(ev) => {
|
||||
handled = widget.on_button_press(ev.event_x, ev.event_y, ev.detail);
|
||||
handled =
|
||||
widget.on_button_press(ev.event_x as i32, ev.event_y as i32, ev.detail as u32);
|
||||
}
|
||||
Event::ButtonRelease(ev) => {
|
||||
handled = widget.on_button_release(ev.event_x, ev.event_y, ev.detail);
|
||||
handled = widget.on_button_release(
|
||||
ev.event_x as i32,
|
||||
ev.event_y as i32,
|
||||
ev.detail as u32,
|
||||
);
|
||||
}
|
||||
Event::MotionNotify(ev) => {
|
||||
handled = widget.on_motion(ev.event_x, ev.event_y);
|
||||
handled = widget.on_motion(ev.event_x as i32, ev.event_y as i32);
|
||||
}
|
||||
Event::KeyPress(ev) => {
|
||||
let keysym = x.keysym_for_keycode(ev.detail);
|
||||
@@ -189,11 +253,11 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
break;
|
||||
}
|
||||
let text = if (0x20..0x7f).contains(&keysym) {
|
||||
(keysym as u8 as char).to_string()
|
||||
Some(keysym as u8 as char)
|
||||
} else {
|
||||
String::new()
|
||||
None
|
||||
};
|
||||
handled = widget.on_key(keysym, &text);
|
||||
handled = widget.on_key(Key::from_keysym(keysym, text), text);
|
||||
}
|
||||
Event::ClientMessage(ev)
|
||||
if ev.type_ == x.wm_protocols && ev.data.as_data32()[0] == x.wm_delete =>
|
||||
@@ -203,7 +267,7 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
_ => {}
|
||||
}
|
||||
if handled {
|
||||
redraw(&mut widget, &mut ctx);
|
||||
redraw(&mut widget, &ctx, &mut target, &mut styles);
|
||||
}
|
||||
}
|
||||
Ok(0)
|
||||
|
||||
@@ -11,4 +11,6 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
xframe-common = { path = "../xframe-common" }
|
||||
xframe-core = { path = "../xframe-core" }
|
||||
xframe-widgets = { path = "../xframe-widgets" }
|
||||
x11rb = "0.13"
|
||||
@@ -1,8 +1,8 @@
|
||||
//! xframe-layout — the LightGUI layout/design window on x11rb.
|
||||
//!
|
||||
//! A faithful port of `widgets/layout/main_layout.cpp`: a 1024x720
|
||||
//! window that instantiates widgets (via the `create_widget` factory —
|
||||
//! currently the stub, see `xframe_common::widget` SEAM docs). The
|
||||
//! window that instantiates widgets (via `xframe_widgets::create` — the
|
||||
//! real widgets, one of the 14 archetypes). The
|
||||
//! widget type selected in the palette (shared selection file) is
|
||||
//! placed with a click, then can be selected, dragged to move, and
|
||||
//! deleted. Double-click shows resize handles (corners + edges);
|
||||
@@ -31,9 +31,11 @@ use x11rb::protocol::xproto::*;
|
||||
use x11rb::protocol::Event;
|
||||
use xframe_common::ipc;
|
||||
use xframe_common::registry::{self, WidgetMeta, WIDGET_COUNT};
|
||||
use xframe_common::style::{StyleManager, FONT_DEFAULT};
|
||||
use xframe_common::widget::{create_widget, Widget};
|
||||
use xframe_common::style::FONT_DEFAULT;
|
||||
use xframe_common::target::CtxTarget;
|
||||
use xframe_common::x11::{DrawCtx, X11};
|
||||
use xframe_core::{Srgb, StyleManager};
|
||||
use xframe_widgets::{create, Widget};
|
||||
|
||||
const WIN_W: u16 = 1024;
|
||||
const WIN_H: u16 = 720;
|
||||
@@ -44,6 +46,48 @@ const KEY_DELETE: u32 = 0xffff;
|
||||
const KEY_BACKSPACE: u32 = 0xff08;
|
||||
const KEY_ESCAPE: u32 = 0xff1b;
|
||||
|
||||
/// `default` / `dark` styles, exactly as registered by every C++ demo
|
||||
/// main (`add_style_ex`, C++ arg order fill/outline/text/line).
|
||||
fn register_styles(styles: &mut StyleManager) {
|
||||
styles.add_style_ex(
|
||||
"default",
|
||||
Srgb::new(225, 225, 225),
|
||||
Srgb::new(120, 120, 120),
|
||||
Srgb::new(20, 20, 20),
|
||||
Srgb::new(60, 60, 60),
|
||||
6,
|
||||
1,
|
||||
FONT_DEFAULT,
|
||||
);
|
||||
styles.add_style_ex(
|
||||
"dark",
|
||||
Srgb::new(45, 45, 48),
|
||||
Srgb::new(20, 20, 22),
|
||||
Srgb::new(240, 240, 240),
|
||||
Srgb::new(200, 200, 200),
|
||||
6,
|
||||
1,
|
||||
FONT_DEFAULT,
|
||||
);
|
||||
}
|
||||
|
||||
/// Build a real widget for `meta` at the given bounds, labelled with its
|
||||
/// registry `kind` (the C++ constructor's default text, approximately —
|
||||
/// see `xframe-demo`'s module docs). `None` for an unknown name.
|
||||
fn make_widget(
|
||||
meta: &'static WidgetMeta,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
) -> Option<Box<dyn Widget>> {
|
||||
let mut wdg = create(meta.name)?;
|
||||
wdg.set_bounds(x, y, w, h);
|
||||
let kind = wdg.kind().to_string();
|
||||
wdg.set_text(&kind);
|
||||
Some(wdg)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq)]
|
||||
enum Handle {
|
||||
Nw = 0,
|
||||
@@ -123,15 +167,7 @@ impl State {
|
||||
let _ = std::fs::write(ipc::layout_state_path(), text);
|
||||
}
|
||||
|
||||
fn place_widget(
|
||||
&mut self,
|
||||
styles: &StyleManager,
|
||||
type_idx: usize,
|
||||
x: i32,
|
||||
y: i32,
|
||||
w: i32,
|
||||
h: i32,
|
||||
) -> i32 {
|
||||
fn place_widget(&mut self, type_idx: usize, x: i32, y: i32, w: i32, h: i32) -> i32 {
|
||||
if type_idx >= WIDGET_COUNT {
|
||||
return -1;
|
||||
}
|
||||
@@ -144,7 +180,9 @@ impl State {
|
||||
} else {
|
||||
y
|
||||
};
|
||||
let wdg = create_widget(meta, styles, x as i16, y as i16, w as u16, h as u16);
|
||||
let Some(wdg) = make_widget(meta, x, y, w, h) else {
|
||||
return -1;
|
||||
};
|
||||
let id = self.next_id;
|
||||
self.next_id += 1;
|
||||
self.last_placed_id = id;
|
||||
@@ -195,7 +233,7 @@ impl State {
|
||||
std::fs::write(path, text).is_ok()
|
||||
}
|
||||
|
||||
fn load_layout(&mut self, styles: &StyleManager, path: &str) -> i32 {
|
||||
fn load_layout(&mut self, path: &str) -> i32 {
|
||||
let text = match std::fs::read_to_string(path) {
|
||||
Ok(t) => t,
|
||||
Err(_) => return -1,
|
||||
@@ -224,7 +262,7 @@ impl State {
|
||||
let Some(t) = registry::index_of(name) else {
|
||||
continue;
|
||||
};
|
||||
if self.place_widget(styles, t, x, y, w, h) >= 0 {
|
||||
if self.place_widget(t, x, y, w, h) >= 0 {
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
@@ -324,10 +362,16 @@ fn apply_handle_drag(p: &mut Placed, h: Handle, dx: i32, dy: i32) {
|
||||
p.y = y;
|
||||
p.w = w;
|
||||
p.h = hh;
|
||||
p.wdg.set_bounds(x as i16, y as i16, w as u16, hh as u16);
|
||||
p.wdg.set_bounds(x, y, w, hh);
|
||||
}
|
||||
|
||||
fn redraw(x: &X11, ctx: &mut DrawCtx, sel_gc: Gcontext, state: &mut State) {
|
||||
fn redraw(
|
||||
x: &X11,
|
||||
ctx: &mut DrawCtx,
|
||||
sel_gc: Gcontext,
|
||||
styles: &mut StyleManager,
|
||||
state: &mut State,
|
||||
) {
|
||||
let (win_w, win_h) = (state.win_w, state.win_h);
|
||||
ctx.clear(255, 255, 255, win_w as u16, win_h as u16);
|
||||
// Dotted canvas grid.
|
||||
@@ -342,8 +386,11 @@ fn redraw(x: &X11, ctx: &mut DrawCtx, sel_gc: Gcontext, state: &mut State) {
|
||||
gx += 24;
|
||||
}
|
||||
ctx.draw_points(&pts, 225, 225, 228);
|
||||
for p in state.placed.iter_mut() {
|
||||
p.wdg.draw(ctx);
|
||||
{
|
||||
let mut target = CtxTarget::new(&*ctx);
|
||||
for p in state.placed.iter_mut() {
|
||||
p.wdg.draw(&mut target, styles);
|
||||
}
|
||||
}
|
||||
// Selection outline + handles.
|
||||
if state.selected_id >= 0 {
|
||||
@@ -476,7 +523,7 @@ fn run_script(
|
||||
ctx: &mut DrawCtx,
|
||||
sel_gc: Gcontext,
|
||||
win: Window,
|
||||
styles: &StyleManager,
|
||||
styles: &mut StyleManager,
|
||||
state: &mut State,
|
||||
path: &str,
|
||||
log: &mut dyn Write,
|
||||
@@ -511,13 +558,13 @@ fn run_script(
|
||||
fails += 1;
|
||||
continue;
|
||||
};
|
||||
let id = state.place_widget(styles, t, x0, y0, w, h);
|
||||
let id = state.place_widget(t, x0, y0, w, h);
|
||||
if id < 0 {
|
||||
log_fail(log, line, "create_widget returned null");
|
||||
log_fail(log, line, "create returned null");
|
||||
fails += 1;
|
||||
continue;
|
||||
}
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let p = state.find_placed_ref(id).unwrap();
|
||||
let _ = writeln!(
|
||||
log,
|
||||
@@ -530,11 +577,11 @@ fn run_script(
|
||||
for t in 0..WIDGET_COUNT {
|
||||
let px = (t % 4) as i32 * 260 + 10;
|
||||
let py = (t / 4) as i32 * 130 + 10;
|
||||
if state.place_widget(styles, t, px, py, 240, 110) >= 0 {
|
||||
if state.place_widget(t, px, py, 240, 110) >= 0 {
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK PLACE_ALL {}/{}", n, WIDGET_COUNT);
|
||||
if n != WIDGET_COUNT {
|
||||
log_fail(log, line, "not all placed");
|
||||
@@ -565,9 +612,9 @@ fn run_script(
|
||||
p.x += dx;
|
||||
p.y += dy;
|
||||
let (nx, ny, nw, nh) = (p.x, p.y, p.w, p.h);
|
||||
p.wdg.set_bounds(nx as i16, ny as i16, nw as u16, nh as u16);
|
||||
p.wdg.set_bounds(nx, ny, nw, nh);
|
||||
state.write_state();
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK MOVE {} -> {} {}", id, nx, ny);
|
||||
}
|
||||
"RESIZE" => {
|
||||
@@ -593,9 +640,9 @@ fn run_script(
|
||||
};
|
||||
p.w = w;
|
||||
p.h = h;
|
||||
p.wdg.set_bounds(p.x as i16, p.y as i16, w as u16, h as u16);
|
||||
p.wdg.set_bounds(p.x, p.y, w, h);
|
||||
state.write_state();
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK RESIZE {} -> {} {}", id, w, h);
|
||||
}
|
||||
"DELETE" => {
|
||||
@@ -614,7 +661,7 @@ fn run_script(
|
||||
fails += 1;
|
||||
continue;
|
||||
}
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK DELETE {} count={}", id, state.placed.len());
|
||||
}
|
||||
"SELECT" => {
|
||||
@@ -636,7 +683,7 @@ fn run_script(
|
||||
state.selected_id = id;
|
||||
state.handles_on = false;
|
||||
state.write_state();
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK SELECT {}", id);
|
||||
}
|
||||
"DBLCLICK" => {
|
||||
@@ -658,7 +705,7 @@ fn run_script(
|
||||
state.selected_id = id;
|
||||
state.handles_on = true;
|
||||
state.write_state();
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK DBLCLICK {} handles=on", id);
|
||||
}
|
||||
"HANDLE" => {
|
||||
@@ -690,7 +737,7 @@ fn run_script(
|
||||
let (ox, oy, ow, oh) = (p.x, p.y, p.w, p.h);
|
||||
apply_handle_drag(p, h, dx, dy);
|
||||
state.write_state();
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let p = state.find_placed_ref(state.selected_id).unwrap();
|
||||
let _ = writeln!(
|
||||
log,
|
||||
@@ -716,7 +763,7 @@ fn run_script(
|
||||
};
|
||||
let (px, py, pw, ph, tname) =
|
||||
(p.x, p.y, p.w, p.h, registry::WIDGETS[p.type_idx].name);
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let n = count_nonwhite(x, win, px, py, pw, ph);
|
||||
if n < 0 {
|
||||
log_fail(log, line, "XGetImage failed");
|
||||
@@ -758,13 +805,13 @@ fn run_script(
|
||||
fails += 1;
|
||||
continue;
|
||||
};
|
||||
let n = state.load_layout(styles, f);
|
||||
let n = state.load_layout(f);
|
||||
if n < 0 {
|
||||
log_fail(log, line, "load failed");
|
||||
fails += 1;
|
||||
continue;
|
||||
}
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
let _ = writeln!(log, "OK LOAD {} count={}", f, n);
|
||||
}
|
||||
"COUNT" => {
|
||||
@@ -773,7 +820,7 @@ fn run_script(
|
||||
"HOLD" => {
|
||||
let secs: u64 = it.next().and_then(|v| v.parse().ok()).unwrap_or(5);
|
||||
for _ in 0..secs * 10 {
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
}
|
||||
let _ = writeln!(log, "OK HOLD {}", secs);
|
||||
@@ -813,7 +860,13 @@ fn run_script(
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_selection(x: &X11, state: &mut State, ctx: &mut DrawCtx, sel_gc: Gcontext) {
|
||||
fn poll_selection(
|
||||
x: &X11,
|
||||
state: &mut State,
|
||||
ctx: &mut DrawCtx,
|
||||
sel_gc: Gcontext,
|
||||
styles: &mut StyleManager,
|
||||
) {
|
||||
let mt = match ipc::file_mtime_nanos(&ipc::selection_path()) {
|
||||
Some(m) => m,
|
||||
None => return,
|
||||
@@ -827,7 +880,7 @@ fn poll_selection(x: &X11, state: &mut State, ctx: &mut DrawCtx, sel_gc: Gcontex
|
||||
state.sel_type = idx as i32;
|
||||
let title = format!("LightGUI Layout — place: {}", name);
|
||||
let _ = x.set_window_title(ctx.drawable, &title);
|
||||
redraw(x, ctx, sel_gc, state);
|
||||
redraw(x, ctx, sel_gc, styles, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -869,11 +922,21 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
&CreateGCAux::new().line_style(LineStyle::ON_OFF_DASH),
|
||||
)?;
|
||||
|
||||
let styles = StyleManager::new();
|
||||
let mut styles = StyleManager::new();
|
||||
register_styles(&mut styles);
|
||||
let mut ctx = DrawCtx::new(&x, win, gc, font);
|
||||
ctx.bind_font();
|
||||
// Font metrics for the styles: without them `draw_text_*` draws no
|
||||
// text at all. Resolving binds the font into the GC via the target.
|
||||
{
|
||||
let mut target = CtxTarget::new(&ctx);
|
||||
if !styles.resolve_font("default", &mut target) && !styles.resolve_font("dark", &mut target)
|
||||
{
|
||||
eprintln!("xframe-layout: warning: no font resolved, labels will be blank");
|
||||
}
|
||||
}
|
||||
let mut state = State::new();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
|
||||
if !script.is_empty() {
|
||||
let mut log: Box<dyn Write> = if logpath.is_empty() {
|
||||
@@ -885,13 +948,20 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
}
|
||||
};
|
||||
let rc = run_script(
|
||||
&x, &mut ctx, sel_gc, win, &styles, &mut state, &script, &mut log,
|
||||
&x,
|
||||
&mut ctx,
|
||||
sel_gc,
|
||||
win,
|
||||
&mut styles,
|
||||
&mut state,
|
||||
&script,
|
||||
&mut log,
|
||||
);
|
||||
return Ok(rc);
|
||||
}
|
||||
|
||||
// Interactive mode.
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc);
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc, &mut styles);
|
||||
let mut dragging = false;
|
||||
let mut handle_drag = false;
|
||||
let mut drag_handle = Handle::Se;
|
||||
@@ -907,16 +977,16 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
Some(ev) => ev,
|
||||
None => {
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc);
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc, &mut styles);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match ev {
|
||||
Event::Expose(_) => redraw(&x, &mut ctx, sel_gc, &mut state),
|
||||
Event::Expose(_) => redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state),
|
||||
Event::ConfigureNotify(ev) => {
|
||||
state.win_w = ev.width as i32;
|
||||
state.win_h = ev.height as i32;
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
Event::ButtonPress(ev) => {
|
||||
if ev.detail != 1 {
|
||||
@@ -946,7 +1016,7 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
state.selected_id = id;
|
||||
state.handles_on = true;
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
continue;
|
||||
}
|
||||
state.selected_id = id;
|
||||
@@ -956,7 +1026,7 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
grab_dx = mx - p.x;
|
||||
grab_dy = my - p.y;
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
} else {
|
||||
last_press_ms = t;
|
||||
last_press_id = -1;
|
||||
@@ -964,14 +1034,14 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
state.handles_on = false;
|
||||
state.selected_id = -1;
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
if state.sel_type >= 0 && my < state.win_h - K_STATUS_H {
|
||||
let t = state.sel_type as usize;
|
||||
let w = registry::WIDGETS[t].default_w as i32;
|
||||
let h = registry::WIDGETS[t].default_h as i32;
|
||||
if state.place_widget(&styles, t, mx - w / 2, my - h / 2, w, h) >= 0 {
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
if state.place_widget(t, mx - w / 2, my - h / 2, w, h) >= 0 {
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -984,21 +1054,21 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
drag_start_x = mx;
|
||||
drag_start_y = my;
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
} else if dragging && state.selected_id >= 0 {
|
||||
if let Some(p) = state.find_placed(state.selected_id) {
|
||||
p.x = (mx - grab_dx).max(0);
|
||||
p.y = (my - grab_dy).max(0);
|
||||
let (nx, ny, nw, nh) = (p.x, p.y, p.w, p.h);
|
||||
p.wdg.set_bounds(nx as i16, ny as i16, nw as u16, nh as u16);
|
||||
p.wdg.set_bounds(nx, ny, nw, nh);
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
} else {
|
||||
for p in state.placed.iter_mut() {
|
||||
if p.wdg.contains(mx as i16, my as i16) {
|
||||
p.wdg.on_motion(mx as i16, my as i16);
|
||||
if p.wdg.contains(mx, my) {
|
||||
p.wdg.on_motion(mx, my);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1006,11 +1076,12 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
Event::ButtonRelease(ev) => {
|
||||
if handle_drag || dragging {
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
if state.selected_id >= 0 {
|
||||
if let Some(p) = state.find_placed(state.selected_id) {
|
||||
p.wdg.on_button_release(ev.event_x, ev.event_y, 1);
|
||||
p.wdg
|
||||
.on_button_release(ev.event_x as i32, ev.event_y as i32, 1);
|
||||
}
|
||||
}
|
||||
dragging = false;
|
||||
@@ -1022,19 +1093,19 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
KEY_DELETE | KEY_BACKSPACE => {
|
||||
if state.selected_id >= 0 {
|
||||
state.delete_widget(state.selected_id);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
}
|
||||
KEY_ESCAPE => {
|
||||
state.handles_on = false;
|
||||
state.selected_id = -1;
|
||||
state.write_state();
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
0x73 => {
|
||||
// 's': save layout.
|
||||
state.save_layout("/tmp/lightgui-layout.txt");
|
||||
redraw(&x, &mut ctx, sel_gc, &mut state);
|
||||
redraw(&x, &mut ctx, sel_gc, &mut styles, &mut state);
|
||||
}
|
||||
0x71 => break, // 'q'
|
||||
_ => {}
|
||||
@@ -1047,7 +1118,7 @@ fn run() -> Result<i32, Box<dyn std::error::Error>> {
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc);
|
||||
poll_selection(&x, &mut state, &mut ctx, sel_gc, &mut styles);
|
||||
}
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
@@ -24,13 +24,17 @@
|
||||
//! released diff > 50; otherwise NORESP (the widget drew but did
|
||||
//! not visibly respond).
|
||||
//!
|
||||
//! Honesty note: the widgets under test are currently the STUB
|
||||
//! placeholders in `xframe-common` (see its `widget` SEAM docs),
|
||||
//! because `xframe-widgets` has not landed yet. These counts
|
||||
//! therefore measure the DEMO HARNESS (launch/draw/respond/exit
|
||||
//! for every registry name), not pixel-parity with the C++ widgets.
|
||||
//! Re-run this driver after the `xframe-widgets` swap for real
|
||||
//! per-widget parity numbers.
|
||||
//! Honesty note: the widgets under test are the REAL `xframe-widgets`
|
||||
//! implementations (one of the 14 archetypes per widget), wired into
|
||||
//! `xframe-demo` via `xframe_common::CtxTarget`. The counts therefore
|
||||
//! measure the real widgets (launch/draw/respond/exit for every registry
|
||||
//! name). Two caveats, both deliberate and documented in the demo:
|
||||
//! geometry is the registry default size, not the per-widget geometry
|
||||
//! of the C++ mains, and the label text is the registry `kind`, not
|
||||
//! each C++ constructor's own default text — so the screenshots are
|
||||
//! not pixel-identical to the C++ ones; compare them with the
|
||||
//! pixel-diff step (see `tools/pixel_diff.py`) and read the diffs,
|
||||
//! don't expect zero.
|
||||
//!
|
||||
//! Usage:
|
||||
//! xframe-test-runner [--demo PATH] [--display :99] [--limit N]
|
||||
|
||||
Reference in new issue
Block a user