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

Start updating TextBox for piet text changes #1274

Merged
merged 4 commits into from
Oct 7, 2020
Merged

Start updating TextBox for piet text changes #1274

merged 4 commits into from
Oct 7, 2020

Conversation

cmyr
Copy link
Member

@cmyr cmyr commented Oct 2, 2020

This is an attempt to integrate a bit of the new text stuff in to the TextBox widget.

Most of this code is a refactor that splits the actual manipulation of the buffer and selections into a new Editor struct that can be used to implement other text-editing widgets.

Apart from that, there are two other commits; one of them changes TextBox from being Widget<String> to be Widget<TextStorage + EditableText>, and the second adds a TextBox::multiline constructor, as a demo of multiline editing.

The multiline stuff is half-baked; the textbox does not support vertical scrolling. Ultimately it should be rewritten (maybe using @jneem's #1248 work?) so that it uses more standard scroll machinery; but this will be a bit fiddly, and I don't want to sink into it right now. I'll write up an issue detailing how I think this should work, though, and maybe someone can take it up?

This also includes a tweak to examples/styled_text.rs to show a multiline texbox; I'm going to replace this with a simple markdown preview app, but that work is mostly done but it's on another computer, and will have to wait til I get to the studio (perhaps later today?)

@cmyr cmyr force-pushed the text-editor branch 2 times, most recently from 3f065ce to 73cd9d3 Compare October 5, 2020 14:55
cmyr added 2 commits October 5, 2020 14:34
this is largely a refactor; the idea is to separate out
the logic of applying edits and managing selection state
from the particular widget.
This is very preliminary; in particular there is no
vertical scrolling.

I think correctly handling scrolling should be a separate
project, but I wanted to include a basic version of this
to demonstrate that it is possible.

Also: there's no vertical movement yet! Oops.
EditAction::Move(mvmt) => self.selection = movement(mvmt, self.selection, data, false),
EditAction::ModifySelection(mvmt) => {
self.selection = movement(mvmt, self.selection, data, true)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you forgot EditAction::SelectAll?

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 catch, thanks!

/// [`update`]: ../trait.Widget.html#tymethod.update
pub fn update(&mut self, ctx: &mut UpdateCtx, new_data: &T, env: &Env) {
if self.data_is_stale(new_data) {
self.layout.set_text(new_data.clone());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here. If the textbox is not multi-lined, we should guard against new_data containing \n characters!

This issue can be seen when a non-multi lined textbox and another multi-lined textbox lens the same data!

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 is... a problem.

I'm not sure how best to think about this. Piet has no concept of a single-line layout, and trying to validate external data changes is difficult. It isn't clear what we should do when they happen.

I think my (bad) answer to this, for now, is that it is up to the programmer to ensure that newlines are not inserted externally. We could also definitely debug_panic if a newline exists in the initial text. 😒

@cmyr cmyr added the S-needs-review waits for review label Oct 7, 2020
druid/src/text/editor.rs Outdated Show resolved Hide resolved
@justinmoon
Copy link
Contributor

Besides the delete button comment, everything here worked (on Windows).

@cmyr
Copy link
Member Author

cmyr commented Oct 7, 2020

Thanks @justinmoon I'll take a look!

This data must be TextStorage + EditableText, which means
that *currently* it is only String, but just imagine
what the future might hold?

Discussion: there's still something funny going on with
the TextStorage trait; this stuff as written isn't
suitable for use with xi_rope, which was sort of the
whole point.
cmyr added a commit that referenced this pull request Oct 7, 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.

61a2c62 looks good. "select all" and "delete" both work now.

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. Obviously text editing will continue to be an area of ongoing work.

/// selection state.
///
/// [`EditableText`]: trait.EditableText.html
#[derive(Debug, Clone)]
Copy link
Contributor

Choose a reason for hiding this comment

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

I personally would appreciate a line of docstring what the type parameter is.

druid/src/text/editor.rs Outdated Show resolved Hide resolved
self.selection = Selection::caret(self.selection.min() + text.len());
}

/// Delete to previous grapheme if in caret mode.
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment is imprecise: the backspace logic sometimes uses backspace granularity, but other times uses finer granularity to match expected behavior (individual vowels and diacritics in Indic scripts, harakat in Arabic, etc).

@cmyr cmyr merged commit 9920850 into master Oct 7, 2020
@cmyr cmyr deleted the text-editor branch October 7, 2020 19:05
@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.

5 participants