Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table fit and add manual system init for test #978

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions namui/namui-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub enum Target {
Wasm32WasiWeb,
#[value(name = "x86_64-pc-windows-msvc")]
X86_64PcWindowsMsvc,
#[value(name = "x86_64-unknown-linux-gnu")]
X86_64UnknownLinuxGnu,
}
impl Display for Target {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -70,6 +72,7 @@ impl Display for Target {
match self {
Target::Wasm32WasiWeb => "wasm32-wasi-web",
Target::X86_64PcWindowsMsvc => "x86_64-pc-windows-msvc",
Target::X86_64UnknownLinuxGnu => "x86_64-unknown-linux-gnu",
}
)
}
Expand All @@ -79,6 +82,7 @@ impl From<namui_user_config::Target> for Target {
match target {
namui_user_config::Target::Wasm32WasiWeb => Target::Wasm32WasiWeb,
namui_user_config::Target::X86_64PcWindowsMsvc => Target::X86_64PcWindowsMsvc,
namui_user_config::Target::X86_64UnknownLinuxGnu => Target::X86_64UnknownLinuxGnu,
}
}
}
Expand All @@ -87,6 +91,7 @@ impl From<Target> for namui_user_config::Target {
match val {
Target::Wasm32WasiWeb => namui_user_config::Target::Wasm32WasiWeb,
Target::X86_64PcWindowsMsvc => namui_user_config::Target::X86_64PcWindowsMsvc,
Target::X86_64UnknownLinuxGnu => namui_user_config::Target::X86_64UnknownLinuxGnu,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions namui/namui-cli/src/procedures/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub async fn build(target: Target, manifest_path: PathBuf, release: bool) -> Res
Target::X86_64PcWindowsMsvc => {
linux::x86_64_pc_windows_msvc::build(&manifest_path, release).await?
}
Target::X86_64UnknownLinuxGnu => todo!(),
}
}
} else if cfg!(target_os = "windows") {
Expand Down
19 changes: 19 additions & 0 deletions namui/namui-cli/src/procedures/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ pub async fn check(target: Target, manifest_path: PathBuf) -> Result<()> {
.wait()
.await?;
}
Target::X86_64UnknownLinuxGnu => {
let mut args = vec![];
args.extend([
"check",
"--target",
"x86_64-unknown-linux-gnu",
"--manifest-path",
manifest_path.to_str().unwrap(),
"--tests",
]);

Command::new("cargo")
.args(args)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()?
.wait()
.await?;
}
}

Ok(())
Expand Down
19 changes: 19 additions & 0 deletions namui/namui-cli/src/procedures/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ pub async fn clippy(target: Target, manifest_path: PathBuf) -> Result<()> {
.wait()
.await?;
}
Target::X86_64UnknownLinuxGnu => {
let mut args = vec![];
args.extend([
"clippy",
"--target",
"x86_64-unknown-linux-gnu",
"--manifest-path",
manifest_path.to_str().unwrap(),
"--tests",
]);

Command::new("cargo")
.args(args)
.stdout(std::process::Stdio::inherit())
.stderr(std::process::Stdio::inherit())
.spawn()?
.wait()
.await?;
}
}

Ok(())
Expand Down
1 change: 1 addition & 0 deletions namui/namui-cli/src/procedures/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod wasm32_wasi_web;
pub mod x86_64_pc_windows_msvc;
pub mod x86_64_unknown_linux_gnu;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod test;

pub use test::*;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::*;
use std::process::Command;

pub fn test(manifest_path: impl AsRef<std::path::Path>) -> Result<()> {
Command::new("cargo")
.args(["test", "--target", "x86_64-unknown-linux-gnu"])
.status()?;

Ok(())
}
1 change: 1 addition & 0 deletions namui/namui-cli/src/procedures/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub async fn start(target: Target, manifest_path: PathBuf, release: bool) -> Res
Target::X86_64PcWindowsMsvc => {
linux::x86_64_pc_windows_msvc::start(&manifest_path, release).await?
}
Target::X86_64UnknownLinuxGnu => todo!(),
}
}
} else if cfg!(target_os = "windows") {
Expand Down
3 changes: 3 additions & 0 deletions namui/namui-cli/src/procedures/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub fn test(target: Target, manifest_path: PathBuf) -> Result<()> {
match target {
Target::Wasm32WasiWeb => linux::wasm32_wasi_web::test(&manifest_path)?,
Target::X86_64PcWindowsMsvc => linux::x86_64_pc_windows_msvc::test(&manifest_path)?,
Target::X86_64UnknownLinuxGnu => {
linux::x86_64_unknown_linux_gnu::test(&manifest_path)?
}
}
}
} else {
Expand Down
13 changes: 12 additions & 1 deletion namui/namui-cli/src/services/resource_collect_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ fn collect_runtime(
));
}
Target::X86_64PcWindowsMsvc => {}
Target::X86_64UnknownLinuxGnu => {}
}
if let Some(additional_runtime_path) = additional_runtime_path {
ops.push(CollectOperation::new(
Expand Down Expand Up @@ -99,6 +100,16 @@ fn collect_rust_build(
PathBuf::from(""),
));
}
Target::X86_64UnknownLinuxGnu => {
let build_dist_path = project_path.join(format!(
"target/namui/target/x86_64-unknown-linux-gnu/{}",
if release { "release" } else { "debug" }
));
ops.push(CollectOperation::new(
build_dist_path.join("namui-runtime-x86_64-unknown-linux-gnu"),
PathBuf::from(""),
));
}
}
Ok(())
}
Expand All @@ -120,7 +131,7 @@ fn collect_deep_link_manifest(
let _ = ops;
match target {
Target::Wasm32WasiWeb => {}
Target::X86_64PcWindowsMsvc => {
Target::X86_64PcWindowsMsvc | Target::X86_64UnknownLinuxGnu => {
// TODO, but not priority
}
}
Expand Down
6 changes: 6 additions & 0 deletions namui/namui-cli/src/services/rust_build_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ async fn run_build_process(build_option: &BuildOption) -> Result<Output> {
.output()
.await?)
}
Target::X86_64UnknownLinuxGnu => todo!(),
}
}

Expand All @@ -121,6 +122,11 @@ fn get_envs(build_option: &BuildOption) -> Vec<(&str, &str)> {
("NAMUI_CFG_TARGET_OS", "windows"),
("NAMUI_CFG_TARGET_ENV", "msvc"),
],
Target::X86_64UnknownLinuxGnu => vec![
("NAMUI_CFG_TARGET_ARCH", "x86_64"),
("NAMUI_CFG_TARGET_OS", "linux"),
("NAMUI_CFG_TARGET_ENV", "gnu"),
],
};

if build_option.watch {
Expand Down
150 changes: 44 additions & 106 deletions namui/namui-prebuilt/src/table/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(test)]
mod tests;

use namui::*;
use std::collections::HashMap;

Expand Down Expand Up @@ -215,10 +218,12 @@ impl Component for InternalSlice<'_> {
};
units.push(unit);

let need_clip = fit_bounding_box_map.get(&index).is_some();

intermediates.push(Intermediate {
key,
render,
need_clip: true,
need_clip,
table_cell_type: TableCellType::Fit { align },
});
}
Expand Down Expand Up @@ -256,8 +261,10 @@ impl Component for InternalSlice<'_> {
let pixel_sizes = pixel_size_or_ratio_list.iter().map(|(pixel_size, ratio)| {
if let Some(pixel_size) = pixel_size {
*pixel_size
} else if let Some(ratio) = ratio {
(direction_pixel_size - non_ratio_pixel_size_sum) * *ratio / ratio_sum
} else {
(direction_pixel_size - non_ratio_pixel_size_sum) * ratio.unwrap() / ratio_sum
0.px()
}
});

Expand Down Expand Up @@ -294,8 +301,8 @@ impl Component for InternalSlice<'_> {
},
};

let rendering_tree = ctx.ghost_compose(key, |ctx| {
let mut ctx = ctx.translate((xywh.x(), xywh.y()));
ctx.compose_with_key(key, |mut ctx| {
ctx = ctx.translate((xywh.x(), xywh.y()));

if let TableCellType::Fit { align } = table_cell_type {
let bounding_box = fit_bounding_box_map.get(&index);
Expand All @@ -319,31 +326,42 @@ impl Component for InternalSlice<'_> {
ctx = ctx.translate((x, y));
}
}
ctx.compose(|ctx| {
let rendering_tree = ctx.ghost_compose(0, |mut ctx| {
if need_clip {
ctx = ctx.clip(
Path::new().add_rect(Rect::Xywh {
x: px(0.0),
y: px(0.0),
width: xywh.width(),
height: xywh.height(),
}),
ClipOp::Intersect,
);
}
render(direction, xywh.wh(), ctx);
});

if need_clip {
ctx = ctx.clip(
Path::new().add_rect(Rect::Xywh {
x: px(0.0),
y: px(0.0),
width: xywh.width(),
height: xywh.height(),
}),
ClipOp::Intersect,
);
}
render(direction, xywh.wh(), ctx);
});

if let TableCellType::Fit { .. } = table_cell_type {
let bounding_box = namui::bounding_box(&rendering_tree);
set_bounding_box_map.mutate({
move |bounding_box_map| {
bounding_box_map.insert(index, bounding_box);
if let TableCellType::Fit { .. } = table_cell_type {
let is_first_draw = fit_bounding_box_map.get(&index).is_none();
let bounding_box = namui::bounding_box(&rendering_tree);
println!("xywh: {:?}", xywh);
println!("rendering_tree: {:#?}", rendering_tree);
println!("bounding_box: {:?}", bounding_box);
namse marked this conversation as resolved.
Show resolved Hide resolved
set_bounding_box_map.mutate({
move |bounding_box_map| {
bounding_box_map.insert(index, bounding_box);
}
});

if !is_first_draw {
ctx.add(rendering_tree);
}
} else {
ctx.add(rendering_tree);
}
});
}

ctx.add(rendering_tree);
});

advanced_pixel_size += pixel_size;
}
Expand Down Expand Up @@ -446,83 +464,3 @@ pub fn fit<'a>(
}),
}
}

// #[cfg(test)]
// mod tests {
// use super::*;
// use std::sync::atomic::AtomicBool;

// #[test]
// fn closure_should_give_right_wh() {
// let button_render_called = AtomicBool::new(false);
// let label_render_called = AtomicBool::new(false);
// let body_render_called = AtomicBool::new(false);
// let body_inner_render_called = AtomicBool::new(false);

// let button = calculative(
// |parent_wh| parent_wh.height,
// |wh, ctx| {
// button_render_called.store(true, std::sync::atomic::Ordering::Relaxed);
// assert_eq!(px(20.0), wh.width);
// assert_eq!(px(20.0), wh.height);
// },
// );

// let label = ratio(1, |wh, ctx| {
// label_render_called.store(true, std::sync::atomic::Ordering::Relaxed);
// assert_eq!(px(280.0), wh.width);
// assert_eq!(px(20.0), wh.height);
// });

// let header = fixed(px(20.0), horizontal([("button", button), ("label", label)]));

// let body = ratio(1.0, |wh, ctx| {
// body_render_called.store(true, std::sync::atomic::Ordering::Relaxed);
// assert_eq!(px(300.0), wh.width);
// assert_eq!(px(480.0), wh.height);
// vertical([
// (
// "0",
// ratio(
// 1,
// padding(5.px(), |wh, ctx| {
// body_inner_render_called
// .store(true, std::sync::atomic::Ordering::Relaxed);
// assert_eq!(px(290.0), wh.width);
// assert_eq!(px(470.0), wh.height);
// }),
// ),
// ),
// // Note: RenderingTree is not testable yet, So you cannot test fit well now.
// ("empty", fit(FitAlign::LeftTop, RenderingTree::Empty)),
// ])(wh, ctx)
// });

// let ctx = todo!();

// vertical([header, body])(
// Wh {
// width: px(300.0),
// height: px(500.0),
// },
// ctx,
// );

// assert_eq!(
// true,
// button_render_called.load(std::sync::atomic::Ordering::Relaxed)
// );
// assert_eq!(
// true,
// label_render_called.load(std::sync::atomic::Ordering::Relaxed)
// );
// assert_eq!(
// true,
// body_render_called.load(std::sync::atomic::Ordering::Relaxed)
// );
// assert_eq!(
// true,
// body_inner_render_called.load(std::sync::atomic::Ordering::Relaxed)
// );
// }
// }
Loading
Loading