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),
|
.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, _, _| {
|
||||||
|
|
|
||||||
|
|
@ -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,31 +75,62 @@ 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![
|
||||||
move |_, input, event: &NumberInputEvent, window, cx| match event {
|
cx.subscribe_in(&input, window, {
|
||||||
NumberInputEvent::Step(action) => input.update(cx, |input, cx| {
|
move |_, input, event: &NumberInputEvent, window, cx| match event {
|
||||||
let value = input.value();
|
NumberInputEvent::Step(action) => input.update(cx, |input, cx| {
|
||||||
if let Ok(value) = value.parse::<f64>() {
|
let value = input.value();
|
||||||
let new_value =
|
if let Ok(value) = value.parse::<f64>() {
|
||||||
if *action == crate::input::StepAction::Increment {
|
let new_value = if *action == StepAction::Increment {
|
||||||
(value + num_options.step).min(num_options.max)
|
value + num_options.step
|
||||||
} 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,
|
cx,
|
||||||
cx,
|
);
|
||||||
);
|
}
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
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,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue