Text interactions break on transformed layers
Description
When working on a transformed layer, selecting a region of text by dragging on egui::TextEdit or egui::Label doesn't work properly.
Clicking on a transformed egui::TextEdit doesn't focus the widget. Holding the button for a bit before releasing works for some reason. Interestingly, this also happens when clicking the edge of the widget, even without transformation.
https://github.com/emilk/egui/assets/43807375/59a8a2c3-8534-4b77-8584-e47e49e02d9e
Steps to reproduce
- use emilk/eframe_template@d168539c2232ad520a0c38d6e45864489b55e513
- update egui and eframe to emilk/egui@bf7ffb982a6e8c0a40d0871c38bccacef9ee8efb
- remove all ui code and add the following inside
egui::CentralPanel:
use egui::{emath::TSTransform, Label, LayerId, Pos2, Rect, TextEdit, Vec2};
// let transform = TSTransform::IDENTITY;
// let transform = TSTransform::from_translation(egui::Vec2::new(50., 0.));
let transform = TSTransform::from_translation(egui::Vec2::new(150., 0.));
let layer_id = LayerId::new(egui::Order::Middle, ui.id().with("child"));
ui.ctx().set_transform_layer(layer_id, transform);
ui.with_layer_id(layer_id, |ui| {
let mut rect = Rect::from_min_size(Pos2::new(20., 30.), Vec2::new(100., 20.));
ui.put(rect, Label::new("this is a test label"));
rect.min.y += 20.;
rect.max.y += 20.;
static mut STR: String = String::new();
ui.put(rect, TextEdit::singleline(unsafe { &mut STR }));
});
Thoughts
It seems like when handling these interactions, the transformation isn't applied properly.
For the focus issue, I think I have identified the relevant code snippet:
https://github.com/emilk/egui/blob/bf7ffb982a6e8c0a40d0871c38bccacef9ee8efb/crates/egui/src/response.rs#L181-L191
In line 188, the untransformed rect is checked against the untransformed pointer position.
When the widget is transformed away, the click happens outside the original area and the focus is removed.
I tried accessing the transform from this function using self.ctx.memory(), however this freezes the app, probably because the memory is already being accessed at this point?
I don't know the code well enough yet to be able to fix this.
I also encountered this and I posted a comment on the PR that introduced the set_transform_layer method: https://github.com/emilk/egui/pull/3906#issuecomment-2166817300
Rather then opening a new issue I will update here:
The relevant line of code causing this I believe is here
https://github.com/emilk/egui/blob/93d458bb68a5c6e1fd05cbebfd64701463e7ab61/crates/egui/src/widgets/text_edit/builder.rs#L546-L548
Changing it to something like this
if let Some(pointer_pos) = ui.ctx().pointer_interact_pos() {
let t = ui.ctx().memory(|m| {
m.layer_transforms
.get(&ui.layer_id())
.map(TSTransform::inverse)
.unwrap_or_default()
});
let pointer_pos = t * pointer_pos;
if response.hovered() && text.is_mutable() {
ui.output_mut(|o| o.mutable_text_under_cursor = true);
should work
I'm a bit afk atm but I will see about opening a PR when I have time