Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ target
# needs at the time, and tracking it would mean a second lock file to update on every
# dependency change, plus a dirty working tree every time the test runs.
compose-rust/tests/fixtures/consumer/Cargo.lock
compose-rust/tests/fixtures/hand-check/Cargo.lock

# The Host's wasm module, built next to the page by
# renderer/web/scripts/build-host.sh. The loader beside it is generated and
Expand Down
32 changes: 31 additions & 1 deletion compose-rust/tests/fixtures/consumer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
//! each press of the button. That is the only way the check can tell that an event arrived
//! all the way at the Host.
//!
//! `--parity` is that window with a title and a smallest size of its own, for
//! `scripts/parity/linux.sh`, which reads both back from the display server.
//!
//! The call to launch stays in the binary because the branch is decided at run time.
//! That is what makes the renderer a load-time dependency of this executable rather than
//! a library the linker drops for being unused.

use compose_rust::boundary::STATUS_OK;
use compose_rust::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError};
use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind};
use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind, Window};
use compose_rust::{Batch, LaunchBuilder, Runtime};
use std::ffi::c_int;
use std::sync::atomic::{AtomicU32, Ordering};
Expand Down Expand Up @@ -151,6 +154,10 @@ impl Runtime for Screen {
}
}

/// The smallest size `--parity` asks the window to allow, which the parity check reads back.
const PARITY_MIN_WIDTH: u16 = 300;
const PARITY_MIN_HEIGHT: u16 = 260;

const FORM: u32 = 10;
const FIELD: u32 = 11;
const SAVE: u32 = 12;
Expand All @@ -160,12 +167,23 @@ const ON_CLICK: u64 = 2;
/// A column holding a text field and a button, and nothing else.
struct Form {
batch: Batch,
/// Asks for a window of its own: a title and a smallest size, which the parity check reads
/// back from the display server on every path.
parity: bool,
}

impl Form {
fn new() -> Self {
Self {
batch: Batch::new(),
parity: false,
}
}

fn for_parity() -> Self {
Self {
batch: Batch::new(),
parity: true,
}
}

Expand Down Expand Up @@ -196,6 +214,14 @@ impl Runtime for Form {
}

fn rebuild(&mut self) {
if self.parity {
self.batch.write(Mutation::SetWindow(Window {
title: "compose-rust parity",
min_width: PARITY_MIN_WIDTH,
min_height: PARITY_MIN_HEIGHT,
..Window::new()
}));
}
for (node_id, widget) in [
(FORM, WidgetKind::Column),
(FIELD, WidgetKind::TextField),
Expand Down Expand Up @@ -301,6 +327,10 @@ fn main() {
LaunchBuilder::new().launch_runtime(|| Box::new(Form::new()) as Box<dyn Runtime>);
return;
}
if std::env::args().any(|argument| argument == "--parity") {
LaunchBuilder::new().launch_runtime(|| Box::new(Form::for_parity()) as Box<dyn Runtime>);
return;
}
if std::env::args().any(|argument| argument == "--launch") {
LaunchBuilder::new().launch_runtime(|| Box::new(Screen::new(false)) as Box<dyn Runtime>);
return;
Expand Down
16 changes: 16 additions & 0 deletions compose-rust/tests/fixtures/hand-check/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# A window to check by hand on a real desktop: text shortcuts, live resize, an input method,
# focus between two fields, a click and a scrolling list with Korean in it.
#
# Like the consumer fixture next to it, this is its own workspace root, so `cargo run` works
# in this directory on Windows, macOS and Linux whatever the enclosing workspace says. It
# names its edition outright for the same reason: an excluded crate cannot inherit one.
[workspace]

[package]
name = "hand-check"
version = "0.0.0"
edition = "2024"
publish = false

[dependencies]
compose-rust = { path = "../../.." }
35 changes: 35 additions & 0 deletions compose-rust/tests/fixtures/hand-check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# hand-check

A window to check a renderer by hand on a real desktop. It has a click counter, two text
fields, a line showing the window's size class, and a scrolling list with Korean rows. What
the fields hold is echoed under each, and every change is printed to standard output as an
`event:` line.

Set `COMPOSE_RUST_RENDERER_DIR` to the renderer you want to try, then run it from this
directory. The commands are the same on every desktop apart from how the variable is set.

```sh
# macOS and Linux
COMPOSE_RUST_RENDERER_DIR=/path/to/renderer/dist cargo run --release
```

```powershell
# Windows (PowerShell). The renderer's bin folder has to be on PATH as well.
$env:COMPOSE_RUST_RENDERER_DIR = "C:\path\to\renderer\dist"
$env:PATH = "C:\path\to\renderer\dist\bin;$env:PATH"
cargo run --release
```

On Linux the X11 window is the default; nothing needs to be set.

## What to check

| Check | How | What you should see |
| --- | --- | --- |
| Text shortcuts | In the first field select with ctrl or cmd+A, then C, X, V and Z | The echo line under the field shows the paste, the cut and the undo; `event: first ...` lines print |
| Clipboard across apps | Copy in another app, paste here, and the other way round | The text, Korean included, arrives |
| Live resize | Drag an edge and a corner, at 100% and at 200% scale | Content follows the edge with no lag or flash; the `window:` line shows the size class and size when it changes |
| Input method | Type Korean (`han-geul`) in a field | Underlined text while composing, and the candidate window beside the caret, not at the corner |
| Focus | Click each field, press Tab | Only one field has the caret, and typing goes to it |
| Click | Press the button | The counter counts and `event: click N` prints |
| Scrolling | Scroll the list with the wheel and the scrollbar | 60 rows move smoothly |
189 changes: 189 additions & 0 deletions compose-rust/tests/fixtures/hand-check/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
//! A window for checking a renderer by hand. Nothing in it is a test: a person uses it and
//! reads what it says, on screen and as `event:` lines on standard output.
//!
//! What is in the window, top to bottom:
//!
//! - a label counting clicks, and the button that counts them;
//! - two text fields, to see which has the keyboard, with what each holds echoed below it, so
//! that what ctrl or cmd with C, V, X, Z and A did is on screen: a paste shows up as the new
//! text, an undo as the text going back, a cut as the text shrinking;
//! - the window's size class and size, as the renderer last reported them (it reports when
//! the class changes, not on every pixel of a resize);
//! - a scrolling list of 60 rows with Korean in them.
//!
//! For an input method, type Korean in either field and watch where the candidate window
//! opens: it should be beside the caret.
//!
//! The renderer comes from `COMPOSE_RUST_RENDERER_DIR`, as it does for any application.

use compose_rust::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError};
use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind, Window};
use compose_rust::{Batch, LaunchBuilder, Runtime};

const COLUMN: u32 = 1;
const CLICKS: u32 = 2;
const BUTTON: u32 = 3;
const FIRST: u32 = 4;
const FIRST_ECHO: u32 = 5;
const SECOND: u32 = 6;
const SECOND_ECHO: u32 = 7;
const SIZE: u32 = 8;
const LIST: u32 = 9;
const ROW0: u32 = 100;

const ON_CLICK: i64 = 1;
const ON_FIRST: i64 = 2;
const ON_SECOND: i64 = 3;

struct App {
batch: Batch,
clicks: u32,
first: String,
second: String,
size: String,
dirty: bool,
}

impl App {
fn text(&mut self, node_id: u32, text: &str) {
self.batch.write(Mutation::SetProp {
node_id,
property: PropertyKind::Text,
value: PropertyValue::String(text),
});
}

fn handler(&mut self, node_id: u32, property: PropertyKind, id: i64) {
self.batch.write(Mutation::SetProp {
node_id,
property,
value: PropertyValue::Integer(id),
});
}
}

impl Runtime for App {
fn batch(&self) -> &Batch {
&self.batch
}

fn batch_mut(&mut self) -> &mut Batch {
&mut self.batch
}

fn rebuild(&mut self) {
let mut window = Window::new();
window.title = "compose-rust hand check";
window.width = 480;
window.height = 700;
window.min_width = 320;
window.min_height = 320;
window.resizable = true;
self.batch.write(Mutation::SetWindow(window));
for (node_id, widget) in [
(COLUMN, WidgetKind::Column),
(CLICKS, WidgetKind::Text),
(BUTTON, WidgetKind::Button),
(FIRST, WidgetKind::TextField),
(FIRST_ECHO, WidgetKind::Text),
(SECOND, WidgetKind::TextField),
(SECOND_ECHO, WidgetKind::Text),
(SIZE, WidgetKind::Text),
(LIST, WidgetKind::ScrollColumn),
] {
self.batch.write(Mutation::Create { node_id, widget });
}
self.text(CLICKS, "clicks: 0");
self.text(BUTTON, "Click me");
self.text(FIRST, "first field: select, copy, paste, undo here");
self.text(FIRST_ECHO, "first holds: first field: select, copy, paste, undo here");
self.text(SECOND, "second field");
self.text(SECOND_ECHO, "second holds: second field");
self.text(SIZE, "window: not reported yet");
self.handler(BUTTON, PropertyKind::OnClick, ON_CLICK);
self.handler(FIRST, PropertyKind::OnValueChange, ON_FIRST);
self.handler(SECOND, PropertyKind::OnValueChange, ON_SECOND);
for row in 0..60u32 {
self.batch.write(Mutation::Create {
node_id: ROW0 + row,
widget: WidgetKind::Text,
});
self.text(ROW0 + row, &format!("List row {row} \u{d55c}\u{ae00} \u{c785}\u{b825}"));
self.batch.write(Mutation::Insert {
parent_id: LIST,
node_id: ROW0 + row,
index: row,
});
}
for (index, node_id) in [CLICKS, BUTTON, FIRST, FIRST_ECHO, SECOND, SECOND_ECHO, SIZE, LIST]
.into_iter()
.enumerate()
{
self.batch.write(Mutation::Insert {
parent_id: COLUMN,
node_id,
index: index as u32,
});
}
}

fn render(&mut self) {
if !self.dirty {
return;
}
self.dirty = false;
let clicks = format!("clicks: {}", self.clicks);
let first = format!("first holds: {}", self.first);
let second = format!("second holds: {}", self.second);
let size = format!("window: {}", self.size);
self.text(CLICKS, &clicks);
self.text(FIRST_ECHO, &first);
self.text(SECOND_ECHO, &second);
self.text(SIZE, &size);
}

fn handle_event(&mut self, event: &HostEvent<'_>) -> Result<i64, ProtocolError> {
match (event.handler_id as i64, &event.payload) {
(ON_CLICK, EventPayload::Clicked) => {
self.clicks += 1;
println!("event: click {}", self.clicks);
}
(ON_FIRST, EventPayload::TextChanged(text)) => {
self.first = (*text).to_owned();
println!("event: first {text}");
}
(ON_SECOND, EventPayload::TextChanged(text)) => {
self.second = (*text).to_owned();
println!("event: second {text}");
}
(
_,
EventPayload::WindowSizeChanged {
width_dp,
height_dp,
class,
..
},
) => {
self.size = format!("{width_dp:.0} x {height_dp:.0} dp, {class:?}");
println!("event: window {width_dp:.0}x{height_dp:.0} {class:?}");
}
_ => return Err(ProtocolError::InvalidValueKind(0)),
}
self.dirty = true;
Ok(0)
}
}

fn main() {
LaunchBuilder::new().launch_runtime(|| {
Box::new(App {
batch: Batch::new(),
clicks: 0,
first: "first field: select, copy, paste, undo here".to_owned(),
second: "second field".to_owned(),
size: "not reported yet".to_owned(),
dirty: false,
}) as Box<dyn Runtime>
});
}
Loading
Loading