Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text input cursor jumping to the end #548

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 41 additions & 13 deletions src/views/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,32 @@ prop_extractor! {
}
}

struct BufferState {
buffer: RwSignal<String>,
last_buffer: String,
}

impl BufferState {
fn update(&mut self, update: impl FnOnce(&mut String)) {
self.buffer.update(|s| {
update(s);
self.last_buffer = s.clone();
});
}

fn get_untracked(&self) -> String {
self.buffer.get_untracked()
}

fn with_untracked<T>(&self, f: impl FnOnce(&String) -> T) -> T {
self.buffer.with_untracked(f)
}
}

/// Text Input View
pub struct TextInput {
id: ViewId,
buffer: RwSignal<String>,
buffer: BufferState,
pub(crate) placeholder_text: Option<String>,
placeholder_buff: Option<TextLayout>,
placeholder_style: PlaceholderStyle,
Expand Down Expand Up @@ -120,7 +142,10 @@ pub fn text_input(buffer: RwSignal<String>) -> TextInput {
placeholder_buff: None,
placeholder_style: Default::default(),
selection_style: Default::default(),
buffer,
buffer: BufferState {
buffer,
last_buffer: buffer.get_untracked(),
},
text_buf: None,
text_node: None,
clipped_text: None,
Expand Down Expand Up @@ -270,7 +295,7 @@ impl TextInput {
false
}),
(Movement::Word, Direction::Left) if self.cursor_glyph_idx > 0 => {
self.buffer.with(|buff| {
self.buffer.with_untracked(|buff| {
let mut prev_word_idx = 0;
for (idx, _) in buff.unicode_word_indices() {
if idx < self.cursor_glyph_idx {
Expand Down Expand Up @@ -318,7 +343,7 @@ impl TextInput {

let new_text = self
.buffer
.get()
.get_untracked()
.chars()
.skip(clip_start)
.take(clip_end - clip_start)
Expand Down Expand Up @@ -527,7 +552,7 @@ impl TextInput {
.layout(text_node)
.cloned()
.unwrap_or_default();
let len = self.buffer.with(|val| val.len());
let len = self.buffer.with_untracked(|val| val.len());
self.cursor_glyph_idx = len;

let text_buf = self.text_buf.as_ref().unwrap();
Expand Down Expand Up @@ -557,7 +582,7 @@ impl TextInput {
if let Some(selection) = &self.selection {
let selection_txt = self
.buffer
.get()
.get_untracked()
.chars()
.skip(selection.start)
.take(selection.end - selection.start)
Expand All @@ -570,7 +595,7 @@ impl TextInput {
if let Some(selection) = &self.selection {
let selection_txt = self
.buffer
.get()
.get_untracked()
.chars()
.skip(selection.start)
.take(selection.end - selection.start)
Expand Down Expand Up @@ -949,13 +974,16 @@ impl View for TextInput {

fn update(&mut self, _cx: &mut UpdateCx, state: Box<dyn Any>) {
if let Ok(state) = state.downcast::<(String, bool)>() {
let (_, is_focused) = *state;
if is_focused {
self.cursor_glyph_idx = self.buffer.with_untracked(|buff| buff.len());
}
let (value, is_focused) = *state;

self.is_focused = is_focused;
self.id.request_layout();
// Only update recomputation if the state has actually changed
if self.is_focused != is_focused || value != self.buffer.last_buffer {
if is_focused {
self.cursor_glyph_idx = self.buffer.with_untracked(|buf| buf.len());
}
self.is_focused = is_focused;
self.id.request_layout();
}
} else {
eprintln!("downcast failed");
}
Expand Down