Refactor cursor API in Editor
Co-authored-by: Neeraj Jaiswal <neerajj85@gmail.com>
This commit is contained in:
parent
2ac62f7512
commit
63e9eeffb5
5 changed files with 95 additions and 47 deletions
|
|
@ -166,14 +166,17 @@ impl text::Editor for () {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cursor(&self) -> text::editor::Cursor {
|
fn cursor(&self) -> text::editor::Cursor {
|
||||||
text::editor::Cursor::Caret(Point::ORIGIN)
|
text::editor::Cursor {
|
||||||
|
position: text::editor::Position { line: 0, column: 0 },
|
||||||
|
selection: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cursor_position(&self) -> (usize, usize) {
|
fn selection(&self) -> text::editor::Selection {
|
||||||
(0, 0)
|
text::editor::Selection::Caret(Point::ORIGIN)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn selection(&self) -> Option<String> {
|
fn copy(&self) -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,13 +20,11 @@ pub trait Editor: Sized + Default {
|
||||||
/// Returns the current [`Cursor`] of the [`Editor`].
|
/// Returns the current [`Cursor`] of the [`Editor`].
|
||||||
fn cursor(&self) -> Cursor;
|
fn cursor(&self) -> Cursor;
|
||||||
|
|
||||||
/// Returns the current cursor position of the [`Editor`].
|
/// Returns the current [`Selection`] of the [`Editor`].
|
||||||
///
|
fn selection(&self) -> Selection;
|
||||||
/// Line and column, respectively.
|
|
||||||
fn cursor_position(&self) -> (usize, usize);
|
|
||||||
|
|
||||||
/// Returns the current selected text of the [`Editor`].
|
/// Returns the current selected text of the [`Editor`].
|
||||||
fn selection(&self) -> Option<String>;
|
fn copy(&self) -> Option<String>;
|
||||||
|
|
||||||
/// Returns the text of the given line in the [`Editor`], if it exists.
|
/// Returns the text of the given line in the [`Editor`], if it exists.
|
||||||
fn line(&self, index: usize) -> Option<Line<'_>>;
|
fn line(&self, index: usize) -> Option<Line<'_>>;
|
||||||
|
|
@ -187,12 +185,31 @@ pub enum Direction {
|
||||||
|
|
||||||
/// The cursor of an [`Editor`].
|
/// The cursor of an [`Editor`].
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Cursor {
|
pub enum Selection {
|
||||||
/// Cursor without a selection
|
/// Cursor without a selection
|
||||||
Caret(Point),
|
Caret(Point),
|
||||||
|
|
||||||
/// Cursor selecting a range of text
|
/// Cursor selecting a range of text
|
||||||
Selection(Vec<Rectangle>),
|
Range(Vec<Rectangle>),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The range of an [`Editor`].
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct Cursor {
|
||||||
|
/// The cursor position.
|
||||||
|
pub position: Position,
|
||||||
|
|
||||||
|
/// The selection position, if any.
|
||||||
|
pub selection: Option<Position>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A cursor position in an [`Editor`].
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct Position {
|
||||||
|
/// The line of text.
|
||||||
|
pub line: usize,
|
||||||
|
/// The column in the line.
|
||||||
|
pub column: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A line of an [`Editor`].
|
/// A line of an [`Editor`].
|
||||||
|
|
|
||||||
|
|
@ -190,9 +190,13 @@ impl Editor {
|
||||||
}),
|
}),
|
||||||
space::horizontal(),
|
space::horizontal(),
|
||||||
text({
|
text({
|
||||||
let (line, column) = self.content.cursor_position();
|
let cursor = self.content.cursor();
|
||||||
|
|
||||||
format!("{}:{}", line + 1, column + 1)
|
format!(
|
||||||
|
"{}:{}",
|
||||||
|
cursor.position.line + 1,
|
||||||
|
cursor.position.column + 1
|
||||||
|
)
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
.spacing(10);
|
.spacing(10);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
//! Draw and edit text.
|
//! Draw and edit text.
|
||||||
use crate::core::text::editor::{
|
use crate::core::text::editor::{
|
||||||
self, Action, Cursor, Direction, Edit, Motion,
|
self, Action, Cursor, Direction, Edit, Motion, Position, Selection,
|
||||||
};
|
};
|
||||||
use crate::core::text::highlighter::{self, Highlighter};
|
use crate::core::text::highlighter::{self, Highlighter};
|
||||||
use crate::core::text::{LineHeight, Wrapping};
|
use crate::core::text::{LineHeight, Wrapping};
|
||||||
|
|
@ -19,7 +19,7 @@ pub struct Editor(Option<Arc<Internal>>);
|
||||||
|
|
||||||
struct Internal {
|
struct Internal {
|
||||||
editor: cosmic_text::Editor<'static>,
|
editor: cosmic_text::Editor<'static>,
|
||||||
cursor: RwLock<Option<Cursor>>,
|
selection: RwLock<Option<Selection>>,
|
||||||
font: Font,
|
font: Font,
|
||||||
bounds: Size,
|
bounds: Size,
|
||||||
topmost_line_changed: Option<usize>,
|
topmost_line_changed: Option<usize>,
|
||||||
|
|
@ -109,14 +109,14 @@ impl editor::Editor for Editor {
|
||||||
self.buffer().lines.len()
|
self.buffer().lines.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn selection(&self) -> Option<String> {
|
fn copy(&self) -> Option<String> {
|
||||||
self.internal().editor.copy_selection()
|
self.internal().editor.copy_selection()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cursor(&self) -> editor::Cursor {
|
fn selection(&self) -> editor::Selection {
|
||||||
let internal = self.internal();
|
let internal = self.internal();
|
||||||
|
|
||||||
if let Ok(Some(cursor)) = internal.cursor.read().as_deref() {
|
if let Ok(Some(cursor)) = internal.selection.read().as_deref() {
|
||||||
return cursor.clone();
|
return cursor.clone();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,7 +166,7 @@ impl editor::Editor for Editor {
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Cursor::Selection(regions)
|
Selection::Range(regions)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let line_height = buffer.metrics().line_height;
|
let line_height = buffer.metrics().line_height;
|
||||||
|
|
@ -234,7 +234,7 @@ impl editor::Editor for Editor {
|
||||||
layout.last().map(|line| line.w).unwrap_or(0.0),
|
layout.last().map(|line| line.w).unwrap_or(0.0),
|
||||||
));
|
));
|
||||||
|
|
||||||
Cursor::Caret(Point::new(
|
Selection::Caret(Point::new(
|
||||||
offset,
|
offset,
|
||||||
(visual_lines_offset + visual_line as i32) as f32
|
(visual_lines_offset + visual_line as i32) as f32
|
||||||
* line_height
|
* line_height
|
||||||
|
|
@ -243,16 +243,38 @@ impl editor::Editor for Editor {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
*internal.cursor.write().expect("Write to cursor cache") =
|
*internal.selection.write().expect("Write to cursor cache") =
|
||||||
Some(cursor.clone());
|
Some(cursor.clone());
|
||||||
|
|
||||||
cursor
|
cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
fn cursor_position(&self) -> (usize, usize) {
|
fn cursor(&self) -> Cursor {
|
||||||
let cursor = self.internal().editor.cursor();
|
let editor = &self.internal().editor;
|
||||||
|
|
||||||
(cursor.line, cursor.index)
|
let position = {
|
||||||
|
let cursor = editor.cursor();
|
||||||
|
|
||||||
|
Position {
|
||||||
|
line: cursor.line,
|
||||||
|
column: cursor.index,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let selection = match editor.selection() {
|
||||||
|
cosmic_text::Selection::None => None,
|
||||||
|
cosmic_text::Selection::Normal(cursor)
|
||||||
|
| cosmic_text::Selection::Line(cursor)
|
||||||
|
| cosmic_text::Selection::Word(cursor) => Some(Position {
|
||||||
|
line: cursor.line,
|
||||||
|
column: cursor.index,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
Cursor {
|
||||||
|
position,
|
||||||
|
selection,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn perform(&mut self, action: Action) {
|
fn perform(&mut self, action: Action) {
|
||||||
|
|
@ -270,7 +292,7 @@ impl editor::Editor for Editor {
|
||||||
|
|
||||||
// Clear cursor cache
|
// Clear cursor cache
|
||||||
let _ = internal
|
let _ = internal
|
||||||
.cursor
|
.selection
|
||||||
.write()
|
.write()
|
||||||
.expect("Write to cursor cache")
|
.expect("Write to cursor cache")
|
||||||
.take();
|
.take();
|
||||||
|
|
@ -572,7 +594,7 @@ impl editor::Editor for Editor {
|
||||||
|
|
||||||
// Clear cursor cache
|
// Clear cursor cache
|
||||||
let _ = internal
|
let _ = internal
|
||||||
.cursor
|
.selection
|
||||||
.write()
|
.write()
|
||||||
.expect("Write to cursor cache")
|
.expect("Write to cursor cache")
|
||||||
.take();
|
.take();
|
||||||
|
|
@ -685,7 +707,7 @@ impl Default for Internal {
|
||||||
line_height: 1.0,
|
line_height: 1.0,
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
cursor: RwLock::new(None),
|
selection: RwLock::new(None),
|
||||||
font: Font::default(),
|
font: Font::default(),
|
||||||
bounds: Size::ZERO,
|
bounds: Size::ZERO,
|
||||||
topmost_line_changed: None,
|
topmost_line_changed: None,
|
||||||
|
|
|
||||||
|
|
@ -55,11 +55,13 @@ use crate::core::{
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::ops;
|
||||||
use std::ops::DerefMut;
|
use std::ops::DerefMut;
|
||||||
use std::ops::Range;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub use text::editor::{Action, Edit, Line, LineEnding, Motion};
|
pub use text::editor::{
|
||||||
|
Action, Edit, Line, LineEnding, Motion, Position, Selection,
|
||||||
|
};
|
||||||
|
|
||||||
/// A multi-line text input.
|
/// A multi-line text input.
|
||||||
///
|
///
|
||||||
|
|
@ -354,9 +356,9 @@ where
|
||||||
let text_bounds = bounds.shrink(self.padding);
|
let text_bounds = bounds.shrink(self.padding);
|
||||||
let translation = text_bounds.position() - Point::ORIGIN;
|
let translation = text_bounds.position() - Point::ORIGIN;
|
||||||
|
|
||||||
let cursor = match internal.editor.cursor() {
|
let cursor = match internal.editor.selection() {
|
||||||
Cursor::Caret(position) => position,
|
Selection::Caret(position) => position,
|
||||||
Cursor::Selection(ranges) => {
|
Selection::Range(ranges) => {
|
||||||
ranges.first().cloned().unwrap_or_default().position()
|
ranges.first().cloned().unwrap_or_default().position()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -416,6 +418,11 @@ where
|
||||||
internal.is_dirty = true;
|
internal.is_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the current cursor position of the [`Content`].
|
||||||
|
pub fn cursor(&self) -> Cursor {
|
||||||
|
self.0.borrow().editor.cursor()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the amount of lines of the [`Content`].
|
/// Returns the amount of lines of the [`Content`].
|
||||||
pub fn line_count(&self) -> usize {
|
pub fn line_count(&self) -> usize {
|
||||||
self.0.borrow().editor.line_count()
|
self.0.borrow().editor.line_count()
|
||||||
|
|
@ -460,21 +467,16 @@ where
|
||||||
contents
|
contents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the selected text of the [`Content`].
|
||||||
|
pub fn selection(&self) -> Option<String> {
|
||||||
|
self.0.borrow().editor.copy()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the kind of [`LineEnding`] used for separating lines in the [`Content`].
|
/// Returns the kind of [`LineEnding`] used for separating lines in the [`Content`].
|
||||||
pub fn line_ending(&self) -> Option<LineEnding> {
|
pub fn line_ending(&self) -> Option<LineEnding> {
|
||||||
Some(self.line(0)?.ending)
|
Some(self.line(0)?.ending)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the selected text of the [`Content`].
|
|
||||||
pub fn selection(&self) -> Option<String> {
|
|
||||||
self.0.borrow().editor.selection()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current cursor position of the [`Content`].
|
|
||||||
pub fn cursor_position(&self) -> (usize, usize) {
|
|
||||||
self.0.borrow().editor.cursor_position()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns whether or not the the [`Content`] is empty.
|
/// Returns whether or not the the [`Content`] is empty.
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.0.borrow().editor.is_empty()
|
self.0.borrow().editor.is_empty()
|
||||||
|
|
@ -1014,8 +1016,8 @@ where
|
||||||
let translation = text_bounds.position() - Point::ORIGIN;
|
let translation = text_bounds.position() - Point::ORIGIN;
|
||||||
|
|
||||||
if let Some(focus) = state.focus.as_ref() {
|
if let Some(focus) = state.focus.as_ref() {
|
||||||
match internal.editor.cursor() {
|
match internal.editor.selection() {
|
||||||
Cursor::Caret(position) if focus.is_cursor_visible() => {
|
Selection::Caret(position) if focus.is_cursor_visible() => {
|
||||||
let cursor =
|
let cursor =
|
||||||
Rectangle::new(
|
Rectangle::new(
|
||||||
position + translation,
|
position + translation,
|
||||||
|
|
@ -1041,7 +1043,7 @@ where
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Cursor::Selection(ranges) => {
|
Selection::Range(ranges) => {
|
||||||
for range in ranges.into_iter().filter_map(|range| {
|
for range in ranges.into_iter().filter_map(|range| {
|
||||||
text_bounds.intersection(&(range + translation))
|
text_bounds.intersection(&(range + translation))
|
||||||
}) {
|
}) {
|
||||||
|
|
@ -1054,7 +1056,7 @@ where
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Cursor::Caret(_) => {}
|
Selection::Caret(_) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1265,7 +1267,7 @@ enum Ime {
|
||||||
Toggle(bool),
|
Toggle(bool),
|
||||||
Preedit {
|
Preedit {
|
||||||
content: String,
|
content: String,
|
||||||
selection: Option<Range<usize>>,
|
selection: Option<ops::Range<usize>>,
|
||||||
},
|
},
|
||||||
Commit(String),
|
Commit(String),
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue