From 9e5048d4f2c1f98f0c0a91892a82c66cad115042 Mon Sep 17 00:00:00 2001 From: William Casarin Date: Fri, 24 May 2024 13:42:57 -0700 Subject: [PATCH] input: fix deadlock on resize weird egui Context quirk Fixes: https://github.com/damus-io/notedeck/issues/97 Signed-off-by: William Casarin --- src/app.rs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/app.rs b/src/app.rs index 37417be1..d529c8d9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -106,9 +106,21 @@ fn send_initial_filters(damus: &mut Damus, relay_url: &str) { } } -fn handle_key_events(input: &egui::InputState, damus: &mut Damus, ctx: &egui::Context) { +enum ContextAction { + SetPixelsPerPoint(f32), +} + +fn handle_key_events( + input: &egui::InputState, + pixels_per_point: f32, + damus: &mut Damus, +) -> Option { let amount = 0.2; + // We can't do things like setting the pixels_per_point when we are holding + // on to an locked InputState context, so we need to pass actions externally + let mut context_action: Option = None; + for event in &input.raw.events { if let egui::Event::Key { key, pressed: true, .. @@ -116,10 +128,12 @@ fn handle_key_events(input: &egui::InputState, damus: &mut Damus, ctx: &egui::Co { match key { egui::Key::Equals => { - ctx.set_pixels_per_point(ctx.pixels_per_point() + amount); + context_action = + Some(ContextAction::SetPixelsPerPoint(pixels_per_point + amount)); } egui::Key::Minus => { - ctx.set_pixels_per_point(ctx.pixels_per_point() - amount); + context_action = + Some(ContextAction::SetPixelsPerPoint(pixels_per_point - amount)); } egui::Key::J => { damus.select_down(); @@ -137,10 +151,20 @@ fn handle_key_events(input: &egui::InputState, damus: &mut Damus, ctx: &egui::Co } } } + + context_action } fn try_process_event(damus: &mut Damus, ctx: &egui::Context) -> Result<()> { - ctx.input(|i| handle_key_events(i, damus, ctx)); + let ppp = ctx.pixels_per_point(); + let res = ctx.input(|i| handle_key_events(i, ppp, damus)); + if let Some(action) = res { + match action { + ContextAction::SetPixelsPerPoint(amt) => { + ctx.set_pixels_per_point(amt); + } + } + } let ctx2 = ctx.clone(); let wakeup = move || {