summaryrefslogtreecommitdiffstats
path: root/graphics/src/text/editor.rs
diff options
context:
space:
mode:
authorLibravatar Héctor <hector@hecrj.dev>2025-02-13 05:19:32 +0100
committerLibravatar GitHub <noreply@github.com>2025-02-13 05:19:32 +0100
commitf889008e21971b461ec7c54d9a7667a23f6ab35b (patch)
tree91d9d34c87796da424104c46705e9f1aceb69f92 /graphics/src/text/editor.rs
parent97f1db3783dca5a4f60a9f89668613de4dfe9edd (diff)
parent89a4dc2ac2a751fdcae921997bb93a76f9b667f9 (diff)
downloadiced-f889008e21971b461ec7c54d9a7667a23f6ab35b.tar.gz
iced-f889008e21971b461ec7c54d9a7667a23f6ab35b.tar.bz2
iced-f889008e21971b461ec7c54d9a7667a23f6ab35b.zip
Merge pull request #2793 from rhysd/issue-2792
Fix the initial candidate window position
Diffstat (limited to 'graphics/src/text/editor.rs')
-rw-r--r--graphics/src/text/editor.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/graphics/src/text/editor.rs b/graphics/src/text/editor.rs
index c73d189c..765de07e 100644
--- a/graphics/src/text/editor.rs
+++ b/graphics/src/text/editor.rs
@@ -11,7 +11,7 @@ use cosmic_text::Edit as _;
use std::borrow::Cow;
use std::fmt;
-use std::sync::{self, Arc};
+use std::sync::{self, Arc, RwLock};
/// A multi-line text editor.
#[derive(Debug, PartialEq)]
@@ -19,6 +19,7 @@ pub struct Editor(Option<Arc<Internal>>);
struct Internal {
editor: cosmic_text::Editor<'static>,
+ cursor: RwLock<Option<Cursor>>,
font: Font,
bounds: Size,
topmost_line_changed: Option<usize>,
@@ -114,10 +115,14 @@ impl editor::Editor for Editor {
fn cursor(&self) -> editor::Cursor {
let internal = self.internal();
+ if let Ok(Some(cursor)) = internal.cursor.read().as_deref() {
+ return cursor.clone();
+ }
+
let cursor = internal.editor.cursor();
let buffer = buffer_from_editor(&internal.editor);
- match internal.editor.selection_bounds() {
+ let cursor = match internal.editor.selection_bounds() {
Some((start, end)) => {
let line_height = buffer.metrics().line_height;
let selected_lines = end.line - start.line + 1;
@@ -237,7 +242,12 @@ impl editor::Editor for Editor {
- buffer.scroll().vertical,
))
}
- }
+ };
+
+ *internal.cursor.write().expect("Write to cursor cache") =
+ Some(cursor.clone());
+
+ cursor
}
fn cursor_position(&self) -> (usize, usize) {
@@ -259,6 +269,13 @@ impl editor::Editor for Editor {
let editor = &mut internal.editor;
+ // Clear cursor cache
+ let _ = internal
+ .cursor
+ .write()
+ .expect("Write to cursor cache")
+ .take();
+
match action {
// Motion events
Action::Move(motion) => {
@@ -527,6 +544,13 @@ impl editor::Editor for Editor {
internal.editor.shape_as_needed(font_system.raw(), false);
+ // Clear cursor cache
+ let _ = internal
+ .cursor
+ .write()
+ .expect("Write to cursor cache")
+ .take();
+
self.0 = Some(Arc::new(internal));
}
@@ -635,6 +659,7 @@ impl Default for Internal {
line_height: 1.0,
},
)),
+ cursor: RwLock::new(None),
font: Font::default(),
bounds: Size::ZERO,
topmost_line_changed: None,