mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 05:40:03 -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();
|
||||
|
||||
@@ -117,6 +117,7 @@ impl epi::App for WrapApp {
|
||||
});
|
||||
|
||||
self.backend_panel.update(ctx, frame);
|
||||
|
||||
if self.backend_panel.open || ctx.memory().everything_is_visible() {
|
||||
egui::SidePanel::left("backend_panel", 150.0).show(ctx, |ui| {
|
||||
self.backend_panel.ui(ui, frame);
|
||||
@@ -128,6 +129,8 @@ impl epi::App for WrapApp {
|
||||
app.update(ctx, frame);
|
||||
}
|
||||
}
|
||||
|
||||
self.backend_panel.end_of_frame(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,6 +219,9 @@ struct BackendPanel {
|
||||
|
||||
#[cfg_attr(feature = "persistence", serde(skip))]
|
||||
frame_history: crate::frame_history::FrameHistory,
|
||||
|
||||
#[cfg_attr(feature = "persistence", serde(skip))]
|
||||
output_event_history: std::collections::VecDeque<egui::OutputEvent>,
|
||||
}
|
||||
|
||||
impl Default for BackendPanel {
|
||||
@@ -227,6 +233,7 @@ impl Default for BackendPanel {
|
||||
max_size_points_ui: egui::Vec2::new(1024.0, 2048.0),
|
||||
max_size_points_active: egui::Vec2::new(1024.0, 2048.0),
|
||||
frame_history: Default::default(),
|
||||
output_event_history: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -242,6 +249,15 @@ impl BackendPanel {
|
||||
}
|
||||
}
|
||||
|
||||
fn end_of_frame(&mut self, ctx: &egui::CtxRef) {
|
||||
for event in &ctx.output().events {
|
||||
self.output_event_history.push_back(event.clone());
|
||||
}
|
||||
while self.output_event_history.len() > 10 {
|
||||
self.output_event_history.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
fn ui(&mut self, ui: &mut egui::Ui, frame: &mut epi::Frame<'_>) {
|
||||
ui.heading("💻 Backend");
|
||||
|
||||
@@ -298,6 +314,14 @@ impl BackendPanel {
|
||||
frame.quit();
|
||||
}
|
||||
}
|
||||
|
||||
ui.collapsing("Output events", |ui| {
|
||||
ui.set_max_width(350.0);
|
||||
ui.label("Recent output events from egui:");
|
||||
for event in &self.output_event_history {
|
||||
ui.monospace(format!("{:?}", event));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn pixels_per_point_ui(
|
||||
|
||||
Reference in New Issue
Block a user