From d654714c0d59a53d1a398c0c0f03a56ab041efed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kav=C3=ADk?= Date: Sun, 9 Jun 2024 19:41:35 +0200 Subject: [PATCH] format BinaryWithGroups and ASCII --- frontend/src/platform/browser.rs | 7 ++- frontend/src/waveform_panel.rs | 3 +- frontend/typescript/bundles/pixi_canvas.js | 2 +- .../typescript/pixi_canvas/pixi_canvas.ts | 2 +- shared/src/var_format.rs | 52 ++++++++++++------- src-tauri/src/lib.rs | 7 ++- 6 files changed, 46 insertions(+), 27 deletions(-) diff --git a/frontend/src/platform/browser.rs b/frontend/src/platform/browser.rs index f0d2ce6..dfd4041 100644 --- a/frontend/src/platform/browser.rs +++ b/frontend/src/platform/browser.rs @@ -102,7 +102,8 @@ fn signal_to_timeline( block_height: u32, ) -> shared::Timeline { const MIN_BLOCK_WIDTH: u32 = 3; - const LETTER_WIDTH: u32 = 15; + // Courier New, 16px, sync with `label_style` in `pixi_canvas.rs` + const LETTER_WIDTH: f64 = 9.61; const LETTER_HEIGHT: u32 = 21; const LABEL_X_PADDING: u32 = 10; @@ -137,9 +138,11 @@ fn signal_to_timeline( continue; } + // @TODO cache? let value = shared::VarFormat::default().format(value); - let value_width = value.chars().count() as u32 * LETTER_WIDTH; + let value_width = (value.chars().count() as f64 * LETTER_WIDTH) as u32; + // @TODO Ellipsis instead of hiding? let label = if (value_width + (2 * LABEL_X_PADDING)) <= block_width { Some(shared::TimeLineBlockLabel { text: value, diff --git a/frontend/src/waveform_panel.rs b/frontend/src/waveform_panel.rs index 5cdac9f..87e74ad 100644 --- a/frontend/src/waveform_panel.rs +++ b/frontend/src/waveform_panel.rs @@ -155,13 +155,14 @@ impl WaveformPanel { let (hovered, hovered_signal) = Mutable::new_and_signal(false); Button::new() .s(Height::exact(ROW_HEIGHT)) + .s(Width::growable()) .s(Background::new().color_signal( hovered_signal.map_bool(|| color!("SlateBlue"), || color!("SlateBlue", 0.8)), )) .s(RoundedCorners::new().left(15).right(5)) .label( El::new() - .s(Align::center()) + .s(Align::new().left()) .s(Padding::new().left(20).right(17).y(10)) .child(name), ) diff --git a/frontend/typescript/bundles/pixi_canvas.js b/frontend/typescript/bundles/pixi_canvas.js index 435ea5f..c253d19 100644 --- a/frontend/typescript/bundles/pixi_canvas.js +++ b/frontend/typescript/bundles/pixi_canvas.js @@ -35221,7 +35221,7 @@ var VarSignalRow = class { align: "center", fill: "White", fontSize: 16, - fontFamily: 'system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"' + fontFamily: '"Courier New", monospace' }); constructor(signal_ref_index, timeline, app, owner, rows_container, row_height, row_gap) { this.signal_ref_index = signal_ref_index; diff --git a/frontend/typescript/pixi_canvas/pixi_canvas.ts b/frontend/typescript/pixi_canvas/pixi_canvas.ts index cb69e41..390ac21 100644 --- a/frontend/typescript/pixi_canvas/pixi_canvas.ts +++ b/frontend/typescript/pixi_canvas/pixi_canvas.ts @@ -125,7 +125,7 @@ class VarSignalRow { align: "center", fill: "White", fontSize: 16, - fontFamily: 'system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"', + fontFamily: '"Courier New", monospace', }); constructor( diff --git a/shared/src/var_format.rs b/shared/src/var_format.rs index 1185ba4..4f9a51c 100644 --- a/shared/src/var_format.rs +++ b/shared/src/var_format.rs @@ -3,10 +3,9 @@ pub enum VarFormat { ASCII, Binary, BinaryWithGroups, - // #[default] + #[default] Hexadecimal, Octal, - #[default] Signed, Unsigned, } @@ -44,16 +43,29 @@ impl VarFormat { } match self { VarFormat::ASCII => { - // @TODO - value - }, - VarFormat::Binary => { - value - }, + let mut formatted_value = String::new(); + for group_index in 0..value.len() / 8 { + let offset = group_index * 8; + let group = &value[offset..offset + 8]; + if let Ok(byte_char) = u8::from_str_radix(group, 2) { + formatted_value.push(byte_char as char); + } + } + formatted_value + } + VarFormat::Binary => value, VarFormat::BinaryWithGroups => { - // @TODO value - }, + .chars() + .enumerate() + .fold(String::new(), |mut value, (index, one_or_zero)| { + value.push(one_or_zero); + if (index + 1) % 4 == 0 { + value.push(' '); + } + value + }) + } VarFormat::Hexadecimal => { let ones_and_zeros = value .chars() @@ -68,7 +80,7 @@ impl VarFormat { .map(|number| char::from_digit(number, 16).unwrap()) .collect(); value - }, + } VarFormat::Octal => { let ones_and_zeros = value .chars() @@ -83,7 +95,7 @@ impl VarFormat { .map(|number| char::from_digit(number, 8).unwrap()) .collect(); value - }, + } VarFormat::Signed => { let mut ones_and_zeros = value .chars() @@ -92,16 +104,16 @@ impl VarFormat { .collect::>(); // https://builtin.com/articles/twos-complement - let sign = if ones_and_zeros.last().unwrap() == &0 { "" } else { "-" }; + let sign = if ones_and_zeros.last().unwrap() == &0 { + "" + } else { + "-" + }; if sign == "-" { let mut one_found = false; for one_or_zero in &mut ones_and_zeros { if one_found { - *one_or_zero = if one_or_zero == &0 { - 1 - } else { - 0 - } + *one_or_zero = if one_or_zero == &0 { 1 } else { 0 } } else if one_or_zero == &1 { one_found = true; } @@ -118,7 +130,7 @@ impl VarFormat { // @TODO chain `sign` before collecting? let value = sign.to_owned() + &value_without_sign; value - }, + } VarFormat::Unsigned => { let ones_and_zeros = value .chars() @@ -133,7 +145,7 @@ impl VarFormat { .map(|number| char::from_digit(number, 10).unwrap()) .collect(); value - }, + } } } } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 3ba64ea..c51649b 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -96,7 +96,8 @@ fn signal_to_timeline( block_height: u32, ) -> shared::Timeline { const MIN_BLOCK_WIDTH: u32 = 3; - const LETTER_WIDTH: u32 = 15; + // Courier New, 16px, sync with `label_style` in `pixi_canvas.rs` + const LETTER_WIDTH: f64 = 9.61; const LETTER_HEIGHT: u32 = 21; const LABEL_X_PADDING: u32 = 10; @@ -131,9 +132,11 @@ fn signal_to_timeline( continue; } + // @TODO cache? let value = shared::VarFormat::default().format(value); - let value_width = value.chars().count() as u32 * LETTER_WIDTH; + let value_width = (value.chars().count() as f64 * LETTER_WIDTH) as u32; + // @TODO Ellipsis instead of hiding? let label = if (value_width + (2 * LABEL_X_PADDING)) <= block_width { Some(shared::TimeLineBlockLabel { text: value,