setting: Supplement subscription InputEvent for number_input (#1740)

This commit is contained in:
Ylin 2025-12-04 14:47:11 +08:00 committed by GitHub
parent 13eceab1ac
commit 3b2746e0ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 60 additions and 24 deletions

View file

@ -266,7 +266,9 @@ impl SettingsStory {
) )
.default_value(default_settings.font_size), .default_value(default_settings.font_size),
) )
.description("Adjust the font size for better readability."), .description(
"Adjust the font size for better readability between 8 and 72.",
),
) )
.item( .item(
SettingItem::new( SettingItem::new(
@ -284,7 +286,9 @@ impl SettingsStory {
) )
.default_value(default_settings.line_height), .default_value(default_settings.line_height),
) )
.description("Adjust the line height for better readability."), .description(
"Adjust the line height for better readability between 8 and 32.",
),
), ),
SettingGroup::new().title("Other").items(vec![ SettingGroup::new().title("Other").items(vec![
SettingItem::render(|options, _, _| { SettingItem::render(|options, _, _| {

View file

@ -2,12 +2,12 @@ use std::rc::Rc;
use gpui::{ use gpui::{
AnyElement, App, AppContext as _, Entity, IntoElement, SharedString, StyleRefinement, Styled, AnyElement, App, AppContext as _, Entity, IntoElement, SharedString, StyleRefinement, Styled,
Window, prelude::FluentBuilder as _, Subscription, Window, prelude::FluentBuilder as _,
}; };
use crate::{ use crate::{
AxisExt, Sizable, StyledExt, AxisExt, Sizable, StyledExt,
input::{InputState, NumberInput, NumberInputEvent}, input::{InputEvent, InputState, NumberInput, NumberInputEvent, StepAction},
setting::{ setting::{
AnySettingField, RenderOptions, AnySettingField, RenderOptions,
fields::{SettingFieldRender, get_value, set_value}, fields::{SettingFieldRender, get_value, set_value},
@ -48,7 +48,8 @@ impl NumberField {
struct State { struct State {
input: Entity<InputState>, input: Entity<InputState>,
_subscription: gpui::Subscription, initial_value: f64,
_subscriptions: Vec<Subscription>,
} }
impl SettingFieldRender for NumberField { impl SettingFieldRender for NumberField {
@ -74,18 +75,17 @@ impl SettingFieldRender for NumberField {
|window, cx| { |window, cx| {
let input = let input =
cx.new(|cx| InputState::new(window, cx).default_value(value.to_string())); cx.new(|cx| InputState::new(window, cx).default_value(value.to_string()));
let _subscription = cx.subscribe_in(&input, window, { let _subscriptions = vec![
cx.subscribe_in(&input, window, {
move |_, input, event: &NumberInputEvent, window, cx| match event { move |_, input, event: &NumberInputEvent, window, cx| match event {
NumberInputEvent::Step(action) => input.update(cx, |input, cx| { NumberInputEvent::Step(action) => input.update(cx, |input, cx| {
let value = input.value(); let value = input.value();
if let Ok(value) = value.parse::<f64>() { if let Ok(value) = value.parse::<f64>() {
let new_value = let new_value = if *action == StepAction::Increment {
if *action == crate::input::StepAction::Increment { value + num_options.step
(value + num_options.step).min(num_options.max)
} else { } else {
(value - num_options.step).max(num_options.min) value - num_options.step
}; };
set_value(new_value, cx);
input.set_value( input.set_value(
SharedString::from(new_value.to_string()), SharedString::from(new_value.to_string()),
window, window,
@ -94,11 +94,43 @@ impl SettingFieldRender for NumberField {
} }
}), }),
} }
}),
cx.subscribe_in(&input, window, {
move |state: &mut State, input, event: &InputEvent, window, cx| {
match event {
InputEvent::Change => {
input.update(cx, |input, cx| {
let value = input.value();
if value == state.initial_value.to_string() {
return;
}
if let Ok(value) = value.parse::<f64>() {
let clamp_value =
value.clamp(num_options.min, num_options.max);
set_value(clamp_value, cx);
state.initial_value = clamp_value;
if clamp_value != value {
input.set_value(
SharedString::from(clamp_value.to_string()),
window,
cx,
);
}
}
}); });
}
_ => {}
}
}
}),
];
State { State {
input, input,
_subscription, initial_value: value,
_subscriptions,
} }
}, },
) )