-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add infrastructure to Parley to facilitate text selection/editing #52
Comments
Never mind. A quick look at parley suggests Basically, you have one function that gives you a cursor from a You're not supposed to feed it back in other parley functions. |
Planned API: pub enum WritingDirection {
LeftToRight,
RightToLeft,
}
pub enum ByteDirection {
Upstream,
Downstream,
}
pub enum Movement {
Grapheme(ByteDirection),
Word(ByteDirection),
Line(ByteDirection),
ParagraphStart,
ParagraphEnd,
LineUp,
LineDown,
PageUp,
PageDown,
DocumentStart,
DocumentEnd,
}
impl<B> Layout<b> {
fn get_direction(&self, index: usize, affinity: ByteDirection) -> WritingDirection;
}
impl WritingDirection {
fn left() -> ByteDirection;
fn right() -> ByteDirection;
}
impl<B> Layout<b> {
fn apply_movement(&self, index: usize, affinity: ByteDirection, x_pos: f64, movement: Movement) -> (usize, ByteDirection);
} On the Masonry side: struct Selection {
anchor_index: usize,
anchor_affinity: ByteDirection,
active_index: usize,
active_affinity: ByteDirection,
x_pos: f64,
}
impl Selection {
fn move(&mut self, layout: &Layout, movement: Movement);
fn select(&mut self, layout: &Layout, movement: Movement);
fn is_caret(&self) -> bool;
}
enum TextAction {
// Arrows, Home, End, etc
Move(Movement),
// Ctrl+A
SelectAll,
// Shift+Arrows, Shift+Home, Shift+End, etc
Select(Movement),
// Backspace, Delete, Ctrl+Backspace, Ctrl+Delete
SelectAndDelete(Movement),
// Regular input, paste, IME
Splice(String),
}
fn convert(event: winit::KeyEvent) -> TextAction; Will take a lot of inspiration from existing code, especially |
Re-reading this, this is the most beautiful API I've written in my freaking life. |
Looks good. I don't see any reason why those selection and action types couldn't also live in parley though. |
Feeding it back into the API was certainly the intended purpose. This is why |
If |
In reality, the type we care about is selection, which I would implement as: struct Selection {
anchor: Cursor,
focus: Cursor,
h_pos: Option<f32>,
} Every movement or edit action would take an input selection and action and then return a new selection (which may be collapsed). |
Remind me what collapsed selections are? |
I would guess that a collapsed cursor is a cursor where the anchor and focus are the same location - i.e. a caret cursor. |
Yeah, what Daniel said. If I remember correctly, the web cursor API refers to that as “collapsed” |
After some discussion with Chad, here is the second iteration of the API I'd propose: pub enum LogicalDirection {
Upstream,
Downstream,
}
pub enum Movement {
Backspace, // Backspace behaves somewhat differently than LeftArrow.
Grapheme(LogicalDirection),
Word(LogicalDirection),
Line(LogicalDirection),
ParagraphStart,
ParagraphEnd,
LineUp(usize),
LineDown(usize),
DocumentStart,
DocumentEnd,
}
impl<B> Layout<b> {
fn logical_left(&self, index: usize, affinity: LogicalDirection) -> LogicalDirection;
fn logical_right(&self, index: usize, affinity: LogicalDirection) -> LogicalDirection;
fn index_and_affinity_from_point(&self, x: f32, y: f32) -> (usize, LogicalDirection);
fn cursor_from_index(index: usize, affinity: LogicalDirection);
fn apply_movement(&self, index: usize, affinity: LogicalDirection, x_pos: f64, movement: Movement) -> (usize, ByteDirection);
} And Masonry would take care of the rest for now. |
We want Parley to implement types and methods that Masonry and other editors will be able to use to create a text-editing widget, or to handle selection in non-editable text.
After some discussion we've settled on the following:
MoveLeft
,MoveToNextWord
,SelectToNextParagraph
,RemovePreviousWord
, etc.Possible signatures:
We might use Parley's Cursor type instead of byte offsets in these interfaces.The text was updated successfully, but these errors were encountered: