-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * Closes #5053 * [x] I have followed the instructions in the PR template This fixes #5053 by adding a Sense parameter to UiBuilder, using that in Context::create_widget, so the Widget is registered with the right Sense / focusable. Additionally, I've added a ignore_focus param to create_widget, so the focus isn't surrendered / reregistered on Ui::interact_bg. The example from #5053 now works correctly: https://github.com/user-attachments/assets/a8a04b5e-7635-4e05-9ed8-e17d64910a35 <details><summary>Updated example code</summary> <p> ```rust ui.button("I can focus"); ui.scope_builder( UiBuilder::new() .sense(Sense::click()) .id_source("focus_test"), |ui| { ui.label("I can focus for a single frame"); let response = ui.interact_bg(); let t = if response.has_focus() { "has focus" } else { "doesn't have focus" }; ui.label(t); }, ); ui.button("I can't focus :("); ``` </p> </details> --- Also, I've added `Ui::interact_scope` to make it easier to read a Ui's response in advance, without having to know about the internals of how the Ui Ids get created. This makes it really easy to created interactive container elements or custom buttons, without having to use Galleys or Painter::add(Shape::Noop) to style based on the interaction. <details><summary> Example usage to create a simple button </summary> <p> ```rust use eframe::egui; use eframe::egui::{Frame, InnerResponse, Label, RichText, UiBuilder, Widget}; use eframe::NativeOptions; use egui::{CentralPanel, Sense, WidgetInfo}; pub fn main() -> eframe::Result { eframe::run_simple_native("focus test", NativeOptions::default(), |ctx, _frame| { CentralPanel::default().show(ctx, |ui| { ui.button("Regular egui Button"); custom_button(ui, |ui| { ui.label("Custom Button"); }); if custom_button(ui, |ui| { ui.label("You can even have buttons inside buttons:"); if ui.button("button inside button").clicked() { println!("Button inside button clicked!"); } }) .response .clicked() { println!("Custom button clicked!"); } }); }) } fn custom_button<R>( ui: &mut egui::Ui, content: impl FnOnce(&mut egui::Ui) -> R, ) -> InnerResponse<R> { let auto_id = ui.next_auto_id(); ui.skip_ahead_auto_ids(1); let response = ui.interact_scope( Sense::click(), UiBuilder::new().id_source(auto_id), |ui, response| { ui.style_mut().interaction.selectable_labels = false; let visuals = response .map(|r| ui.style().interact(&r)) .unwrap_or(&ui.visuals().noninteractive()); let text_color = visuals.text_color(); Frame::none() .fill(visuals.bg_fill) .stroke(visuals.bg_stroke) .rounding(visuals.rounding) .inner_margin(ui.spacing().button_padding) .show(ui, |ui| { ui.visuals_mut().override_text_color = Some(text_color); content(ui) }) .inner }, ); response .response .widget_info(|| WidgetInfo::new(egui::WidgetType::Button)); response } ``` </p> </details> https://github.com/user-attachments/assets/281bd65f-f616-4621-9764-18fd0d07698b --------- Co-authored-by: Emil Ernerfeldt <[email protected]>
- Loading branch information
1 parent
bfafddf
commit 1b8737c
Showing
11 changed files
with
258 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.