settings_ui: Correct stepper increment and enforce max value for Centered Layout Padding (#40751)

Closes #40748

This PR improves the Centered Layout Padding in settings ui by limiting the numeric stepper to be within valid values and adding a custom schema generated to improve the JSON LSP completions and warnings when editing the setting field manually.

Release Notes:

- settings ui: limit stepper increment for centered padding between 0 and 0.4 and increment by 0.05 now

---------

Co-authored by: Anthony Eid <anthony@zed.dev>
This commit is contained in:
Delvin
2025-10-21 19:20:42 +00:00
committed by GitHub
parent a398f80ba6
commit 4b429033e7
5 changed files with 89 additions and 16 deletions
@@ -850,3 +850,65 @@ impl From<f32> for InactiveOpacity {
Self(x)
}
}
/// Centered layout related setting (left/right).
///
/// Valid range: 0.0 to 0.4
/// Default: 2.0
#[derive(
Clone,
Copy,
Debug,
Serialize,
Deserialize,
MergeFrom,
PartialEq,
PartialOrd,
derive_more::FromStr,
)]
#[serde(transparent)]
pub struct CenteredPaddingSettings(
#[serde(serialize_with = "serialize_f32_with_two_decimal_places")] pub f32,
);
impl CenteredPaddingSettings {
pub const MIN_PADDING: f32 = 0.0;
// This is an f64 so serde_json can give a type hint without random numbers in the back
pub const DEFAULT_PADDING: f64 = 0.2;
pub const MAX_PADDING: f32 = 0.4;
}
impl Display for CenteredPaddingSettings {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:.2}", self.0)
}
}
impl From<f32> for CenteredPaddingSettings {
fn from(x: f32) -> Self {
Self(x)
}
}
impl Default for CenteredPaddingSettings {
fn default() -> Self {
Self(Self::DEFAULT_PADDING as f32)
}
}
impl schemars::JsonSchema for CenteredPaddingSettings {
fn schema_name() -> std::borrow::Cow<'static, str> {
"CenteredPaddingSettings".into()
}
fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
use schemars::json_schema;
json_schema!({
"type": "number",
"minimum": Self::MIN_PADDING,
"maximum": Self::MAX_PADDING,
"default": Self::DEFAULT_PADDING,
"description": "Centered layout related setting (left/right)."
})
}
}
@@ -7,8 +7,8 @@ use serde_with::skip_serializing_none;
use settings_macros::MergeFrom;
use crate::{
DelayMs, DockPosition, DockSide, InactiveOpacity, ScrollbarSettingsContent, ShowIndentGuides,
serialize_optional_f32_with_two_decimal_places,
CenteredPaddingSettings, DelayMs, DockPosition, DockSide, InactiveOpacity,
ScrollbarSettingsContent, ShowIndentGuides, serialize_optional_f32_with_two_decimal_places,
};
#[skip_serializing_none]
@@ -463,22 +463,20 @@ pub enum PaneSplitDirectionVertical {
Right,
}
#[skip_serializing_none]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema, MergeFrom, PartialEq, Default)]
#[serde(rename_all = "snake_case")]
#[skip_serializing_none]
pub struct CenteredLayoutSettings {
/// The relative width of the left padding of the central pane from the
/// workspace when the centered layout is used.
///
/// Default: 0.2
#[serde(serialize_with = "serialize_optional_f32_with_two_decimal_places")]
pub left_padding: Option<f32>,
pub left_padding: Option<CenteredPaddingSettings>,
// The relative width of the right padding of the central pane from the
// workspace when the centered layout is used.
///
/// Default: 0.2
#[serde(serialize_with = "serialize_optional_f32_with_two_decimal_places")]
pub right_padding: Option<f32>,
pub right_padding: Option<CenteredPaddingSettings>,
}
#[derive(