number_input: Remove InputEvent from NumberInputEvent (#884)

## Break Change
- Number input no longer emits `InputEvent`; you need an additional
subscription for `InputEvent`.
This commit is contained in:
Floyd Wang 2025-05-22 14:38:43 +08:00 committed by GitHub
parent 119d348e4a
commit 244090ddc5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 27 deletions

View file

@ -79,8 +79,11 @@ impl NumberInputStory {
});
let _subscriptions = vec![
cx.subscribe_in(&number_input1, window, Self::on_input_event),
cx.subscribe_in(&number_input1, window, Self::on_number_input_event),
cx.subscribe_in(&number_input2, window, Self::on_input_event),
cx.subscribe_in(&number_input2, window, Self::on_number_input_event),
cx.subscribe_in(&number_input3, window, Self::on_input_event),
cx.subscribe_in(&number_input3, window, Self::on_number_input_event),
];
@ -103,6 +106,38 @@ impl NumberInputStory {
self.cycle_focus(false, window, cx);
}
fn on_input_event(
&mut self,
this: &Entity<InputState>,
event: &InputEvent,
_: &mut Window,
_: &mut Context<Self>,
) {
match event {
InputEvent::Change(text) => {
if this == &self.number_input1 {
if let Ok(value) = text.parse::<i64>() {
self.number_input1_value = value;
}
} else if this == &self.number_input2 {
if let Ok(value) = text.parse::<u64>() {
self.number_input2_value = value;
}
} else if this == &self.number_input3 {
if let Ok(value) = text.parse::<f64>() {
self.number_input3_value = value;
}
}
println!("Change: {}", text);
}
InputEvent::PressEnter { secondary } => {
println!("PressEnter secondary: {}", secondary)
}
InputEvent::Focus => println!("Focus"),
InputEvent::Blur => println!("Blur"),
}
}
fn on_number_input_event(
&mut self,
this: &Entity<InputState>,
@ -111,29 +146,6 @@ impl NumberInputStory {
cx: &mut Context<Self>,
) {
match event {
NumberInputEvent::Input(input_event) => match input_event {
InputEvent::Change(text) => {
if this == &self.number_input1 {
if let Ok(value) = text.parse::<i64>() {
self.number_input1_value = value;
}
} else if this == &self.number_input2 {
if let Ok(value) = text.parse::<u64>() {
self.number_input2_value = value;
}
} else if this == &self.number_input3 {
if let Ok(value) = text.parse::<f64>() {
self.number_input3_value = value;
}
}
println!("Change: {}", text);
}
InputEvent::PressEnter { secondary } => {
println!("PressEnter secondary: {}", secondary)
}
InputEvent::Focus => println!("Focus"),
InputEvent::Blur => println!("Blur"),
},
NumberInputEvent::Step(step_action) => match step_action {
StepAction::Decrement => {
if this == &self.number_input1 {

View file

@ -6,9 +6,7 @@ use gpui::{
use crate::{
button::{Button, ButtonVariants as _},
h_flex,
input::InputEvent,
ActiveTheme, IconName, Sizable, Size, StyleSized, StyledExt as _,
h_flex, ActiveTheme, IconName, Sizable, Size, StyleSized, StyledExt as _,
};
use super::{InputState, TextInput};
@ -90,7 +88,6 @@ pub enum StepAction {
Increment,
}
pub enum NumberInputEvent {
Input(InputEvent),
Step(StepAction),
}
impl EventEmitter<NumberInputEvent> for InputState {}