diff --git a/crates/core/src/input.rs b/crates/core/src/input.rs index 1efb521..ff3f60e 100644 --- a/crates/core/src/input.rs +++ b/crates/core/src/input.rs @@ -529,6 +529,56 @@ pub struct InputState { impl EventEmitter 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(mut self, text: T) -> Self + where + T: Into, + { + 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 { + 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 { @@ -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(text: T, cx: &mut App) -> Entity - where - T: Into, - { - 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) { - 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) { - 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>, 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(text: T, multiline: bool) -> String where T: Into, diff --git a/examples/gallery/src/main.rs b/examples/gallery/src/main.rs index a3d5635..b03859c 100644 --- a/examples/gallery/src/main.rs +++ b/examples/gallery/src/main.rs @@ -867,31 +867,20 @@ struct InputDemo { password: Entity, notes: Entity, search: Entity, - _subscriptions: Vec, } impl InputDemo { fn new(cx: &mut Context) -> 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(¬es, |_, _, 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 { 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.