Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

UI: Quick Apply rows are left-aligned

aionggbd1618f1314c
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 33675cc6f..e5c1c0a23 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1942,7 +1942,13 @@ pub fn palette(app: &mut DesignApp, ctx: &egui::Context) {
} else if !it.kind.is_empty() {
b = b.shortcut_text(it.kind);
}
- if ui.add_sized([460.0, 22.0], b).clicked() || (i == 0 && ui.input(|i| i.key_pressed(egui::Key::Enter))) {
+ // The button aligns its label by the layout it's in: left, like the rows with a shortcut.
+ let size = egui::vec2(460.0, 22.0);
+ let row =
+ ui.allocate_ui_with_layout(size, egui::Layout::left_to_right(egui::Align::Center).with_main_align(egui::Align::Min), |ui| {
+ ui.add(b.min_size(size))
+ });
+ if row.inner.clicked() || (i == 0 && ui.input(|i| i.key_pressed(egui::Key::Enter))) {
run = Some((it.id.clone(), it.params.clone()));
}
}
@@ -2410,6 +2416,39 @@ mod tests {
assert_eq!(app.session.documents().len(), 1, "the first entry (New Document) didn't run");
}
+ #[test]
+ fn quick_apply_rows_are_left_aligned() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.session.execute("file.new", &json!({})).unwrap();
+ app.ui.palette = Some(String::new());
+ let ctx = egui::Context::default();
+ let mut shapes = Vec::new();
+ for _ in 0..3 {
+ let input =
+ egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(1400.0, 900.0))), ..Default::default() };
+ let mut out = ctx.run_ui(input, |ui| {
+ app.logic(&ui.ctx().clone());
+ app.ui(ui);
+ });
+ out.textures_delta.clear();
+ shapes = out.shapes;
+ }
+ let label_x = |label: &str| {
+ // The palette is painted last, over anything else with the same text.
+ shapes.iter().rev().find_map(|s| match &s.shape {
+ egui::Shape::Text(t) if t.galley.text() == label => Some(t.pos.x),
+ _ => None,
+ })
+ };
+ // Rows that show a shortcut or a style kind, and rows that show neither, among the first visible ones.
+ let items = quick_apply_items(&app.session, "");
+ let first = items.iter().take(10);
+ let plain = first.clone().find(|it| it.shortcut.is_none() && it.kind.is_empty()).expect("a row without a shortcut");
+ let with_sc = first.clone().find(|it| it.shortcut.is_some()).expect("a row with a shortcut");
+ let (a, b) = (label_x(&plain.label).expect("plain row drawn"), label_x(&with_sc.label).expect("shortcut row drawn"));
+ assert!((a - b).abs() < 0.5, "{:?} at x {a}, {:?} at x {b}", plain.label, with_sc.label);
+ }
+
#[test]
fn sample_scripts_run() {
let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());