core: add builder for input state creation
This commit is contained in:
+54
-35
@@ -529,6 +529,56 @@ pub struct InputState {
|
|||||||
|
|
||||||
impl EventEmitter<InputStateEvent> for 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 {
|
impl InputState {
|
||||||
fn new_inner(cx: &mut Context<Self>) -> Self {
|
fn new_inner(cx: &mut Context<Self>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -569,19 +619,9 @@ impl InputState {
|
|||||||
cx.new(Self::new_inner)
|
cx.new(Self::new_inner)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates the state entity with initial text, caret at the end.
|
/// Returns a builder for creation-time settings like text, multiline and password.
|
||||||
pub fn new_with_text<T>(text: T, cx: &mut App) -> Entity<Self>
|
pub fn builder() -> InputStateBuilder {
|
||||||
where
|
InputStateBuilder::default()
|
||||||
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
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The current text.
|
/// The current text.
|
||||||
@@ -648,17 +688,6 @@ impl InputState {
|
|||||||
self.read_only
|
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.
|
/// Whether the field is a password field.
|
||||||
pub fn is_password(&self) -> bool {
|
pub fn is_password(&self) -> bool {
|
||||||
self.password
|
self.password
|
||||||
@@ -677,15 +706,6 @@ impl InputState {
|
|||||||
self.revealed
|
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.
|
/// Whether the field holds multiple lines.
|
||||||
pub fn is_multiline(&self) -> bool {
|
pub fn is_multiline(&self) -> bool {
|
||||||
self.multiline
|
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
|
/// Newlines have no meaning in a single-line field.
|
||||||
/// them and drop carriage returns.
|
|
||||||
fn sanitize<T>(text: T, multiline: bool) -> String
|
fn sanitize<T>(text: T, multiline: bool) -> String
|
||||||
where
|
where
|
||||||
T: Into<String>,
|
T: Into<String>,
|
||||||
|
|||||||
@@ -867,31 +867,20 @@ struct InputDemo {
|
|||||||
password: Entity<InputState>,
|
password: Entity<InputState>,
|
||||||
notes: Entity<InputState>,
|
notes: Entity<InputState>,
|
||||||
search: Entity<InputState>,
|
search: Entity<InputState>,
|
||||||
_subscriptions: Vec<gpui::Subscription>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputDemo {
|
impl InputDemo {
|
||||||
fn new(cx: &mut Context<Self>) -> Self {
|
fn new(cx: &mut Context<Self>) -> Self {
|
||||||
let text = InputState::new(cx);
|
let text = InputState::new(cx);
|
||||||
let password = InputState::new(cx);
|
let password = InputState::builder().password().build(cx);
|
||||||
password.update(cx, |state, cx| state.set_password(true, cx));
|
let notes = InputState::builder().multiline().build(cx);
|
||||||
let notes = InputState::new(cx);
|
|
||||||
notes.update(cx, |state, cx| state.set_multiline(true, cx));
|
|
||||||
let search = InputState::new(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 {
|
Self {
|
||||||
text,
|
text,
|
||||||
password,
|
password,
|
||||||
notes,
|
notes,
|
||||||
search,
|
search,
|
||||||
_subscriptions: subscriptions,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -969,7 +958,7 @@ struct FieldDemo {
|
|||||||
impl FieldDemo {
|
impl FieldDemo {
|
||||||
fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
|
fn new(_window: &mut Window, cx: &mut Context<Self>) -> Self {
|
||||||
let url = InputState::new(cx);
|
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
|
// 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
|
// entities it reads are notified. Both inputs are read at render time
|
||||||
// (validation, error text), so every change must bust the cache.
|
// (validation, error text), so every change must bust the cache.
|
||||||
|
|||||||
Reference in New Issue
Block a user