diff --git a/crates/ui-widgets/src/text/zoom_field.rs b/crates/ui-widgets/src/text/zoom_field.rs index 66cdb7027..7e3306a7b 100644 --- a/crates/ui-widgets/src/text/zoom_field.rs +++ b/crates/ui-widgets/src/text/zoom_field.rs @@ -7,7 +7,7 @@ use crate::runtime::env::{Env, Label, UnitKind}; use craft_behavior::canvas::zoom; use craft_behavior::kurbo::Size; use craft_behavior::{CanvasView, EditEvents, ZoomLadder}; -use egui::{Response, Ui}; +use egui::{Rect, Response, Ui}; #[cfg(feature = "testing")] mod stories; @@ -93,6 +93,15 @@ impl ZoomField { /// Shows the field and reports the view changes it made. pub fn show(self, ui: &mut Ui) -> ZoomFieldOutput { + self.show_in(ui, None) + } + + /// Shows the field in a caller-allocated rectangle, including narrow slots such as a rail. + pub fn show_at(self, ui: &mut Ui, rect: Rect) -> ZoomFieldOutput { + self.show_in(ui, Some(rect)) + } + + fn show_in(self, ui: &mut Ui, rect: Option) -> ZoomFieldOutput { let env = Env::of(ui); let pixels_per_point = self.scale.pixels_per_unit(env.scale.0); let document = self.document; @@ -113,7 +122,10 @@ impl ZoomField { if self.scrub { field = field.scrub_by_label(); } - let shown = field.show(ui); + let shown = match rect { + Some(rect) => field.show_at(ui, rect), + None => field.show(ui), + }; let invalid_zoom = range.filter(|_| shown.cut).map(|range| InvalidZoom { low: *range.start(), high: *range.end() }); let view = match self.view { Some(current) => shown diff --git a/crates/ui-widgets/tests/it/text/zoom_field.rs b/crates/ui-widgets/tests/it/text/zoom_field.rs index 6e278eb84..530a8664a 100644 --- a/crates/ui-widgets/tests/it/text/zoom_field.rs +++ b/crates/ui-widgets/tests/it/text/zoom_field.rs @@ -333,3 +333,28 @@ fn cut_copy_and_delete_with_a_caret_never_reach_the_app() { assert_eq!(text(&fixture), "", "with a selection, Cmd+X cuts the field's text"); assert_eq!(app_edits_fired(&fixture), Vec::::new(), "and the app's Cut stays unfired"); } + +/// A host with a narrow fixed slot can keep the zoom field and its editable text inside it. +#[trial] +fn zoom_field_fits_a_narrow_host_slot() { + let slot = Rc::new(Cell::new(Rect::NOTHING)); + let held = slot.clone(); + let kept = Kept::new(); + let app = kept.clone(); + let mut fixture = Fixture::new(move |ui| { + let (rect, _) = ui.allocate_exact_size(Vec2::new(40.0, 24.0), egui::Sense::hover()); + held.set(rect); + let out = ZoomField::new(ZOOM, Some(app.view.get()), DOCUMENT).show_at(ui, rect); + if let Some(view) = out.view.committed() { + app.view.set(*view); + } + }); + fixture.settle(); + let field = fixture.by_label(ZOOM).expect("the zoom field"); + assert!(slot.get().contains_rect(field.rect), "zoom field {:?} fits its host slot {:?}", field.rect, slot.get()); + fixture.click(&field).settle(); + fixture.key(Key::A, Modifiers::COMMAND); + fixture.type_text("200"); + fixture.key(Key::Enter, Modifiers::NONE).settle(); + assert!((kept.zoom() - 200.0).abs() < 0.01, "the field remains editable"); +}