ui_input.rs

 1//! This crate provides UI components that can be used for form-like scenarios, such as a input and number field.
 2//!
 3//! It can't be located in the `ui` crate because it depends on `editor`.
 4//!
 5mod input_field;
 6
 7use std::{
 8    any::Any,
 9    sync::{Arc, OnceLock},
10};
11
12use gpui::{FocusHandle, Subscription};
13pub use input_field::*;
14use ui::{AnyElement, App, Window};
15
16pub trait ErasedEditor: 'static {
17    fn text(&self, cx: &App) -> String;
18    fn set_text(&self, text: &str, window: &mut Window, cx: &mut App);
19    fn clear(&self, window: &mut Window, cx: &mut App);
20    fn set_placeholder_text(&self, text: &str, window: &mut Window, _: &mut App);
21    fn move_selection_to_end(&self, window: &mut Window, _: &mut App);
22
23    fn focus_handle(&self, cx: &App) -> FocusHandle;
24
25    fn subscribe(
26        &self,
27        callback: Box<dyn FnMut(ErasedEditorEvent, &mut Window, &mut App) + 'static>,
28        window: &mut Window,
29        cx: &mut App,
30    ) -> Subscription;
31    fn render(&self, window: &mut Window, cx: &App) -> AnyElement;
32    fn as_any(&self) -> &dyn Any;
33}
34
35#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36pub enum ErasedEditorEvent {
37    BufferEdited,
38    Blurred,
39}
40pub static ERASED_EDITOR_FACTORY: OnceLock<fn(&mut Window, &mut App) -> Arc<dyn ErasedEditor>> =
41    OnceLock::new();