10000 requesting default focus · Issue #1655 · emilk/egui · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

Open
lunixbochs opened this issue May 21, 2022 · 4 comments
Open

requesting default focus #1655

lunixbochs opened this issue May 21, 2022 · 4 comments
Labels
feature New feature or request

Comments

@lunixbochs
Copy link
Contributor
lunixbochs commented May 21, 2022

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).

let button = ui.button("OK");
{
    let mut memory = ui.memory();
    if memory.focus() == None {
        memory.request_focus(button.id);
    }                      
}

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:

  1. Clicking in a background section will flash the widget's focus for a frame. (I think I'd prefer in this case that clicking on the UI background doesn't affect tab focus, but it's probably more nuanced than that, e.g. input boxes?)
  2. Clicking any other button unsets the focus afterwards, which re-focuses the default button and flashes both buttons for a frame.
@lunixbochs lunixbochs added the feature New feature or request label May 21, 2022
@fundon
Copy link
Contributor
fundon commented Dec 29, 2022

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);
            }
        });
    }
}

@tedsteen
Copy link

Any updates here, is this something you do manually or is there an egui way now?

@mackler
Copy link
mackler commented Apr 13, 2024

Any updates here, is this something you do manually or is there an egui way now?

I, too, am interested in this.

@sux2mfgj
Copy link

You can use the request_focus of Response.

like

let resp = ui.add(egui::TextEdit::singleline(&mut self.text));
resp.request_forcus();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants
0