Craft Anything: understand how any software works, and craft your own.
Rust
Put the switches under the prompt box on two lines when they don't fit (#989)
BFlata3da53ad4008
diff --git a/crates/ui-egui/src/project.rs b/crates/ui-egui/src/project.rsindex 96ac0e01..fd901f86 100644--- a/crates/ui-egui/src/project.rs+++ b/crates/ui-egui/src/project.rs@@ -1868,16 +1868,37 @@ fn questions_switch(app: &mut CraftApp, ui: &mut Ui) {let root = ws.root.clone();let t = Tokens::get(ui.ctx());ui.add_space(4.0);+ let (sending, asking) = (["Change it", "Just ask"], ["Ask me", "Decide yourself"]);+ let title = widgets::label_galley(ui, &tr(QUESTIONS_TITLE), theme::SMALL, t.text_dim).size().x;+ let gaps = ui.spacing().item_spacing.x * 3.0;+ // Side by side when both switches fit, else one under the other (a longer language, a narrow window).+ let fits = widgets::segmented_width(ui, &sending) + widgets::segmented_width(ui, &asking) + title + gaps <= ui.available_width();+ let hint = |ui: &mut Ui| {+ ui.add(egui::Label::new(RichText::new(tr("or start with /btw to just ask")).color(t.text_faint).size(theme::SMALL)).truncate());+ };+ let right = egui::Layout::right_to_left(egui::Align::Center);ui.horizontal(|ui| {// What sending does: ask for a change, or just ask about the build.- if let Some(i) = widgets::segmented(ui, &["Change it", "Just ask"], usize::from(app.state.just_ask), 24.0) {+ if let Some(i) = widgets::segmented(ui, &sending, usize::from(app.state.just_ask), 24.0) {app.state.just_ask = i == 1;}- ui.label(RichText::new(tr("or start with /btw to just ask")).color(t.text_faint).size(theme::SMALL));- ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| questions_controls(app, ui, now, &root, &t));+ if fits {+ ui.with_layout(right, |ui| {+ questions_controls(app, ui, now, &root, &t);+ ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), hint);+ });+ } else {+ hint(ui);+ }});+ if !fits {+ ui.add_space(4.0);+ ui.horizontal(|ui| ui.with_layout(right, |ui| questions_controls(app, ui, now, &root, &t)));+ }}+const QUESTIONS_TITLE: &str = "Builder's questions:";+fn questions_controls(app: &mut CraftApp, ui: &mut Ui, now: craft_anything_engine::workspace::Questions, root: &Path, t: &Tokens) {use craft_anything_engine::workspace::Questions;{@@ -1891,7 +1912,7 @@ fn questions_controls(app: &mut CraftApp, ui: &mut Ui, now: craft_anything_engin} else {tr("The builder answers its own questions and says what it chose (\"I decided: …\"), so you can change it.")};- ui.label(RichText::new(tr("Builder's questions:")).color(t.text_dim).size(theme::SMALL)).on_hover_text(tip);+ ui.label(RichText::new(tr(QUESTIONS_TITLE)).color(t.text_dim).size(theme::SMALL)).on_hover_text(tip);}}diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rsindex 83849692..6c078d33 100644--- a/crates/ui-egui/src/widgets.rs+++ b/crates/ui-egui/src/widgets.rs@@ -238,7 +238,7 @@ pub fn segmented(ui: &mut Ui, options: &[&str], selected: usize, height: f32) ->pub fn segmented_sized(ui: &mut Ui, options: &[&str], selected: usize, height: f32, text: f32) -> Option<usize> {let t = Tokens::get(ui.ctx());let galleys: Vec<_> = options.iter().map(|o| label_galley(ui, &tr(o), text, t.text)).collect();- let widths: Vec<f32> = galleys.iter().map(|g| g.size().x + text * 2.0 + 2.0).collect();+ let widths: Vec<f32> = galleys.iter().map(|g| segment_width(g.size().x, text)).collect();let total: f32 = widths.iter().sum::<f32>() + 4.0;let (rect, _) = ui.allocate_exact_size(Vec2::new(total, height), Sense::hover());ui.painter().rect(rect, t.radius, t.raised, Stroke::new(1.0, t.card_border), egui::StrokeKind::Inside);@@ -266,6 +266,16 @@ pub fn segmented_sized(ui: &mut Ui, options: &[&str], selected: usize, height: fpicked}+fn segment_width(label: f32, text: f32) -> f32 {+ label + text * 2.0 + 2.0+}++/// How wide [`segmented`] draws `options`, for a row that needs to know before drawing them.+pub fn segmented_width(ui: &Ui, options: &[&str]) -> f32 {+ let t = Tokens::get(ui.ctx());+ options.iter().map(|o| segment_width(label_galley(ui, &tr(o), 12.0, t.text).size().x, 12.0)).sum::<f32>() + 4.0+}+/// A small status label on a tint.pub fn pill(ui: &mut Ui, text: &str, fg: Color32, bg: Color32) -> Response {let t = Tokens::get(ui.ctx());diff --git a/crates/ui-egui/tests/smoke.rs b/crates/ui-egui/tests/smoke.rsindex 6393e72b..1ee96819 100644--- a/crates/ui-egui/tests/smoke.rs+++ b/crates/ui-egui/tests/smoke.rs@@ -2834,6 +2834,29 @@ fn a_long_status_note_in_the_work_rows_stays_in_the_window() {}}+/// #983: in a longer language or a narrow window the two switches go on two lines, never over each other.+#[test]+fn the_switches_under_the_prompt_box_never_overlap() {+ let d = tempfile::tempdir().unwrap();+ let ws = craft_anything_engine::workspace::Workspace::create(d.path(), "Notes", "A notes app", craft_anything_engine::workspace::TargetRef::None).unwrap();+ for width in [1600.0, 1000.0, 700.0, 560.0] {+ let ws = ws.clone();+ let mut h = harness_sized(egui::vec2(width, 800.0), move || {+ let mut a = demo::app(demo::engine(Mode::Beginner, true));+ a.workspace = Some(ws);+ a.state.screen = Screen::Project;+ a+ });+ h.run_steps(3);+ let sending = h.get_by_label("Just ask").rect();+ let (asking, title) = (h.get_by_label("Ask me").rect(), h.get_by_label("Builder's questions:").rect());+ let hint = h.get_by_label("or start with /btw to just ask").rect();+ assert!(!sending.intersects(title) && !sending.intersects(asking), "at {width}: {sending:?}, {title:?}, {asking:?}");+ assert!(!hint.intersects(title) && !hint.intersects(asking), "at {width}: {hint:?}, {title:?}, {asking:?}");+ assert!(title.left() >= 0.0 && asking.right() <= width, "at {width} they stay in the window: {title:?}, {asking:?}");+ }+}+#[test]fn a_very_long_app_file_name_keeps_start_inside_the_window() {use craft_anything_engine::workspace::TargetRef;