mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 13:20:05 -04:00
Output events when widgets gain keyboard focus
Part of https://github.com/emilk/egui/issues/167
This commit is contained in:
@@ -93,8 +93,7 @@ impl super::Demo for ManualLayoutTest {
|
||||
|
||||
impl super::View for ManualLayoutTest {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
use egui::*;
|
||||
reset_button(ui, self);
|
||||
egui::reset_button(ui, self);
|
||||
let Self {
|
||||
widget_offset,
|
||||
widget_size,
|
||||
@@ -107,29 +106,30 @@ impl super::View for ManualLayoutTest {
|
||||
ui.radio_value(widget_type, WidgetType::Label, "Label");
|
||||
ui.radio_value(widget_type, WidgetType::TextEdit, "TextEdit");
|
||||
});
|
||||
Grid::new("pos_size").show(ui, |ui| {
|
||||
egui::Grid::new("pos_size").show(ui, |ui| {
|
||||
ui.label("Widget position:");
|
||||
ui.add(Slider::f32(&mut widget_offset.x, 0.0..=400.0));
|
||||
ui.add(Slider::f32(&mut widget_offset.y, 0.0..=400.0));
|
||||
ui.add(egui::Slider::f32(&mut widget_offset.x, 0.0..=400.0));
|
||||
ui.add(egui::Slider::f32(&mut widget_offset.y, 0.0..=400.0));
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Widget size:");
|
||||
ui.add(Slider::f32(&mut widget_size.x, 0.0..=400.0));
|
||||
ui.add(Slider::f32(&mut widget_size.y, 0.0..=400.0));
|
||||
ui.add(egui::Slider::f32(&mut widget_size.x, 0.0..=400.0));
|
||||
ui.add(egui::Slider::f32(&mut widget_size.y, 0.0..=400.0));
|
||||
ui.end_row();
|
||||
});
|
||||
let widget_rect = Rect::from_min_size(ui.min_rect().min + *widget_offset, *widget_size);
|
||||
let widget_rect =
|
||||
egui::Rect::from_min_size(ui.min_rect().min + *widget_offset, *widget_size);
|
||||
|
||||
// Showing how to place a widget anywhere in the `Ui`:
|
||||
match *widget_type {
|
||||
WidgetType::Button => {
|
||||
ui.put(widget_rect, Button::new("Example button"));
|
||||
ui.put(widget_rect, egui::Button::new("Example button"));
|
||||
}
|
||||
WidgetType::Label => {
|
||||
ui.put(widget_rect, Label::new("Example label"));
|
||||
ui.put(widget_rect, egui::Label::new("Example label"));
|
||||
}
|
||||
WidgetType::TextEdit => {
|
||||
ui.put(widget_rect, TextEdit::multiline(text_edit_contents));
|
||||
ui.put(widget_rect, egui::TextEdit::multiline(text_edit_contents));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,11 @@ pub fn toggle_ui(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
||||
// This is where we get a region of the screen assigned.
|
||||
// We also tell the Ui to sense clicks in the allocated region.
|
||||
let (rect, mut response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
|
||||
if response.gained_kb_focus() {
|
||||
// Inform accessibility systems that the widget is selected:
|
||||
ui.output()
|
||||
.push_gained_focus_event(egui::WidgetType::Checkbox, "");
|
||||
}
|
||||
|
||||
// 3. Interact: Time to check for clicks!
|
||||
if response.clicked() {
|
||||
@@ -67,6 +72,10 @@ pub fn toggle_ui(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
||||
fn toggle_ui_compact(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
||||
let desired_size = ui.spacing().interact_size.y * egui::vec2(2.0, 1.0);
|
||||
let (rect, mut response) = ui.allocate_exact_size(desired_size, egui::Sense::click());
|
||||
if response.gained_kb_focus() {
|
||||
ui.output()
|
||||
.push_gained_focus_event(egui::WidgetType::Checkbox, "");
|
||||
}
|
||||
if response.clicked() {
|
||||
*on = !*on;
|
||||
response.mark_changed();
|
||||
|
||||
Reference in New Issue
Block a user