Add move_to method to Editor

This commit is contained in:
Héctor Ramón Jiménez 2025-12-01 20:11:42 +01:00
parent 63e9eeffb5
commit 4428f31b4f
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
4 changed files with 295 additions and 258 deletions

View file

@ -190,6 +190,8 @@ impl text::Editor for () {
fn perform(&mut self, _action: text::editor::Action) {}
fn move_to(&mut self, _cursor: text::editor::Cursor) {}
fn bounds(&self) -> Size {
Size::ZERO
}

View file

@ -35,6 +35,9 @@ pub trait Editor: Sized + Default {
/// Performs an [`Action`] on the [`Editor`].
fn perform(&mut self, action: Action);
/// Moves the cursor to the given position.
fn move_to(&mut self, cursor: Cursor);
/// Returns the current boundaries of the [`Editor`].
fn bounds(&self) -> Size;

View file

@ -56,6 +56,31 @@ impl Editor {
.as_ref()
.expect("Editor should always be initialized")
}
fn with_internal_mut<T>(
&mut self,
f: impl FnOnce(&mut Internal) -> T,
) -> T {
let editor =
self.0.take().expect("Editor should always be initialized");
// TODO: Handle multiple strong references somehow
let mut internal = Arc::try_unwrap(editor)
.expect("Editor cannot have multiple strong references");
// Clear cursor cache
let _ = internal
.selection
.write()
.expect("Write to cursor cache")
.take();
let result = f(&mut internal);
self.0 = Some(Arc::new(internal));
result
}
}
impl editor::Editor for Editor {
@ -281,22 +306,9 @@ impl editor::Editor for Editor {
let mut font_system =
text::font_system().write().expect("Write font system");
let editor =
self.0.take().expect("Editor should always be initialized");
// TODO: Handle multiple strong references somehow
let mut internal = Arc::try_unwrap(editor)
.expect("Editor cannot have multiple strong references");
self.with_internal_mut(|internal| {
let editor = &mut internal.editor;
// Clear cursor cache
let _ = internal
.selection
.write()
.expect("Write to cursor cache")
.take();
match action {
// Motion events
Action::Move(motion) => {
@ -312,7 +324,9 @@ impl editor::Editor for Editor {
| Motion::DocumentEnd => {
editor.action(
font_system.raw(),
cosmic_text::Action::Motion(to_motion(motion)),
cosmic_text::Action::Motion(to_motion(
motion,
)),
);
}
// Other motions simply move the cursor to one end of the selection
@ -334,8 +348,9 @@ impl editor::Editor for Editor {
let cursor = editor.cursor();
if editor.selection_bounds().is_none() {
editor
.set_selection(cosmic_text::Selection::Normal(cursor));
editor.set_selection(cosmic_text::Selection::Normal(
cursor,
));
}
editor.action(
@ -445,8 +460,9 @@ impl editor::Editor for Editor {
.map(|(start, _)| start)
.unwrap_or(cursor);
internal.topmost_line_changed =
Some(selection_start.line.min(topmost_line_before_edit));
internal.topmost_line_changed = Some(
selection_start.line.min(topmost_line_before_edit),
);
}
// Mouse events
@ -481,13 +497,37 @@ impl editor::Editor for Editor {
font_system.raw(),
cosmic_text::Action::Scroll {
pixels: lines as f32
* buffer_from_editor(editor).metrics().line_height,
* buffer_from_editor(editor)
.metrics()
.line_height,
},
);
}
}
});
}
self.0 = Some(Arc::new(internal));
fn move_to(&mut self, cursor: Cursor) {
self.with_internal_mut(|internal| {
// TODO: Expose `Affinity`
internal.editor.set_cursor(cosmic_text::Cursor {
line: cursor.position.line,
index: cursor.position.column,
affinity: cosmic_text::Affinity::Before,
});
if let Some(selection) = cursor.selection {
internal
.editor
.set_selection(cosmic_text::Selection::Normal(
cosmic_text::Cursor {
line: selection.line,
index: selection.column,
affinity: cosmic_text::Affinity::Before,
},
));
}
});
}
fn bounds(&self) -> Size {
@ -512,12 +552,7 @@ impl editor::Editor for Editor {
new_wrapping: Wrapping,
new_highlighter: &mut impl Highlighter,
) {
let editor =
self.0.take().expect("Editor should always be initialized");
let mut internal = Arc::try_unwrap(editor)
.expect("Editor cannot have multiple strong references");
self.with_internal_mut(|internal| {
let mut font_system =
text::font_system().write().expect("Write font system");
@ -581,25 +616,19 @@ impl editor::Editor for Editor {
internal.bounds = new_bounds;
}
if let Some(topmost_line_changed) = internal.topmost_line_changed.take()
if let Some(topmost_line_changed) =
internal.topmost_line_changed.take()
{
log::trace!(
"Notifying highlighter of line change: {topmost_line_changed}"
"Notifying highlighter of line \
change: {topmost_line_changed}"
);
new_highlighter.change_line(topmost_line_changed);
}
internal.editor.shape_as_needed(font_system.raw(), false);
// Clear cursor cache
let _ = internal
.selection
.write()
.expect("Write to cursor cache")
.take();
self.0 = Some(Arc::new(internal));
});
}
fn highlight<H: Highlighter>(

View file

@ -390,7 +390,6 @@ where
R: text::Renderer,
{
editor: R::Editor,
is_dirty: bool,
}
impl<R> Content<R>
@ -406,7 +405,6 @@ where
pub fn with_text(text: &str) -> Self {
Self(RefCell::new(Internal {
editor: R::Editor::with_text(text),
is_dirty: true,
}))
}
@ -415,7 +413,13 @@ where
let internal = self.0.get_mut();
internal.editor.perform(action);
internal.is_dirty = true;
}
/// Moves the current cursor to reflect the given one.
pub fn move_to(&mut self, cursor: Cursor) {
let internal = self.0.get_mut();
internal.editor.move_to(cursor);
}
/// Returns the current cursor position of the [`Content`].
@ -511,7 +515,6 @@ where
f.debug_struct("Content")
.field("editor", &internal.editor)
.field("is_dirty", &internal.is_dirty)
.finish()
}
}