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

TextEdit autocompletion example #777

Closed
wants to merge 2 commits into from

Conversation

crumblingstatue
Copy link
Contributor

@crumblingstatue crumblingstatue commented Oct 7, 2021

I'm opening this pull request for experimenting and getting early feedback on how to implement autocompletion for TextEdit.
It adds an autocomplete demo for easy experimentation.
This is very much a heavy work in progress, the only thing I managed to implement so far is setting the cursor to end in a crude way.

Main problems I identified with autocompletion:

  • Need a way to set cursor position, or at least a way to set it to the end on demand (after the user applies the autocompletion)
  • Need a way to ignore certain keyboard events (for example when the user is navigating the autocomplete list with Up/Down arrow keys)

I implemented the bare functionality I want for autocomplete to work properly, but I'm looking for feedback and guidance of how to integrate this into egui properly.

@crumblingstatue crumblingstatue marked this pull request as draft October 7, 2021 22:53
@crumblingstatue
Copy link
Contributor Author

I implemented the bare features to showcase what I want to achieve. Here is a video of how it looks like, and it's available to try as a demo.
https://user-images.githubusercontent.com/1521976/136600706-df4b1156-fba9-4e33-b654-e9dc1599cd2b.mp4

I now welcome feedback on how to integrate this functionality into egui proper.

@crumblingstatue crumblingstatue changed the title [WIP/draft/proof of concept] TextEdit Autocompletion [Draft] TextEdit Autocompletion Oct 8, 2021
@crumblingstatue crumblingstatue force-pushed the autocomplete branch 2 times, most recently from 2b1aa5c to 7c5ffdf Compare October 27, 2021 21:56
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Mar 30, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Mar 31, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Apr 1, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Apr 4, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Apr 5, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Apr 5, 2022
Titaniumtown added a commit to Titaniumtown/egui that referenced this pull request Apr 6, 2022
@Titaniumtown
Copy link
Contributor

Titaniumtown commented Apr 7, 2022

oop seems i've been accidentally spamming this PR from my fork of egui. my apologizes

@crumblingstatue
Copy link
Contributor Author

As of this point, no modifications to egui are needed for at least basic autocomplete support, thanks to InputState::consume_key.
I updated the pull request to reflect this.

@emilk
Copy link
Owner

emilk commented May 4, 2022

I finally got around to test this now - very nice work!

There are some issues though - when I press enter to select a word, the suggestion box lingers, and the focus is not returned to the TextEdit.

@crumblingstatue
Copy link
Contributor Author

yeah, it's kind of messed up, since it went through multiple stages of egui updates. I'll try cleaning it up tomorrow if an autocompletion example is desirable.

@emilk
Copy link
Owner

emilk commented May 5, 2022

I find it very desirable, and a good starting point for others to continue from. Sorry for taking so long before taking a look at this

@crumblingstatue crumblingstatue changed the title [Draft] TextEdit Autocompletion [Draft] TextEdit autocompletion example May 5, 2022
@crumblingstatue crumblingstatue marked this pull request as ready for review May 5, 2022 13:00
@crumblingstatue crumblingstatue changed the title [Draft] TextEdit autocompletion example TextEdit autocompletion example May 5, 2022
@crumblingstatue
Copy link
Contributor Author

I force pushed a more functional version, and removed the draft status of the PR.
Now it's ready for more serious review.

@emilk
Copy link
Owner

emilk commented May 23, 2022

The functionality is nice, but the code is quite complicated imho. The purpose of most demos and examples is to show "This is how easy doing X is!" and this doesn't feel very simple to be honest :)

Perhaps it could be refactored into shorter and more reusable functions?

@crumblingstatue
Copy link
Contributor Author

I'll try doing some refactoring tomorrow. If you have an idea of how to make it shorter and more reusable, feel free to give input.

@crumblingstatue
Copy link
Contributor Author

I have decided to adopt a different strategy.

Originally, I opened this pull request because changes needed to be made to egui to make a functioning autocomplete popup.
However, since then, egui has acquired all the features required to implement such a thing.

This caused this pull request to instead try to add an example for autocompletion.
However, I don't think I can push here an autocompletion example that is both simple, and demonstrates all the features that I need.

So instead, I'm going to create an egui autocompletion library.
If the project is successful, I might submit a pull request for inclusion in egui_extras, along with an example.

Thank you for your patience!

@SerHanmer
Copy link

@crumblingstatue
Were you able to successfully create an autocompletion library?

@crumblingstatue
Copy link
Contributor Author

@crumblingstatue Were you able to successfully create an autocompletion library?

Sorry, I haven't had the motivation to finish creating one.
I just use custom completion code in my application right now

@SerHanmer
Copy link

@crumblingstatue Were you able to successfully create an autocompletion library?

Sorry, I haven't had the motivation to finish creating one. I just use custom completion code in my application right now

No worries, Thanks for getting back to me.

@gabrieldechichi
Copy link

For anyone reading this in the future, I'm extracting the relevant part of the code for setting the cursor position to the end:

fn set_cursor_pos(ui: &mut Ui, te_id: Id, char_pos: usize) {
    let mut state = TextEdit::load_state(ui.ctx(), te_id).unwrap();
    let ccursor = text::CCursor::new(char_pos);
    state.set_ccursor_range(Some(text::CCursorRange::one(ccursor)));
    TextEdit::store_state(ui.ctx(), te_id, state);
}

Which can be used like

let input_id = ui.make_persistent_id("cmd_input_id");
TextEdit::singleline(&mut user_input)
                .id(input_id)
                .ui(ui);
//will set the cursor to the end of user_input
set_cursor_pos(ui, input_id, user_input.chars().count());

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