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 {}
|
||||
|
||||
/// 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>,
|
||||
|
||||
Reference in New Issue
Block a user