Improve example

This commit is contained in:
Emil Ernerfeldt 2022-11-29 15:09:57 +01:00
parent 2b2834ff9d
commit a646c1259d
2 changed files with 21 additions and 21 deletions

8
Cargo.lock generated
View file

@ -2070,6 +2070,14 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "keyboard_events"
version = "0.1.0"
dependencies = [
"eframe",
"tracing-subscriber",
]
[[package]] [[package]]
name = "khronos-egl" name = "khronos-egl"
version = "4.1.0" version = "4.1.0"

View file

@ -14,42 +14,34 @@ fn main() {
); );
} }
#[derive(Default)]
struct Content { struct Content {
text: String, text: String,
} }
impl Default for Content {
fn default() -> Self {
Self {
text: "".to_owned(),
}
}
}
impl eframe::App for Content { impl eframe::App for Content {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Press/Hold/Release example"); ui.heading("Press/Hold/Release example. Press A to test.");
let text_style = TextStyle::Body; if ui.button("Clear").clicked() {
let row_height = ui.text_style_height(&text_style); self.text.clear();
}
ScrollArea::vertical() ScrollArea::vertical()
.auto_shrink([false; 2]) .auto_shrink([false; 2])
.stick_to_bottom(true) .stick_to_bottom(true)
.show_rows(ui, row_height, self.text.len(), |ui, _row_range| { .show(ui, |ui| {
//for row in row_range { ui.label(&self.text);
for line in self.text.lines() {
ui.label(line);
}
}); });
if ctx.input().key_released(Key::A) {
self.text.push_str("\nReleased");
}
if ctx.input().key_pressed(Key::A) { if ctx.input().key_pressed(Key::A) {
self.text.push_str("\npressed"); self.text.push_str("\nPressed");
} }
if ctx.input().key_down(Key::A) { 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");
} }
}); });
} }