Skip to main content

gpui_deck/input/input/
mod.rs

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