-
Notifications
You must be signed in to change notification settings - Fork 1.7k
requesting default focus #1655
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
Labels
feature
New feature or request
Comments
In this case, need a flag to set the input is focused. #[derive(Clone, Default, PartialEq, Eq, Deserialize, Serialize)]
pub struct AddWindow {
name: String,
autofocus: bool,
}
impl Window for AddWindow {
fn show(
&mut self,
data: Option<Message>,
) {
// first rendered
if let Some(Message::Normal) = data {
self.autofocus = true;
}
egui::Window::new("the window")
.resizable(false)
.default_width(280.0)
.open(true)
.show(ctx, |ui| self.ui(ui));
}
}
impl View for AddWindow {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.add_sized((50., 24.), egui::Label::new("Name:"));
let resp =
ui.add(egui::TextEdit::singleline(&mut self.name).hint_text("Write folder name"));
// turn off `autofocus`
if self.autofocus {
self.autofocus = false;
ui.memory().request_focus(resp.id);
}
});
}
} |
Any updates here, is this something you do manually or is there an egui way now? |
I, too, am interested in this. |
You can use the request_focus of Response. like
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I'm playing with dialog prompts in egui.
It would be nice to be able to request that a widget receives "default focus" upon appearing, so the user can just press enter to activate the default button without pressing tab first.
(e.g. when a window first appears, focus the "ok" button)
I tried this pattern, which works ok, though there's weirdness (see the end of this comment).
A method like
ui.memory().default_focus(widget.id)
that sets focus only if it's not already set, may work well as a starting point for this.I think one alternative is tracking a "first open" flag somewhere in my own code for each UI container that cares about default focus, however that may not handle something like a multi-page wizard where the contents change without creating a new container.
Weirdness with my memory approach:
The text was updated successfully, but these errors were encountered: