Skip to content

Commit

Permalink
Improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 29, 2022
1 parent 2b2834f commit a646c12
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 13 additions & 21 deletions examples/keyboard_events/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,34 @@ fn main() {
);
}

#[derive(Default)]
struct Content {
text: String,
}

impl Default for Content {
fn default() -> Self {
Self {
text: "".to_owned(),
}
}
}

impl eframe::App for Content {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Press/Hold/Release example");
let text_style = TextStyle::Body;
let row_height = ui.text_style_height(&text_style);
ui.heading("Press/Hold/Release example. Press A to test.");
if ui.button("Clear").clicked() {
self.text.clear();
}
ScrollArea::vertical()
.auto_shrink([false; 2])
.stick_to_bottom(true)
.show_rows(ui, row_height, self.text.len(), |ui, _row_range| {
//for row in row_range {
for line in self.text.lines() {
ui.label(line);
}
.show(ui, |ui| {
ui.label(&self.text);
});

if ctx.input().key_released(Key::A) {
self.text.push_str("\nReleased");
}
if ctx.input().key_pressed(Key::A) {
self.text.push_str("\npressed");
self.text.push_str("\nPressed");
}
if ctx.input().key_down(Key::A) {
self.text.push_str("\nheld");
self.text.push_str("\nHeld");
ui.ctx().request_repaint(); // make sure we note the holding.
}
if ctx.input().key_released(Key::A) {
self.text.push_str("\nReleased");
}
});
}
Expand Down

0 comments on commit a646c12

Please sign in to comment.