diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index e1f3ffc0b76..15d3965d3d2 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -200,6 +200,7 @@ pub struct Plot { allow_scroll: bool, allow_double_click_reset: bool, allow_boxed_zoom: bool, + allow_auto_bounds: bool, auto_bounds: AxisBools, min_auto_bounds: PlotBounds, margin_fraction: Vec2, @@ -242,6 +243,7 @@ impl Plot { allow_scroll: true, allow_double_click_reset: true, allow_boxed_zoom: true, + allow_auto_bounds: true, auto_bounds: false.into(), min_auto_bounds: PlotBounds::NOTHING, margin_fraction: Vec2::splat(0.05), @@ -375,6 +377,22 @@ impl Plot { self } + /// Whether to allow auto bounds. Default: `true`. + /// If `false`, it set `bounds_modified` to true to prevent auto adjusting bounds, + /// also it check if the user double clicked to reset bounds. If so, it set `bounds_modified` to false. + /// + /// It's needed when a new plot is started, such as when resetting egui memory and pan/zoom that + /// have not been used before, as the limits can be saved as persistent data. + /// + /// Currently, if `allow_auto_bounds(false)` and `plot_ui.set_plot_bounds(PlotBounds)` are used in + /// same time `set_plot_bounds` will be ignored. + /// + /// TODO: Refactor `Plot::allow_auto_bounds` and `Plot::set_plot_bounds` feature to work together. + pub fn allow_auto_bounds(mut self, on: bool) -> Self { + self.allow_auto_bounds = on; + self + } + /// Config the button pointer to use for boxed zooming. Default: [`Secondary`](PointerButton::Secondary) pub fn boxed_zoom_pointer_button(mut self, boxed_zoom_pointer_button: PointerButton) -> Self { self.boxed_zoom_pointer_button = boxed_zoom_pointer_button; @@ -711,6 +729,7 @@ impl Plot { show_grid, linked_axes, linked_cursors, + allow_auto_bounds, clamp_grid, grid_spacers, @@ -951,23 +970,29 @@ impl Plot { }); }; + let reset_bounds = allow_double_click_reset && response.double_clicked(); + // Allow double clicking to reset to the initial bounds. - if allow_double_click_reset && response.double_clicked() { + if reset_bounds { bounds_modified = false.into(); } - // Apply bounds modifications. - for modification in bounds_modifications { - match modification { - BoundsModification::Set(new_bounds) => { - bounds = new_bounds; - bounds_modified = true.into(); - } - BoundsModification::Translate(delta) => { - bounds.translate(delta); - bounds_modified = true.into(); + if allow_auto_bounds { + // Apply bounds modifications. + for modification in bounds_modifications { + match modification { + BoundsModification::Set(new_bounds) => { + bounds = new_bounds; + bounds_modified = true.into(); + } + BoundsModification::Translate(delta) => { + bounds.translate(delta); + bounds_modified = true.into(); + } } } + } else if !reset_bounds { + bounds_modified = true.into(); } // Reset bounds to initial bounds if they haven't been modified.