Configurable grid

This commit is contained in:
Adam Chalmers
2025-06-20 10:46:18 -05:00
parent cd3b850a92
commit bc136c302c
3 changed files with 55 additions and 19 deletions

View File

@ -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<kcmc::units::UnitLength>,
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<kcmc::units::UnitLength>,
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<String>, engine_addr: Option<String>) -> any
Ok(client)
}
#[derive(Copy, Clone, Debug)]
pub enum GridScaleBehavior {
ScaleWithZoom,
Fixed(Option<kcmc::units::UnitLength>),
}
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),
}),
}
}
}

View File

@ -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<TypedPath>,
/// 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<crate::settings::types::Configuration> 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<crate::settings::types::project::ProjectConfiguration> for ExecutorSet
replay: None,
project_directory: None,
current_file: None,
fixed_size_grid: true,
}
}
}
@ -345,6 +350,7 @@ impl From<crate::settings::types::ModelingSettings> for ExecutorSettings {
replay: None,
project_directory: None,
current_file: None,
fixed_size_grid: true,
}
}
}
@ -358,6 +364,7 @@ impl From<crate::settings::types::project::ProjectModelingSettings> 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<ExecOutcome, KclErrorWithOutputs> {
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) => {

View File

@ -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<Option<u32>, D::Error>