Skip to main content

gpui_base/input/input/
mod.rs

1use gpui::{App, Entity, IntoElement, RenderOnce, Window};
2
3use super::{InputBaseState, InputMode};
4
5/// State for a single-line text input.
6///
7/// This is the shared editing engine in its single-line kind. Multi-line
8/// layout, auto-grow, and code-editor configuration do not exist on this type —
9/// those methods live on [`super::TextareaState`] and [`super::EditorState`].
10pub type InputState = InputBaseState<InputMode>;
11
12/// An unstyled single-line text input.
13///
14/// Applications that need a fully styled control can wrap this state with
15/// their own presentation or use `gpui-component::Input`.
16#[derive(IntoElement)]
17pub struct Input {
18    presentation: super::InlineTokenPresentation,
19    state: Entity<InputState>,
20}
21
22impl Input {
23    pub fn new(state: &Entity<InputState>) -> Self {
24        Self {
25            state: state.clone(),
26            presentation: Default::default(),
27        }
28    }
29    /// The element each atomic token renders as; the input keeps editing and history.
30    pub fn token<R: IntoElement>(
31        mut self,
32        render: impl Fn(&super::InlineTokenContext, &mut Window, &mut App) -> R + 'static,
33    ) -> Self {
34        self.presentation = self.presentation.token(render);
35        self
36    }
37    /// Open a reference after a completed, unconsumed token click.
38    pub fn on_token_click(
39        mut self,
40        listener: impl Fn(&super::InlineTokenClickEvent, &mut Window, &mut App) + 'static,
41    ) -> Self {
42        self.presentation = self.presentation.on_token_click(listener);
43        self
44    }
45}
46
47impl RenderOnce for Input {
48    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
49        self.state.update(cx, |state, _| {
50            state.set_token_presentation(self.presentation)
51        });
52        self.state
53    }
54}