diff --git a/rust/kcl-lib/src/engine/mod.rs b/rust/kcl-lib/src/engine/mod.rs index 0e1b8516c..1ff5a97bc 100644 --- a/rust/kcl-lib/src/engine/mod.rs +++ b/rust/kcl-lib/src/engine/mod.rs @@ -322,7 +322,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static { settings: &crate::ExecutorSettings, source_range: SourceRange, id_generator: &mut IdGenerator, - grid_scale_unit: Option, + grid_scale_unit: GridScaleBehavior, ) -> Result<(), crate::errors::KclError> { // Set the edge visibility. self.set_edge_visibility(settings.highlight_edges, source_range, id_generator) @@ -761,7 +761,7 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static { async fn modify_grid( &self, hidden: bool, - base_unit: Option, + grid_scale_behavior: GridScaleBehavior, source_range: SourceRange, id_generator: &mut IdGenerator, ) -> Result<(), KclError> { @@ -776,16 +776,12 @@ pub trait EngineManager: std::fmt::Debug + Send + Sync + 'static { ) .await?; - let grid_scale = if let Some(units) = base_unit { - // The grid is 10x10, so setting the value to 10 means that - // users whose unit is cm will see each cell as 1cm by 1cm, - // which is useful for sketching. - ModelingCmd::from(mcmd::SetGridScale { value: 10.0, units }) - } else { - ModelingCmd::from(mcmd::SetGridAutoScale {}) - }; - self.batch_modeling_cmd(id_generator.next_uuid(), source_range, &grid_scale) - .await?; + self.batch_modeling_cmd( + id_generator.next_uuid(), + source_range, + &grid_scale_behavior.into_modeling_cmd(), + ) + .await?; // Hide/show the grid scale text. self.batch_modeling_cmd( @@ -899,3 +895,22 @@ pub fn new_zoo_client(token: Option, engine_addr: Option) -> any Ok(client) } + +#[derive(Copy, Clone, Debug)] +pub enum GridScaleBehavior { + ScaleWithZoom, + Fixed(Option), +} + +impl GridScaleBehavior { + fn into_modeling_cmd(self) -> ModelingCmd { + const NUMBER_OF_GRID_COLUMNS: f32 = 10.0; + match self { + GridScaleBehavior::ScaleWithZoom => ModelingCmd::from(mcmd::SetGridAutoScale {}), + GridScaleBehavior::Fixed(unit_length) => ModelingCmd::from(mcmd::SetGridScale { + value: NUMBER_OF_GRID_COLUMNS, + units: unit_length.unwrap_or(kcmc::units::UnitLength::Millimeters), + }), + } + } +} diff --git a/rust/kcl-lib/src/execution/mod.rs b/rust/kcl-lib/src/execution/mod.rs index 0fead2790..ac49949e5 100644 --- a/rust/kcl-lib/src/execution/mod.rs +++ b/rust/kcl-lib/src/execution/mod.rs @@ -31,7 +31,7 @@ pub use state::{ExecState, MetaSettings}; use uuid::Uuid; use crate::{ - engine::EngineManager, + engine::{EngineManager, GridScaleBehavior}, errors::{KclError, KclErrorDetails}, execution::{ cache::{CacheInformation, CacheResult}, @@ -295,6 +295,8 @@ pub struct ExecutorSettings { /// This is the path to the current file being executed. /// We use this for preventing cyclic imports. pub current_file: Option, + /// Whether or not to automatically scale the grid when user zooms. + pub fixed_size_grid: bool, } impl Default for ExecutorSettings { @@ -306,6 +308,7 @@ impl Default for ExecutorSettings { replay: None, project_directory: None, current_file: None, + fixed_size_grid: true, } } } @@ -319,6 +322,7 @@ impl From for ExecutorSettings { replay: None, project_directory: None, current_file: None, + fixed_size_grid: config.settings.app.fixed_size_grid, } } } @@ -332,6 +336,7 @@ impl From for ExecutorSet replay: None, project_directory: None, current_file: None, + fixed_size_grid: true, } } } @@ -345,6 +350,7 @@ impl From for ExecutorSettings { replay: None, project_directory: None, current_file: None, + fixed_size_grid: true, } } } @@ -358,6 +364,7 @@ impl From for Executor replay: None, project_directory: None, current_file: None, + fixed_size_grid: true, } } } @@ -592,12 +599,18 @@ impl ExecutorContext { pub async fn run_with_caching(&self, program: crate::Program) -> Result { assert!(!self.is_mock()); - let grid_scale = program - .meta_settings() - .ok() - .flatten() - .map(|s| s.default_length_units) - .map(kcmc::units::UnitLength::from); + let grid_scale = if self.settings.auto_scale_grid { + GridScaleBehavior::ScaleWithZoom + } else { + GridScaleBehavior::Fixed( + program + .meta_settings() + .ok() + .flatten() + .map(|s| s.default_length_units) + .map(kcmc::units::UnitLength::from), + ) + }; let (program, exec_state, result) = match cache::read_old_ast().await { Some(mut cached_state) => { diff --git a/rust/kcl-lib/src/settings/types/mod.rs b/rust/kcl-lib/src/settings/types/mod.rs index 1350aa29f..a3a0a45d6 100644 --- a/rust/kcl-lib/src/settings/types/mod.rs +++ b/rust/kcl-lib/src/settings/types/mod.rs @@ -94,6 +94,14 @@ pub struct AppSettings { /// of the app to aid in development. #[serde(default, skip_serializing_if = "is_default")] pub show_debug_panel: bool, + /// If true, the grid cells will be fixed-size, where the width is the user's default length unit. + /// If false, the grid's size will scale as the user zooms in and out. + #[serde(default = "make_it_so", skip_serializing_if = "is_default")] + pub fixed_size_grid: bool, +} + +fn make_it_so() -> bool { + true } fn deserialize_stream_idle_mode<'de, D>(deserializer: D) -> Result, D::Error>