setting: Supplement subscription InputEvent for number_input (#1740)
This commit is contained in:
parent
13eceab1ac
commit
3b2746e0ba
2 changed files with 60 additions and 24 deletions
|
|
@ -266,7 +266,9 @@ impl SettingsStory {
|
|||
)
|
||||
.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(
|
||||
SettingItem::new(
|
||||
|
|
@ -284,7 +286,9 @@ impl SettingsStory {
|
|||
)
|
||||
.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![
|
||||
SettingItem::render(|options, _, _| {
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@ use std::rc::Rc;
|
|||
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext as _, Entity, IntoElement, SharedString, StyleRefinement, Styled,
|
||||
Window, prelude::FluentBuilder as _,
|
||||
Subscription, Window, prelude::FluentBuilder as _,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
AxisExt, Sizable, StyledExt,
|
||||
input::{InputState, NumberInput, NumberInputEvent},
|
||||
input::{InputEvent, InputState, NumberInput, NumberInputEvent, StepAction},
|
||||
setting::{
|
||||
AnySettingField, RenderOptions,
|
||||
fields::{SettingFieldRender, get_value, set_value},
|
||||
|
|
@ -48,7 +48,8 @@ impl NumberField {
|
|||
|
||||
struct State {
|
||||
input: Entity<InputState>,
|
||||
_subscription: gpui::Subscription,
|
||||
initial_value: f64,
|
||||
_subscriptions: Vec<Subscription>,
|
||||
}
|
||||
|
||||
impl SettingFieldRender for NumberField {
|
||||
|
|
@ -74,18 +75,17 @@ impl SettingFieldRender for NumberField {
|
|||
|window, cx| {
|
||||
let input =
|
||||
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 {
|
||||
NumberInputEvent::Step(action) => input.update(cx, |input, cx| {
|
||||
let value = input.value();
|
||||
if let Ok(value) = value.parse::<f64>() {
|
||||
let new_value =
|
||||
if *action == crate::input::StepAction::Increment {
|
||||
(value + num_options.step).min(num_options.max)
|
||||
let new_value = if *action == StepAction::Increment {
|
||||
value + num_options.step
|
||||
} else {
|
||||
(value - num_options.step).max(num_options.min)
|
||||
value - num_options.step
|
||||
};
|
||||
set_value(new_value, cx);
|
||||
input.set_value(
|
||||
SharedString::from(new_value.to_string()),
|
||||
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 {
|
||||
input,
|
||||
_subscription,
|
||||
initial_value: value,
|
||||
_subscriptions,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue