Implement text_input::State::focused
This commit is contained in:
parent
621e5a55b7
commit
657e513651
2 changed files with 19 additions and 0 deletions
|
|
@ -91,6 +91,13 @@ impl State {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn focused(value: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
is_focused: true,
|
||||||
|
cursor_position: Value::new(value).len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn move_cursor_right(&mut self, value: &Value) {
|
pub fn move_cursor_right(&mut self, value: &Value) {
|
||||||
let current = self.cursor_position(value);
|
let current = self.cursor_position(value);
|
||||||
|
|
||||||
|
|
@ -107,6 +114,10 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn move_cursor_to_end(&mut self, value: &Value) {
|
||||||
|
self.cursor_position = value.len();
|
||||||
|
}
|
||||||
|
|
||||||
pub fn cursor_position(&self, value: &Value) -> usize {
|
pub fn cursor_position(&self, value: &Value) -> usize {
|
||||||
self.cursor_position.min(value.len())
|
self.cursor_position.min(value.len())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ where
|
||||||
Renderer: self::Renderer,
|
Renderer: self::Renderer,
|
||||||
Message: Clone + std::fmt::Debug,
|
Message: Clone + std::fmt::Debug,
|
||||||
{
|
{
|
||||||
|
fn width(&self) -> Length {
|
||||||
|
self.width
|
||||||
|
}
|
||||||
|
|
||||||
fn layout(
|
fn layout(
|
||||||
&self,
|
&self,
|
||||||
renderer: &Renderer,
|
renderer: &Renderer,
|
||||||
|
|
@ -46,6 +50,10 @@ where
|
||||||
}) => {
|
}) => {
|
||||||
self.state.is_focused =
|
self.state.is_focused =
|
||||||
layout.bounds().contains(cursor_position);
|
layout.bounds().contains(cursor_position);
|
||||||
|
|
||||||
|
if self.state.cursor_position(&self.value) == 0 {
|
||||||
|
self.state.move_cursor_to_end(&self.value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Event::Keyboard(keyboard::Event::CharacterReceived(c))
|
Event::Keyboard(keyboard::Event::CharacterReceived(c))
|
||||||
if self.state.is_focused && !c.is_control() =>
|
if self.state.is_focused && !c.is_control() =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue