diff --git a/crates/story/src/input_story.rs b/crates/story/src/input_story.rs index 6e48cbf2..8c4804df 100644 --- a/crates/story/src/input_story.rs +++ b/crates/story/src/input_story.rs @@ -1,11 +1,13 @@ use gpui::{ - App, AppContext as _, Context, Entity, InteractiveElement, IntoElement, ParentElement as _, - Render, Styled, Subscription, Window, div, + App, AppContext as _, ClickEvent, Context, Entity, InteractiveElement, IntoElement, + ParentElement as _, Render, Styled, Subscription, Window, div, }; use crate::section; use gpui_component::{button::*, input::*, *}; +const CODE_EXAMPLE: &str = r#"{"single_line":"code editor"}"#; + pub fn init(_: &mut App) {} pub struct InputStory { @@ -93,7 +95,7 @@ impl InputStory { InputState::new(window, cx) .code_editor("json") .multi_line(false) - .default_value(r#"{"single_line":"code editor"}"#) + .default_value(CODE_EXAMPLE) }); let _subscriptions = vec![ @@ -151,6 +153,12 @@ impl InputStory { InputEvent::Blur => println!("Blur"), }; } + + fn on_click_reset(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context) { + self.code_input.update(cx, |input_state, cx| { + input_state.set_value(CODE_EXAMPLE, window, cx); + }); + } } impl Render for InputStory { @@ -262,9 +270,15 @@ impl Render for InputStory { ), ) .child( - section("Single line code editor") - .max_w_md() - .child(Input::new(&self.code_input)), + section("Single line code editor").max_w_md().child( + Input::new(&self.code_input).suffix( + Button::new("code-reset") + .ghost() + .label("Reset") + .xsmall() + .on_click(cx.listener(Self::on_click_reset)), + ), + ), ) } } diff --git a/crates/ui/src/input/state.rs b/crates/ui/src/input/state.rs index f777eb2f..3dc987c2 100644 --- a/crates/ui/src/input/state.rs +++ b/crates/ui/src/input/state.rs @@ -606,15 +606,19 @@ impl InputState { self.replace_text(value, window, cx); self.disabled = was_disabled; self.history.ignore = false; + // Ensure cursor to start when set text if self.mode.is_single_line() { self.selected_range = (self.text.len()..self.text.len()).into(); } else { self.selected_range.clear(); + } + if self.mode.is_code_editor() { self._pending_update = true; self.lsp.reset(); } + // Move scroll to top self.scroll_handle.set_offset(point(px(0.), px(0.)));