Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Keep a big cost from painting over its caption (#1074)

BFlat804a05510d3d
diff --git a/crates/ui-egui/src/project.rs b/crates/ui-egui/src/project.rs
index e825cf1c..01f1d761 100644
--- a/crates/ui-egui/src/project.rs
+++ b/crates/ui-egui/src/project.rs
@@ -1167,16 +1167,27 @@ fn steps(app: &CraftApp, ui: &mut Ui, ws: &Workspace) {
});
}
-/// A caption on the left and its value on the right.
+/// A caption on the left and its value on the right, or the value on a line of its own when the
+/// two would overlap: a right-to-left layout doesn't make room for what's already on the row.
fn stat_row(ui: &mut Ui, label: &str, value: &str) -> egui::Response {
let t = Tokens::get(ui.ctx());
- ui.horizontal(|ui| {
- widgets::caption(ui, label);
- ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
- ui.label(RichText::new(value).font(theme::mono(13.0)).color(t.text));
- });
- })
- .response
+ let text = RichText::new(value).font(theme::mono(13.0)).color(t.text);
+ let value_width =
+ egui::WidgetText::from(text.clone()).into_galley(ui, Some(egui::TextWrapMode::Extend), f32::INFINITY, egui::FontSelection::Default).size().x;
+ let caption_width = widgets::label_galley(ui, &tr(label), 12.0, t.text_faint).size().x;
+ if caption_width + ui.spacing().item_spacing.x + value_width <= ui.available_width() {
+ ui.horizontal(|ui| {
+ widgets::caption(ui, label);
+ ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| ui.label(text));
+ })
+ .response
+ } else {
+ ui.vertical(|ui| {
+ widgets::caption(ui, label);
+ ui.with_layout(egui::Layout::top_down(egui::Align::Max), |ui| ui.label(text));
+ })
+ .response
+ }
}
fn feed(app: &mut CraftApp, ui: &mut Ui, ws: &Workspace) {
diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rs
index da5b4af7..c5894021 100644
--- a/crates/ui-egui/tests/smoke.rs
+++ b/crates/ui-egui/tests/smoke.rs
@@ -2053,6 +2053,30 @@ fn a_projects_saved_time_and_ai_use_show_beside_it_and_the_session_total_in_the_
h.get_by_label("about $3.21 (estimate)");
}
+#[test]
+fn a_big_cost_doesnt_paint_over_its_caption() {
+ use craft_anything_engine::workspace::ProjectUsage;
+ let d = tempfile::tempdir().unwrap();
+ let root = d.path().to_path_buf();
+ ProjectUsage::update(&root, |u| {
+ u.work_ms = 725_000;
+ u.add_turn(1_180_000, 42_000, Some(13_021.36), true);
+ })
+ .unwrap();
+ let mut h = harness(move || {
+ let mut a = demo::app(demo::engine(Mode::Beginner, true));
+ demo::fill_project(&mut a);
+ if let Some(ws) = a.workspace.as_mut() {
+ ws.root = root.clone();
+ }
+ a
+ });
+ h.run_steps(3);
+ let caption = h.get_by_label("AI cost (estimate)").rect();
+ let value = h.get_by_label("about $13021.36").rect();
+ assert!(!caption.intersects(value), "{caption:?} and {value:?}");
+}
+
#[test]
fn ghidras_memory_is_automatic_until_the_person_sets_it() {
let mut h = harness_sized(egui::vec2(1280.0, 1000.0), || {