Skip to content
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

Implement vertical movement (for text editing) #1280

Merged
merged 1 commit into from
Oct 8, 2020
Merged

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented Oct 5, 2020

Based off of #1274

This implements vertical motion for text editing.

I think the traits we use here may change over time, but this gets something working for now.

2020-10-05 11 00 03

(this is slightly buggy because it relies on a pending piet release)

@cmyr cmyr force-pushed the text-editor branch 2 times, most recently from 8163550 to 10160ed Compare October 5, 2020 18:35
@cmyr cmyr added the S-blocked waits for another PR or dependency label Oct 5, 2020
Copy link
Contributor

@justinmoon justinmoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the one variable naming nit, this works great for me on Windows

@@ -28,13 +28,20 @@ pub struct Selection {

/// The active edge of a selection, as a byte offset.
pub end: usize,

/// The saved horizontal position, during vertical movement.
pub h_pos: Option<f64>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a little weird how this variable is called h_pos here and x_pos on 105

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree :)

Base automatically changed from text-editor to master October 7, 2020 19:05
@cmyr cmyr added S-needs-review waits for review and removed S-blocked waits for another PR or dependency labels Oct 7, 2020
Copy link
Contributor

@raphlinus raphlinus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally this looks good, very small nits inline.

It's fun to see the capabilities slowly grow :)

} else {
let lm = layout.line_metric(cur_pos.line).unwrap();
let h_pos = s.h_pos.unwrap_or(cur_pos.point.x);
// may not work correctly for point sizes below 1.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I did in xi2 is add half the line height. I think that's most robust but I'm not sure it matters that much - point sizes below 1.0 are not really a thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was intended to be somewhat tongue in cheek 😅.

Wouldn't adding half the line-height would fail if our current line-height is more than 2x the height of the preceding line, which doesn't seem impossible?

This seemed like the most robust approach, but there may be issues I'm not considering.

///
/// returns a new selection representing the state after the movement.
///
/// If `modify` is true, only the leading edge (the 'end') of the selection
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "leading" the right word here? It seems to me "trailing".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair, I guess "active" is the better word.

Movement::Up => {
let cur_pos = layout.hit_test_text_position(s.end);
if cur_pos.line == 0 {
(0, Some(cur_pos.point.x))
Copy link
Contributor

@raphlinus raphlinus Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the behavior of our existing code, but I think Some(s.h_pos.unwrap_or(cur_pos.point.x)) is slightly more precise. And that might lead to logic simplification because I think it's the same in both branches.

So I'm testing different editors and not finding a really authoritative answer. TextEdit on mac is consistent with the suggestion I posted, as is Sublime (tested on mac). VS Code (tested on Windows) is consistent with the code you have. WordPad (Windows) doesn't move the cursor in this case. So I think any of these behaviors are reasonable, and if you want to go with what you have, I won't block on it.

Note that the code as you have it matches xi2, so I certainly can't complain :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I think I prefer preserving the existing h_pos if possible.

druid/src/text/movement.rs Outdated Show resolved Hide resolved
@cmyr cmyr merged commit ac936c2 into master Oct 8, 2020
@cmyr cmyr deleted the vertical-movement branch October 8, 2020 14:10
@maan2003 maan2003 removed the S-needs-review waits for review label May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants