core: add builder for input state creation

This commit is contained in:
2026-11-12 08:45:08 +07:00
parent 72118bdd6c
commit ccd8997801
2 changed files with 58 additions and 50 deletions
+54 -35
View File
@@ -529,6 +529,56 @@ pub struct InputState {
impl EventEmitter<InputStateEvent> for InputState {}
/// Builder for [`InputState`] settings that are fixed at creation.
#[derive(Default)]
pub struct InputStateBuilder {
text: String,
multiline: bool,
password: bool,
}
impl InputStateBuilder {
/// Sets the initial text, caret at the end.
pub fn text<T>(mut self, text: T) -> Self
where
T: Into<String>,
{
self.text = text.into();
self
}
/// Makes the field hold multiple lines.
pub fn multiline(mut self) -> Self {
self.multiline = true;
self
}
/// Makes the field a password field.
pub fn password(mut self) -> Self {
self.password = true;
self
}
/// Creates the state entity.
pub fn build(self, cx: &mut App) -> Entity<InputState> {
let InputStateBuilder {
text,
multiline,
password,
} = self;
cx.new(|cx| {
let mut state = InputState::new_inner(cx);
state.text = sanitize(text, multiline);
state.cursor = state.text.len();
state.anchor = state.cursor;
state.multiline = multiline;
state.password = password;
state
})
}
}
impl InputState {
fn new_inner(cx: &mut Context<Self>) -> Self {
Self {
@@ -569,19 +619,9 @@ impl InputState {
cx.new(Self::new_inner)
}
/// Creates the state entity with initial text, caret at the end.
pub fn new_with_text<T>(text: T, cx: &mut App) -> Entity<Self>
where
T: Into<String>,
{
let text = sanitize(text.into(), false);
cx.new(|cx| {
let mut state = Self::new_inner(cx);
state.text = text;
state.cursor = state.text.len();
state.anchor = state.cursor;
state
})
/// Returns a builder for creation-time settings like text, multiline and password.
pub fn builder() -> InputStateBuilder {
InputStateBuilder::default()
}
/// The current text.
@@ -648,17 +688,6 @@ impl InputState {
self.read_only
}
/// Sets whether the field is a password field.
pub fn set_password(&mut self, password: bool, cx: &mut Context<Self>) {
if self.password != password {
self.password = password;
if !password {
self.revealed = false;
}
cx.notify();
}
}
/// Whether the field is a password field.
pub fn is_password(&self) -> bool {
self.password
@@ -677,15 +706,6 @@ impl InputState {
self.revealed
}
/// Sets whether the field holds multiple lines.
pub fn set_multiline(&mut self, multiline: bool, cx: &mut Context<Self>) {
if self.multiline != multiline {
self.multiline = multiline;
self.scroll_offset = point(Pixels::ZERO, Pixels::ZERO);
cx.notify();
}
}
/// Whether the field holds multiple lines.
pub fn is_multiline(&self) -> bool {
self.multiline
@@ -2472,8 +2492,7 @@ fn real_index(mask_map: &Option<Vec<(usize, usize)>>, display: usize) -> usize {
}
}
/// Newlines have no meaning in a single-line field; multiline fields keep
/// them and drop carriage returns.
/// Newlines have no meaning in a single-line field.
fn sanitize<T>(text: T, multiline: bool) -> String
where
T: Into<String>,
+4 -15
View File
@@ -867,31 +867,20 @@ struct InputDemo {
password: Entity<InputState>,
notes: Entity<InputState>,
search: Entity<InputState>,
_subscriptions: Vec<gpui::Subscription>,
}
impl InputDemo {
fn new(cx: &mut Context<Self>) -> Self {
let text = InputState::new(cx);
let password = InputState::new(cx);
password.update(cx, |state, cx| state.set_password(true, cx));
let notes = InputState::new(cx);
notes.update(cx, |state, cx| state.set_multiline(true, cx));
let password = InputState::builder().password().build(cx);
let notes = InputState::builder().multiline().build(cx);
let search = InputState::new(cx);
// The panel is a cached view in the dock: the value readouts re-render
// only when the inputs it reads are notified.
let subscriptions = vec![
cx.observe(&text, |_, _, cx| cx.notify()),
cx.observe(&password, |_, _, cx| cx.notify()),
cx.observe(&notes, |_, _, cx| cx.notify()),
cx.observe(&search, |_, _, cx| cx.notify()),
];
Self {
text,
password,
notes,
search,
_subscriptions: subscriptions,
}
}
}
@@ -969,7 +958,7 @@ struct FieldDemo {
impl FieldDemo {
fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
let url = InputState::new(cx);
let name = InputState::new_with_text("reya", cx);
let name = InputState::builder().text("reya").build(cx);
// The panel is a cached view in the dock: it re-renders only when the
// entities it reads are notified. Both inputs are read at render time
// (validation, error text), so every change must bust the cache.