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

Save and resume editor state / handle external #285

Open
tigregalis opened this issue Jul 10, 2024 · 1 comment
Open

Save and resume editor state / handle external #285

tigregalis opened this issue Jul 10, 2024 · 1 comment

Comments

@tigregalis
Copy link
Contributor

Possibly related to #283

I'd like to use Editor actions to drive changes to "editor state" that lives "outside" so to speak. Without having to impl Edit for MyType and reimplement Editor, which already contains all of the behaviours I need. Currently my workaround is:

  1. Construct an ephemeral TempEditor (wraps cosmic_text::Editor) around the existing cosmic_text::Buffer,
  2. Apply any changes to the TempEditor and it will be reflected in the Buffer
  3. Extract and store any state that needs to be persisted between frames in EditorState (Cursor, Selection) and drop the TempEditor
  4. Working backwards, update the bevy Text component from the updated Buffer

See https://github.com/tigregalis/bevy_spans_ent/blob/main/examples/editor.rs#L128

There's a few possibilities:

  1. I'm running into several bugs with this so it might not be the right solution, but it may be that I'm not getting enough information out of the Editor, so one possibility would be to provide a method to save and resume editor state (i.e. basically everything that Editor currently holds, except the BufferRef: it would be up to the owner to make sure they resume Editor with the same effective Buffer) - I think this could be generally useful.
  2. Have some means (e.g. by passing "handlers") to operate on "external" state in response to Editor actions, but ideally without having to reimplement all of Editor.
  3. I noticed there was a Change list that was part of Editor which might also serve my purposes but no examples of how that might be used.
@tigregalis
Copy link
Contributor Author

tigregalis commented Jul 13, 2024

I implemented Option 1 as it was fairly straightforward, but saving/resume the internal state wasn't the issue as far as my implementation went: I've resolved that now. (It turns out there is some nuance with BufferLine's AttrsList::spans that I wasn't aware of. I think this could benefit from some documentation, so I'll submit a PR for that.)

Cursor and Selection (which are already accessible) is possibly enough state to save.

However, I still think these ideas of saving/resuming editor state, or mutating external state in response to editor actions, could be generally useful though, especially when something else owns the buffer, but I don't have a clear suggestion of what that API or implementation looks like.

Another option here is to make all Editor/Edit methods take a &mut Buffer as a parameter, and have a BorrowedWithBuffer helper, in the same way we made the split between Buffer and FontSystem and have BorrowedWithFontSystem.

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

No branches or pull requests

1 participant