emilk / emilk/egui

`TextCursorState.set_char_range()` interacts wrongly with user click

Open
#3,883 3 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

Considering the repro code below, regardless of how the text edit gains focus, I'm expecting **all of the text to be selected**. It works as expected when `Tab`ing, or clicking in the text edit whitespace.

However, when clicking on the text itself, only the text _up to the mouse cursor_ is selected.

https://github.com/emilk/egui/assets/49431240/0cf923a9-453e-42cf-8b6d-c0362748dc85

```rust
use eframe::egui;

fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|cc| {
// This gives us image support:
egui_extras::install_image_loaders(&cc.egui_ctx);

Box::::default()
}),
)
}

struct MyApp {
name: String,
}

impl Default for MyApp {
fn default() -> Self {
Self {
name: "Arthur".to_owned(),
}
}
}

impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");

let mut output = egui::TextEdit::singleline(&mut self.name).show(ui);

if output.response.gained_focus() {
output.response.request_focus();
let min = egui::text::CCursor::new(0);
let max = egui::text::CCursor::new(self.name.len());
let new_range = egui::text::CCursorRange::two(min, max);
output.state.cursor.set_char_range(Some(new_range));
output.state.store(ui.ctx(), output.response.id);
}
});
});
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start with TextCursorState.set_char_range() and reproduce the behavior using the Rust example in the issue. Trace how a click on the text changes the cursor selection, then verify that focusing the edit selects all text regardless of where the click lands.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.