summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-12 08:46:35 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-12 08:46:35 +0100
commit7979125ed793918dd4a0e5a1ddec8d17bffbd5bf (patch)
tree6633f5f9bf45d4c7378afb1480617a7a247d0a89 /graphics
parent97f1db3783dca5a4f60a9f89668613de4dfe9edd (diff)
downloadiced-7979125ed793918dd4a0e5a1ddec8d17bffbd5bf.tar.gz
iced-7979125ed793918dd4a0e5a1ddec8d17bffbd5bf.tar.bz2
iced-7979125ed793918dd4a0e5a1ddec8d17bffbd5bf.zip
Simplify `InputMethod` API with only two states
Co-authored-by: rhysd <lin90162@yahoo.co.jp> Co-authored-by: KENZ <KENZ.gelsoft@gmail.com>
Diffstat (limited to 'graphics')
-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,